summaryrefslogtreecommitdiff
path: root/Lib/test/test_zipfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_zipfile.py')
-rw-r--r--Lib/test/test_zipfile.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 2851051425..3bb9ce995c 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -1856,11 +1856,14 @@ class OtherTests(unittest.TestCase):
self.assertEqual(zipf.comment, b"an updated comment")
# check that comments are correctly shortened in append mode
+ # and the file is indeed truncated
with zipfile.ZipFile(TESTFN,mode="w") as zipf:
zipf.comment = b"original comment that's longer"
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
+ original_zip_size = os.path.getsize(TESTFN)
with zipfile.ZipFile(TESTFN,mode="a") as zipf:
zipf.comment = b"shorter comment"
+ self.assertTrue(original_zip_size > os.path.getsize(TESTFN))
with zipfile.ZipFile(TESTFN,mode="r") as zipf:
self.assertEqual(zipf.comment, b"shorter comment")
@@ -2886,6 +2889,33 @@ class TestPath(unittest.TestCase):
data = strm.read()
assert data == "content of a"
+ def test_open_write(self):
+ """
+ If the zipfile is open for write, it should be possible to
+ write bytes or text to it.
+ """
+ zf = zipfile.Path(zipfile.ZipFile(io.BytesIO(), mode='w'))
+ with zf.joinpath('file.bin').open('wb') as strm:
+ strm.write(b'binary contents')
+ with zf.joinpath('file.txt').open('w') as strm:
+ strm.write('text file')
+
+ def test_open_extant_directory(self):
+ """
+ Attempting to open a directory raises IsADirectoryError.
+ """
+ zf = zipfile.Path(add_dirs(build_alpharep_fixture()))
+ with self.assertRaises(IsADirectoryError):
+ zf.joinpath('b').open()
+
+ def test_open_missing_directory(self):
+ """
+ Attempting to open a missing directory raises FileNotFoundError.
+ """
+ zf = zipfile.Path(add_dirs(build_alpharep_fixture()))
+ with self.assertRaises(FileNotFoundError):
+ zf.joinpath('z').open()
+
def test_read(self):
for alpharep in self.zipfile_alpharep():
root = zipfile.Path(alpharep)
@@ -2983,6 +3013,12 @@ class TestPath(unittest.TestCase):
data = ['/'.join(string.ascii_lowercase + str(n)) for n in range(10000)]
zipfile.CompleteDirs._implied_dirs(data)
+ def test_read_does_not_close(self):
+ for alpharep in self.zipfile_ondisk():
+ with zipfile.ZipFile(alpharep) as file:
+ for rep in range(2):
+ zipfile.Path(file, 'a.txt').read_text()
+
if __name__ == "__main__":
unittest.main()