Skip to content

Commit

Permalink
Merge pull request #36 from uktrade/tests/7z-symlinks
Browse files Browse the repository at this point in the history
tests: 7z symlinks
  • Loading branch information
michalc authored Apr 23, 2023
2 parents e0683ca + 0b078ca commit 1692c62
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions test_stream_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from io import BytesIO
import contextlib
import os
import stat
import subprocess
from tempfile import TemporaryDirectory
from zipfile import ZipFile
Expand All @@ -24,6 +25,16 @@
)


@contextlib.contextmanager
def cwd(new_dir):
old_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(old_dir)


def test_with_stream_unzip_zip_64():
now = datetime.fromisoformat('2021-01-01 21:01:12')
perms = 0o600
Expand Down Expand Up @@ -580,15 +591,6 @@ def test_bsdcpio(method):
('file-1', now, perms, method, (b'contents',)),
)))

@contextlib.contextmanager
def cwd(new_dir):
old_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(old_dir)

def read(path):
with open(path, 'rb') as f:
return f.read()
Expand All @@ -608,3 +610,32 @@ def read(path):
assert a == (b'', b'1 block\n')
assert p.returncode == 0
assert read('file-1') == b'contents'


@pytest.mark.parametrize(
"method",
[
ZIP_32,
ZIP_64,
],
)
def test_7z_symbolic_link(method):
modified_at = datetime.now()
member_files = (
('my-file-1.txt', modified_at, 0o600, ZIP_64, (b'Some bytes 1',)),
('my-link.txt', modified_at, stat.S_IFLNK | 0o600, ZIP_64, (b'my-file-1.txt',)),
)
zipped_chunks = stream_zip(member_files)

with \
TemporaryDirectory() as d, \
cwd(d): \

with open('test.zip', 'wb') as fp:
for zipped_chunk in zipped_chunks:
fp.write(zipped_chunk)

subprocess.run(['7z', 'e', 'test.zip'])

with open('my-link.txt') as f:
assert f.read() == 'Some bytes 1'

0 comments on commit 1692c62

Please sign in to comment.