@@ -1563,42 +1563,6 @@ class TestArchives(BaseTest, unittest.TestCase):
1563
1563
1564
1564
### shutil.make_archive
1565
1565
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
-
1602
1566
def _tarinfo (self , path ):
1603
1567
with tarfile .open (path ) as tar :
1604
1568
names = tar .getnames ()
@@ -1619,6 +1583,92 @@ def _create_files(self, base_dir='dist'):
1619
1583
write_file ((root_dir , 'outer' ), 'xxx' )
1620
1584
return root_dir , base_dir
1621
1585
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
+
1622
1672
@support .requires_zlib ()
1623
1673
@unittest .skipUnless (shutil .which ('tar' ),
1624
1674
'Need the tar command to run' )
@@ -1668,40 +1718,89 @@ def test_tarfile_vs_tar(self):
1668
1718
1669
1719
@support .requires_zlib ()
1670
1720
def test_make_zipfile (self ):
1671
- # creating something to zip
1672
1721
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'
F42D
span>, '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' ])
1673
1758
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' ])
1684
1788
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' ])
1705
1804
1706
1805
@support .requires_zlib ()
1707
1806
@unittest .skipUnless (shutil .which ('zip' ),
@@ -1871,17 +1970,19 @@ def archiver(base_name, base_dir, **kw):
1871
1970
unregister_archive_format ('xxx' )
1872
1971
1873
1972
def test_make_tarfile_in_curdir (self ):
1874
- # Issue #21280
1973
+ # Issue #21280: Test with the archive in the current directory.
1875
1974
root_dir = self .mkdtemp ()
1876
1975
with os_helper .change_cwd (root_dir ), no_chdir :
1976
+ # root_dir must be None, so the archive path is relative.
1877
1977
self .assertEqual (make_archive ('test' , 'tar' ), 'test.tar' )
1878
1978
self .assertTrue (os .path .isfile ('test.tar' ))
1879
1979
1880
1980
@support .requires_zlib ()
1881
1981
def test_make_zipfile_in_curdir (self ):
1882
- # Issue #21280
1982
+ # Issue #21280: Test with the archive in the current directory.
1883
1983
root_dir = self .mkdtemp ()
1884
1984
with os_helper .change_cwd (root_dir ), no_chdir :
1985
+ # root_dir must be None, so the archive path is relative.
1885
1986
self .assertEqual (make_archive ('test' , 'zip' ), 'test.zip' )
1886
1987
self .assertTrue (os .path .isfile ('test.zip' ))
1887
1988
@@ -1902,10 +2003,11 @@ def test_register_archive_format(self):
1902
2003
self .assertNotIn ('xxx' , formats )
1903
2004
1904
2005
def test_make_tarfile_rootdir_nodir (self ):
1905
- # GH-99203
2006
+ # GH-99203: Test with root_dir is not a real directory.
1906
2007
self .addCleanup (os_helper .unlink , f'{ TESTFN } .tar' )
1907
2008
for dry_run in (False , True ):
1908
2009
with self .subTest (dry_run = dry_run ):
2010
+ # root_dir does not exist.
1909
2011
tmp_dir = self .mkdtemp ()
1910
2012
nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
1911
2013
with self .assertRaises (FileNotFoundError ) as cm :
@@ -1914,6 +2016,7 @@ def test_make_tarfile_rootdir_nodir(self):
1914
2016
self .assertEqual (cm .exception .filename , nonexisting_file )
1915
2017
self .assertFalse (os .path .exists (f'{ TESTFN } .tar' ))
1916
2018
2019
+ # root_dir is a file.
1917
2020
tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
1918
2021
os .close (tmp_fd )
1919
2022
with self .assertRaises (NotADirectoryError ) as cm :
@@ -1924,10 +2027,11 @@ def test_make_tarfile_rootdir_nodir(self):
1924
2027
1925
2028
@support .requires_zlib ()
1926
2029
def test_make_zipfile_rootdir_nodir (self ):
1927
- # GH-99203
2030
+ # GH-99203: Test with root_dir is not a real directory.
1928
2031
self .addCleanup (os_helper .unlink , f'{ TESTFN } .zip' )
1929
2032
for dry_run in (False , True ):
1930
2033
with self .subTest (dry_run = dry_run ):
2034
+ # root_dir does not exist.
1931
2035
tmp_dir = self .mkdtemp ()
1932
2036
nonexisting_file = os .path .join (tmp_dir , 'nonexisting' )
1933
2037
with self .assertRaises (FileNotFoundError ) as cm :
@@ -1936,6 +2040,7 @@ def test_make_zipfile_rootdir_nodir(self):
1936
2040
self .assertEqual (cm .exception .filename , nonexisting_file )
1937
2041
self .assertFalse (os .path .exists (f'{ TESTFN } .zip' ))
1938
2042
2043
+ # root_dir is a file.
1939
2044
tmp_fd , tmp_file = tempfile .mkstemp (dir = tmp_dir )
1940
2045
os .close (tmp_fd )
1941
2046
with self .assertRaises (NotADirectoryError ) as cm :
0 commit comments