Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/fsutil/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
assert_not_exists,
assert_not_file,
is_dir,
is_empty_dir,
is_file,
)
from fsutil.operations import make_dirs, make_dirs_for_file, remove_file
Expand Down Expand Up @@ -57,6 +58,9 @@ def _write_content_to_tar_file(
# symlink cycle: stop walking to prevent infinite recursion
return
ancestors = ancestors | {path_realpath}
if basedir and is_empty_dir(path):
# realpath to store a symlinked directory as a directory, not a link
file.add(path_realpath, basedir)
for item_name in os.listdir(path):
item_path = join_path(path, item_name)
item_basedir = (
Expand Down Expand Up @@ -105,6 +109,8 @@ def _write_content_to_zip_file(
# symlink cycle: stop walking to prevent infinite recursion
return
ancestors = ancestors | {path_realpath}
if basedir and is_empty_dir(path):
file.write(path, basedir)
for item_name in os.listdir(path):
item_path = join_path(path, item_name)
item_basedir = (
Expand Down
40 changes: 40 additions & 0 deletions tests/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ def test_create_zip_file(temp_path):
assert fsutil.get_file_size(zip_path) > 0


@pytest.mark.parametrize(
("create_archive", "extract_archive"),
[
(fsutil.create_zip_file, fsutil.extract_zip_file),
(fsutil.create_tar_file, fsutil.extract_tar_file),
],
)
def test_create_archive_with_empty_dirs(temp_path, create_archive, extract_archive):
fsutil.create_dir(temp_path("a/b"))
fsutil.create_dir(temp_path("a/c/d"))
fsutil.create_file(temp_path("a/c/f1.txt"), content="hello world 1")
archive_path = temp_path("archive")
create_archive(archive_path, [temp_path("a")])
extract_archive(archive_path, temp_path("unarchive"))
assert fsutil.is_empty_dir(temp_path("unarchive/b"))
assert fsutil.is_empty_dir(temp_path("unarchive/c/d"))
assert fsutil.is_file(temp_path("unarchive/c/f1.txt"))


@pytest.mark.skipif(sys.platform.startswith("win"), reason="Test skipped on Windows")
@pytest.mark.parametrize(
("create_archive", "extract_archive"),
[
(fsutil.create_zip_file, fsutil.extract_zip_file),
(fsutil.create_tar_file, fsutil.extract_tar_file),
],
)
def test_create_archive_with_symlink_to_empty_dir(
temp_path, create_archive, extract_archive
):
fsutil.create_dir(temp_path("a"))
fsutil.create_dir(temp_path("x"))
os.symlink(temp_path("x"), temp_path("a/x-link"))
archive_path = temp_path("archive")
create_archive(archive_path, [temp_path("a")])
extract_archive(archive_path, temp_path("unarchive"))
assert fsutil.is_empty_dir(temp_path("unarchive/x-link"))
assert not os.path.islink(temp_path("unarchive/x-link"))


def test_create_tar_file(temp_path):
tar_path = temp_path("archive.tar")
f1_path = temp_path("a/b/f1.txt")
Expand Down
Loading