8000 [3.12] gh-118148: Improve tests for shutil.make_archive() (GH-118149)… · python/cpython@312d819 · GitHub
[go: up one dir, main page]

Skip to content

Commit 312d819

Browse files
[3.12] gh-118148: Improve tests for shutil.make_archive() (GH-118149) (GH-118151)
(cherry picked from commit 287d939) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent b1e5f27 commit 312d819

File tree

1 file changed

+176
-71
lines changed

1 file changed

+176
-71
lines changed

Lib/test/test_shutil.py

Lines changed: 176 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,42 +1563,6 @@ class TestArchives(BaseTest, unittest.TestCase):
15631563

15641564
### shutil.make_archive
15651565

1566-
@support.requires_zlib()
1567-
def test_make_tarball(self):
1568-
# creating something to tar
1569-
root_dir, base_dir = self._create_files('')
1570-
1571-
tmpdir2 = self.mkdtemp()
1572-
# force shutil to create the directory
1573-
os.rmdir(tmpdir2)
1574-
# working with relative paths
1575-
work_dir = os.path.dirname(tmpdir2)
1576-
rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive')
1577-
1578-
with os_helper.change_cwd(work_dir), no_chdir:
1579-
base_name = os.path.abspath(rel_base_name)
1580-
tarball = make_archive(rel_base_name, 'gztar', root_dir, '.')
1581-
1582-
# check if the compressed tarball was created
1583-
self.assertEqual(tarball, base_name + '.tar.gz')
1584-
self.assertTrue(os.path.isfile(tarball))
1585-
self.assertTrue(tarfile.is_tarfile(tarball))
1586-
with tarfile.open(tarball, 'r:gz') as tf:
1587-
self.assertCountEqual(tf.getnames(),
1588-
['.', './sub', './sub2',
1589-
'./file1', './file2', './sub/file3'])
1590-
1591-
# trying an uncompressed one
1592-
with os_helper.change_cwd(work_dir), no_chdir:
1593-
tarball = make_archive(rel_base_name, 'tar', root_dir, '.')
1594-
self.assertEqual(tarball, base_name + '.tar')
1595-
self.assertTrue(os.path.isfile(tarball))
1596-
self.assertTrue(tarfile.is_tarfile(tarball))
1597-
with tarfile.open(tarball, 'r') as tf:
1598-
self.assertCountEqual(tf.getnames(),
1599-
['.', './sub', './sub2',
1600-
'./file1', './file2', './sub/file3'])
1601-
16021566
def _tarinfo(self, path):
16031567
with tarfile.open(path) as tar:
16041568
names = tar.getnames()
@@ -1619,6 +1583,92 @@ def _create_files(self, base_dir='dist'):
16191583
write_file((root_dir, 'outer'), 'xxx')
16201584
return root_dir, base_dir
16211585

