@@ -1362,8 +1362,11 @@ class ZstdWriterTests(AbstractWriterTests, unittest.TestCase):
1362
1362
compression = zipfile .ZIP_ZSTANDARD
1363
1363
1364
1364
1365
- def ComparableZipInfo (zinfo ):
1366
- return (zinfo .filename , zinfo .header_offset , zinfo .compress_size , zinfo .CRC )
1365
+ class ComparableZipInfo :
1366
+ keys = [i for i in zipfile .ZipInfo .__slots__ if not i .startswith ('_' )]
1367
+
1368
+ def __new__ (cls , zinfo ):
1369
+ return {i : getattr (zinfo , i ) for i in cls .keys }
1367
1370
1368
1371
_struct_pack = struct .pack
1369
1372
@@ -1379,6 +1382,8 @@ def struct_pack_no_dd_sig(fmt, *values):
1379
1382
1380
1383
class RepackHelperMixin :
1381
1384
"""Common helpers for remove and repack."""
1385
+ maxDiff = 8192
1386
+
1382
1387
@classmethod
1383
1388
def _prepare_test_files (cls ):
1384
1389
return [
@@ -1389,14 +1394,11 @@ def _prepare_test_files(cls):
1389
1394
1390
1395
@classmethod
1391
1396
def _prepare_zip_from_test_files (cls , zfname , test_files , force_zip64 = False ):
1392
- zinfos = []
1393
1397
with zipfile .ZipFile (zfname , 'w' , cls .compression ) as zh :
1394
1398
for file , data in test_files :
1395
1399
with zh .open (file , 'w' , force_zip64 = force_zip64 ) as fh :
1396
1400
fh .write (data )
1397
- zinfo = zh .getinfo (file )
1398
- zinfos .append (ComparableZipInfo (zinfo ))
1399
- return zinfos
1401
+ return list (zh .infolist ())
1400
1402
1401
1403
class AbstractRemoveTests (RepackHelperMixin ):
1402
1404
@classmethod
@@ -1416,7 +1418,7 @@ def test_remove_by_name(self):
1416
1418
# check infolist
1417
1419
self .assertEqual (
1418
1420
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1419
- [zi for j , zi in enumerate (zinfos ) if j != i ],
1421
+ [ComparableZipInfo ( zi ) for j , zi in enumerate (zinfos ) if j != i ],
1420
1422
)
1421
1423
1422
1424
# check NameToInfo cache
@@ -1437,7 +1439,7 @@ def test_remove_by_zinfo(self):
1437
1439
# check infolist
1438
1440
self .assertEqual (
1439
1441
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1440
- [zi for j , zi in enumerate (zinfos ) if j != i ],
1442
+ [ComparableZipInfo ( zi ) for j , zi in enumerate (zinfos ) if j != i ],
1441
1443
)
1442
1444
1443
1445
# check NameToInfo cache
@@ -1478,13 +1480,13 @@ def test_remove_by_name_duplicated(self):
1478
1480
# check infolist
1479
1481
self .assertEqual (
1480
1482
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1481
- [zinfos [0 ], zinfos [2 ]],
1483
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [0 ], zinfos [2 ] ]],
1482
1484
)
1483
1485
1484
1486
# check NameToInfo cache
1485
1487
self .assertEqual (
1486
1488
ComparableZipInfo (zh .getinfo ('file.txt' )),
1487
- zinfos [0 ],
1489
+ ComparableZipInfo ( zinfos [0 ]) ,
1488
1490
)
1489
1491
1490
1492
# make sure the zip file is still valid
@@ -1499,7 +1501,7 @@ def test_remove_by_name_duplicated(self):
1499
1501
# check infolist
1500
1502
self .assertEqual (
1501
1503
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1502
- [zinfos [2 ]],
1504
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [2 ] ]],
1503
1505
)
1504
1506
1505
1507
# check NameToInfo cache
@@ -1528,13 +1530,13 @@ def test_remove_by_zinfo_duplicated(self):
1528
1530
# check infolist
1529
1531
self .assertEqual (
1530
1532
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1531
- [zinfos [1 ], zinfos [2 ]],
1533
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [1 ], zinfos [2 ] ]],
1532
1534
)
1533
1535
1534
1536
# check NameToInfo cache
1535
1537
self .assertEqual (
1536
1538
ComparableZipInfo (zh .getinfo ('file.txt' )),
1537
- zinfos [1 ],
1539
+ ComparableZipInfo ( zinfos [1 ]) ,
1538
1540
)
1539
1541
1540
1542
# make sure the zip file is still valid
@@ -1548,13 +1550,13 @@ def test_remove_by_zinfo_duplicated(self):
1548
1550
# check infolist
1549
1551
self .assertEqual (
1550
1552
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1551
- [zinfos [0 ], zinfos [2 ]],
1553
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [0 ], zinfos [2 ] ]],
1552
1554
)
1553
1555
1554
1556
# check NameToInfo cache
1555
1557
self .assertEqual (
1556
1558
ComparableZipInfo (zh .getinfo ('file.txt' )),
1557
- zinfos [0 ],
1559
+ ComparableZipInfo ( zinfos [0 ]) ,
1558
1560
)
1559
1561
1560
1562
# make sure the zip file is still valid
@@ -1570,7 +1572,7 @@ def test_remove_by_zinfo_duplicated(self):
1570
1572
# check infolist
1571
1573
self .assertEqual (
1572
1574
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1573
- [zinfos [2 ]],
1575
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [2 ] ]],
1574
1576
)
1575
1577
1576
1578
# check NameToInfo cache
@@ -1591,7 +1593,7 @@ def test_remove_zip64(self):
1591
1593
# check infolist
1592
1594
self .assertEqual (
1593
1595
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1594
- [zi for j , zi in enumerate (zinfos ) if j != i ],
1596
+ [ComparableZipInfo ( zi ) for j , zi in enumerate (zinfos ) if j != i ],
1595
1597
)
1596
1598
1597
1599
# check NameToInfo cache
@@ -1626,14 +1628,14 @@ def test_remove_mode_w(self):
1626
1628
with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1627
1629
for file , data in self .test_files :
1628
1630
zh .writestr (file , data )
1629
- zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
1631
+ zinfos = list ( zh .infolist ())
1630
1632
1631
1633
zh .remove (self .test_files [0 ][0 ])
1632
1634
1633
1635
# check infolist
1634
1636
self .assertEqual (
1635
1637
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1636
- [zinfos [1 ], zinfos [2 ]],
1638
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [1 ], zinfos [2 ] ]],
1637
1639
)
1638
1640
1639
1641
# check NameToInfo cache
@@ -1648,14 +1650,14 @@ def test_remove_mode_x(self):
1648
1650
with zipfile .ZipFile (TESTFN , 'x' ) as zh :
1649
1651
for file , data in self .test_files :
1650
1652
zh .writestr (file , data )
1651
- zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
1653
+ zinfos = list ( zh .infolist ())
1652
1654
1653
1655
zh .remove (self .test_files [0 ][0 ])
1654
1656
1655
1657
# check infolist
1656
1658
self .assertEqual (
1657
1659
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1658
- [zinfos [1 ], zinfos [2 ]],
1660
+ [ComparableZipInfo ( zi ) for zi in [ zinfos [1 ], zinfos [2 ] ]],
1659
1661
)
1660
1662
1661
1663
# check NameToInfo cache
@@ -1714,7 +1716,7 @@ def test_repack_basic(self):
1714
1716
# check infolist
1715
1717
self .assertEqual (
1716
1718
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1717
- expected_zinfos ,
1719
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1718
1720
)
1719
1721
1720
1722
# check file size
@@ -1766,7 +1768,7 @@ def test_repack_bytes_before_first_file(self):
1766
1768
# check infolist
1767
1769
self .assertEqual (
1768
1770
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1769
- expected_zinfos ,
1771
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1770
1772
)
1771
1773
1772
1774
# check file size
@@ -1800,7 +1802,7 @@ def test_repack_magic_before_first_file(self):
1800
1802
# check infolist
1801
1803
self .assertEqual (
1802
1804
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1803
- expected_zinfos ,
1805
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1804
1806
)
1805
1807
1806
1808
# check file size
@@ -1846,7 +1848,7 @@ def test_repack_file_entry_before_first_file(self):
1846
1848
# check infolist
1847
1849
self .assertEqual (
1848
1850
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1849
- expected_zinfos ,
1851
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1850
1852
)
1851
1853
1852
1854
# check file size
@@ -1856,6 +1858,7 @@ def test_repack_file_entry_before_first_file(self):
1856
1858
with zipfile .ZipFile (TESTFN ) as zh :
1857
1859
self .assertIsNone (zh .testzip ())
1858
1860
1861
+ @mock .patch .object (time , 'time' , new = lambda : 315504000 ) # fix time for ZipFile.writestr()
1859
1862
def test_repack_bytes_before_removed_files (self ):
1860
1863
"""Should preserve if there are bytes before stale local file entries."""
1861
1864
for ii in ([1 ], [1 , 2 ], [2 ]):
@@ -1870,7 +1873,7 @@ def test_repack_bytes_before_removed_files(self):
1870
1873
zh .writestr (file , data )
1871
1874
for i in ii :
1872
1875
zh .remove (self .test_files [i ][0 ])
1873
- expected_zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
1876
+ expected_zinfos = list ( zh .infolist ())
1874
1877
expected_size = os .path .getsize (TESTFN )
1875
1878
1876
1879
# do the removal and check the result
@@ -1889,7 +1892,7 @@ def test_repack_bytes_before_removed_files(self):
1889
1892
# check infolist
1890
1893
self .assertEqual (
1891
1894
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1892
- expected_zinfos ,
1895
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1893
1896
)
1894
1897
1895
1898
# check file size
@@ -1899,6 +1902,7 @@ def test_repack_bytes_before_removed_files(self):
1899
1902
with zipfile .ZipFile (TESTFN ) as zh :
1900
1903
self .assertIsNone (zh .testzip ())
1901
1904
1905
+ @mock .patch .object (time , 'time' , new = lambda : 315504000 ) # fix time for ZipFile.writestr()
1902
1906
def test_repack_bytes_after_removed_files (self ):
1903
1907
"""Should keep extra bytes if there are bytes after stale local file entries."""
1904
1908
for ii in ([1 ], [1 , 2 ], [2 ]):
@@ -1912,7 +1916,7 @@ def test_repack_bytes_after_removed_files(self):
1912
1916
if i == ii [- 1 ]:
1913
1917
fh .write (b' dummy bytes ' )
1914
1918
zh .start_dir = fh .tell ()
1915
- expected_zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
1919
+ expected_zinfos = list ( zh .infolist ())
1916
1920
expected_size = os .path .getsize (TESTFN )
1917
1921
1918
1922
# do the removal and check the result
@@ -1931,7 +1935,7 @@ def test_repack_bytes_after_removed_files(self):
1931
1935
# check infolist
1932
1936
self .assertEqual (
1933
1937
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1934
- expected_zinfos ,
1938
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1935
1939
)
1936
1940
1937
1941
# check file size
@@ -1941,6 +1945,7 @@ def test_repack_bytes_after_removed_files(self):
1941
1945
with zipfile .ZipFile (TESTFN ) as zh :
1942
1946
self .assertIsNone (zh .testzip ())
1943
1947
1948
+ @mock .patch .object (time , 'time' , new = lambda : 315504000 ) # fix time for ZipFile.writestr()
1944
1949
def test_repack_bytes_between_removed_files (self ):
1945
1950
"""Should strip only local file entries before random bytes."""
1946
1951
# calculate the expected results
@@ -1951,7 +1956,7 @@ def test_repack_bytes_between_removed_files(self):
1951
1956
zh .start_dir = fh .tell ()
1952
1957
zh .writestr (* self .test_files [2 ])
1953
1958
zh .remove (self .test_files [2 ][0 ])
1954
- expected_zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
1959
+ expected_zinfos = list ( zh .infolist ())
1955
1960
expected_size = os .path .getsize (TESTFN )
1956
1961
1957
1962
# do the removal and check the result
@@ -1970,7 +1975,7 @@ def test_repack_bytes_between_removed_files(self):
1970
1975
# check infolist
1971
1976
self .assertEqual (
1972
1977
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
1973
- expected_zinfos ,
1978
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
1974
1979
)
1975
1980
1976
1981
# check file size
@@ -1992,7 +1997,7 @@ def test_repack_prepended_bytes(self):
1992
1997
fh .write (b'dummy ' )
1993
1998
fh .write (fz .read ())
1994
1999
with zipfile .ZipFile (TESTFN ) as zh :
1995
- expected_zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
2000
+ expected_zinfos = list ( zh .infolist ())
1996
2001
expected_size = os .path .getsize (TESTFN )
1997
2002
1998
2003
# do the removal and check the result
@@ -2010,7 +2015,7 @@ def test_repack_prepended_bytes(self):
2010
2015
# check infolist
2011
2016
self .assertEqual (
2012
2017
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
2013
- expected_zinfos ,
2018
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
2014
2019
)
2015
2020
2016
2021
# check file size
@@ -2055,7 +2060,7 @@ def test_repack_removed_basic(self):
2055
2060
# check infolist
2056
2061
self .assertEqual (
2057
2062
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
2058
- expected_zinfos ,
2063
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
2059
2064
)
2060
2065
2061
2066
# check file size
@@ -2098,20 +2103,20 @@ def test_repack_removed_partial(self):
2098
2103
with zipfile .ZipFile (TESTFN ) as zh :
2099
2104
self .assertIsNone (zh .testzip ())
2100
2105
2106
+ @mock .patch .object (time , 'time' , new = lambda : 315504000 ) # fix time for ZipFile.writestr()
2101
2107
def test_repack_removed_bytes_between_files (self ):
2102
2108
"""Should not remove bytes between local file entries."""
2103
2109
for ii in ([0 ], [1 ], [2 ]):
2104
2110
with self .subTest (removed = ii ):
2105
2111
# calculate the expected results
2106
- expected_zinfos = []
2107
2112
with open (TESTFN , 'wb' ) as fh :
2108
2113
with zipfile .ZipFile (fh , 'w' , self .compression ) as zh :
2109
2114
for j , (file , data ) in enumerate (self .test_files ):
2110
2115
if j not in ii :
2111
2116
zh .writestr (file , data )
2112
- expected_zinfos .append (ComparableZipInfo (zh .getinfo (file )))
2113
2117
fh .write (b' dummy bytes ' )
2114
2118
zh .start_dir = fh .tell ()
2119
+ expected_zinfos = list (zh .infolist ())
2115
2120
expected_size = os .path .getsize (TESTFN )
2116
2121
2117
2122
# do the removal and check the result
@@ -2128,7 +2133,7 @@ def test_repack_removed_bytes_between_files(self):
2128
2133
# check infolist
2129
2134
self .assertEqual (
2130
2135
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
2131
- expected_zinfos ,
2136
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
2132
2137
)
2133
2138
2134
2139
# check file size
@@ -2184,7 +2189,7 @@ def test_repack_removed_prepended_bytes(self):
2184
2189
fh .write (b'dummy ' )
2185
2190
fh .write (fz .read ())
2186
2191
with zipfile .ZipFile (TESTFN ) as zh :
2187
- expected_zinfos = [ ComparableZipInfo ( zi ) for zi in zh .infolist ()]
2192
+ expected_zinfos = list ( zh .infolist ())
2188
2193
expected_size = os .path .getsize (TESTFN )
2189
2194
2190
2195
# do the removal and check the result
@@ -2201,7 +2206,7 @@ def test_repack_removed_prepended_bytes(self):
2201
2206
# check infolist
2202
2207
self .assertEqual (
2203
2208
[ComparableZipInfo (zi ) for zi in zh .infolist ()],
2204
- expected_zinfos ,
2209
+ [ ComparableZipInfo ( zi ) for zi in expected_zinfos ] ,
2205
2210
)
2206
2211
2207
2212
# check file size
0 commit comments