gh-130379: Fix incorrect zipapp logic to avoid including the target in itself (gh-130509)

This commit is contained in:
Paul Moore 2025-02-26 11:25:30 +00:00 committed by GitHub
parent f976892b7d
commit 64ccbbbf36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 2 deletions

View file

@ -89,6 +89,30 @@ class ZipAppTest(unittest.TestCase):
self.assertIn('test.py', z.namelist())
self.assertNotIn('test.pyc', z.namelist())
def test_create_archive_self_insertion(self):
# When creating an archive, we shouldn't
# include the archive in the list of files to add.
source = self.tmpdir
(source / '__main__.py').touch()
(source / 'test.py').touch()
target = self.tmpdir / 'target.pyz'
zipapp.create_archive(source, target)
with zipfile.ZipFile(target, 'r') as z:
self.assertEqual(len(z.namelist()), 2)
self.assertIn('__main__.py', z.namelist())
self.assertIn('test.py', z.namelist())
def test_target_overwrites_source_file(self):
# The target cannot be one of the files to add.
source = self.tmpdir
(source / '__main__.py').touch()
target = source / 'target.pyz'
target.touch()
with self.assertRaises(zipapp.ZipAppError):
zipapp.create_archive(source, target)
def test_create_archive_filter_exclude_dir(self):
# Test packing a directory and using a filter to exclude a
# subdirectory (ensures that the path supplied to include