1586+
@support.requires_zlib()
1587+
def test_make_tarfile(self):
1588+
root_dir, base_dir = self._create_files()
1589+
# Test without base_dir.
1590+
with os_helper.temp_cwd(), no_chdir:
1591+
base_name = os.path.join('dst', 'archive')
1592+
archive = make_archive(base_name, 'tar', root_dir)
1593+
# check if the compressed tarball was created
1594+
self.assertEqual(archive, os.path.abspath(base_name) + '.tar')
1595+
self.assertTrue(os.path.isfile(archive A3D4 ))
1596+
self.assertTrue(tarfile.is_tarfile(archive))
1597+
with tarfile.open(archive, 'r') as tf:
1598+
self.assertCountEqual(tf.getnames(),
1599+
['.', './dist', './dist/sub', './dist/sub2',
1600+
'./dist/file1', './dist/file2', './dist/sub/file3',
1601+
'./outer'])
1602+
1603+
# Test with base_dir.
1604+
with os_helper.temp_cwd(), no_chdir:
1605+
base_name = os.path.join('dst2', 'archive')
1606+
archive = make_archive(base_name, 'tar', root_dir, base_dir)
1607+
self.assertEqual(archive, os.path.abspath(base_name) + '.tar')
1608+
# check if the uncompressed tarball was created
1609+
self.assertTrue(os.path.isfile(archive))
1610+
self.assertTrue(tarfile.is_tarfile(archive))
1611+
with tarfile.open(archive, 'r') as tf:
1612+
self.assertCountEqual(tf.getnames(),
1613+
['dist', 'dist/sub', 'dist/sub2',
1614+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
1615+
1616+
# Test with multi-component base_dir.
1617+
with os_helper.temp_cwd(), no_chdir:
1618+
base_name = os.path.join('dst3', 'archive')
1619+
archive = make_archive(base_name, 'tar', root_dir,
1620+
os.path.join(base_dir, 'sub'))
1621+
self.assertEqual(archive, os.path.abspath(base_name) + '.tar')
1622+
self.assertTrue(os.path.isfile(archive))
1623+
self.assertTrue(tarfile.is_tarfile(archive))
1624+
with tarfile.open(archive, 'r') as tf:
1625+
self.assertCountEqual(tf.getnames(),
1626+
['dist/sub', 'dist/sub/file3'])
1627+
1628+
@support.requires_zlib()
1629+
def test_make_tarfile_without_rootdir(self):
1630+
root_dir, base_dir = self._create_files()
1631+
# Test without base_dir.
1632+
base_name = os.path.join(self.mkdtemp(), 'dst', 'archive')
1633+
base_name = os.path.relpath(base_name, root_dir)
1634+
with os_helper.change_cwd(root_dir), no_chdir:
1635+
archive = make_archive(base_name, 'gztar')
1636+
self.assertEqual(archive, base_name + '.tar.gz')
1637+
self.assertTrue(os.path.isfile(archive))
1638+
self.assertTrue(tarfile.is_tarfile(archive))
1639+
with tarfile.open(archive, 'r:gz') as tf:
1640+
self.assertCountEqual(tf.getnames(),
1641+
['.', './dist', './dist/sub', './dist/sub2',
1642+
'./dist/file1', './dist/file2', './dist/sub/file3',
1643+
'./outer'])
1644+
1645+
# Test with base_dir.
1646+
with os_helper.change_cwd(root_dir), no_chdir:
1647+
base_name = os.path.join('dst', 'archive')
1648+
archive = make_archive(base_name, 'tar', base_dir=base_dir)
1649+
self.assertEqual(archive, base_name + '.tar')
1650+
self.assertTrue(os.path.isfile(archive))
1651+
self.assertTrue(tarfile.is_tarfile(archive))
1652+
with tarfile.open(archive, 'r') as tf:
1653+
self.assertCountEqual(tf.getnames(),
1654+
['dist', 'dist/sub', 'dist/sub2',
1655+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
1656+
1657+
def test_make_tarfile_with_explicit_curdir(self):
1658+
# Test with base_dir=os.curdir.
1659+
root_dir, base_dir = self._create_files()
1660+
with os_helper.temp_cwd(), no_chdir:
1661+
base_name = os.path.join('dst', 'archive')
1662+
archive = mak 10000 e_archive(base_name, 'tar', root_dir, os.curdir)
1663+
self.assertEqual(archive, os.path.abspath(base_name) + '.tar')
1664+
self.assertTrue(os.path.isfile(archive))
1665+
self.assertTrue(tarfile.is_tarfile(archive))
1666+
with tarfile.open(archive, 'r') as tf:
1667+
self.assertCountEqual(tf.getnames(),
1668+
['.', './dist', './dist/sub', './dist/sub2',
1669+
'./dist/file1', './dist/file2', './dist/sub/file3',
1670+
'./outer'])
1671+
16221672
@support.requires_zlib()
16231673
@unittest.skipUnless(shutil.which('tar'),
16241674
'Need the tar command to run')
@@ -1668,40 +1718,89 @@ def test_tarfile_vs_tar(self):
16681718

16691719
@support.requires_zlib()
16701720
def test_make_zipfile(self):
1671-
# creating something to zip
16721721
root_dir, base_dir = self._create_files()
1722+
# Test without base_dir.
1723+
with os_helper.temp_cwd(), no_chdir:
1724+
base_name = os.path.join('dst', 'archive')
1725+
archive = make_archive(base_name, 'zip', root_dir)
1726+
self.assertEqual(archive, os.path.abspath(base_name) + '.zip')
1727+
self.assertTrue(os.path.isfile(archive))
1728+
self.assertTrue(zipfile.is_zipfile(archive))
1729+
with zipfile.ZipFile(archive) as zf:
1730+
self.assertCountEqual(zf.namelist(),
1731+
['dist/', 'dist/sub/', 'dist/sub2/',
1732+
'dist/file1', 'dist/file2', 'dist/sub/file3',
1733+
'outer'])
1734+
1735+
# Test with base_dir.
1736+
with os_helper.temp_cwd(), no_chdir:
1737+
base_name = os.path.join('dst2', 'archive')
1738+
archive = make_archive(base_name, 'zip', root_dir, base_dir)
1739+
self.assertEqual(archive, os.path.abspath(base_name) + '.zip')
1740+
self.assertTrue(os.path.isfile(archive))
1741+
self.assertTrue(zipfile.is_zipfile(archive))
1742+
with zipfile.ZipFile(archive) as zf:
1743+
self.assertCountEqual(zf.namelist(),
1744+
['dist/', 'dist/sub/', 'dist/sub2/',
1745+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
1746+
1747+
# Test with multi-component base_dir.
1748+
with os_helper.temp_cwd(), no_chdir:
1749+
base_name = os.path.join('dst3', 'archive')
1750+
archive = make_archive(base_name, 'zip', root_dir,
1751+
os.path.join(base_dir, 'sub'))
1752+
self.assertEqual(archive, os.path.abspath(base_name) + '.zip')
1753+
self.assertTrue(os.path.isfile(archive))
1754+
self.assertTrue(zipfile.is_zipfile(archive))
1755+
with zipfile.ZipFile(archive) as zf:
1756+
self.assertCountEqual(zf.namelist(),
1757+
['dist/sub/', 'dist/sub/file3'])
16731758

1674-
tmpdir2 = self.mkdtemp()
1675-
# force shutil to create the directory
1676-
os.rmdir(tmpdir2)
1677-
# working with relative paths
1678-
work_dir = os.path.dirname(tmpdir2)
1679-
rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive')
1680-
1681-
with os_helper.change_cwd(work_dir), no_chdir:
1682-
base_name = os.path.abspath(rel_base_name)
1683-
res = make_archive(rel_base_name, 'zip', root_dir)
1759+
@support.requires_zlib()
1760+
def test_make_zipfile_without_rootdir(self):
1761+
root_dir, base_dir = self._create_files()
1762+
# Test without base_dir.
1763+
base_name = os.path.join(self.mkdtemp(), 'dst', 'archive')
1764+
base_name = os.path.relpath(base_name, root_dir)
1765+
with os_helper.change_cwd(root_dir), no_chdir:
1766+
archive = make_archive(base_name, 'zip')
1767+
self.assertEqual(archive, base_name + '.zip')
1768+
self.assertTrue(os.path.isfile(archive))
1769+
self.assertTrue(zipfile.is_zipfile(archive))
1770+
with zipfile.ZipFile(archive) as zf:
1771+
self.assertCountEqual(zf.namelist(),
1772+
['dist/', 'dist/sub/', 'dist/sub2/',
1773+
'dist/file1', 'dist/file2', 'dist/sub/file3',
1774+
'outer'])
1775+
1776+
# Test with base_dir.
1777+
root_dir, base_dir = self._create_files()
1778+
with os_helper.change_cwd(root_dir), no_chdir:
1779+
base_name = os.path.join('dst', 'archive')
1780+
archive = make_archive(base_name, 'zip', base_dir=base_dir)
1781+
self.assertEqual(archive, base_name + '.zip')
1782+
self.assertTrue(os.path.isfile(archive))
1783+
self.assertTrue(zipfile.is_zipfile(archive))
1784+
with zipfile.ZipFile(archive) as zf:
1785+
self.assertCountEqual(zf.namelist(),
1786+
['dist/', 'dist/sub/', 'dist/sub2/',
1787+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
16841788

1685-
self.assertEqual(res, base_name + '.zip')
1686-
self.assertTrue(os.path.isfile(res))
1687-
self.assertTrue(zipfile.is_zipfile(res))
1688-
with zipfile.ZipFile(res) as zf:
1689-
self.assertCountEqual(zf.namelist(),
1690-
['dist/', 'dist/sub/', 'dist/sub2/',
1691-
'dist/file1', 'dist/file2', 'dist/sub/file3',
1692-
'outer'])
1693-
1694-
with os_helper.change_cwd(work_dir), no_chdir:
1695-
base_name = os.path.abspath(rel_base_name)
1696-
res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
1697-
1698-
self.assertEqual(res, base_name + '.zip')
1699-
self.assertTrue(os.path.isfile(res))
1700-
self.assertTrue(zipfile.is_zipfile(res))
1701-
with zipfile.ZipFile(res) as zf:
1702-
self.assertCountEqual(zf.namelist(),
1703-
['dist/', 'dist/sub/', 'dist/sub2/',
1704-
'dist/file1', 'dist/file2', 'dist/sub/file3'])
1789+
@support.requires_zlib()
1790+
def test_make_zipfile_with_explicit_curdir(self):
1791+
# Test with base_dir=os.curdir.
1792+
root_dir, base_dir = self._create_files()
1793+
with os_helper.temp_cwd(), no_chdir:
1794+
base_name = os.path.join('dst', 'archive')
1795+
archive = make_archive(base_name, 'zip', root_dir, os.curdir)
1796+
self.assertEqual(archive, os.path.abspath(base_name) + '.zip')
1797+
self.assertTrue(os.path.isfile(archive))
1798+
self.assertTrue(zipfile.is_zipfile(archive))
1799+
with zipfile.ZipFile(archive) as zf:
1800+
self.assertCountEqual(zf.namelist(),
1801+
['dist/', 'dist/sub/', 'dist/sub2/',
1802+
'dist/file1', 'dist/file2', 'dist/sub/file3',
1803+
'outer'])
17051804

17061805
@support.requires_zlib()
17071806
@unittest.skipUnless(shutil.which('zip'),
@@ -1871,17 +1970,19 @@ def archiver(base_name, base_dir, **kw):
18711970
unregister_archive_format('xxx')
18721971

18731972
def test_make_tarfile_in_curdir(self):
1874-
# Issue #21280
1973+
# Issue #21280: Test with the archive in the current directory.
18751974
root_dir = self.mkdtemp()
18761975
with os_helper.change_cwd(root_dir), no_chdir:
1976+
# root_dir must be None, so the archive path is relative.
18771977
self.assertEqual(make_archive('test', 'tar'), 'test.tar')
18781978
self.assertTrue(os.path.isfile('test.tar'))
18791979

18801980
@support.requires_zlib()
18811981
def test_make_zipfile_in_curdir(self):
1882-
# Issue #21280
1982+
# Issue #21280: Test with the archive in the current directory.
18831983
root_dir = self.mkdtemp()
18841984
with os_helper.change_cwd(root_dir), no_chdir:
1985+
# root_dir must be None, so the archive path is relative.
18851986
self.assertEqual(make_archive('test', 'zip'), 'test.zip')
18861987
self.assertTrue(os.path.isfile('test.zip'))
18871988

@@ -1902,10 +2003,11 @@ def test_register_archive_format(self):
19022003
self.assertNotIn('xxx', formats)
19032004

19042005
def test_make_tarfile_rootdir_nodir(self):
1905-
# GH-99203
2006+
# GH-99203: Test with root_dir is not a real directory.
19062007
self.addCleanup(os_helper.unlink, f'{TESTFN}.tar')
19072008
for dry_run in (False, True):
19082009
with self.subTest(dry_run=dry_run):
2010+
# root_dir does not exist.
19092011
tmp_dir = self.mkdtemp()
19102012
nonexisting_file = os.path.join(tmp_dir, 'nonexisting')
19112013
with self.assertRaises(FileNotFoundError) as cm:
@@ -1914,6 +2016,7 @@ def test_make_tarfile_rootdir_nodir(self):
19142016
self.assertEqual(cm.exception.filename, nonexisting_file)
19152017
self.assertFalse(os.path.exists(f'{TESTFN}.tar'))
19162018

2019+
# root_dir is a file.
19172020
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir)
19182021
os.close(tmp_fd)
19192022
with self.assertRaises(NotADirectoryError) as cm:
@@ -1924,10 +2027,11 @@ def test_make_tarfile_rootdir_nodir(self):
19242027

19252028
@support.requires_zlib()
19262029
def test_make_zipfile_rootdir_nodir(self):
1927-
# GH-99203
2030+
# GH-99203: Test with root_dir is not a real directory.
19282031
self.addCleanup(os_helper.unlink, f'{TESTFN}.zip')
19292032
for dry_run in (False, True):
19302033
with self.subTest(dry_run=dry_run):
2034+
# root_dir does not exist.
19312035
tmp_dir = self.mkdtemp()
19322036
nonexisting_file = os.path.join(tmp_dir, 'nonexisting')
19332037
with self.assertRaises(FileNotFoundError) as cm:
@@ -1936,6 +2040,7 @@ def test_make_zipfile_rootdir_nodir(self):
19362040
self.assertEqual(cm.exception.filename, nonexisting_file)
19372041
self.assertFalse(os.path.exists(f'{TESTFN}.zip'))
19382042

2043+
# root_dir is a file.
19392044
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir)
19402045
os.close(tmp_fd)
19412046
with self.assertRaises(NotADirectoryError) as cm:

0 commit comments

Comments
 (0)
0