Skip to content

Commit

Permalink
Merge branch 'main' into work/remove-mantic
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau authored Jul 22, 2024
2 parents 18df1bf + ac08e98 commit 5ebaea1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
11 changes: 6 additions & 5 deletions charmcraft/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def build_zip(zip_path: PathOrString, prime_dir: PathOrString) -> None:
"""
zip_path = pathlib.Path(zip_path).resolve()
prime_dir = pathlib.Path(prime_dir).resolve()
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as file:
for file_path in prime_dir.rglob("*"):
if not file_path.is_file():
continue
file.write(file_path, file_path.relative_to(prime_dir))
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zip_file:
# Using os.walk() because Path.walk() is only added in 3.12
for dir_path_str, _, filenames in os.walk(prime_dir, followlinks=True):
for filename in filenames:
file_path = pathlib.Path(dir_path_str, filename)
zip_file.write(file_path, file_path.relative_to(prime_dir))
53 changes: 29 additions & 24 deletions tests/unit/utils/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,41 +105,46 @@ def test_zipbuild_simple(tmp_path):


@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_zipbuild_symlink_simple(tmp_path):
def test_zipbuild_symlinks(tmp_path: pathlib.Path):
"""Symlinks are supported."""
build_dir = tmp_path / "somedir"
build_dir.mkdir()

testfile1 = build_dir / "real.txt"
testfile1.write_bytes(b"123\x00456")
testfile2 = build_dir / "link.txt"
testfile2.symlink_to(testfile1)
outside_dir = tmp_path / "another_dir"
outside_dir.mkdir()
outside_file = outside_dir / "some_file"
outside_file.write_bytes(b"123\x00456")

zip_filepath = tmp_path / "testresult.zip"
build_zip(zip_filepath, build_dir)
internal_dir = build_dir / "subdirectory"
internal_dir.mkdir()
real_file = internal_dir / "real.txt"
real_file.write_bytes(b"123\x00456")

zf = zipfile.ZipFile(zip_filepath)
assert sorted(x.filename for x in zf.infolist()) == ["link.txt", "real.txt"]
assert zf.read("real.txt") == b"123\x00456"
assert zf.read("link.txt") == b"123\x00456"
internal_file_link = build_dir / "link.txt"
internal_file_link.symlink_to(real_file)

internal_dir_link = build_dir / "link_dir"
internal_dir_link.symlink_to(internal_dir)

@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_zipbuild_symlink_outside(tmp_path):
"""No matter where the symlink points to."""
# outside the build dir
testfile1 = tmp_path / "real.txt"
testfile1.write_bytes(b"123\x00456")
external_file_link = build_dir / "external_link.txt"
external_file_link.symlink_to(outside_file)

# inside the build dir
build_dir = tmp_path / "somedir"
build_dir.mkdir()
testfile2 = build_dir / "link.txt"
testfile2.symlink_to(testfile1)
external_dir_link = build_dir / "external_link_dir"
external_dir_link.symlink_to(outside_dir)

zip_filepath = tmp_path / "testresult.zip"
build_zip(zip_filepath, build_dir)

zf = zipfile.ZipFile(zip_filepath)
assert sorted(x.filename for x in zf.infolist()) == ["link.txt"]
assert zf.read("link.txt") == b"123\x00456"

expected_files = [
"external_link.txt",
"external_link_dir/some_file",
"link.txt",
"link_dir/real.txt",
"subdirectory/real.txt",
]

assert sorted(x.filename for x in zf.infolist()) == expected_files
for file_name in expected_files:
assert zf.read(file_name) == b"123\x00456"

0 comments on commit 5ebaea1

Please sign in to comment.