Skip to content

Commit 8f0a504

Browse files
committed
Fix and improve validation tests
- Separate individual validation tests. - Check underlying repacker not called in validation. - Use `unlink` to prevent FileNotFoundError. - Fix mode 'x' test.
1 parent 0d971d8 commit 8f0a504

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,23 +1586,22 @@ def test_remove_zip64(self):
15861586
# make sure the zip file is still valid
15871587
self.assertIsNone(zh.testzip())
15881588

1589-
def test_remove_validate(self):
1590-
# closed: error out and do nothing
1591-
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
1589+
def test_remove_closed(self):
1590+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
15921591
with zipfile.ZipFile(TESTFN, 'a') as zh:
15931592
zh.close()
15941593
with self.assertRaises(ValueError):
15951594
zh.remove(self.test_files[0][0])
15961595

1597-
# writing: error out and do nothing
1598-
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
1596+
def test_remove_writing(self):
1597+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
15991598
with zipfile.ZipFile(TESTFN, 'a') as zh:
16001599
with zh.open('newfile.txt', 'w') as fh:
16011600
with self.assertRaises(ValueError):
16021601
zh.remove(self.test_files[0][0])
16031602

1604-
# mode 'r': error out and do nothing
1605-
zinfos = self._prepare_zip_from_test_files(TESTFN, self.test_files)
1603+
def test_remove_mode_r(self):
1604+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
16061605
with zipfile.ZipFile(TESTFN, 'r') as zh:
16071606
with self.assertRaises(ValueError):
16081607
zh.remove(self.test_files[0][0])
@@ -1629,8 +1628,8 @@ def test_remove_mode_w(self):
16291628
self.assertIsNone(zh.testzip())
16301629

16311630
def test_remove_mode_x(self):
1632-
os.remove(TESTFN)
1633-
with zipfile.ZipFile(TESTFN, 'w') as zh:
1631+
unlink(TESTFN)
1632+
with zipfile.ZipFile(TESTFN, 'x') as zh:
16341633
for file, data in self.test_files:
16351634
zh.writestr(file, data)
16361635
zinfos = [ComparableZipInfo(zi) for zi in zh.infolist()]
@@ -2025,45 +2024,46 @@ def test_repack_data_descriptor_no_sig_and_zip64(self):
20252024
# check file size
20262025
self.assertEqual(os.path.getsize(TESTFN), expected_size)
20272026

2028-
def test_repack_validate(self):
2029-
file = 'datafile.txt'
2030-
data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
2031-
2032-
# closed: error out and do nothing
2033-
with zipfile.ZipFile(TESTFN, 'w') as zh:
2034-
zh.writestr(file, data)
2027+
@mock.patch('zipfile._ZipRepacker')
2028+
def test_repack_closed(self, m_repack):
2029+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
20352030
with zipfile.ZipFile(TESTFN, 'a') as zh:
20362031
zh.close()
20372032
with self.assertRaises(ValueError):
20382033
zh.repack()
2034+
m_repack.assert_not_called()
20392035

2040-
# writing: error out and do nothing
2041-
with zipfile.ZipFile(TESTFN, 'w') as zh:
2042-
zh.writestr(file, data)
2036+
@mock.patch('zipfile._ZipRepacker')
2037+
def test_repack_writing(self, m_repack):
2038+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
20432039
with zipfile.ZipFile(TESTFN, 'a') as zh:
20442040
with zh.open('newfile.txt', 'w') as fh:
20452041
with self.assertRaises(ValueError):
20462042
zh.repack()
2043+
m_repack.assert_not_called()
20472044

2048-
# mode 'r': error out and do nothing
2049-
with zipfile.ZipFile(TESTFN, 'w') as zh:
2050-
zh.writestr(file, data)
2045+
@mock.patch('zipfile._ZipRepacker')
2046+
def test_repack_mode_r(self, m_repack):
2047+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
20512048
with zipfile.ZipFile(TESTFN, 'r') as zh:
20522049
with self.assertRaises(ValueError):
20532050
zh.repack()
2051+
m_repack.assert_not_called()
20542052

2055-
# mode 'w': error out and do nothing
2056-
with zipfile.ZipFile(TESTFN, 'w') as zh:
2057-
zh.writestr(file, data)
2053+
@mock.patch('zipfile._ZipRepacker')
2054+
def test_repack_mode_w(self, m_repack):
20582055
with zipfile.ZipFile(TESTFN, 'w') as zh:
20592056
with self.assertRaises(ValueError):
20602057
zh.repack()
2058+
m_repack.assert_not_called()
20612059

2062-
# mode 'x': error out and do nothing
2063-
os.remove(TESTFN)
2060+
@mock.patch('zipfile._ZipRepacker')
2061+
def test_repack_mode_x(self, m_repack):
2062+
unlink(TESTFN)
20642063
with zipfile.ZipFile(TESTFN, 'x') as zh:
20652064
with self.assertRaises(ValueError):
20662065
zh.repack()
2066+
m_repack.assert_not_called()
20672067

20682068
class StoredRepackTests(AbstractRepackTests, unittest.TestCase):
20692069
compression = zipfile.ZIP_STORED

0 commit comments

Comments
 (0)