1 | n/a | """Test harness for the zipapp module.""" |
---|
2 | n/a | |
---|
3 | n/a | import io |
---|
4 | n/a | import pathlib |
---|
5 | n/a | import stat |
---|
6 | n/a | import sys |
---|
7 | n/a | import tempfile |
---|
8 | n/a | import unittest |
---|
9 | n/a | import zipapp |
---|
10 | n/a | import zipfile |
---|
11 | n/a | |
---|
12 | n/a | from unittest.mock import patch |
---|
13 | n/a | |
---|
14 | n/a | class ZipAppTest(unittest.TestCase): |
---|
15 | n/a | |
---|
16 | n/a | """Test zipapp module functionality.""" |
---|
17 | n/a | |
---|
18 | n/a | def setUp(self): |
---|
19 | n/a | tmpdir = tempfile.TemporaryDirectory() |
---|
20 | n/a | self.addCleanup(tmpdir.cleanup) |
---|
21 | n/a | self.tmpdir = pathlib.Path(tmpdir.name) |
---|
22 | n/a | |
---|
23 | n/a | def test_create_archive(self): |
---|
24 | n/a | # Test packing a directory. |
---|
25 | n/a | source = self.tmpdir / 'source' |
---|
26 | n/a | source.mkdir() |
---|
27 | n/a | (source / '__main__.py').touch() |
---|
28 | n/a | target = self.tmpdir / 'source.pyz' |
---|
29 | n/a | zipapp.create_archive(str(source), str(target)) |
---|
30 | n/a | self.assertTrue(target.is_file()) |
---|
31 | n/a | |
---|
32 | n/a | def test_create_archive_with_pathlib(self): |
---|
33 | n/a | # Test packing a directory using Path objects for source and target. |
---|
34 | n/a | source = self.tmpdir / 'source' |
---|
35 | n/a | source.mkdir() |
---|
36 | n/a | (source / '__main__.py').touch() |
---|
37 | n/a | target = self.tmpdir / 'source.pyz' |
---|
38 | n/a | zipapp.create_archive(source, target) |
---|
39 | n/a | self.assertTrue(target.is_file()) |
---|
40 | n/a | |
---|
41 | n/a | def test_create_archive_with_subdirs(self): |
---|
42 | n/a | # Test packing a directory includes entries for subdirectories. |
---|
43 | n/a | source = self.tmpdir / 'source' |
---|
44 | n/a | source.mkdir() |
---|
45 | n/a | (source / '__main__.py').touch() |
---|
46 | n/a | (source / 'foo').mkdir() |
---|
47 | n/a | (source / 'bar').mkdir() |
---|
48 | n/a | (source / 'foo' / '__init__.py').touch() |
---|
49 | n/a | target = io.BytesIO() |
---|
50 | n/a | zipapp.create_archive(str(source), target) |
---|
51 | n/a | target.seek(0) |
---|
52 | n/a | with zipfile.ZipFile(target, 'r') as z: |
---|
53 | n/a | self.assertIn('foo/', z.namelist()) |
---|
54 | n/a | self.assertIn('bar/', z.namelist()) |
---|
55 | n/a | |
---|
56 | n/a | def test_create_archive_default_target(self): |
---|
57 | n/a | # Test packing a directory to the default name. |
---|
58 | n/a | source = self.tmpdir / 'source' |
---|
59 | n/a | source.mkdir() |
---|
60 | n/a | (source / '__main__.py').touch() |
---|
61 | n/a | zipapp.create_archive(str(source)) |
---|
62 | n/a | expected_target = self.tmpdir / 'source.pyz' |
---|
63 | n/a | self.assertTrue(expected_target.is_file()) |
---|
64 | n/a | |
---|
65 | n/a | def test_no_main(self): |
---|
66 | n/a | # Test that packing a directory with no __main__.py fails. |
---|
67 | n/a | source = self.tmpdir / 'source' |
---|
68 | n/a | source.mkdir() |
---|
69 | n/a | (source / 'foo.py').touch() |
---|
70 | n/a | target = self.tmpdir / 'source.pyz' |
---|
71 | n/a | with self.assertRaises(zipapp.ZipAppError): |
---|
72 | n/a | zipapp.create_archive(str(source), str(target)) |
---|
73 | n/a | |
---|
74 | n/a | def test_main_and_main_py(self): |
---|
75 | n/a | # Test that supplying a main argument with __main__.py fails. |
---|
76 | n/a | source = self.tmpdir / 'source' |
---|
77 | n/a | source.mkdir() |
---|
78 | n/a | (source / '__main__.py').touch() |
---|
79 | n/a | target = self.tmpdir / 'source.pyz' |
---|
80 | n/a | with self.assertRaises(zipapp.ZipAppError): |
---|
81 | n/a | zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
---|
82 | n/a | |
---|
83 | n/a | def test_main_written(self): |
---|
84 | n/a | # Test that the __main__.py is written correctly. |
---|
85 | n/a | source = self.tmpdir / 'source' |
---|
86 | n/a | source.mkdir() |
---|
87 | n/a | (source / 'foo.py').touch() |
---|
88 | n/a | target = self.tmpdir / 'source.pyz' |
---|
89 | n/a | zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
---|
90 | n/a | with zipfile.ZipFile(str(target), 'r') as z: |
---|
91 | n/a | self.assertIn('__main__.py', z.namelist()) |
---|
92 | n/a | self.assertIn(b'pkg.mod.fn()', z.read('__main__.py')) |
---|
93 | n/a | |
---|
94 | n/a | def test_main_only_written_once(self): |
---|
95 | n/a | # Test that we don't write multiple __main__.py files. |
---|
96 | n/a | # The initial implementation had this bug; zip files allow |
---|
97 | n/a | # multiple entries with the same name |
---|
98 | n/a | source = self.tmpdir / 'source' |
---|
99 | n/a | source.mkdir() |
---|
100 | n/a | # Write 2 files, as the original bug wrote __main__.py |
---|
101 | n/a | # once for each file written :-( |
---|
102 | n/a | # See http://bugs.python.org/review/23491/diff/13982/Lib/zipapp.py#newcode67Lib/zipapp.py:67 |
---|
103 | n/a | # (line 67) |
---|
104 | n/a | (source / 'foo.py').touch() |
---|
105 | n/a | (source / 'bar.py').touch() |
---|
106 | n/a | target = self.tmpdir / 'source.pyz' |
---|
107 | n/a | zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
---|
108 | n/a | with zipfile.ZipFile(str(target), 'r') as z: |
---|
109 | n/a | self.assertEqual(1, z.namelist().count('__main__.py')) |
---|
110 | n/a | |
---|
111 | n/a | def test_main_validation(self): |
---|
112 | n/a | # Test that invalid values for main are rejected. |
---|
113 | n/a | source = self.tmpdir / 'source' |
---|
114 | n/a | source.mkdir() |
---|
115 | n/a | target = self.tmpdir / 'source.pyz' |
---|
116 | n/a | problems = [ |
---|
117 | n/a | '', 'foo', 'foo:', ':bar', '12:bar', 'a.b.c.:d', |
---|
118 | n/a | '.a:b', 'a:b.', 'a:.b', 'a:silly name' |
---|
119 | n/a | ] |
---|
120 | n/a | for main in problems: |
---|
121 | n/a | with self.subTest(main=main): |
---|
122 | n/a | with self.assertRaises(zipapp.ZipAppError): |
---|
123 | n/a | zipapp.create_archive(str(source), str(target), main=main) |
---|
124 | n/a | |
---|
125 | n/a | def test_default_no_shebang(self): |
---|
126 | n/a | # Test that no shebang line is written to the target by default. |
---|
127 | n/a | source = self.tmpdir / 'source' |
---|
128 | n/a | source.mkdir() |
---|
129 | n/a | (source / '__main__.py').touch() |
---|
130 | n/a | target = self.tmpdir / 'source.pyz' |
---|
131 | n/a | zipapp.create_archive(str(source), str(target)) |
---|
132 | n/a | with target.open('rb') as f: |
---|
133 | n/a | self.assertNotEqual(f.read(2), b'#!') |
---|
134 | n/a | |
---|
135 | n/a | def test_custom_interpreter(self): |
---|
136 | n/a | # Test that a shebang line with a custom interpreter is written |
---|
137 | n/a | # correctly. |
---|
138 | n/a | source = self.tmpdir / 'source' |
---|
139 | n/a | source.mkdir() |
---|
140 | n/a | (source / '__main__.py').touch() |
---|
141 | n/a | target = self.tmpdir / 'source.pyz' |
---|
142 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
143 | n/a | with target.open('rb') as f: |
---|
144 | n/a | self.assertEqual(f.read(2), b'#!') |
---|
145 | n/a | self.assertEqual(b'python\n', f.readline()) |
---|
146 | n/a | |
---|
147 | n/a | def test_pack_to_fileobj(self): |
---|
148 | n/a | # Test that we can pack to a file object. |
---|
149 | n/a | source = self.tmpdir / 'source' |
---|
150 | n/a | source.mkdir() |
---|
151 | n/a | (source / '__main__.py').touch() |
---|
152 | n/a | target = io.BytesIO() |
---|
153 | n/a | zipapp.create_archive(str(source), target, interpreter='python') |
---|
154 | n/a | self.assertTrue(target.getvalue().startswith(b'#!python\n')) |
---|
155 | n/a | |
---|
156 | n/a | def test_read_shebang(self): |
---|
157 | n/a | # Test that we can read the shebang line correctly. |
---|
158 | n/a | source = self.tmpdir / 'source' |
---|
159 | n/a | source.mkdir() |
---|
160 | n/a | (source / '__main__.py').touch() |
---|
161 | n/a | target = self.tmpdir / 'source.pyz' |
---|
162 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
163 | n/a | self.assertEqual(zipapp.get_interpreter(str(target)), 'python') |
---|
164 | n/a | |
---|
165 | n/a | def test_read_missing_shebang(self): |
---|
166 | n/a | # Test that reading the shebang line of a file without one returns None. |
---|
167 | n/a | source = self.tmpdir / 'source' |
---|
168 | n/a | source.mkdir() |
---|
169 | n/a | (source / '__main__.py').touch() |
---|
170 | n/a | target = self.tmpdir / 'source.pyz' |
---|
171 | n/a | zipapp.create_archive(str(source), str(target)) |
---|
172 | n/a | self.assertEqual(zipapp.get_interpreter(str(target)), None) |
---|
173 | n/a | |
---|
174 | n/a | def test_modify_shebang(self): |
---|
175 | n/a | # Test that we can change the shebang of a file. |
---|
176 | n/a | source = self.tmpdir / 'source' |
---|
177 | n/a | source.mkdir() |
---|
178 | n/a | (source / '__main__.py').touch() |
---|
179 | n/a | target = self.tmpdir / 'source.pyz' |
---|
180 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
181 | n/a | new_target = self.tmpdir / 'changed.pyz' |
---|
182 | n/a | zipapp.create_archive(str(target), str(new_target), interpreter='python2.7') |
---|
183 | n/a | self.assertEqual(zipapp.get_interpreter(str(new_target)), 'python2.7') |
---|
184 | n/a | |
---|
185 | n/a | def test_write_shebang_to_fileobj(self): |
---|
186 | n/a | # Test that we can change the shebang of a file, writing the result to a |
---|
187 | n/a | # file object. |
---|
188 | n/a | source = self.tmpdir / 'source' |
---|
189 | n/a | source.mkdir() |
---|
190 | n/a | (source / '__main__.py').touch() |
---|
191 | n/a | target = self.tmpdir / 'source.pyz' |
---|
192 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
193 | n/a | new_target = io.BytesIO() |
---|
194 | n/a | zipapp.create_archive(str(target), new_target, interpreter='python2.7') |
---|
195 | n/a | self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n')) |
---|
196 | n/a | |
---|
197 | n/a | def test_read_from_pathobj(self): |
---|
198 | n/a | # Test that we can copy an archive using a pathlib.Path object |
---|
199 | n/a | # for the source. |
---|
200 | n/a | source = self.tmpdir / 'source' |
---|
201 | n/a | source.mkdir() |
---|
202 | n/a | (source / '__main__.py').touch() |
---|
203 | n/a | target1 = self.tmpdir / 'target1.pyz' |
---|
204 | n/a | target2 = self.tmpdir / 'target2.pyz' |
---|
205 | n/a | zipapp.create_archive(source, target1, interpreter='python') |
---|
206 | n/a | zipapp.create_archive(target1, target2, interpreter='python2.7') |
---|
207 | n/a | self.assertEqual(zipapp.get_interpreter(target2), 'python2.7') |
---|
208 | n/a | |
---|
209 | n/a | def test_read_from_fileobj(self): |
---|
210 | n/a | # Test that we can copy an archive using an open file object. |
---|
211 | n/a | source = self.tmpdir / 'source' |
---|
212 | n/a | source.mkdir() |
---|
213 | n/a | (source / '__main__.py').touch() |
---|
214 | n/a | target = self.tmpdir / 'source.pyz' |
---|
215 | n/a | temp_archive = io.BytesIO() |
---|
216 | n/a | zipapp.create_archive(str(source), temp_archive, interpreter='python') |
---|
217 | n/a | new_target = io.BytesIO() |
---|
218 | n/a | temp_archive.seek(0) |
---|
219 | n/a | zipapp.create_archive(temp_archive, new_target, interpreter='python2.7') |
---|
220 | n/a | self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n')) |
---|
221 | n/a | |
---|
222 | n/a | def test_remove_shebang(self): |
---|
223 | n/a | # Test that we can remove the shebang from a file. |
---|
224 | n/a | source = self.tmpdir / 'source' |
---|
225 | n/a | source.mkdir() |
---|
226 | n/a | (source / '__main__.py').touch() |
---|
227 | n/a | target = self.tmpdir / 'source.pyz' |
---|
228 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
229 | n/a | new_target = self.tmpdir / 'changed.pyz' |
---|
230 | n/a | zipapp.create_archive(str(target), str(new_target), interpreter=None) |
---|
231 | n/a | self.assertEqual(zipapp.get_interpreter(str(new_target)), None) |
---|
232 | n/a | |
---|
233 | n/a | def test_content_of_copied_archive(self): |
---|
234 | n/a | # Test that copying an archive doesn't corrupt it. |
---|
235 | n/a | source = self.tmpdir / 'source' |
---|
236 | n/a | source.mkdir() |
---|
237 | n/a | (source / '__main__.py').touch() |
---|
238 | n/a | target = io.BytesIO() |
---|
239 | n/a | zipapp.create_archive(str(source), target, interpreter='python') |
---|
240 | n/a | new_target = io.BytesIO() |
---|
241 | n/a | target.seek(0) |
---|
242 | n/a | zipapp.create_archive(target, new_target, interpreter=None) |
---|
243 | n/a | new_target.seek(0) |
---|
244 | n/a | with zipfile.ZipFile(new_target, 'r') as z: |
---|
245 | n/a | self.assertEqual(set(z.namelist()), {'__main__.py'}) |
---|
246 | n/a | |
---|
247 | n/a | # (Unix only) tests that archives with shebang lines are made executable |
---|
248 | n/a | @unittest.skipIf(sys.platform == 'win32', |
---|
249 | n/a | 'Windows does not support an executable bit') |
---|
250 | n/a | def test_shebang_is_executable(self): |
---|
251 | n/a | # Test that an archive with a shebang line is made executable. |
---|
252 | n/a | source = self.tmpdir / 'source' |
---|
253 | n/a | source.mkdir() |
---|
254 | n/a | (source / '__main__.py').touch() |
---|
255 | n/a | target = self.tmpdir / 'source.pyz' |
---|
256 | n/a | zipapp.create_archive(str(source), str(target), interpreter='python') |
---|
257 | n/a | self.assertTrue(target.stat().st_mode & stat.S_IEXEC) |
---|
258 | n/a | |
---|
259 | n/a | @unittest.skipIf(sys.platform == 'win32', |
---|
260 | n/a | 'Windows does not support an executable bit') |
---|
261 | n/a | def test_no_shebang_is_not_executable(self): |
---|
262 | n/a | # Test that an archive with no shebang line is not made executable. |
---|
263 | n/a | source = self.tmpdir / 'source' |
---|
264 | n/a | source.mkdir() |
---|
265 | n/a | (source / '__main__.py').touch() |
---|
266 | n/a | target = self.tmpdir / 'source.pyz' |
---|
267 | n/a | zipapp.create_archive(str(source), str(target), interpreter=None) |
---|
268 | n/a | self.assertFalse(target.stat().st_mode & stat.S_IEXEC) |
---|
269 | n/a | |
---|
270 | n/a | |
---|
271 | n/a | class ZipAppCmdlineTest(unittest.TestCase): |
---|
272 | n/a | |
---|
273 | n/a | """Test zipapp module command line API.""" |
---|
274 | n/a | |
---|
275 | n/a | def setUp(self): |
---|
276 | n/a | tmpdir = tempfile.TemporaryDirectory() |
---|
277 | n/a | self.addCleanup(tmpdir.cleanup) |
---|
278 | n/a | self.tmpdir = pathlib.Path(tmpdir.name) |
---|
279 | n/a | |
---|
280 | n/a | def make_archive(self): |
---|
281 | n/a | # Test that an archive with no shebang line is not made executable. |
---|
282 | n/a | source = self.tmpdir / 'source' |
---|
283 | n/a | source.mkdir() |
---|
284 | n/a | (source / '__main__.py').touch() |
---|
285 | n/a | target = self.tmpdir / 'source.pyz' |
---|
286 | n/a | zipapp.create_archive(source, target) |
---|
287 | n/a | return target |
---|
288 | n/a | |
---|
289 | n/a | def test_cmdline_create(self): |
---|
290 | n/a | # Test the basic command line API. |
---|
291 | n/a | source = self.tmpdir / 'source' |
---|
292 | n/a | source.mkdir() |
---|
293 | n/a | (source / '__main__.py').touch() |
---|
294 | n/a | args = [str(source)] |
---|
295 | n/a | zipapp.main(args) |
---|
296 | n/a | target = source.with_suffix('.pyz') |
---|
297 | n/a | self.assertTrue(target.is_file()) |
---|
298 | n/a | |
---|
299 | n/a | def test_cmdline_copy(self): |
---|
300 | n/a | # Test copying an archive. |
---|
301 | n/a | original = self.make_archive() |
---|
302 | n/a | target = self.tmpdir / 'target.pyz' |
---|
303 | n/a | args = [str(original), '-o', str(target)] |
---|
304 | n/a | zipapp.main(args) |
---|
305 | n/a | self.assertTrue(target.is_file()) |
---|
306 | n/a | |
---|
307 | n/a | def test_cmdline_copy_inplace(self): |
---|
308 | n/a | # Test copying an archive in place fails. |
---|
309 | n/a | original = self.make_archive() |
---|
310 | n/a | target = self.tmpdir / 'target.pyz' |
---|
311 | n/a | args = [str(original), '-o', str(original)] |
---|
312 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
313 | n/a | zipapp.main(args) |
---|
314 | n/a | # Program should exit with a non-zero returm code. |
---|
315 | n/a | self.assertTrue(cm.exception.code) |
---|
316 | n/a | |
---|
317 | n/a | def test_cmdline_copy_change_main(self): |
---|
318 | n/a | # Test copying an archive doesn't allow changing __main__.py. |
---|
319 | n/a | original = self.make_archive() |
---|
320 | n/a | target = self.tmpdir / 'target.pyz' |
---|
321 | n/a | args = [str(original), '-o', str(target), '-m', 'foo:bar'] |
---|
322 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
323 | n/a | zipapp.main(args) |
---|
324 | n/a | # Program should exit with a non-zero returm code. |
---|
325 | n/a | self.assertTrue(cm.exception.code) |
---|
326 | n/a | |
---|
327 | n/a | @patch('sys.stdout', new_callable=io.StringIO) |
---|
328 | n/a | def test_info_command(self, mock_stdout): |
---|
329 | n/a | # Test the output of the info command. |
---|
330 | n/a | target = self.make_archive() |
---|
331 | n/a | args = [str(target), '--info'] |
---|
332 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
333 | n/a | zipapp.main(args) |
---|
334 | n/a | # Program should exit with a zero returm code. |
---|
335 | n/a | self.assertEqual(cm.exception.code, 0) |
---|
336 | n/a | self.assertEqual(mock_stdout.getvalue(), "Interpreter: <none>\n") |
---|
337 | n/a | |
---|
338 | n/a | def test_info_error(self): |
---|
339 | n/a | # Test the info command fails when the archive does not exist. |
---|
340 | n/a | target = self.tmpdir / 'dummy.pyz' |
---|
341 | n/a | args = [str(target), '--info'] |
---|
342 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
343 | n/a | zipapp.main(args) |
---|
344 | n/a | # Program should exit with a non-zero returm code. |
---|
345 | n/a | self.assertTrue(cm.exception.code) |
---|
346 | n/a | |
---|
347 | n/a | |
---|
348 | n/a | if __name__ == "__main__": |
---|
349 | n/a | unittest.main() |
---|