Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(628)

Unified Diff: Lib/test/test_zipfile.py

Issue 6818: remove/delete method for zipfile/tarfile objects
Patch Set: Created 2 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Doc/library/zipfile.rst ('k') | Lib/zipfile.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_zipfile.py Sun Mar 06 18:10:58 2011 -0600
+++ b/Lib/test/test_zipfile.py Tue Mar 15 03:02:13 2011 +0200
@@ -1440,11 +1440,116 @@
unlink(TESTFN2)
+class RemoveTests(unittest.TestCase):
+ def test_simple(self):
+ fname = "foo.txt"
+ # remove with fname
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ zf.writestr(fname, "just add a file with a name and some data")
+ self.assertEqual(zf.infolist()[0].filename, fname)
+ with zipfile.ZipFile(TESTFN, "a") as zf:
+ zf.remove(fname)
+ self.assertEqual(len(zf.infolist()), 0)
+
+ # remove with zipinfo
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ zf.writestr(fname, "just add a file with a name and some data")
+ self.assertEqual(zf.infolist()[0].filename, fname)
+ with zipfile.ZipFile(TESTFN, "a") as zf:
+ zinfo = zf.getinfo(fname)
+ zf.remove(zinfo)
+ self.assertEqual(len(zf.infolist()), 0)
+
+ def write_three_and_remove_one(self, index):
+ data_list = [bytes([randint(0,255) for i in range(10)]) for i in range(3)]
+ fname_list = ["firstfile", "secondfile", "thirdfile"]
+
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ for fname, data in zip(fname_list, data_list):
+ zf.writestr(fname, data)
+
+ with zipfile.ZipFile(TESTFN, "a") as zf:
+ zf.remove(fname_list[index])
+
+ i_to_check = list(range(len(data_list)))
+ i_to_check.remove(index)
+
+ with zipfile.ZipFile(TESTFN, "r") as zf:
+ for i in i_to_check:
+ # the last reads fail if the pointers weren't corrected
+ self.assertEqual(zf.read(fname_list[i]), data_list[i])
+
+ def test_remove_middle(self):
+ self.write_three_and_remove_one(0)
+ self.write_three_and_remove_one(1)
+ self.write_three_and_remove_one(2)
+
+ def test_with_data_descriptor(self):
+ fname = "foo.txt"
+ data = "just add a file with a name and some data"
+
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ zinfo = zipfile.ZipInfo(fname)
+ zinfo.flag_bits = zinfo.flag_bits | zipfile._FHF_HAS_DATA_DESCRIPTOR
+ zf.writestr(zinfo, data)
+
+ with zipfile.ZipFile(TESTFN, "r") as zf:
+ zinfo = zf.getinfo(fname)
+ data_desc_size = zf._get_data_descriptor_size(zinfo)
+ zlen = len(zinfo.FileHeader()) + zinfo.compress_size + len(zf._central_dir_header(zinfo)) + data_desc_size
+
+ size = os.path.getsize(TESTFN)
+ with zipfile.ZipFile(TESTFN, "a") as zf:
+ zf.remove(fname)
+
+ new_size = os.path.getsize(TESTFN)
+
+ size_delta = size - new_size
+ self.assertEqual(size_delta, zlen)
+
+ def test_shrinks(self):
+ fname = "foo.txt"
+ data = "just add a file with a name and some data"
+
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ zf.writestr(fname, data)
+ zinfo = zf.getinfo(fname)
+ # ignoring descriptor size because ZipFile by default doesn't use it
+ zlen = len(zinfo.FileHeader()) + zinfo.compress_size + len(zf._central_dir_header(zinfo))
+
+ size = os.path.getsize(TESTFN)
+ with zipfile.ZipFile(TESTFN, "a") as zf:
+ zf.remove(fname)
+
+ new_size = os.path.getsize(TESTFN)
+
+ # size was 22 bytes vs 153 bytes on my machine btw
+ self.assertEqual(new_size, size - zlen)
+
+ def test_verifies_requirements(self):
+ fname = "foo.txt"
+ # test no remove on closed zipfile
+ with self.assertRaises(RuntimeError):
+ zf = zipfile.ZipFile(TESTFN, "w")
+ zf.writestr(fname, "just add a file with a name and some data")
+ zf.close()
+ zf.remove(fname)
+
+ # test no remove without "a"
+ with self.assertRaises(RuntimeError):
+ with zipfile.ZipFile(TESTFN, "w") as zf:
+ zf.writestr(fname, "just add a file with a name and some data")
+ zf.remove(fname)
+
+ def tearDown(self):
+ unlink(TESTFN)
+
+
def test_main():
run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
TestWithDirectory, UniversalNewlineTests,
- TestsWithRandomBinaryFiles)
+ TestsWithRandomBinaryFiles, RemoveTests)
if __name__ == "__main__":
test_main()
« no previous file with comments | « Doc/library/zipfile.rst ('k') | Lib/zipfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7