def zip_children(self): zip_path = zipp.Path(self.root) names = zip_path.root.namelist() self.joinpath = zip_path.joinpath return dict.fromkeys( child.split(posixpath.sep, 1)[0] for child in names)
def test_subdir_is_dir(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) assert (root / 'b').is_dir() assert (root / 'b/').is_dir() assert (root / 'g').is_dir() assert (root / 'g/').is_dir()
def test_joinpath(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.Path(zipfile_abcde) a = root.joinpath("a") assert a.is_file() e = root.joinpath("b").joinpath("d").joinpath("e.txt") assert e.read_text() == "content of e"
def test_pathlike_construction(self): """ zipp.Path should be constructable from a path-like object """ for zipfile_ondisk in self.zipfile_ondisk(): pathlike = pathlib.Path(str(zipfile_ondisk)) zipp.Path(pathlike)
def test_traverse_truediv(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.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_open(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) a, b, g = root.iterdir() with a.open() as strm: data = strm.read() assert data == "content of a"
def test_joinpath(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) a = root.joinpath("a") assert a.is_file() e = root.joinpath("b").joinpath("d").joinpath("e.txt") assert e.read_text() == "content of e"
def zip_children(self): zip_path = zipp.Path(self.root) names = zip_path.root.namelist() self.joinpath = zip_path.joinpath return unique_ordered( child.split(posixpath.sep, 1)[0] for child in names)
def test_zipfile_zippath_open(self): """Attempt to open POSIX only zipfile paths.""" zf = ZipFile(TESTCASE_PATH / "testcases.zip") memotest_path = zipp.Path(zf, at="testcases/memotest.dbf") memomemo_path = zipp.Path(zf, at="testcases/memotest.FPT") with memotest_path.open("rb") as mt: with memomemo_path.open("rb") as mm: data = DBF("memotest", filedata=mt, memofile=VFPMemoFile(mm.read())) assert data.__class__ is DBF dataset = list(data) self.check_alice_bob(dataset[0], dataset[1])
def test_pathlike_construction(self, alpharep): """ zipp.Path should be constructable from a path-like object """ zipfile_ondisk = self.zipfile_ondisk(alpharep) pathlike = pathlib.Path(str(zipfile_ondisk)) zipp.Path(pathlike)
def test_open_missing_directory(self): """ Attempting to open a missing directory raises FileNotFoundError. """ zf = zipp.Path(add_dirs(build_alpharep_fixture())) with self.assertRaises(FileNotFoundError): zf.joinpath('z').open()
def test_open_extant_directory(self): """ Attempting to open a directory raises IsADirectoryError. """ zf = zipp.Path(add_dirs(build_alpharep_fixture())) with self.assertRaises(IsADirectoryError): zf.joinpath('b').open()
def test_open(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.Path(zipfile_abcde) a, b = root.iterdir() with a.open() as strm: data = strm.read() assert data == b"content of a"
def _local(cls, root='.'): from pep517 import build, meta system = build.compat_system(root) builder = functools.partial( meta.build, source_dir=root, system=system, ) return PathDistribution(zipp.Path(meta.build_as_zip(builder)))
def test_joinpath_constant_time(self): """ Ensure joinpath on items in zipfile is linear time. """ root = zipp.Path(self.huge_zipfile()) entries = jaraco.itertools.Counter(root.iterdir()) for entry in entries: entry.joinpath('suffix') # Check the file iterated all items assert entries.count == self.HUGE_ZIPFILE_NUM_ENTRIES
def test_open_write(self): """ If the zipfile is open for write, it should be possible to write bytes or text to it. """ zf = zipp.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_iterdir_istype(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.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_mutability(self, alpharep): """ If the underlying zipfile is changed, the Path object should reflect that change. """ root = zipp.Path(alpharep) a, b, g = root.iterdir() alpharep.writestr('foo.txt', 'foo') alpharep.writestr('bar/baz.txt', 'baz') assert any(child.name == 'foo.txt' for child in root.iterdir()) assert (root / 'foo.txt').read_text() == 'foo' (baz,) = (root / 'bar').iterdir() assert baz.read_text() == 'baz'
def test_iterdir_and_types(self, alpharep): root = zipp.Path(alpharep) assert root.is_dir() a, b, g = root.iterdir() assert a.is_file() assert b.is_dir() assert g.is_dir() c, f, d = b.iterdir() assert c.is_file() and f.is_file() (e,) = d.iterdir() assert e.is_file() (h,) = g.iterdir() (i,) = h.iterdir() assert i.is_file()
def test_iterdir_and_types(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) assert root.is_dir() a, b, g = root.iterdir() assert a.is_file() assert b.is_dir() assert g.is_dir() c, f, d = b.iterdir() assert c.is_file() and f.is_file() e, = d.iterdir() assert e.is_file() h, = g.iterdir() i, = h.iterdir() assert i.is_file()
def test_root_unnamed(self, alpharep): """ It is an error to attempt to get the name or parent of an unnamed zipfile. """ alpharep.filename = None root = zipp.Path(alpharep) with self.assertRaises(TypeError): root.name with self.assertRaises(TypeError): root.parent # .name and .parent should still work on subs sub = root / "b" assert sub.name == "b" assert sub.parent
def zip_children(self): zip_path = zipp.Path(self.root) names = zip_path.root.namelist() self.joinpath = zip_path.joinpath return (posixpath.split(child)[0] for child in names)
def _switch_path(path): if not PYPY_OPEN_BUG or os.path.isfile(path): # pragma: no branch with suppress(Exception): return zipp.Path(path) return pathlib.Path(path)
def _switch_path(path): with suppress(Exception): return zipp.Path(path) return pathlib.Path(path)
def test_parent(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.Path(zipfile_abcde) assert (root / 'a').parent.at == '' assert (root / 'a' / 'b').parent.at == 'a/'
def test_traverse_pathlike(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.Path(zipfile_abcde) root / pathlib.Path("a")
def test_traverse_pathlike(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) root / pathlib.Path("a")
def test_dir_parent(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) assert (root / 'b').parent.at == '' assert (root / 'b/').parent.at == ''
def test_missing_dir_parent(self): for alpharep in self.zipfile_alpharep(): root = zipp.Path(alpharep) assert (root / 'missing dir/').parent.at == ''
def test_read(self): for zipfile_abcde in self.zipfile_abcde(): root = zipp.Path(zipfile_abcde) a, b = root.iterdir() assert a.read_text() == "content of a" assert a.read_bytes() == b"content of a"