Skip to content

Commit a4b410b

Browse files
committed
Add tests for mode w and x for remove()
1 parent 72c2a66 commit a4b410b

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,31 +1587,68 @@ def test_remove_zip64(self):
15871587
self.assertIsNone(zh.testzip())
15881588

15891589
def test_remove_validate(self):
1590-
file = 'datafile.txt'
1591-
data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
1592-
15931590
# closed: error out and do nothing
1594-
with zipfile.ZipFile(TESTFN, 'w') as zh:
1595-
zh.writestr(file, data)
1591+
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
15961592
with zipfile.ZipFile(TESTFN, 'a') as zh:
15971593
zh.close()
15981594
with self.assertRaises(ValueError):
1599-
zh.remove(file)
1595+
zh.remove(self.test_files[0][0])
16001596

16011597
# writing: error out and do nothing
1602-
with zipfile.ZipFile(TESTFN, 'w') as zh:
1603-
zh.writestr(file, data)
1598+
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
16041599
with zipfile.ZipFile(TESTFN, 'a') as zh:
16051600
with zh.open('newfile.txt', 'w') as fh:
16061601
with self.assertRaises(ValueError):
1607-
zh.remove(file)
1602+
zh.remove(self.test_files[0][0])
16081603

16091604
# mode 'r': error out and do nothing
1610-
with zipfile.ZipFile(TESTFN, 'w') as zh:
1611-
zh.writestr(file, data)
1605+
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
16121606
with zipfile.ZipFile(TESTFN, 'r') as zh:
16131607
with self.assertRaises(ValueError):
1614-
zh.remove(file)
1608+
zh.remove(self.test_files[0][0])
1609+
1610+
def test_remove_mode_w(self):
1611+
with zipfile.ZipFile(TESTFN, 'w') as zh:
1612+
for file, data in self.test_files:
1613+
zh.writestr(file, data)
1614+
zinfos = [ComparableZipInfo(zi) for zi in zh.infolist()]
1615+
1616+
zh.remove(self.test_files[0][0])
1617+
1618+
# check infolist
1619+
self.assertEqual(
1620+
[ComparableZipInfo(zi) for zi in zh.infolist()],
1621+
[zinfos[1], zinfos[2]],
1622+
)
1623+
1624+
# check NameToInfo cache
1625+
with self.assertRaises(KeyError):
1626+
zh.getinfo(self.test_files[0][0])
1627+
1628+
# make sure the zip file is still valid
1629+
self.assertIsNone(zh.testzip())
1630+
1631+
def test_remove_mode_x(self):
1632+
os.remove(TESTFN)
1633+
with zipfile.ZipFile(TESTFN, 'w') as zh:
1634+
for file, data in self.test_files:
1635+
zh.writestr(file, data)
1636+
zinfos = [ComparableZipInfo(zi) for zi in zh.infolist()]
1637+
1638+
zh.remove(self.test_files[0][0])
1639+
1640+
# check infolist
1641+
self.assertEqual(
1642+
[ComparableZipInfo(zi) for zi in zh.infolist()],
1643+
[zinfos[1], zinfos[2]],
1644+
)
1645+
1646+
# check NameToInfo cache
1647+
with self.assertRaises(KeyError):
1648+
zh.getinfo(self.test_files[0][0])
1649+
1650+
# make sure the zip file is still valid
1651+
self.assertIsNone(zh.testzip())
16151652

16161653
class StoredRemoveTests(AbstractRemoveTests, unittest.TestCase):
16171654
compression = zipfile.ZIP_STORED

0 commit comments

Comments
 (0)