@@ -1615,42 +1615,6 @@ class TestArchives(BaseTest, unittest.TestCase):
16151615
16161616 ### shutil.make_archive
16171617
1618- @support .requires_zlib ()
1619- def test_make_tarball (self ):
1620- # creating something to tar
1621- root_dir , base_dir = self ._create_files ('' )
1622-
1623- tmpdir2 = self .mkdtemp ()
1624- # force shutil to create the directory
1625- os .rmdir (tmpdir2 )
1626- # working with relative paths
1627- work_dir = os .path .dirname (tmpdir2 )
1628- rel_base_name = os .path .join (os .path .basename (tmpdir2 ), 'archive' )
1629-
1630- with os_helper .change_cwd (work_dir ), no_chdir :
1631- base_name = os .path .abspath (rel_base_name )
1632- tarball = make_archive (rel_base_name , 'gztar' , root_dir , '.' )
1633-
1634- # check if the compressed tarball was created
1635- self .assertEqual (tarball , base_name + '.tar.gz' )
1636- self .assertTrue (os .path .isfile (tarball ))
1637- self .assertTrue (tarfile .is_tarfile (tarball ))
1638- with tarfile .open (tarball , 'r:gz' ) as tf :
1639- self .assertCountEqual (tf .getnames (),
1640- ['.' , './sub' , './sub2' ,
1641- './file1' , './file2' , './sub/file3' ])
1642-
1643- # trying an uncompressed one
1644- with os_helper .change_cwd (work_dir ), no_chdir :
1645- tarball = make_archive (rel_base_name , 'tar' , root_dir , '.' )
1646- self .assertEqual (tarball , base_name + '.tar' )
1647- self .assertTrue (os .path .isfile (tarball ))
1648- self .assertTrue (tarfile .is_tarfile (tarball ))
1649- with tarfile .open (tarball , 'r' ) as tf :
1650- self .assertCountEqual (tf .getnames (),
1651- ['.' , './sub' , './sub2' ,
1652- './file1' , './file2' , './sub/file3' ])
1653-
16541618 def _tarinfo (self , path ):
16551619 with tarfile .open (path ) as tar :
16561620 names = tar .getnames ()
@@ -1671,6 +1635,92 @@ def _create_files(self, base_dir='dist'):
16711635 write_file ((root_dir , 'outer' ), 'xxx' )
16721636 return root_dir , base_dir
16731637
1638+ @support .requires_zlib ()
1639+ def test_make_tarfile (self ):
1640+ root_dir , base_dir = self ._create_files ()
1641+ # Test without base_dir.
1642+ with os_helper .temp_cwd (), no_chdir :
1643+ base_name = os .path .join ('dst' , 'archive' )
1644+ archive = make_archive (base_name , 'tar' , root_dir )
1645+ # check if the compressed tarball was created
1646+ self .assertEqual (archive , os .path .abspath (base_name ) + '.tar' )
1647+ self .assertTrue (os .path .isfile (archive ))
1648+ self .assertTrue (tarfile .is_tarfile (archive ))
1649+ with tarfile .open (archive , 'r' ) as tf :
1650+ self .assertCountEqual (tf .getnames (),
1651+ ['.' , './dist' , './dist/sub' , './dist/sub2' ,
1652+ './dist/file1' , './dist/file2' , './dist/sub/file3' ,
1653+ './outer' ])
1654+
1655+ # Test with base_dir.
1656+ with os_helper .temp_cwd (), no_chdir :
1657+ base_name = os .path .join ('dst2' , 'archive' )
1658+ archive = make_archive (base_name , 'tar' , root_dir , base_dir )
1659+ self .assertEqual (archive , os .path .abspath (base_name ) + '.tar' )
1660+ # check if the uncompressed tarball was created
1661+ self .assertTrue (os .path .isfile (archive ))
1662+ self .assertTrue (tarfile .is_tarfile (archive ))
1663+ with tarfile .open (archive , 'r' ) as tf :
1664+ self .assertCountEqual (tf .getnames (),
1665+ ['dist' , 'dist/sub' , 'dist/sub2' ,
1666+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ])
1667+
1668+ # Test with multi-component base_dir.
1669+ with os_helper .temp_cwd (), no_chdir :
1670+ base_name = os .path .join ('dst3' , 'archive' )
1671+ archive = make_archive (base_name , 'tar' , root_dir ,
1672+ os .path .join (base_dir , 'sub' ))
1673+ self .assertEqual (archive , os .path .abspath (base_name ) + '.tar' )
1674+ self .assertTrue (os .path .isfile (archive ))
1675+ self .assertTrue (tarfile .is_tarfile (archive ))
1676+ with tarfile .open (archive , 'r' ) as tf :
1677+ self .assertCountEqual (tf .getnames (),
1678+ ['dist/sub' , 'dist/sub/file3' ])
1679+
1680+ @support .requires_zlib ()
1681+ def test_make_tarfile_without_rootdir (self ):
1682+ root_dir , base_dir = self ._create_files ()
1683+ # Test without base_dir.
1684+ base_name = os .path .join (self .mkdtemp (), 'dst' , 'archive' )
1685+ base_name = os .path .relpath (base_name , root_dir )
1686+ with os_helper .change_cwd (root_dir ), no_chdir :
1687+ archive = make_archive (base_name , 'gztar' )
1688+ self .assertEqual (archive , base_name + '.tar.gz' )
1689+ self .assertTrue (os .path .isfile (archive ))
1690+ self .assertTrue (tarfile .is_tarfile (archive ))
1691+ with tarfile .open (archive , 'r:gz' ) as tf :
1692+ self .assertCountEqual (tf .getnames (),
1693+ ['.' , './dist' , './dist/sub' , './dist/sub2' ,
1694+ './dist/file1' , './dist/file2' , './dist/sub/file3' ,
1695+ './outer' ])
1696+
1697+ # Test with base_dir.
1698+ with os_helper .change_cwd (root_dir ), no_chdir :
1699+ base_name = os .path .join ('dst' , 'archive' )
1700+ archive = make_archive (base_name , 'tar' , base_dir = base_dir )
1701+ self .assertEqual (archive , base_name + '.tar' )
1702+ self .assertTrue (os .path .isfile (archive ))
1703+ self .assertTrue (tarfile .is_tarfile (archive ))
1704+ with tarfile .open (archive , 'r' ) as tf :
1705+ self .assertCountEqual (tf .getnames (),
1706+ ['dist' , 'dist/sub' , 'dist/sub2' ,
1707+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ])
1708+
1709+ def test_make_tarfile_with_explicit_curdir (self ):
1710+ # Test with base_dir=os.curdir.
1711+ root_dir , base_dir = self ._create_files ()
1712+ with os_helper .temp_cwd (), no_chdir :
1713+ base_name = os .path .join ('dst' , 'archive' )
1714+ archive = make_archive (base_name , 'tar' , root_dir , os .curdir )
1715+ self .assertEqual (archive , os .path .abspath (base_name ) + '.tar' )
1716+ self .assertTrue (os .path .isfile (archive ))
1717+ self .assertTrue (tarfile .is_tarfile (archive ))
1718+ with tarfile .open (archive , 'r' ) as tf :
1719+ self .assertCountEqual (tf .getnames (),
1720+ ['.' , './dist' , './dist/sub' , './dist/sub2' ,
1721+ './dist/file1' , './dist/file2' , './dist/sub/file3' ,
1722+ './outer' ])
1723+
16741724 @support .requires_zlib ()
16751725 @unittest .skipUnless (shutil .which ('tar' ),
16761726 'Need the tar command to run' )
@@ -1720,40 +1770,89 @@ def test_tarfile_vs_tar(self):
17201770
17211771 @support .requires_zlib ()
17221772 def test_make_zipfile (self ):
1723- # creating something to zip
17241773 root_dir , base_dir = self ._create_files ()
1774+ # Test without base_dir.
1775+ with os_helper .temp_cwd (), no_chdir :
1776+ base_name = os .path .join ('dst' , 'archive' )
1777+ archive = make_archive (base_name , 'zip' , root_dir )
1778+ self .assertEqual (archive , os .path .abspath (base_name ) + '.zip' )
1779+ self .assertTrue (os .path .isfile (archive ))
1780+ self .assertTrue (zipfile .is_zipfile (archive ))
1781+ with zipfile .ZipFile (archive ) as zf :
1782+ self .assertCountEqual (zf .namelist (),
1783+ ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1784+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ,
1785+ 'outer' ])
1786+
1787+ # Test with base_dir.
1788+ with os_helper .temp_cwd (), no_chdir :
1789+ base_name = os .path .join ('dst2' , 'archive' )
1790+ archive = make_archive (base_name , 'zip' , root_dir , base_dir )
1791+ self .assertEqual (archive , os .path .abspath (base_name ) + '.zip' )
1792+ self .assertTrue (os .path .isfile (archive ))
1793+ self .assertTrue (zipfile .is_zipfile (archive ))
1794+ with zipfile .ZipFile (archive ) as zf :
1795+ self .assertCountEqual (zf .namelist (),
1796+ ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1797+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ])
1798+
1799+ # Test with multi-component base_dir.
1800+ with os_helper .temp_cwd (), no_chdir :
1801+ base_name = os .path .join ('dst3' , 'archive' )
1802+ archive = make_archive (base_name , 'zip' , root_dir ,
1803+ os .path .join (base_dir , 'sub' ))
1804+ self .assertEqual (archive , os .path .abspath (base_name ) + '.zip' )
1805+ self .assertTrue (os .path .isfile (archive ))
1806+ self .assertTrue (zipfile .is_zipfile (archive ))
1807+ with zipfile .ZipFile (archive ) as zf :
1808+ self .assertCountEqual (zf .namelist (),
1809+ ['dist/sub/' , 'dist/sub/file3' ])
17251810
1726- tmpdir2 = self .mkdtemp ()
1727- # force shutil to create the directory
1728- os .rmdir (tmpdir2 )
1729- # working with relative paths
1730- work_dir = os .path .dirname (tmpdir2 )
1731- rel_base_name = os .path .join (os .path .basename (tmpdir2 ), 'archive' )
1732-
1733- with os_helper .change_cwd (work_dir ), no_chdir :
1734- base_name = os .path .abspath (rel_base_name )
1735- res = make_archive (rel_base_name , 'zip' , root_dir )
1811+ @support .requires_zlib ()
1812+ def test_make_zipfile_without_rootdir (self ):
1813+ root_dir , base_dir = self ._create_files ()
1814+ # Test without base_dir.
1815+ base_name = os .path .join (self .mkdtemp (), 'dst' , 'archive' )
1816+ base_name = os .path .relpath (base_name , root_dir )
1817+ with os_helper .change_cwd (root_dir ), no_chdir :
1818+ archive = make_archive (base_name , 'zip' )
1819+ self .assertEqual (archive , base_name + '.zip' )
1820+ self .assertTrue (os .path .isfile (archive ))
1821+ self .assertTrue (zipfile .is_zipfile (archive ))
1822+ with zipfile .ZipFile (archive ) as zf :
1823+ self .assertCountEqual (zf .namelist (),
1824+ ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1825+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ,
1826+ 'outer' ])
1827+
1828+ # Test with base_dir.
1829+ root_dir , base_dir = self ._create_files ()
1830+ with os_helper .change_cwd (root_dir ), no_chdir :
1831+ base_name = os .path .join ('dst' , 'archive' )
1832+ archive = make_archive (base_name , 'zip' , base_dir = base_dir )
1833+ self .assertEqual (archive , base_name + '.zip' )
1834+ self .assertTrue (os .path .isfile (archive ))
1835+ self .assertTrue (zipfile .is_zipfile (archive ))
1836+ with zipfile .ZipFile (archive ) as zf :
1837+ self .assertCountEqual (zf .namelist (),
1838+ ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1839+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ])
17361840
1737- self .assertEqual (res , base_name + '.zip' )
1738- self .assertTrue (os .path .isfile (res ))
1739- self .assertTrue (zipfile .is_zipfile (res ))
1740- with zipfile .ZipFile (res ) as zf :
1741- self .assertCountEqual (zf .namelist (),
1742- ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1743- 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ,
1744- 'outer' ])
1745-
1746- with os_helper .change_cwd (work_dir ), no_chdir :
1747- base_name = os .path .abspath (rel_base_name )
1748- res = make_archive (rel_base_name , 'zip' , root_dir , base_dir )
1749-
1750- self .assertEqual (res , base_name + '.zip' )
1751- self .assertTrue (os .path .isfile (res ))
1752- self .assertTrue (zipfile .is_zipfile (res ))
1753- with zipfile .ZipFile (res ) as zf :
1754- self .assertCountEqual (zf .namelist (),
1755- ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1756- 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ])
1841+ @support .requires_zlib ()
1842+ def test_make_zipfile_with_explicit_curdir (self ):
1843+ # Test with base_dir=os.curdir.
1844+ root_dir , base_dir = self ._create_files ()
1845+ with os_helper .temp_cwd (), no_chdir :
1846+ base_name = os .path .join ('dst' , 'archive' )
1847+ archive = make_archive (base_name , 'zip' , root_dir , os .curdir )
1848+ self .assertEqual (archive , os .path .abspath (base_name ) + '.zip' )
1849+ self .assertTrue (os .path .isfile (archive ))
1850+ self .assertTrue (zipfile .is_zipfile (archive ))
1851+ with zipfile .ZipFile (archive ) as zf :
1852+ self .assertCountEqual (zf .namelist (),
1853+ ['dist/' , 'dist/sub/' , 'dist/sub2/' ,
1854+ 'dist/file1' , 'dist/file2' , 'dist/sub/file3' ,
1855+ 'outer' ])
17571856
17581857 @support .requires_zlib ()
17591858 @unittest .skipUnless (shutil .which ('zip' ),
@@ -1923,17 +2022,19 @@ def archiver(base_name, base_dir, **kw):
19232022 unregister_archive_format ('xxx' )
19242023
19252024 def test_make_tarfile_in_curdir (self ):
1926- # Issue #21280
2025+ # Issue #21280: Test with the archive in the current directory.
19272026 root_dir = self .mkdtemp ()
19282027 with os_helper .change_cwd (root_dir ), no_chdir :
2028+ # root_dir must be None, so the archive path is relative.
19292029 self .assertEqual (make_archive ('test' , 'tar' ), 'test.tar' )
19302030 self .assertTrue (os .path .isfile ('test.tar' ))
19312031
19322032 @support .requires_zlib ()
19332033 def test_make_zipfile_in_curdir (self ):
1934- # Issue #21280
2034+ # Issue #21280: Test with the archive in the current directory.
19352035 root_dir = self .mkdtemp ()
19362036 with os_helper .change_cwd (root_dir ), no_chdir :
2037+ # root_dir must be None, so the archive path is relative.
19372038 self .assertEqual (make_archive ('test' , 'zip' ), 'test.zip' )
19382039 self .assertTrue (os .path .isfile ('test.zip' ))
19392040
@@ -1954,10 +2055,11 @@ def test_register_archive_format(self):
19542055 self .assertNotIn ('xxx' , formats )
19552056
19562057 def test_make_tarfile_rootdir_nodir (self ):
1957- # GH-99203
2058+ # GH-99203: Test with root_dir is not a real directory.
19582059 self .addCleanup (os_helper .unlink , f'{ TESTFN } .tar' )
19592060 for dry_run in (False , True ):
19602061 with self .subTest (dry_run = dry_run ):
2062+ # root_dir does not exist.
19612063 tmp_dir = self .mkdtemp ()
19622064 nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
19632065 with self .assertRaises (FileNotFoundError ) as cm :
@@ -1966,6 +2068,7 @@ def test_make_tarfile_rootdir_nodir(self):
19662068 self .assertEqual (cm .exception .filename , nonexisting_file )
19672069 self .assertFalse (os .path .exists (f'{ TESTFN } .tar' ))
19682070
2071+ # root_dir is a file.
19692072 tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
19702073 os .close (tmp_fd )
19712074 with self .assertRaises (NotADirectoryError ) as cm :
@@ -1976,10 +2079,11 @@ def test_make_tarfile_rootdir_nodir(self):
19762079
19772080 @support .requires_zlib ()
19782081 def test_make_zipfile_rootdir_nodir (self ):
1979- # GH-99203
2082+ # GH-99203: Test with root_dir is not a real directory.
19802083 self .addCleanup (os_helper .unlink , f'{ TESTFN } .zip' )
19812084 for dry_run in (False , True ):
19822085 with self .subTest (dry_run = dry_run ):
2086+ # root_dir does not exist.
19832087 tmp_dir = self .mkdtemp ()
19842088 nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
19852089 with self .assertRaises (FileNotFoundError ) as cm :
@@ -1988,6 +2092,7 @@ def test_make_zipfile_rootdir_nodir(self):
19882092 self .assertEqual (cm .exception .filename , nonexisting_file )
19892093 self .assertFalse (os .path .exists (f'{ TESTFN } .zip' ))
19902094
2095+ # root_dir is a file.
19912096 tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
19922097 os .close (tmp_fd )
19932098 with self .assertRaises (NotADirectoryError ) as cm :
0 commit comments