| 1 |
import contextlib |
| 2 |
import io |
| 3 |
import itertools |
| 4 |
import pathlib |
| 5 |
import pickle |
| 6 |
import stat |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
import zipfile |
| 10 |
import zipfile._path |
| 11 |
|
| 12 |
from test.support.os_helper import FakePath, temp_dir |
| 13 |
|
| 14 |
from ._functools import compose |
| 15 |
from ._itertools import Counter |
| 16 |
from ._test_params import Invoked, parameterize |
| 17 |
|
| 18 |
|
| 19 |
class jaraco: |
| 20 |
class itertools: |
| 21 |
Counter = Counter |
| 22 |
|
| 23 |
|
| 24 |
def _make_link(info: zipfile.ZipInfo): # type: ignore[name-defined] |
| 25 |
info.external_attr |= stat.S_IFLNK << 16 |
| 26 |
|
| 27 |
|
| 28 |
def build_alpharep_fixture(): |
| 29 |
""" |
| 30 |
Create a zip file with this structure: |
| 31 |
|
| 32 |
. |
| 33 |
├── a.txt |
| 34 |
├── n.txt (-> a.txt) |
| 35 |
├── b |
| 36 |
│ ├── c.txt |
| 37 |
│ ├── d |
| 38 |
│ │ └── e.txt |
| 39 |
│ └── f.txt |
| 40 |
├── g |
| 41 |
│ └── h |
| 42 |
│ └── i.txt |
| 43 |
└── j |
| 44 |
├── k.bin |
| 45 |
├── l.baz |
| 46 |
└── m.bar |
| 47 |
|
| 48 |
This fixture has the following key characteristics: |
| 49 |
|
| 50 |
- a file at the root (a) |
| 51 |
- a file two levels deep (b/d/e) |
| 52 |
- multiple files in a directory (b/c, b/f) |
| 53 |
- a directory containing only a directory (g/h) |
| 54 |
- a directory with files of different extensions (j/klm) |
| 55 |
- a symlink (n) pointing to (a) |
| 56 |
|
| 57 |
"alpha" because it uses alphabet |
| 58 |
"rep" because it's a representative example |
| 59 |
""" |
| 60 |
data = io.BytesIO() |
| 61 |
zf = zipfile.ZipFile(data, "w") |
| 62 |
zf.writestr("a.txt", b"content of a") |
| 63 |
zf.writestr("b/c.txt", b"content of c") |
| 64 |
zf.writestr("b/d/e.txt", b"content of e") |
| 65 |
zf.writestr("b/f.txt", b"content of f") |
| 66 |
zf.writestr("g/h/i.txt", b"content of i") |
| 67 |
zf.writestr("j/k.bin", b"content of k") |
| 68 |
zf.writestr("j/l.baz", b"content of l") |
| 69 |
zf.writestr("j/m.bar", b"content of m") |
| 70 |
zf.writestr("n.txt", b"a.txt") |
| 71 |
_make_link(zf.infolist()[-1]) |
| 72 |
|
| 73 |
zf.filename = "alpharep.zip" |
| 74 |
return zf |
| 75 |
|
| 76 |
|
| 77 |
alpharep_generators = [ |
| 78 |
Invoked.wrap(build_alpharep_fixture), |
| 79 |
Invoked.wrap(compose(zipfile._path.CompleteDirs.inject, build_alpharep_fixture)), |
| 80 |
] |
| 81 |
|
| 82 |
pass_alpharep = parameterize(['alpharep'], alpharep_generators) |
| 83 |
|
| 84 |
|
| 85 |
class TestPath(unittest.TestCase): |
| 86 |
def setUp(self): |
| 87 |
self.fixtures = contextlib.ExitStack() |
| 88 |
self.addCleanup(self.fixtures.close) |
| 89 |
|
| 90 |
def zipfile_ondisk(self, alpharep): |
| 91 |
tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir())) |
| 92 |
buffer = alpharep.fp |
| 93 |
alpharep.close() |
| 94 |
path = tmpdir / alpharep.filename |
| 95 |
with path.open("wb") as strm: |
| 96 |
strm.write(buffer.getvalue()) |
| 97 |
return path |
| 98 |
|
| 99 |
@pass_alpharep |
| 100 |
def test_iterdir_and_types(self, alpharep): |
| 101 |
root = zipfile.Path(alpharep) |
| 102 |
assert root.is_dir() |
| 103 |
a, n, b, g, j = root.iterdir() |
| 104 |
assert a.is_file() |
| 105 |
assert b.is_dir() |
| 106 |
assert g.is_dir() |
| 107 |
c, f, d = b.iterdir() |
| 108 |
assert c.is_file() and f.is_file() |
| 109 |
(e,) = d.iterdir() |
| 110 |
assert e.is_file() |
| 111 |
(h,) = g.iterdir() |
| 112 |
(i,) = h.iterdir() |
| 113 |
assert i.is_file() |
| 114 |
|
| 115 |
@pass_alpharep |
| 116 |
def test_is_file_missing(self, alpharep): |
| 117 |
root = zipfile.Path(alpharep) |
| 118 |
assert not root.joinpath('missing.txt').is_file() |
| 119 |
|
| 120 |
@pass_alpharep |
| 121 |
def test_iterdir_on_file(self, alpharep): |
| 122 |
root = zipfile.Path(alpharep) |
| 123 |
a, n, b, g, j = root.iterdir() |
| 124 |
with self.assertRaises(ValueError): |
| 125 |
a.iterdir() |
| 126 |
|
| 127 |
@pass_alpharep |
| 128 |
def test_subdir_is_dir(self, alpharep): |
| 129 |
root = zipfile.Path(alpharep) |
| 130 |
assert (root / 'b').is_dir() |
| 131 |
assert (root / 'b/').is_dir() |
| 132 |
assert (root / 'g').is_dir() |
| 133 |
assert (root / 'g/').is_dir() |
| 134 |
|
| 135 |
@pass_alpharep |
| 136 |
def test_open(self, alpharep): |
| 137 |
root = zipfile.Path(alpharep) |
| 138 |
a, n, b, g, j = root.iterdir() |
| 139 |
with a.open(encoding="utf-8") as strm: |
| 140 |
data = strm.read() |
| 141 |
self.assertEqual(data, "content of a") |
| 142 |
with a.open('r', "utf-8") as strm: # not a kw, no gh-101144 TypeError |
| 143 |
data = strm.read() |
| 144 |
self.assertEqual(data, "content of a") |
| 145 |
|
| 146 |
def test_open_encoding_utf16(self): |
| 147 |
in_memory_file = io.BytesIO() |
| 148 |
zf = zipfile.ZipFile(in_memory_file, "w") |
| 149 |
zf.writestr("path/16.txt", "This was utf-16".encode("utf-16")) |
| 150 |
zf.filename = "test_open_utf16.zip" |
| 151 |
root = zipfile.Path(zf) |
| 152 |
(path,) = root.iterdir() |
| 153 |
u16 = path.joinpath("16.txt") |
| 154 |
with u16.open('r', "utf-16") as strm: |
| 155 |
data = strm.read() |
| 156 |
assert data == "This was utf-16" |
| 157 |
with u16.open(encoding="utf-16") as strm: |
| 158 |
data = strm.read() |
| 159 |
assert data == "This was utf-16" |
| 160 |
|
| 161 |
def test_open_encoding_errors(self): |
| 162 |
in_memory_file = io.BytesIO() |
| 163 |
zf = zipfile.ZipFile(in_memory_file, "w") |
| 164 |
zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.") |
| 165 |
zf.filename = "test_read_text_encoding_errors.zip" |
| 166 |
root = zipfile.Path(zf) |
| 167 |
(path,) = root.iterdir() |
| 168 |
u16 = path.joinpath("bad-utf8.bin") |
| 169 |
|
| 170 |
# encoding= as a positional argument for gh-101144. |
| 171 |
data = u16.read_text("utf-8", errors="ignore") |
| 172 |
assert data == "invalid utf-8: ." |
| 173 |
with u16.open("r", "utf-8", errors="surrogateescape") as f: |
| 174 |
assert f.read() == "invalid utf-8: \udcff\udcff." |
| 175 |
|
| 176 |
# encoding= both positional and keyword is an error; gh-101144. |
| 177 |
with self.assertRaisesRegex(TypeError, "encoding"): |
| 178 |
data = u16.read_text("utf-8", encoding="utf-8") |
| 179 |
|
| 180 |
# both keyword arguments work. |
| 181 |
with u16.open("r", encoding="utf-8", errors="strict") as f: |
| 182 |
# error during decoding with wrong codec. |
| 183 |
with self.assertRaises(UnicodeDecodeError): |
| 184 |
f.read() |
| 185 |
|
| 186 |
@unittest.skipIf( |
| 187 |
not getattr(sys.flags, 'warn_default_encoding', 0), |
| 188 |
"Requires warn_default_encoding", |
| 189 |
) |
| 190 |
@pass_alpharep |
| 191 |
def test_encoding_warnings(self, alpharep): |
| 192 |
"""EncodingWarning must blame the read_text and open calls.""" |
| 193 |
assert sys.flags.warn_default_encoding |
| 194 |
root = zipfile.Path(alpharep) |
| 195 |
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296) |
| 196 |
root.joinpath("a.txt").read_text() |
| 197 |
assert __file__ == wc.filename |
| 198 |
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296) |
| 199 |
root.joinpath("a.txt").open("r").close() |
| 200 |
assert __file__ == wc.filename |
| 201 |
|
| 202 |
def test_open_write(self): |
| 203 |
""" |
| 204 |
If the zipfile is open for write, it should be possible to |
| 205 |
write bytes or text to it. |
| 206 |
""" |
| 207 |
zf = zipfile.Path(zipfile.ZipFile(io.BytesIO(), mode='w')) |
| 208 |
with zf.joinpath('file.bin').open('wb') as strm: |
| 209 |
strm.write(b'binary contents') |
| 210 |
with zf.joinpath('file.txt').open('w', encoding="utf-8") as strm: |
| 211 |
strm.write('text file') |
| 212 |
|
| 213 |
@pass_alpharep |
| 214 |
def test_open_extant_directory(self, alpharep): |
| 215 |
""" |
| 216 |
Attempting to open a directory raises IsADirectoryError. |
| 217 |
""" |
| 218 |
zf = zipfile.Path(alpharep) |
| 219 |
with self.assertRaises(IsADirectoryError): |
| 220 |
zf.joinpath('b').open() |
| 221 |
|
| 222 |
@pass_alpharep |
| 223 |
def test_open_binary_invalid_args(self, alpharep): |
| 224 |
root = zipfile.Path(alpharep) |
| 225 |
with self.assertRaises(ValueError): |
| 226 |
root.joinpath('a.txt').open('rb', encoding='utf-8') |
| 227 |
with self.assertRaises(ValueError): |
| 228 |
root.joinpath('a.txt').open('rb', 'utf-8') |
| 229 |
|
| 230 |
@pass_alpharep |
| 231 |
def test_open_missing_directory(self, alpharep): |
| 232 |
""" |
| 233 |
Attempting to open a missing directory raises FileNotFoundError. |
| 234 |
""" |
| 235 |
zf = zipfile.Path(alpharep) |
| 236 |
with self.assertRaises(FileNotFoundError): |
| 237 |
zf.joinpath('z').open() |
| 238 |
|
| 239 |
@pass_alpharep |
| 240 |
def test_read(self, alpharep): |
| 241 |
root = zipfile.Path(alpharep) |
| 242 |
a, n, b, g, j = root.iterdir() |
| 243 |
assert a.read_text(encoding="utf-8") == "content of a" |
| 244 |
# Also check positional encoding arg (gh-101144). |
| 245 |
assert a.read_text("utf-8") == "content of a" |
| 246 |
assert a.read_bytes() == b"content of a" |
| 247 |
|
| 248 |
@pass_alpharep |
| 249 |
def test_joinpath(self, alpharep): |
| 250 |
root = zipfile.Path(alpharep) |
| 251 |
a = root.joinpath("a.txt") |
| 252 |
assert a.is_file() |
| 253 |
e = root.joinpath("b").joinpath("d").joinpath("e.txt") |
| 254 |
assert e.read_text(encoding="utf-8") == "content of e" |
| 255 |
|
| 256 |
@pass_alpharep |
| 257 |
def test_joinpath_multiple(self, alpharep): |
| 258 |
root = zipfile.Path(alpharep) |
| 259 |
e = root.joinpath("b", "d", "e.txt") |
| 260 |
assert e.read_text(encoding="utf-8") == "content of e" |
| 261 |
|
| 262 |
@pass_alpharep |
| 263 |
def test_traverse_truediv(self, alpharep): |
| 264 |
root = zipfile.Path(alpharep) |
| 265 |
a = root / "a.txt" |
| 266 |
assert a.is_file() |
| 267 |
e = root / "b" / "d" / "e.txt" |
| 268 |
assert e.read_text(encoding="utf-8") == "content of e" |
| 269 |
|
| 270 |
@pass_alpharep |
| 271 |
def test_pathlike_construction(self, alpharep): |
| 272 |
""" |
| 273 |
zipfile.Path should be constructable from a path-like object |
| 274 |
""" |
| 275 |
zipfile_ondisk = self.zipfile_ondisk(alpharep) |
| 276 |
pathlike = FakePath(str(zipfile_ondisk)) |
| 277 |
root = zipfile.Path(pathlike) |
| 278 |
root.root.close() |
| 279 |
|
| 280 |
@pass_alpharep |
| 281 |
def test_traverse_pathlike(self, alpharep): |
| 282 |
root = zipfile.Path(alpharep) |
| 283 |
root / FakePath("a") |
| 284 |
|
| 285 |
@pass_alpharep |
| 286 |
def test_parent(self, alpharep): |
| 287 |
root = zipfile.Path(alpharep) |
| 288 |
assert (root / 'a').parent.at == '' |
| 289 |
assert (root / 'a' / 'b').parent.at == 'a/' |
| 290 |
|
| 291 |
@pass_alpharep |
| 292 |
def test_dir_parent(self, alpharep): |
| 293 |
root = zipfile.Path(alpharep) |
| 294 |
assert (root / 'b').parent.at == '' |
| 295 |
assert (root / 'b/').parent.at == '' |
| 296 |
|
| 297 |
@pass_alpharep |
| 298 |
def test_missing_dir_parent(self, alpharep): |
| 299 |
root = zipfile.Path(alpharep) |
| 300 |
assert (root / 'missing dir/').parent.at == '' |
| 301 |
|
| 302 |
@pass_alpharep |
| 303 |
def test_mutability(self, alpharep): |
| 304 |
""" |
| 305 |
If the underlying zipfile is changed, the Path object should |
| 306 |
reflect that change. |
| 307 |
""" |
| 308 |
root = zipfile.Path(alpharep) |
| 309 |
a, n, b, g, j = root.iterdir() |
| 310 |
alpharep.writestr('foo.txt', 'foo') |
| 311 |
alpharep.writestr('bar/baz.txt', 'baz') |
| 312 |
assert any(child.name == 'foo.txt' for child in root.iterdir()) |
| 313 |
assert (root / 'foo.txt').read_text(encoding="utf-8") == 'foo' |
| 314 |
(baz,) = (root / 'bar').iterdir() |
| 315 |
assert baz.read_text(encoding="utf-8") == 'baz' |
| 316 |
|
| 317 |
HUGE_ZIPFILE_NUM_ENTRIES = 2**13 |
| 318 |
|
| 319 |
def huge_zipfile(self): |
| 320 |
"""Create a read-only zipfile with a huge number of entries entries.""" |
| 321 |
strm = io.BytesIO() |
| 322 |
zf = zipfile.ZipFile(strm, "w") |
| 323 |
for entry in map(str, range(self.HUGE_ZIPFILE_NUM_ENTRIES)): |
| 324 |
zf.writestr(entry, entry) |
| 325 |
zf.mode = 'r' |
| 326 |
return zf |
| 327 |
|
| 328 |
def test_joinpath_constant_time(self): |
| 329 |
""" |
| 330 |
Ensure joinpath on items in zipfile is linear time. |
| 331 |
""" |
| 332 |
root = zipfile.Path(self.huge_zipfile()) |
| 333 |
entries = jaraco.itertools.Counter(root.iterdir()) |
| 334 |
for entry in entries: |
| 335 |
entry.joinpath('suffix') |
| 336 |
# Check the file iterated all items |
| 337 |
assert entries.count == self.HUGE_ZIPFILE_NUM_ENTRIES |
| 338 |
|
| 339 |
@pass_alpharep |
| 340 |
def test_read_does_not_close(self, alpharep): |
| 341 |
alpharep = self.zipfile_ondisk(alpharep) |
| 342 |
with zipfile.ZipFile(alpharep) as file: |
| 343 |
for rep in range(2): |
| 344 |
zipfile.Path(file, 'a.txt').read_text(encoding="utf-8") |
| 345 |
|
| 346 |
@pass_alpharep |
| 347 |
def test_subclass(self, alpharep): |
| 348 |
class Subclass(zipfile.Path): |
| 349 |
pass |
| 350 |
|
| 351 |
root = Subclass(alpharep) |
| 352 |
assert isinstance(root / 'b', Subclass) |
| 353 |
|
| 354 |
@pass_alpharep |
| 355 |
def test_filename(self, alpharep): |
| 356 |
root = zipfile.Path(alpharep) |
| 357 |
assert root.filename == pathlib.Path('alpharep.zip') |
| 358 |
|
| 359 |
@pass_alpharep |
| 360 |
def test_root_name(self, alpharep): |
| 361 |
""" |
| 362 |
The name of the root should be the name of the zipfile |
| 363 |
""" |
| 364 |
root = zipfile.Path(alpharep) |
| 365 |
assert root.name == 'alpharep.zip' == root.filename.name |
| 366 |
|
| 367 |
@pass_alpharep |
| 368 |
def test_root_on_disk(self, alpharep): |
| 369 |
""" |
| 370 |
The name/stem of the root should match the zipfile on disk. |
| 371 |
|
| 372 |
This condition must hold across platforms. |
| 373 |
""" |
| 374 |
root = zipfile.Path(self.zipfile_ondisk(alpharep)) |
| 375 |
assert root.name == 'alpharep.zip' == root.filename.name |
| 376 |
assert root.stem == 'alpharep' == root.filename.stem |
| 377 |
root.root.close() |
| 378 |
|
| 379 |
@pass_alpharep |
| 380 |
def test_suffix(self, alpharep): |
| 381 |
""" |
| 382 |
The suffix of the root should be the suffix of the zipfile. |
| 383 |
The suffix of each nested file is the final component's last suffix, if any. |
| 384 |
Includes the leading period, just like pathlib.Path. |
| 385 |
""" |
| 386 |
root = zipfile.Path(alpharep) |
| 387 |
assert root.suffix == '.zip' == root.filename.suffix |
| 388 |
|
| 389 |
b = root / "b.txt" |
| 390 |
assert b.suffix == ".txt" |
| 391 |
|
| 392 |
c = root / "c" / "filename.tar.gz" |
| 393 |
assert c.suffix == ".gz" |
| 394 |
|
| 395 |
d = root / "d" |
| 396 |
assert d.suffix == "" |
| 397 |
|
| 398 |
@pass_alpharep |
| 399 |
def test_suffixes(self, alpharep): |
| 400 |
""" |
| 401 |
The suffix of the root should be the suffix of the zipfile. |
| 402 |
The suffix of each nested file is the final component's last suffix, if any. |
| 403 |
Includes the leading period, just like pathlib.Path. |
| 404 |
""" |
| 405 |
root = zipfile.Path(alpharep) |
| 406 |
assert root.suffixes == ['.zip'] == root.filename.suffixes |
| 407 |
|
| 408 |
b = root / 'b.txt' |
| 409 |
assert b.suffixes == ['.txt'] |
| 410 |
|
| 411 |
c = root / 'c' / 'filename.tar.gz' |
| 412 |
assert c.suffixes == ['.tar', '.gz'] |
| 413 |
|
| 414 |
d = root / 'd' |
| 415 |
assert d.suffixes == [] |
| 416 |
|
| 417 |
e = root / '.hgrc' |
| 418 |
assert e.suffixes == [] |
| 419 |
|
| 420 |
@pass_alpharep |
| 421 |
def test_suffix_no_filename(self, alpharep): |
| 422 |
alpharep.filename = None |
| 423 |
root = zipfile.Path(alpharep) |
| 424 |
assert root.joinpath('example').suffix == "" |
| 425 |
assert root.joinpath('example').suffixes == [] |
| 426 |
|
| 427 |
@pass_alpharep |
| 428 |
def test_stem(self, alpharep): |
| 429 |
""" |
| 430 |
The final path component, without its suffix |
| 431 |
""" |
| 432 |
root = zipfile.Path(alpharep) |
| 433 |
assert root.stem == 'alpharep' == root.filename.stem |
| 434 |
|
| 435 |
b = root / "b.txt" |
| 436 |
assert b.stem == "b" |
| 437 |
|
| 438 |
c = root / "c" / "filename.tar.gz" |
| 439 |
assert c.stem == "filename.tar" |
| 440 |
|
| 441 |
d = root / "d" |
| 442 |
assert d.stem == "d" |
| 443 |
|
| 444 |
assert (root / ".gitignore").stem == ".gitignore" |
| 445 |
|
| 446 |
@pass_alpharep |
| 447 |
def test_root_parent(self, alpharep): |
| 448 |
root = zipfile.Path(alpharep) |
| 449 |
assert root.parent == pathlib.Path('.') |
| 450 |
root.root.filename = 'foo/bar.zip' |
| 451 |
assert root.parent == pathlib.Path('foo') |
| 452 |
|
| 453 |
@pass_alpharep |
| 454 |
def test_root_unnamed(self, alpharep): |
| 455 |
""" |
| 456 |
It is an error to attempt to get the name |
| 457 |
or parent of an unnamed zipfile. |
| 458 |
""" |
| 459 |
alpharep.filename = None |
| 460 |
root = zipfile.Path(alpharep) |
| 461 |
with self.assertRaises(TypeError): |
| 462 |
root.name |
| 463 |
with self.assertRaises(TypeError): |
| 464 |
root.parent |
| 465 |
|
| 466 |
# .name and .parent should still work on subs |
| 467 |
sub = root / "b" |
| 468 |
assert sub.name == "b" |
| 469 |
assert sub.parent |
| 470 |
|
| 471 |
@pass_alpharep |
| 472 |
def test_match_and_glob(self, alpharep): |
| 473 |
root = zipfile.Path(alpharep) |
| 474 |
assert not root.match("*.txt") |
| 475 |
|
| 476 |
assert list(root.glob("b/c.*")) == [zipfile.Path(alpharep, "b/c.txt")] |
| 477 |
assert list(root.glob("b/*.txt")) == [ |
| 478 |
zipfile.Path(alpharep, "b/c.txt"), |
| 479 |
zipfile.Path(alpharep, "b/f.txt"), |
| 480 |
] |
| 481 |
|
| 482 |
@pass_alpharep |
| 483 |
def test_glob_recursive(self, alpharep): |
| 484 |
root = zipfile.Path(alpharep) |
| 485 |
files = root.glob("**/*.txt") |
| 486 |
assert all(each.match("*.txt") for each in files) |
| 487 |
|
| 488 |
assert list(root.glob("**/*.txt")) == list(root.rglob("*.txt")) |
| 489 |
|
| 490 |
@pass_alpharep |
| 491 |
def test_glob_dirs(self, alpharep): |
| 492 |
root = zipfile.Path(alpharep) |
| 493 |
assert list(root.glob('b')) == [zipfile.Path(alpharep, "b/")] |
| 494 |
assert list(root.glob('b*')) == [zipfile.Path(alpharep, "b/")] |
| 495 |
|
| 496 |
@pass_alpharep |
| 497 |
def test_glob_subdir(self, alpharep): |
| 498 |
root = zipfile.Path(alpharep) |
| 499 |
assert list(root.glob('g/h')) == [zipfile.Path(alpharep, "g/h/")] |
| 500 |
assert list(root.glob('g*/h*')) == [zipfile.Path(alpharep, "g/h/")] |
| 501 |
|
| 502 |
@pass_alpharep |
| 503 |
def test_glob_subdirs(self, alpharep): |
| 504 |
root = zipfile.Path(alpharep) |
| 505 |
|
| 506 |
assert list(root.glob("*/i.txt")) == [] |
| 507 |
assert list(root.rglob("*/i.txt")) == [zipfile.Path(alpharep, "g/h/i.txt")] |
| 508 |
|
| 509 |
@pass_alpharep |
| 510 |
def test_glob_does_not_overmatch_dot(self, alpharep): |
| 511 |
root = zipfile.Path(alpharep) |
| 512 |
|
| 513 |
assert list(root.glob("*.xt")) == [] |
| 514 |
|
| 515 |
@pass_alpharep |
| 516 |
def test_glob_single_char(self, alpharep): |
| 517 |
root = zipfile.Path(alpharep) |
| 518 |
|
| 519 |
assert list(root.glob("a?txt")) == [zipfile.Path(alpharep, "a.txt")] |
| 520 |
assert list(root.glob("a[.]txt")) == [zipfile.Path(alpharep, "a.txt")] |
| 521 |
assert list(root.glob("a[?]txt")) == [] |
| 522 |
|
| 523 |
@pass_alpharep |
| 524 |
def test_glob_chars(self, alpharep): |
| 525 |
root = zipfile.Path(alpharep) |
| 526 |
|
| 527 |
assert list(root.glob("j/?.b[ai][nz]")) == [ |
| 528 |
zipfile.Path(alpharep, "j/k.bin"), |
| 529 |
zipfile.Path(alpharep, "j/l.baz"), |
| 530 |
] |
| 531 |
|
| 532 |
def test_glob_empty(self): |
| 533 |
root = zipfile.Path(zipfile.ZipFile(io.BytesIO(), 'w')) |
| 534 |
with self.assertRaises(ValueError): |
| 535 |
root.glob('') |
| 536 |
|
| 537 |
@pass_alpharep |
| 538 |
def test_eq_hash(self, alpharep): |
| 539 |
root = zipfile.Path(alpharep) |
| 540 |
assert root == zipfile.Path(alpharep) |
| 541 |
|
| 542 |
assert root != (root / "a.txt") |
| 543 |
assert (root / "a.txt") == (root / "a.txt") |
| 544 |
|
| 545 |
root = zipfile.Path(alpharep) |
| 546 |
assert root in {root} |
| 547 |
|
| 548 |
@pass_alpharep |
| 549 |
def test_is_symlink(self, alpharep): |
| 550 |
root = zipfile.Path(alpharep) |
| 551 |
assert not root.joinpath('a.txt').is_symlink() |
| 552 |
assert root.joinpath('n.txt').is_symlink() |
| 553 |
|
| 554 |
@pass_alpharep |
| 555 |
def test_relative_to(self, alpharep): |
| 556 |
root = zipfile.Path(alpharep) |
| 557 |
relative = root.joinpath("b", "c.txt").relative_to(root / "b") |
| 558 |
assert str(relative) == "c.txt" |
| 559 |
|
| 560 |
relative = root.joinpath("b", "d", "e.txt").relative_to(root / "b") |
| 561 |
assert str(relative) == "d/e.txt" |
| 562 |
|
| 563 |
@pass_alpharep |
| 564 |
def test_inheritance(self, alpharep): |
| 565 |
cls = type('PathChild', (zipfile.Path,), {}) |
| 566 |
file = cls(alpharep).joinpath('some dir').parent |
| 567 |
assert isinstance(file, cls) |
| 568 |
|
| 569 |
@parameterize( |
| 570 |
['alpharep', 'path_type', 'subpath'], |
| 571 |
itertools.product( |
| 572 |
alpharep_generators, |
| 573 |
[str, FakePath], |
| 574 |
['', 'b/'], |
| 575 |
), |
| 576 |
) |
| 577 |
def test_pickle(self, alpharep, path_type, subpath): |
| 578 |
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep))) |
| 579 |
root = zipfile.Path(zipfile_ondisk, at=subpath) |
| 580 |
saved_1 = pickle.dumps(root) |
| 581 |
root.root.close() |
| 582 |
restored_1 = pickle.loads(saved_1) |
| 583 |
first, *rest = restored_1.iterdir() |
| 584 |
assert first.read_text(encoding='utf-8').startswith('content of ') |
| 585 |
restored_1.root.close() |
| 586 |
|
| 587 |
@pass_alpharep |
| 588 |
def test_extract_orig_with_implied_dirs(self, alpharep): |
| 589 |
""" |
| 590 |
A zip file wrapped in a Path should extract even with implied dirs. |
| 591 |
""" |
| 592 |
source_path = self.zipfile_ondisk(alpharep) |
| 593 |
zf = zipfile.ZipFile(source_path) |
| 594 |
# wrap the zipfile for its side effect |
| 595 |
zipfile.Path(zf) |
| 596 |
zf.extractall(source_path.parent) |
| 597 |
zf.close() |
| 598 |
|
| 599 |
@pass_alpharep |
| 600 |
def test_getinfo_missing(self, alpharep): |
| 601 |
""" |
| 602 |
Validate behavior of getinfo on original zipfile after wrapping. |
| 603 |
""" |
| 604 |
zipfile.Path(alpharep) |
| 605 |
with self.assertRaises(KeyError): |
| 606 |
alpharep.getinfo('does-not-exist') |
| 607 |
|
| 608 |
def test_malformed_paths(self): |
| 609 |
""" |
| 610 |
Path should handle malformed paths gracefully. |
| 611 |
|
| 612 |
Paths with leading slashes are not visible. |
| 613 |
|
| 614 |
Paths with dots are treated like regular files. |
| 615 |
""" |
| 616 |
data = io.BytesIO() |
| 617 |
zf = zipfile.ZipFile(data, "w") |
| 618 |
zf.writestr("/one-slash.txt", b"content") |
| 619 |
zf.writestr("//two-slash.txt", b"content") |
| 620 |
zf.writestr("../parent.txt", b"content") |
| 621 |
zf.filename = '' |
| 622 |
root = zipfile.Path(zf) |
| 623 |
assert list(map(str, root.iterdir())) == ['../'] |
| 624 |
assert root.joinpath('..').joinpath('parent.txt').read_bytes() == b'content' |
| 625 |
|
| 626 |
def test_unsupported_names(self): |
| 627 |
""" |
| 628 |
Path segments with special characters are readable. |
| 629 |
|
| 630 |
On some platforms or file systems, characters like |
| 631 |
``:`` and ``?`` are not allowed, but they are valid |
| 632 |
in the zip file. |
| 633 |
""" |
| 634 |
data = io.BytesIO() |
| 635 |
zf = zipfile.ZipFile(data, "w") |
| 636 |
zf.writestr("path?", b"content") |
| 637 |
zf.writestr("V: NMS.flac", b"fLaC...") |
| 638 |
zf.filename = '' |
| 639 |
root = zipfile.Path(zf) |
| 640 |
contents = root.iterdir() |
| 641 |
assert next(contents).name == 'path?' |
| 642 |
assert next(contents).name == 'V: NMS.flac' |
| 643 |
assert root.joinpath('V: NMS.flac').read_bytes() == b"fLaC..." |
| 644 |
|
| 645 |
def test_backslash_not_separator(self): |
| 646 |
""" |
| 647 |
In a zip file, backslashes are not separators. |
| 648 |
""" |
| 649 |
data = io.BytesIO() |
| 650 |
zf = zipfile.ZipFile(data, "w") |
| 651 |
zf.writestr(DirtyZipInfo("foo\\bar")._for_archive(zf), b"content") |
| 652 |
zf.filename = '' |
| 653 |
root = zipfile.Path(zf) |
| 654 |
(first,) = root.iterdir() |
| 655 |
assert not first.is_dir() |
| 656 |
assert first.name == 'foo\\bar' |
| 657 |
|
| 658 |
@pass_alpharep |
| 659 |
def test_interface(self, alpharep): |
| 660 |
from importlib.resources.abc import Traversable |
| 661 |
|
| 662 |
zf = zipfile.Path(alpharep) |
| 663 |
assert isinstance(zf, Traversable) |
| 664 |
|
| 665 |
|
| 666 |
class DirtyZipInfo(zipfile.ZipInfo): |
| 667 |
""" |
| 668 |
Bypass name sanitization. |
| 669 |
""" |
| 670 |
|
| 671 |
def __init__(self, filename, *args, **kwargs): |
| 672 |
super().__init__(filename, *args, **kwargs) |
| 673 |
self.filename = filename |