How to use create_archive method in localstack

Best Python code snippet using localstack_python

test_zipapp.py

Source:test_zipapp.py Github

copy

Full Screen

...14 def setUp(self):15 tmpdir = tempfile.TemporaryDirectory()16 self.addCleanup(tmpdir.cleanup)17 self.tmpdir = pathlib.Path(tmpdir.name)18 def test_create_archive(self):19 # Test packing a directory.20 source = self.tmpdir / 'source'21 source.mkdir()22 (source / '__main__.py').touch()23 target = self.tmpdir / 'source.pyz'24 zipapp.create_archive(str(source), str(target))25 self.assertTrue(target.is_file())26 def test_create_archive_with_pathlib(self):27 # Test packing a directory using Path objects for source and target.28 source = self.tmpdir / 'source'29 source.mkdir()30 (source / '__main__.py').touch()31 target = self.tmpdir / 'source.pyz'32 zipapp.create_archive(source, target)33 self.assertTrue(target.is_file())34 def test_create_archive_with_subdirs(self):35 # Test packing a directory includes entries for subdirectories.36 source = self.tmpdir / 'source'37 source.mkdir()38 (source / '__main__.py').touch()39 (source / 'foo').mkdir()40 (source / 'bar').mkdir()41 (source / 'foo' / '__init__.py').touch()42 target = io.BytesIO()43 zipapp.create_archive(str(source), target)44 target.seek(0)45 with zipfile.ZipFile(target, 'r') as z:46 self.assertIn('foo/', z.namelist())47 self.assertIn('bar/', z.namelist())48 def test_create_archive_with_filter(self):49 # Test packing a directory and using filter to specify50 # which files to include.51 def skip_pyc_files(path):52 return path.suffix != '.pyc'53 source = self.tmpdir / 'source'54 source.mkdir()55 (source / '__main__.py').touch()56 (source / 'test.py').touch()57 (source / 'test.pyc').touch()58 target = self.tmpdir / 'source.pyz'59 zipapp.create_archive(source, target, filter=skip_pyc_files)60 with zipfile.ZipFile(target, 'r') as z:61 self.assertIn('__main__.py', z.namelist())62 self.assertIn('test.py', z.namelist())63 self.assertNotIn('test.pyc', z.namelist())64 def test_create_archive_filter_exclude_dir(self):65 # Test packing a directory and using a filter to exclude a66 # subdirectory (ensures that the path supplied to include67 # is relative to the source location, as expected).68 def skip_dummy_dir(path):69 return path.parts[0] != 'dummy'70 source = self.tmpdir / 'source'71 source.mkdir()72 (source / '__main__.py').touch()73 (source / 'test.py').touch()74 (source / 'dummy').mkdir()75 (source / 'dummy' / 'test2.py').touch()76 target = self.tmpdir / 'source.pyz'77 zipapp.create_archive(source, target, filter=skip_dummy_dir)78 with zipfile.ZipFile(target, 'r') as z:79 self.assertEqual(len(z.namelist()), 2)80 self.assertIn('__main__.py', z.namelist())81 self.assertIn('test.py', z.namelist())82 def test_create_archive_default_target(self):83 # Test packing a directory to the default name.84 source = self.tmpdir / 'source'85 source.mkdir()86 (source / '__main__.py').touch()87 zipapp.create_archive(str(source))88 expected_target = self.tmpdir / 'source.pyz'89 self.assertTrue(expected_target.is_file())90 @requires_zlib91 def test_create_archive_with_compression(self):92 # Test packing a directory into a compressed archive.93 source = self.tmpdir / 'source'94 source.mkdir()95 (source / '__main__.py').touch()96 (source / 'test.py').touch()97 target = self.tmpdir / 'source.pyz'98 zipapp.create_archive(source, target, compressed=True)99 with zipfile.ZipFile(target, 'r') as z:100 for name in ('__main__.py', 'test.py'):101 self.assertEqual(z.getinfo(name).compress_type,102 zipfile.ZIP_DEFLATED)103 def test_no_main(self):104 # Test that packing a directory with no __main__.py fails.105 source = self.tmpdir / 'source'106 source.mkdir()107 (source / 'foo.py').touch()108 target = self.tmpdir / 'source.pyz'109 with self.assertRaises(zipapp.ZipAppError):110 zipapp.create_archive(str(source), str(target))111 def test_main_and_main_py(self):112 # Test that supplying a main argument with __main__.py fails.113 source = self.tmpdir / 'source'114 source.mkdir()115 (source / '__main__.py').touch()116 target = self.tmpdir / 'source.pyz'117 with self.assertRaises(zipapp.ZipAppError):118 zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')119 def test_main_written(self):120 # Test that the __main__.py is written correctly.121 source = self.tmpdir / 'source'122 source.mkdir()123 (source / 'foo.py').touch()124 target = self.tmpdir / 'source.pyz'125 zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')126 with zipfile.ZipFile(str(target), 'r') as z:127 self.assertIn('__main__.py', z.namelist())128 self.assertIn(b'pkg.mod.fn()', z.read('__main__.py'))129 def test_main_only_written_once(self):130 # Test that we don't write multiple __main__.py files.131 # The initial implementation had this bug; zip files allow132 # multiple entries with the same name133 source = self.tmpdir / 'source'134 source.mkdir()135 # Write 2 files, as the original bug wrote __main__.py136 # once for each file written :-(137 # See http://bugs.python.org/review/23491/diff/13982/Lib/zipapp.py#newcode67Lib/zipapp.py:67138 # (line 67)139 (source / 'foo.py').touch()140 (source / 'bar.py').touch()141 target = self.tmpdir / 'source.pyz'142 zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')143 with zipfile.ZipFile(str(target), 'r') as z:144 self.assertEqual(1, z.namelist().count('__main__.py'))145 def test_main_validation(self):146 # Test that invalid values for main are rejected.147 source = self.tmpdir / 'source'148 source.mkdir()149 target = self.tmpdir / 'source.pyz'150 problems = [151 '', 'foo', 'foo:', ':bar', '12:bar', 'a.b.c.:d',152 '.a:b', 'a:b.', 'a:.b', 'a:silly name'153 ]154 for main in problems:155 with self.subTest(main=main):156 with self.assertRaises(zipapp.ZipAppError):157 zipapp.create_archive(str(source), str(target), main=main)158 def test_default_no_shebang(self):159 # Test that no shebang line is written to the target by default.160 source = self.tmpdir / 'source'161 source.mkdir()162 (source / '__main__.py').touch()163 target = self.tmpdir / 'source.pyz'164 zipapp.create_archive(str(source), str(target))165 with target.open('rb') as f:166 self.assertNotEqual(f.read(2), b'#!')167 def test_custom_interpreter(self):168 # Test that a shebang line with a custom interpreter is written169 # correctly.170 source = self.tmpdir / 'source'171 source.mkdir()172 (source / '__main__.py').touch()173 target = self.tmpdir / 'source.pyz'174 zipapp.create_archive(str(source), str(target), interpreter='python')175 with target.open('rb') as f:176 self.assertEqual(f.read(2), b'#!')177 self.assertEqual(b'python\n', f.readline())178 def test_pack_to_fileobj(self):179 # Test that we can pack to a file object.180 source = self.tmpdir / 'source'181 source.mkdir()182 (source / '__main__.py').touch()183 target = io.BytesIO()184 zipapp.create_archive(str(source), target, interpreter='python')185 self.assertTrue(target.getvalue().startswith(b'#!python\n'))186 def test_read_shebang(self):187 # Test that we can read the shebang line correctly.188 source = self.tmpdir / 'source'189 source.mkdir()190 (source / '__main__.py').touch()191 target = self.tmpdir / 'source.pyz'192 zipapp.create_archive(str(source), str(target), interpreter='python')193 self.assertEqual(zipapp.get_interpreter(str(target)), 'python')194 def test_read_missing_shebang(self):195 # Test that reading the shebang line of a file without one returns None.196 source = self.tmpdir / 'source'197 source.mkdir()198 (source / '__main__.py').touch()199 target = self.tmpdir / 'source.pyz'200 zipapp.create_archive(str(source), str(target))201 self.assertEqual(zipapp.get_interpreter(str(target)), None)202 def test_modify_shebang(self):203 # Test that we can change the shebang of a file.204 source = self.tmpdir / 'source'205 source.mkdir()206 (source / '__main__.py').touch()207 target = self.tmpdir / 'source.pyz'208 zipapp.create_archive(str(source), str(target), interpreter='python')209 new_target = self.tmpdir / 'changed.pyz'210 zipapp.create_archive(str(target), str(new_target), interpreter='python2.7')211 self.assertEqual(zipapp.get_interpreter(str(new_target)), 'python2.7')212 def test_write_shebang_to_fileobj(self):213 # Test that we can change the shebang of a file, writing the result to a214 # file object.215 source = self.tmpdir / 'source'216 source.mkdir()217 (source / '__main__.py').touch()218 target = self.tmpdir / 'source.pyz'219 zipapp.create_archive(str(source), str(target), interpreter='python')220 new_target = io.BytesIO()221 zipapp.create_archive(str(target), new_target, interpreter='python2.7')222 self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))223 def test_read_from_pathobj(self):224 # Test that we can copy an archive using a pathlib.Path object225 # for the source.226 source = self.tmpdir / 'source'227 source.mkdir()228 (source / '__main__.py').touch()229 target1 = self.tmpdir / 'target1.pyz'230 target2 = self.tmpdir / 'target2.pyz'231 zipapp.create_archive(source, target1, interpreter='python')232 zipapp.create_archive(target1, target2, interpreter='python2.7')233 self.assertEqual(zipapp.get_interpreter(target2), 'python2.7')234 def test_read_from_fileobj(self):235 # Test that we can copy an archive using an open file object.236 source = self.tmpdir / 'source'237 source.mkdir()238 (source / '__main__.py').touch()239 target = self.tmpdir / 'source.pyz'240 temp_archive = io.BytesIO()241 zipapp.create_archive(str(source), temp_archive, interpreter='python')242 new_target = io.BytesIO()243 temp_archive.seek(0)244 zipapp.create_archive(temp_archive, new_target, interpreter='python2.7')245 self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))246 def test_remove_shebang(self):247 # Test that we can remove the shebang from a file.248 source = self.tmpdir / 'source'249 source.mkdir()250 (source / '__main__.py').touch()251 target = self.tmpdir / 'source.pyz'252 zipapp.create_archive(str(source), str(target), interpreter='python')253 new_target = self.tmpdir / 'changed.pyz'254 zipapp.create_archive(str(target), str(new_target), interpreter=None)255 self.assertEqual(zipapp.get_interpreter(str(new_target)), None)256 def test_content_of_copied_archive(self):257 # Test that copying an archive doesn't corrupt it.258 source = self.tmpdir / 'source'259 source.mkdir()260 (source / '__main__.py').touch()261 target = io.BytesIO()262 zipapp.create_archive(str(source), target, interpreter='python')263 new_target = io.BytesIO()264 target.seek(0)265 zipapp.create_archive(target, new_target, interpreter=None)266 new_target.seek(0)267 with zipfile.ZipFile(new_target, 'r') as z:268 self.assertEqual(set(z.namelist()), {'__main__.py'})269 # (Unix only) tests that archives with shebang lines are made executable270 @unittest.skipIf(sys.platform == 'win32',271 'Windows does not support an executable bit')272 def test_shebang_is_executable(self):273 # Test that an archive with a shebang line is made executable.274 source = self.tmpdir / 'source'275 source.mkdir()276 (source / '__main__.py').touch()277 target = self.tmpdir / 'source.pyz'278 zipapp.create_archive(str(source), str(target), interpreter='python')279 self.assertTrue(target.stat().st_mode & stat.S_IEXEC)280 @unittest.skipIf(sys.platform == 'win32',281 'Windows does not support an executable bit')282 def test_no_shebang_is_not_executable(self):283 # Test that an archive with no shebang line is not made executable.284 source = self.tmpdir / 'source'285 source.mkdir()286 (source / '__main__.py').touch()287 target = self.tmpdir / 'source.pyz'288 zipapp.create_archive(str(source), str(target), interpreter=None)289 self.assertFalse(target.stat().st_mode & stat.S_IEXEC)290class ZipAppCmdlineTest(unittest.TestCase):291 """Test zipapp module command line API."""292 def setUp(self):293 tmpdir = tempfile.TemporaryDirectory()294 self.addCleanup(tmpdir.cleanup)295 self.tmpdir = pathlib.Path(tmpdir.name)296 def make_archive(self):297 # Test that an archive with no shebang line is not made executable.298 source = self.tmpdir / 'source'299 source.mkdir()300 (source / '__main__.py').touch()301 target = self.tmpdir / 'source.pyz'302 zipapp.create_archive(source, target)303 return target304 def test_cmdline_create(self):305 # Test the basic command line API.306 source = self.tmpdir / 'source'307 source.mkdir()308 (source / '__main__.py').touch()309 args = [str(source)]310 zipapp.main(args)311 target = source.with_suffix('.pyz')312 self.assertTrue(target.is_file())313 def test_cmdline_copy(self):314 # Test copying an archive.315 original = self.make_archive()316 target = self.tmpdir / 'target.pyz'...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful