summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-12-29 18:30:53 (GMT)
committerGitHub <noreply@github.com>2024-12-29 18:30:53 (GMT)
commit7e819ce0f32068de7914cd1ba3b4b95e91ea9873 (patch)
treeee71bf301533e0f2396839052d79ac288d0a55ac /Lib/test
parentffece5590e95e89d3b5b6847e3beb443ff65c3db (diff)
downloadcpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.zip
cpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.tar.gz
cpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.tar.bz2
gh-123424: add `ZipInfo._for_archive` to set suitable default properties (#123429)
--------- Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_zipfile/_path/test_path.py19
-rw-r--r--Lib/test/test_zipfile/test_core.py29
2 files changed, 30 insertions, 18 deletions
diff --git a/Lib/test/test_zipfile/_path/test_path.py b/Lib/test/test_zipfile/_path/test_path.py
index aba5155..1ee45f5 100644
--- a/Lib/test/test_zipfile/_path/test_path.py
+++ b/Lib/test/test_zipfile/_path/test_path.py
@@ -634,7 +634,7 @@ class TestPath(unittest.TestCase):
"""
data = io.BytesIO()
zf = zipfile.ZipFile(data, "w")
- zf.writestr(DirtyZipInfo.for_name("foo\\bar", zf), b"content")
+ zf.writestr(DirtyZipInfo("foo\\bar")._for_archive(zf), b"content")
zf.filename = ''
root = zipfile.Path(zf)
(first,) = root.iterdir()
@@ -657,20 +657,3 @@ class DirtyZipInfo(zipfile.ZipInfo):
def __init__(self, filename, *args, **kwargs):
super().__init__(filename, *args, **kwargs)
self.filename = filename
-
- @classmethod
- def for_name(cls, name, archive):
- """
- Construct the same way that ZipFile.writestr does.
-
- TODO: extract this functionality and re-use
- """
- self = cls(filename=name, date_time=time.localtime(time.time())[:6])
- self.compress_type = archive.compression
- self.compress_level = archive.compresslevel
- if self.filename.endswith('/'): # pragma: no cover
- self.external_attr = 0o40775 << 16 # drwxrwxr-x
- self.external_attr |= 0x10 # MS-DOS directory flag
- else:
- self.external_attr = 0o600 << 16 # ?rw-------
- return self
diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py
index 124e088..49f39b9 100644
--- a/Lib/test/test_zipfile/test_core.py
+++ b/Lib/test/test_zipfile/test_core.py
@@ -5,6 +5,7 @@ import io
import itertools
import os
import posixpath
+import stat
import struct
import subprocess
import sys
@@ -2211,6 +2212,34 @@ class OtherTests(unittest.TestCase):
zi = zipfile.ZipInfo(filename="empty")
self.assertEqual(repr(zi), "<ZipInfo filename='empty' file_size=0>")
+ def test_for_archive(self):
+ base_filename = TESTFN2.rstrip('/')
+
+ with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
+ compression=zipfile.ZIP_STORED) as zf:
+ # no trailing forward slash
+ zi = zipfile.ZipInfo(base_filename)._for_archive(zf)
+ self.assertEqual(zi.compress_level, 1)
+ self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
+ # ?rw- --- ---
+ filemode = stat.S_IRUSR | stat.S_IWUSR
+ # filemode is stored as the highest 16 bits of external_attr
+ self.assertEqual(zi.external_attr >> 16, filemode)
+ self.assertEqual(zi.external_attr & 0xFF, 0) # no MS-DOS flag
+
+ with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
+ compression=zipfile.ZIP_STORED) as zf:
+ # with a trailing slash
+ zi = zipfile.ZipInfo(f'{base_filename}/')._for_archive(zf)
+ self.assertEqual(zi.compress_level, 1)
+ self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
+ # d rwx rwx r-x
+ filemode = stat.S_IFDIR
+ filemode |= stat.S_IRWXU | stat.S_IRWXG
+ filemode |= stat.S_IROTH | stat.S_IXOTH
+ self.assertEqual(zi.external_attr >> 16, filemode)
+ self.assertEqual(zi.external_attr & 0xFF, 0x10) # MS-DOS flag
+
def test_create_empty_zipinfo_default_attributes(self):
"""Ensure all required attributes are set."""
zi = zipfile.ZipInfo()