From b94e386798b57f3e9272b5aeec311837b05a0198 Mon Sep 17 00:00:00 2001 From: Umut Alisoglu Date: Sun, 25 Oct 2026 22:31:27 +0100 Subject: [PATCH 1/3] Preserve empty child directories in ZIP and TAR archives --- src/fsutil/archives.py | 10 ++++++++-- tests/test_archives.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/fsutil/archives.py b/src/fsutil/archives.py index 3c52a58..9435ad6 100644 --- a/src/fsutil/archives.py +++ b/src/fsutil/archives.py @@ -49,7 +49,10 @@ def _write_content_to_tar_file( filepath = join_path(basedir, filename) file.add(path, filepath) elif is_dir(path): - for item_name in os.listdir(path): + item_names = os.listdir(path) + if not item_names and basedir: + file.add(path, basedir, recursive=False) + for item_name in item_names: item_path = join_path(path, item_name) item_basedir = ( join_path(basedir, item_name) if is_dir(item_path) else basedir @@ -89,7 +92,10 @@ def _write_content_to_zip_file( filepath = join_path(basedir, filename) file.write(path, filepath) elif is_dir(path): - for item_name in os.listdir(path): + item_names = os.listdir(path) + if not item_names and basedir: + file.write(path, basedir) + for item_name in item_names: item_path = join_path(path, item_name) item_basedir = ( join_path(basedir, item_name) if is_dir(item_path) else basedir diff --git a/tests/test_archives.py b/tests/test_archives.py index 2d15bbb..332a076 100644 --- a/tests/test_archives.py +++ b/tests/test_archives.py @@ -28,6 +28,32 @@ def test_create_zip_file(temp_path): assert fsutil.get_file_size(zip_path) > 0 +@pytest.mark.parametrize( + ("extension", "create_archive", "extract_archive"), + [ + ("zip", fsutil.create_zip_file, fsutil.extract_zip_file), + ("tar", fsutil.create_tar_file, fsutil.extract_tar_file), + ], +) +def test_archive_preserves_empty_subdirectories( + temp_path, extension, create_archive, extract_archive +): + source = temp_path("source") + fsutil.create_dir(temp_path("source/empty")) + fsutil.create_dir(temp_path("source/nested/empty")) + fsutil.create_file(temp_path("source/nested/file.txt"), content="file content") + + archive_path = temp_path(f"archive.{extension}") + create_archive(archive_path, [source]) + extract_archive(archive_path, temp_path("output")) + + for name in ("empty", "nested/empty"): + path = temp_path(f"output/{name}") + assert fsutil.is_dir(path) + assert fsutil.is_empty_dir(path) + assert fsutil.read_file(temp_path("output/nested/file.txt")) == "file content" + + def test_create_tar_file(temp_path): tar_path = temp_path("archive.tar") f1_path = temp_path("a/b/f1.txt") From e50d0d27ad6ee16d4a688533b65bbf27fe7d1a0b Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Fri, 27 Nov 2026 17:15:59 +0100 Subject: [PATCH 2/3] Use `is_empty_dir` to add empty directories to archives and store symlinked ones as directories. --- src/fsutil/archives.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/fsutil/archives.py b/src/fsutil/archives.py index 9435ad6..ced6b75 100644 --- a/src/fsutil/archives.py +++ b/src/fsutil/archives.py @@ -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 @@ -49,10 +50,10 @@ def _write_content_to_tar_file( filepath = join_path(basedir, filename) file.add(path, filepath) elif is_dir(path): - item_names = os.listdir(path) - if not item_names and basedir: - file.add(path, basedir, recursive=False) - for item_name in item_names: + if basedir and is_empty_dir(path): + # realpath to store a symlinked directory as a directory, not a link + file.add(os.path.realpath(path), basedir) + for item_name in os.listdir(path): item_path = join_path(path, item_name) item_basedir = ( join_path(basedir, item_name) if is_dir(item_path) else basedir @@ -92,10 +93,9 @@ def _write_content_to_zip_file( filepath = join_path(basedir, filename) file.write(path, filepath) elif is_dir(path): - item_names = os.listdir(path) - if not item_names and basedir: + if basedir and is_empty_dir(path): file.write(path, basedir) - for item_name in item_names: + for item_name in os.listdir(path): item_path = join_path(path, item_name) item_basedir = ( join_path(basedir, item_name) if is_dir(item_path) else basedir From 0a8c43f49a271a174ea6ed6f42a732f0f5c01f22 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Fri, 27 Nov 2026 17:16:00 +0100 Subject: [PATCH 3/3] Simplify empty directories archive test and cover symlinks to empty directories. --- tests/test_archives.py | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/tests/test_archives.py b/tests/test_archives.py index 332a076..fc345cc 100644 --- a/tests/test_archives.py +++ b/tests/test_archives.py @@ -1,3 +1,5 @@ +import os +import sys import tarfile import pytest @@ -29,29 +31,43 @@ def test_create_zip_file(temp_path): @pytest.mark.parametrize( - ("extension", "create_archive", "extract_archive"), + ("create_archive", "extract_archive"), [ - ("zip", fsutil.create_zip_file, fsutil.extract_zip_file), - ("tar", fsutil.create_tar_file, fsutil.extract_tar_file), + (fsutil.create_zip_file, fsutil.extract_zip_file), + (fsutil.create_tar_file, fsutil.extract_tar_file), ], ) -def test_archive_preserves_empty_subdirectories( - temp_path, extension, create_archive, extract_archive +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 ): - source = temp_path("source") - fsutil.create_dir(temp_path("source/empty")) - fsutil.create_dir(temp_path("source/nested/empty")) - fsutil.create_file(temp_path("source/nested/file.txt"), content="file content") - - archive_path = temp_path(f"archive.{extension}") - create_archive(archive_path, [source]) - extract_archive(archive_path, temp_path("output")) - - for name in ("empty", "nested/empty"): - path = temp_path(f"output/{name}") - assert fsutil.is_dir(path) - assert fsutil.is_empty_dir(path) - assert fsutil.read_file(temp_path("output/nested/file.txt")) == "file content" + 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):