| 1 |
"""Test harness for the zipapp module.""" |
| 2 |
|
| 3 |
import io |
| 4 |
import pathlib |
| 5 |
import stat |
| 6 |
import sys |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
import zipapp |
| 10 |
import zipfile |
| 11 |
from test.support import requires_zlib |
| 12 |
from test.support import os_helper |
| 13 |
|
| 14 |
from unittest.mock import patch |
| 15 |
|
| 16 |
class ZipAppTest(unittest.TestCase): |
| 17 |
|
| 18 |
"""Test zipapp module functionality.""" |
| 19 |
|
| 20 |
def setUp(self): |
| 21 |
tmpdir = tempfile.TemporaryDirectory() |
| 22 |
self.addCleanup(tmpdir.cleanup) |
| 23 |
self.tmpdir = pathlib.Path(tmpdir.name) |
| 24 |
|
| 25 |
def test_create_archive(self): |
| 26 |
# Test packing a directory. |
| 27 |
source = self.tmpdir / 'source' |
| 28 |
source.mkdir() |
| 29 |
(source / '__main__.py').touch() |
| 30 |
target = self.tmpdir / 'source.pyz' |
| 31 |
zipapp.create_archive(str(source), str(target)) |
| 32 |
self.assertTrue(target.is_file()) |
| 33 |
|
| 34 |
def test_create_archive_with_pathlib(self): |
| 35 |
# Test packing a directory using Path objects for source and target. |
| 36 |
source = self.tmpdir / 'source' |
| 37 |
source.mkdir() |
| 38 |
(source / '__main__.py').touch() |
| 39 |
target = self.tmpdir / 'source.pyz' |
| 40 |
zipapp.create_archive(source, target) |
| 41 |
self.assertTrue(target.is_file()) |
| 42 |
|
| 43 |
def test_create_archive_with_subdirs(self): |
| 44 |
# Test packing a directory includes entries for subdirectories. |
| 45 |
source = self.tmpdir / 'source' |
| 46 |
source.mkdir() |
| 47 |
(source / '__main__.py').touch() |
| 48 |
(source / 'foo').mkdir() |
| 49 |
(source / 'bar').mkdir() |
| 50 |
(source / 'foo' / '__init__.py').touch() |
| 51 |
target = io.BytesIO() |
| 52 |
zipapp.create_archive(str(source), target) |
| 53 |
target.seek(0) |
| 54 |
with zipfile.ZipFile(target, 'r') as z: |
| 55 |
self.assertIn('foo/', z.namelist()) |
| 56 |
self.assertIn('bar/', z.namelist()) |
| 57 |
|
| 58 |
def test_create_sorted_archive(self): |
| 59 |
# Test that zipapps order their files by name |
| 60 |
source = self.tmpdir / 'source' |
| 61 |
source.mkdir() |
| 62 |
(source / 'zed.py').touch() |
| 63 |
(source / 'bin').mkdir() |
| 64 |
(source / 'bin' / 'qux').touch() |
| 65 |
(source / 'bin' / 'baz').touch() |
| 66 |
(source / '__main__.py').touch() |
| 67 |
target = io.BytesIO() |
| 68 |
zipapp.create_archive(str(source), target) |
| 69 |
target.seek(0) |
| 70 |
with zipfile.ZipFile(target, 'r') as zf: |
| 71 |
self.assertEqual(zf.namelist(), |
| 72 |
["__main__.py", "bin/", "bin/baz", "bin/qux", "zed.py"]) |
| 73 |
|
| 74 |
def test_create_archive_with_filter(self): |
| 75 |
# Test packing a directory and using filter to specify |
| 76 |
# which files to include. |
| 77 |
def skip_pyc_files(path): |
| 78 |
return path.suffix != '.pyc' |
| 79 |
source = self.tmpdir / 'source' |
| 80 |
source.mkdir() |
| 81 |
(source / '__main__.py').touch() |
| 82 |
(source / 'test.py').touch() |
| 83 |
(source / 'test.pyc').touch() |
| 84 |
target = self.tmpdir / 'source.pyz' |
| 85 |
|
| 86 |
zipapp.create_archive(source, target, filter=skip_pyc_files) |
| 87 |
with zipfile.ZipFile(target, 'r') as z: |
| 88 |
self.assertIn('__main__.py', z.namelist()) |
| 89 |
self.assertIn('test.py', z.namelist()) |
| 90 |
self.assertNotIn('test.pyc', z.namelist()) |
| 91 |
|
| 92 |
def test_create_archive_self_insertion(self): |
| 93 |
# When creating an archive, we shouldn't |
| 94 |
# include the archive in the list of files to add. |
| 95 |
source = self.tmpdir |
| 96 |
(source / '__main__.py').touch() |
| 97 |
(source / 'test.py').touch() |
| 98 |
target = self.tmpdir / 'target.pyz' |
| 99 |
|
| 100 |
zipapp.create_archive(source, target) |
| 101 |
with zipfile.ZipFile(target, 'r') as z: |
| 102 |
self.assertEqual(len(z.namelist()), 2) |
| 103 |
self.assertIn('__main__.py', z.namelist()) |
| 104 |
self.assertIn('test.py', z.namelist()) |
| 105 |
|
| 106 |
def test_target_overwrites_source_file(self): |
| 107 |
# The target cannot be one of the files to add. |
| 108 |
source = self.tmpdir |
| 109 |
(source / '__main__.py').touch() |
| 110 |
target = source / 'target.pyz' |
| 111 |
target.touch() |
| 112 |
|
| 113 |
with self.assertRaises(zipapp.ZipAppError): |
| 114 |
zipapp.create_archive(source, target) |
| 115 |
|
| 116 |
def test_target_overwrites_filtered_source_file(self): |
| 117 |
# If there's a filter that excludes the target, |
| 118 |
# the overwrite check shouldn't trigger. |
| 119 |
source = self.tmpdir |
| 120 |
(source / '__main__.py').touch() |
| 121 |
target = source / 'target.pyz' |
| 122 |
target.touch() |
| 123 |
pyz_filter = lambda p: not p.match('*.pyz') |
| 124 |
zipapp.create_archive(source, target, filter=pyz_filter) |
| 125 |
with zipfile.ZipFile(target, 'r') as z: |
| 126 |
self.assertEqual(len(z.namelist()), 1) |
| 127 |
self.assertIn('__main__.py', z.namelist()) |
| 128 |
|
| 129 |
def test_create_archive_filter_exclude_dir(self): |
| 130 |
# Test packing a directory and using a filter to exclude a |
| 131 |
# subdirectory (ensures that the path supplied to include |
| 132 |
# is relative to the source location, as expected). |
| 133 |
def skip_dummy_dir(path): |
| 134 |
return path.parts[0] != 'dummy' |
| 135 |
source = self.tmpdir / 'source' |
| 136 |
source.mkdir() |
| 137 |
(source / '__main__.py').touch() |
| 138 |
(source / 'test.py').touch() |
| 139 |
(source / 'dummy').mkdir() |
| 140 |
(source / 'dummy' / 'test2.py').touch() |
| 141 |
target = self.tmpdir / 'source.pyz' |
| 142 |
|
| 143 |
zipapp.create_archive(source, target, filter=skip_dummy_dir) |
| 144 |
with zipfile.ZipFile(target, 'r') as z: |
| 145 |
self.assertEqual(len(z.namelist()), 2) |
| 146 |
self.assertIn('__main__.py', z.namelist()) |
| 147 |
self.assertIn('test.py', z.namelist()) |
| 148 |
|
| 149 |
def test_create_archive_default_target(self): |
| 150 |
# Test packing a directory to the default name. |
| 151 |
source = self.tmpdir / 'source' |
| 152 |
source.mkdir() |
| 153 |
(source / '__main__.py').touch() |
| 154 |
zipapp.create_archive(str(source)) |
| 155 |
expected_target = self.tmpdir / 'source.pyz' |
| 156 |
self.assertTrue(expected_target.is_file()) |
| 157 |
|
| 158 |
@requires_zlib() |
| 159 |
def test_create_archive_with_compression(self): |
| 160 |
# Test packing a directory into a compressed archive. |
| 161 |
source = self.tmpdir / 'source' |
| 162 |
source.mkdir() |
| 163 |
(source / '__main__.py').touch() |
| 164 |
(source / 'test.py').touch() |
| 165 |
target = self.tmpdir / 'source.pyz' |
| 166 |
|
| 167 |
zipapp.create_archive(source, target, compressed=True) |
| 168 |
with zipfile.ZipFile(target, 'r') as z: |
| 169 |
for name in ('__main__.py', 'test.py'): |
| 170 |
self.assertEqual(z.getinfo(name).compress_type, |
| 171 |
zipfile.ZIP_DEFLATED) |
| 172 |
|
| 173 |
def test_no_main(self): |
| 174 |
# Test that packing a directory with no __main__.py fails. |
| 175 |
source = self.tmpdir / 'source' |
| 176 |
source.mkdir() |
| 177 |
(source / 'foo.py').touch() |
| 178 |
target = self.tmpdir / 'source.pyz' |
| 179 |
with self.assertRaises(zipapp.ZipAppError): |
| 180 |
zipapp.create_archive(str(source), str(target)) |
| 181 |
|
| 182 |
def test_main_and_main_py(self): |
| 183 |
# Test that supplying a main argument with __main__.py fails. |
| 184 |
source = self.tmpdir / 'source' |
| 185 |
source.mkdir() |
| 186 |
(source / '__main__.py').touch() |
| 187 |
target = self.tmpdir / 'source.pyz' |
| 188 |
with self.assertRaises(zipapp.ZipAppError): |
| 189 |
zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
| 190 |
|
| 191 |
def test_main_written(self): |
| 192 |
# Test that the __main__.py is written correctly. |
| 193 |
source = self.tmpdir / 'source' |
| 194 |
source.mkdir() |
| 195 |
(source / 'foo.py').touch() |
| 196 |
target = self.tmpdir / 'source.pyz' |
| 197 |
zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
| 198 |
with zipfile.ZipFile(str(target), 'r') as z: |
| 199 |
self.assertIn('__main__.py', z.namelist()) |
| 200 |
self.assertIn(b'pkg.mod.fn()', z.read('__main__.py')) |
| 201 |
|
| 202 |
def test_main_only_written_once(self): |
| 203 |
# Test that we don't write multiple __main__.py files. |
| 204 |
# The initial implementation had this bug; zip files allow |
| 205 |
# multiple entries with the same name |
| 206 |
source = self.tmpdir / 'source' |
| 207 |
source.mkdir() |
| 208 |
# Write 2 files, as the original bug wrote __main__.py |
| 209 |
# once for each file written :-( |
| 210 |
# See http://bugs.python.org/review/23491/diff/13982/Lib/zipapp.py#newcode67Lib/zipapp.py:67 |
| 211 |
# (line 67) |
| 212 |
(source / 'foo.py').touch() |
| 213 |
(source / 'bar.py').touch() |
| 214 |
target = self.tmpdir / 'source.pyz' |
| 215 |
zipapp.create_archive(str(source), str(target), main='pkg.mod:fn') |
| 216 |
with zipfile.ZipFile(str(target), 'r') as z: |
| 217 |
self.assertEqual(1, z.namelist().count('__main__.py')) |
| 218 |
|
| 219 |
def test_main_validation(self): |
| 220 |
# Test that invalid values for main are rejected. |
| 221 |
source = self.tmpdir / 'source' |
| 222 |
source.mkdir() |
| 223 |
target = self.tmpdir / 'source.pyz' |
| 224 |
problems = [ |
| 225 |
'', 'foo', 'foo:', ':bar', '12:bar', 'a.b.c.:d', |
| 226 |
'.a:b', 'a:b.', 'a:.b', 'a:silly name' |
| 227 |
] |
| 228 |
for main in problems: |
| 229 |
with self.subTest(main=main): |
| 230 |
with self.assertRaises(zipapp.ZipAppError): |
| 231 |
zipapp.create_archive(str(source), str(target), main=main) |
| 232 |
|
| 233 |
def test_default_no_shebang(self): |
| 234 |
# Test that no shebang line is written to the target by default. |
| 235 |
source = self.tmpdir / 'source' |
| 236 |
source.mkdir() |
| 237 |
(source / '__main__.py').touch() |
| 238 |
target = self.tmpdir / 'source.pyz' |
| 239 |
zipapp.create_archive(str(source), str(target)) |
| 240 |
with target.open('rb') as f: |
| 241 |
self.assertNotEqual(f.read(2), b'#!') |
| 242 |
|
| 243 |
def test_custom_interpreter(self): |
| 244 |
# Test that a shebang line with a custom interpreter is written |
| 245 |
# correctly. |
| 246 |
source = self.tmpdir / 'source' |
| 247 |
source.mkdir() |
| 248 |
(source / '__main__.py').touch() |
| 249 |
target = self.tmpdir / 'source.pyz' |
| 250 |
zipapp.create_archive(str(source), str(target), interpreter='python') |
| 251 |
with target.open('rb') as f: |
| 252 |
self.assertEqual(f.read(2), b'#!') |
| 253 |
self.assertEqual(b'python\n', f.readline()) |
| 254 |
|
| 255 |
def test_pack_to_fileobj(self): |
| 256 |
# Test that we can pack to a file object. |
| 257 |
source = self.tmpdir / 'source' |
| 258 |
source.mkdir() |
| 259 |
(source / '__main__.py').touch() |
| 260 |
target = io.BytesIO() |
| 261 |
zipapp.create_archive(str(source), target, interpreter='python') |
| 262 |
self.assertStartsWith(target.getvalue(), b'#!python\n') |
| 263 |
|
| 264 |
def test_read_shebang(self): |
| 265 |
# Test that we can read the shebang line correctly. |
| 266 |
source = self.tmpdir / 'source' |
| 267 |
source.mkdir() |
| 268 |
(source / '__main__.py').touch() |
| 269 |
target = self.tmpdir / 'source.pyz' |
| 270 |
zipapp.create_archive(str(source), str(target), interpreter='python') |
| 271 |
self.assertEqual(zipapp.get_interpreter(str(target)), 'python') |
| 272 |
|
| 273 |
def test_read_missing_shebang(self): |
| 274 |
# Test that reading the shebang line of a file without one returns None. |
| 275 |
source = self.tmpdir / 'source' |
| 276 |
source.mkdir() |
| 277 |
(source / '__main__.py').touch() |
| 278 |
target = self.tmpdir / 'source.pyz' |
| 279 |
zipapp.create_archive(str(source), str(target)) |
| 280 |
self.assertEqual(zipapp.get_interpreter(str(target)), None) |
| 281 |
|
| 282 |
def test_modify_shebang(self): |
| 283 |
# Test that we can change the shebang of a file. |
| 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='python') |
| 289 |
new_target = self.tmpdir / 'changed.pyz' |
| 290 |
zipapp.create_archive(str(target), str(new_target), interpreter='python2.7') |
| 291 |
self.assertEqual(zipapp.get_interpreter(str(new_target)), 'python2.7') |
| 292 |
|
| 293 |
def test_write_shebang_to_fileobj(self): |
| 294 |
# Test that we can change the shebang of a file, writing the result to a |
| 295 |
# file object. |
| 296 |
source = self.tmpdir / 'source' |
| 297 |
source.mkdir() |
| 298 |
(source / '__main__.py').touch() |
| 299 |
target = self.tmpdir / 'source.pyz' |
| 300 |
zipapp.create_archive(str(source), str(target), interpreter='python') |
| 301 |
new_target = io.BytesIO() |
| 302 |
zipapp.create_archive(str(target), new_target, interpreter='python2.7') |
| 303 |
self.assertStartsWith(new_target.getvalue(), b'#!python2.7\n') |
| 304 |
|
| 305 |
def test_read_from_pathlike_obj(self): |
| 306 |
# Test that we can copy an archive using a path-like object |
| 307 |
# for the source. |
| 308 |
source = self.tmpdir / 'source' |
| 309 |
source.mkdir() |
| 310 |
(source / '__main__.py').touch() |
| 311 |
source = os_helper.FakePath(str(source)) |
| 312 |
target1 = os_helper.FakePath(str(self.tmpdir / 'target1.pyz')) |
| 313 |
target2 = os_helper.FakePath(str(self.tmpdir / 'target2.pyz')) |
| 314 |
zipapp.create_archive(source, target1, interpreter='python') |
| 315 |
zipapp.create_archive(target1, target2, interpreter='python2.7') |
| 316 |
self.assertEqual(zipapp.get_interpreter(target2), 'python2.7') |
| 317 |
|
| 318 |
def test_read_from_fileobj(self): |
| 319 |
# Test that we can copy an archive using an open file object. |
| 320 |
source = self.tmpdir / 'source' |
| 321 |
source.mkdir() |
| 322 |
(source / '__main__.py').touch() |
| 323 |
target = self.tmpdir / 'source.pyz' |
| 324 |
temp_archive = io.BytesIO() |
| 325 |
zipapp.create_archive(str(source), temp_archive, interpreter='python') |
| 326 |
new_target = io.BytesIO() |
| 327 |
temp_archive.seek(0) |
| 328 |
zipapp.create_archive(temp_archive, new_target, interpreter='python2.7') |
| 329 |
self.assertStartsWith(new_target.getvalue(), b'#!python2.7\n') |
| 330 |
|
| 331 |
def test_remove_shebang(self): |
| 332 |
# Test that we can remove the shebang from a file. |
| 333 |
source = self.tmpdir / 'source' |
| 334 |
source.mkdir() |
| 335 |
(source / '__main__.py').touch() |
| 336 |
target = self.tmpdir / 'source.pyz' |
| 337 |
zipapp.create_archive(str(source), str(target), interpreter='python') |
| 338 |
new_target = self.tmpdir / 'changed.pyz' |
| 339 |
zipapp.create_archive(str(target), str(new_target), interpreter=None) |
| 340 |
self.assertEqual(zipapp.get_interpreter(str(new_target)), None) |
| 341 |
|
| 342 |
def test_content_of_copied_archive(self): |
| 343 |
# Test that copying an archive doesn't corrupt it. |
| 344 |
source = self.tmpdir / 'source' |
| 345 |
source.mkdir() |
| 346 |
(source / '__main__.py').touch() |
| 347 |
target = io.BytesIO() |
| 348 |
zipapp.create_archive(str(source), target, interpreter='python') |
| 349 |
new_target = io.BytesIO() |
| 350 |
target.seek(0) |
| 351 |
zipapp.create_archive(target, new_target, interpreter=None) |
| 352 |
new_target.seek(0) |
| 353 |
with zipfile.ZipFile(new_target, 'r') as z: |
| 354 |
self.assertEqual(set(z.namelist()), {'__main__.py'}) |
| 355 |
|
| 356 |
# (Unix only) tests that archives with shebang lines are made executable |
| 357 |
@unittest.skipIf(sys.platform == 'win32', |
| 358 |
'Windows does not support an executable bit') |
| 359 |
@os_helper.skip_unless_working_chmod |
| 360 |
def test_shebang_is_executable(self): |
| 361 |
# Test that an archive with a shebang line is made executable. |
| 362 |
source = self.tmpdir / 'source' |
| 363 |
source.mkdir() |
| 364 |
(source / '__main__.py').touch() |
| 365 |
target = self.tmpdir / 'source.pyz' |
| 366 |
zipapp.create_archive(str(source), str(target), interpreter='python') |
| 367 |
self.assertTrue(target.stat().st_mode & stat.S_IEXEC) |
| 368 |
|
| 369 |
@unittest.skipIf(sys.platform == 'win32', |
| 370 |
'Windows does not support an executable bit') |
| 371 |
def test_no_shebang_is_not_executable(self): |
| 372 |
# Test that an archive with no shebang line is not made executable. |
| 373 |
source = self.tmpdir / 'source' |
| 374 |
source.mkdir() |
| 375 |
(source / '__main__.py').touch() |
| 376 |
target = self.tmpdir / 'source.pyz' |
| 377 |
zipapp.create_archive(str(source), str(target), interpreter=None) |
| 378 |
self.assertFalse(target.stat().st_mode & stat.S_IEXEC) |
| 379 |
|
| 380 |
|
| 381 |
class ZipAppCmdlineTest(unittest.TestCase): |
| 382 |
|
| 383 |
"""Test zipapp module command line API.""" |
| 384 |
|
| 385 |
def setUp(self): |
| 386 |
tmpdir = tempfile.TemporaryDirectory() |
| 387 |
self.addCleanup(tmpdir.cleanup) |
| 388 |
self.tmpdir = pathlib.Path(tmpdir.name) |
| 389 |
|
| 390 |
def make_archive(self): |
| 391 |
# Test that an archive with no shebang line is not made executable. |
| 392 |
source = self.tmpdir / 'source' |
| 393 |
source.mkdir() |
| 394 |
(source / '__main__.py').touch() |
| 395 |
target = self.tmpdir / 'source.pyz' |
| 396 |
zipapp.create_archive(source, target) |
| 397 |
return target |
| 398 |
|
| 399 |
def test_cmdline_create(self): |
| 400 |
# Test the basic command line API. |
| 401 |
source = self.tmpdir / 'source' |
| 402 |
source.mkdir() |
| 403 |
(source / '__main__.py').touch() |
| 404 |
args = [str(source)] |
| 405 |
zipapp.main(args) |
| 406 |
target = source.with_suffix('.pyz') |
| 407 |
self.assertTrue(target.is_file()) |
| 408 |
|
| 409 |
def test_cmdline_copy(self): |
| 410 |
# Test copying an archive. |
| 411 |
original = self.make_archive() |
| 412 |
target = self.tmpdir / 'target.pyz' |
| 413 |
args = [str(original), '-o', str(target)] |
| 414 |
zipapp.main(args) |
| 415 |
self.assertTrue(target.is_file()) |
| 416 |
|
| 417 |
def test_cmdline_copy_inplace(self): |
| 418 |
# Test copying an archive in place fails. |
| 419 |
original = self.make_archive() |
| 420 |
target = self.tmpdir / 'target.pyz' |
| 421 |
args = [str(original), '-o', str(original)] |
| 422 |
with self.assertRaises(SystemExit) as cm: |
| 423 |
zipapp.main(args) |
| 424 |
# Program should exit with a non-zero return code. |
| 425 |
self.assertTrue(cm.exception.code) |
| 426 |
|
| 427 |
def test_cmdline_copy_change_main(self): |
| 428 |
# Test copying an archive doesn't allow changing __main__.py. |
| 429 |
original = self.make_archive() |
| 430 |
target = self.tmpdir / 'target.pyz' |
| 431 |
args = [str(original), '-o', str(target), '-m', 'foo:bar'] |
| 432 |
with self.assertRaises(SystemExit) as cm: |
| 433 |
zipapp.main(args) |
| 434 |
# Program should exit with a non-zero return code. |
| 435 |
self.assertTrue(cm.exception.code) |
| 436 |
|
| 437 |
@patch('sys.stdout', new_callable=io.StringIO) |
| 438 |
def test_info_command(self, mock_stdout): |
| 439 |
# Test the output of the info command. |
| 440 |
target = self.make_archive() |
| 441 |
args = [str(target), '--info'] |
| 442 |
with self.assertRaises(SystemExit) as cm: |
| 443 |
zipapp.main(args) |
| 444 |
# Program should exit with a zero return code. |
| 445 |
self.assertEqual(cm.exception.code, 0) |
| 446 |
self.assertEqual(mock_stdout.getvalue(), "Interpreter: <none>\n") |
| 447 |
|
| 448 |
def test_info_error(self): |
| 449 |
# Test the info command fails when the archive does not exist. |
| 450 |
target = self.tmpdir / 'dummy.pyz' |
| 451 |
args = [str(target), '--info'] |
| 452 |
with self.assertRaises(SystemExit) as cm: |
| 453 |
zipapp.main(args) |
| 454 |
# Program should exit with a non-zero return code. |
| 455 |
self.assertTrue(cm.exception.code) |
| 456 |
|
| 457 |
|
| 458 |
if __name__ == "__main__": |
| 459 |
unittest.main() |