@@ -1615,42 +1615,6 @@ class TestArchives(BaseTest, unittest.TestCase):
1615
1615
1616
1616
### shutil.make_archive
1617
1617
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
-
1654
1618
def _tarinfo (self , path ):
1655
1619
with tarfile .open (path ) as tar :
1656
1620
names = tar .getnames ()
@@ -1671,6 +1635,92 @@ def _create_files(self, base_dir='dist'):
1671
1635
write_file ((root_dir , 'outer' ), 'xxx' )
1672
1636
return root_dir , base_dir
1673
1637
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
+
1674
1724
@support .requires_zlib ()
1675
1725
@unittest .skipUnless (shutil .which ('tar' ),
1676
1726
'Need the tar command to run' )
@@ -1720,40 +1770,89 @@ def test_tarfile_vs_tar(self):
1720
1770
1721
1771
@support .requires_zlib ()
1722
1772
def test_make_zipfile (self ):
1723
- # creating something to zip
1724
1773
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' ])
1725
1810
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' ])
1736
1840
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' ])
1757
1856
1758
1857
@support .requires_zlib ()
1759
1858
@unittest .skipUnless (shutil .which ('zip' ),
@@ -1923,17 +2022,19 @@ def archiver(base_name, base_dir, **kw):
1923
2022
unregister_archive_format ('xxx' )
1924
2023
1925
2024
def test_make_tarfile_in_curdir (self ):
1926
- # Issue #21280
2025
+ # Issue #21280: Test with the archive in the current directory.
1927
2026
root_dir = self .mkdtemp ()
1928
2027
with os_helper .change_cwd (root_dir ), no_chdir :
2028
+ # root_dir must be None, so the archive path is relative.
1929
2029
self .assertEqual (make_archive ('test' , 'tar' ), 'test.tar' )
1930
2030
self .assertTrue (os .path .isfile ('test.tar' ))
1931
2031
1932
2032
@support .requires_zlib ()
1933
2033
def test_make_zipfile_in_curdir (self ):
1934
- # Issue #21280
2034
+ # Issue #21280: Test with the archive in the current directory.
1935
2035
root_dir = self .mkdtemp ()
1936
2036
with os_helper .change_cwd (root_dir ), no_chdir :
2037
+ # root_dir must be None, so the archive path is relative.
1937
2038
self .assertEqual (make_archive ('test' , 'zip' ), 'test.zip' )
1938
2039
self .assertTrue (os .path .isfile ('test.zip' ))
1939
2040
@@ -1954,10 +2055,11 @@ def test_register_archive_format(self):
1954
2055
self .assertNotIn ('xxx' , formats )
1955
2056
1956
2057
def test_make_tarfile_rootdir_nodir (self ):
1957
- # GH-99203
2058
+ # GH-99203: Test with root_dir is not a real directory.
1958
2059
self .addCleanup (os_helper .unlink , f'{ TESTFN } .tar' )
1959
2060
for dry_run in (False , True ):
1960
2061
with self .subTest (dry_run = dry_run ):
2062
+ # root_dir does not exist.
1961
2063
tmp_dir = self .mkdtemp ()
1962
2064
nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
1963
2065
with self .assertRaises (FileNotFoundError ) as cm :
@@ -1966,6 +2068,7 @@ def test_make_tarfile_rootdir_nodir(self):
1966
2068
self .assertEqual (cm .exception .filename , nonexisting_file )
1967
2069
self .assertFalse (os .path .exists (f'{ TESTFN } .tar' ))
1968
2070
2071
+ # root_dir is a file.
1969
2072
tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
1970
2073
os .close (tmp_fd )
1971
2074
with self .assertRaises (NotADirectoryError ) as cm :
@@ -1976,10 +2079,11 @@ def test_make_tarfile_rootdir_nodir(self):
1976
2079
1977
2080
@support .requires_zlib ()
1978
2081
def test_make_zipfile_rootdir_nodir (self ):
1979
- # GH-99203
2082
+ # GH-99203: Test with root_dir is not a real directory.
1980
2083
self .addCleanup (os_helper .unlink , f'{ TESTFN } .zip' )
1981
2084
for dry_run in (False , True ):
1982
2085
with self .subTest (dry_run = dry_run ):
2086
+ # root_dir does not exist.
1983
2087
tmp_dir = self .mkdtemp ()
1984
2088
nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
1985
2089
with self .assertRaises (FileNotFoundError ) as cm :
@@ -1988,6 +2092,7 @@ def test_make_zipfile_rootdir_nodir(self):
1988
2092
self .assertEqual (cm .exception .filename , nonexisting_file )
1989
2093
self .assertFalse (os .path .exists (f'{ TESTFN } .zip' ))
1990
2094
2095
+ # root_dir is a file.
1991
2096
tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
1992
2097
os .close (tmp_fd )
1993
2098
with self .assertRaises (NotADirectoryError ) as cm :
0 commit comments