summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_zipfile.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2019-05-08 13:45:06 (GMT)
committerBarry Warsaw <barry@python.org>2019-05-08 13:45:05 (GMT)
commitb2758ff9553d8bebe4e9dd1cb3996212473810e3 (patch)
tree60ee775ed37a5262b66dec6f4122888da43df2e3 /Lib/test/test_zipfile.py
parent70b80541bb044e8cb7037acaf97f64890fef418e (diff)
downloadcpython-b2758ff9553d8bebe4e9dd1cb3996212473810e3.zip
cpython-b2758ff9553d8bebe4e9dd1cb3996212473810e3.tar.gz
cpython-b2758ff9553d8bebe4e9dd1cb3996212473810e3.tar.bz2
bpo-36832: add zipfile.Path (#13153)
* bpo-36832: add zipfile.Path * bpo-36832: add documentation for zipfile.Path * 📜🤖 Added by blurb_it. * Remove module reference from blurb. * Sort the imports * Update docstrings and docs per recommendations. * Rely on test.support.temp_dir * Signal that 'root' is the parameter. * Correct spelling of 'mod' * Convert docstring to comment for brevity. * Fix more errors in the docs
Diffstat (limited to 'Lib/test/test_zipfile.py')
-rw-r--r--Lib/test/test_zipfile.py116
1 files changed, 113 insertions, 3 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 14e1e08..538d4ee 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -1,13 +1,15 @@
import contextlib
+import importlib.util
import io
import os
-import importlib.util
import pathlib
import posixpath
-import time
+import shutil
import struct
-import zipfile
+import tempfile
+import time
import unittest
+import zipfile
from tempfile import TemporaryFile
@@ -2392,5 +2394,113 @@ class CommandLineTest(unittest.TestCase):
with open(path, 'rb') as f:
self.assertEqual(f.read(), zf.read(zi))
+
+# Poor man's technique to consume a (smallish) iterable.
+consume = tuple
+
+
+def add_dirs(zipfile):
+ """
+ Given a writable zipfile, inject directory entries for
+ any directories implied by the presence of children.
+ """
+ names = zipfile.namelist()
+ consume(
+ zipfile.writestr(name + "/", b"")
+ for name in map(posixpath.dirname, names)
+ if name and name + "/" not in names
+ )
+ return zipfile
+
+
+def build_abcde_files():
+ """
+ Create a zip file with this structure:
+
+ .
+ ├── a.txt
+ └── b
+ ├── c.txt
+ └── d
+ └── e.txt
+ """
+ data = io.BytesIO()
+ zf = zipfile.ZipFile(data, "w")
+ zf.writestr("a.txt", b"content of a")
+ zf.writestr("b/c.txt", b"content of c")
+ zf.writestr("b/d/e.txt", b"content of e")
+ zf.filename = "abcde.zip"
+ return zf
+
+
+class TestPath(unittest.TestCase):
+ def setUp(self):
+ self.fixtures = contextlib.ExitStack()
+ self.addCleanup(self.fixtures.close)
+
+ def zipfile_abcde(self):
+ with self.subTest():
+ yield build_abcde_files()
+ with self.subTest():
+ yield add_dirs(build_abcde_files())
+
+ def zipfile_ondisk(self):
+ tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir()))
+ for zipfile_abcde in self.zipfile_abcde():
+ buffer = zipfile_abcde.fp
+ zipfile_abcde.close()
+ path = tmpdir / zipfile_abcde.filename
+ with path.open("wb") as strm:
+ strm.write(buffer.getvalue())
+ yield path
+
+ def test_iterdir_istype(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ assert root.is_dir()
+ a, b = root.iterdir()
+ assert a.is_file()
+ assert b.is_dir()
+ c, d = b.iterdir()
+ assert c.is_file()
+ e, = d.iterdir()
+ assert e.is_file()
+
+ def test_open(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ a, b = root.iterdir()
+ with a.open() as strm:
+ data = strm.read()
+ assert data == b"content of a"
+
+ def test_read(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ a, b = root.iterdir()
+ assert a.read_text() == "content of a"
+ assert a.read_bytes() == b"content of a"
+
+ def test_traverse_truediv(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ a = root / "a"
+ assert a.is_file()
+ e = root / "b" / "d" / "e.txt"
+ assert e.read_text() == "content of e"
+
+ def test_pathlike_construction(self):
+ """
+ zipfile.Path should be constructable from a path-like object
+ """
+ for zipfile_ondisk in self.zipfile_ondisk():
+ pathlike = pathlib.Path(str(zipfile_ondisk))
+ zipfile.Path(pathlike)
+
+ def test_traverse_pathlike(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ root / pathlib.Path("a")
+
if __name__ == "__main__":
unittest.main()