From ca4e2a62394a13d8f30076967671d9f486d940df Mon Sep 17 00:00:00 2001 From: aryeg Date: Sun, 8 Feb 2026 10:32:44 +0200 Subject: [PATCH 1/2] fix co0mpact error due to wrong order --- src/littlefs/__main__.py | 16 +++++++--------- test/cli/test_create_and_extract.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/littlefs/__main__.py b/src/littlefs/__main__.py index f92b077..118f110 100644 --- a/src/littlefs/__main__.py +++ b/src/littlefs/__main__.py @@ -108,15 +108,13 @@ def create(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int: if args.verbose: print(f"Compacting... {fs.used_block_count} / {args.block_count}") compact_fs = _fs_from_args(args, block_count=fs.used_block_count) - for root, dirs, files in fs.walk("/"): - if not root.endswith("/"): - root += "/" - for _dir in dirs: - compact_fs.makedirs(root + _dir, exist_ok=True) - for file in files: - path = root + file - with fs.open(path, "rb") as src, compact_fs.open(path, "wb") as dest: - dest.write(src.read()) + for path in sources: + rel_path = path.relative_to(root) + if path.is_dir(): + compact_fs.mkdir(rel_path.as_posix()) + else: + with compact_fs.open(rel_path.as_posix(), "wb") as dest: + dest.write(path.read_bytes()) compact_fs.fs_grow(args.block_count) data = compact_fs.context.buffer if not args.no_pad: diff --git a/test/cli/test_create_and_extract.py b/test/cli/test_create_and_extract.py index f27c407..9ab4df4 100644 --- a/test/cli/test_create_and_extract.py +++ b/test/cli/test_create_and_extract.py @@ -50,3 +50,31 @@ def test_create_and_extract(tmp_path): # Verify file contents assert (extract_dir / "file1.txt").read_text() == "hello world" assert (extract_dir / "subdir" / "file2.txt").read_text() == "test content" + + +def test_create_compact_various_structures(tmp_path): + """Test creating compact filesystems with various file configurations.""" + # (num_files, file_size) + configs = [ + (1, 10), (5, 100), (10, 200), (15, 300), (20, 400), (30, 500), + (1, 10000), (5, 5000), (5, 100), + ] + + image_file = tmp_path / "test_compact.bin" + + for num_files, file_size in configs: + source_dir = tmp_path / f"source_{num_files}_{file_size}" + source_dir.mkdir(exist_ok=True) + + for i in range(num_files): + (source_dir / f"file_{i}.txt").write_text("x" * file_size) + + create_argv = [ + "littlefs", "create", str(source_dir), str(image_file), + "--block-size", "512", "--fs-size", "64KB", + "--compact", "--no-pad", + ] + + assert main(create_argv) == 0 + assert image_file.exists() + assert image_file.stat().st_size < 64 * 1024 From 781cf76ba65db063b208358269b0fb8ac873fb95 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sun, 8 Feb 2026 08:42:49 -0500 Subject: [PATCH 2/2] fix generator re-use --- src/littlefs/__main__.py | 2 +- test/cli/test_create_and_extract.py | 63 +++++++++++++++++++---------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/littlefs/__main__.py b/src/littlefs/__main__.py index 118f110..7119994 100644 --- a/src/littlefs/__main__.py +++ b/src/littlefs/__main__.py @@ -85,7 +85,7 @@ def create(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int: source = Path(args.source).absolute() if source.is_dir(): - sources = _walk_all(source) + sources = list(_walk_all(source)) root = source else: sources = [source] diff --git a/test/cli/test_create_and_extract.py b/test/cli/test_create_and_extract.py index 9ab4df4..9be48ed 100644 --- a/test/cli/test_create_and_extract.py +++ b/test/cli/test_create_and_extract.py @@ -1,6 +1,8 @@ from pathlib import Path import filecmp +import pytest + from littlefs.__main__ import main @@ -52,29 +54,48 @@ def test_create_and_extract(tmp_path): assert (extract_dir / "subdir" / "file2.txt").read_text() == "test content" -def test_create_compact_various_structures(tmp_path): - """Test creating compact filesystems with various file configurations.""" - # (num_files, file_size) - configs = [ - (1, 10), (5, 100), (10, 200), (15, 300), (20, 400), (30, 500), - (1, 10000), (5, 5000), (5, 100), - ] +@pytest.mark.parametrize( + "num_files, file_size", + [ + (1, 10), + (5, 100), + (10, 200), + (15, 300), + (20, 400), + (30, 500), + (1, 10000), + (5, 5000), + ], +) +def test_create_compact_roundtrip(tmp_path, num_files, file_size): + """Test that --compact creates a valid image with all files preserved.""" + source_dir = tmp_path / "source" + source_dir.mkdir() - image_file = tmp_path / "test_compact.bin" + # Create source files with deterministic content + for i in range(num_files): + (source_dir / f"file_{i}.txt").write_text(f"content_{i}_" + "x" * file_size) - for num_files, file_size in configs: - source_dir = tmp_path / f"source_{num_files}_{file_size}" - source_dir.mkdir(exist_ok=True) + image_file = tmp_path / "test_compact.bin" + create_argv = [ + "littlefs", "create", str(source_dir), str(image_file), + "--block-size", "512", "--fs-size", "64KB", + "--compact", "--no-pad", + ] - for i in range(num_files): - (source_dir / f"file_{i}.txt").write_text("x" * file_size) + assert main(create_argv) == 0 + assert image_file.exists() + assert image_file.stat().st_size < 64 * 1024 - create_argv = [ - "littlefs", "create", str(source_dir), str(image_file), - "--block-size", "512", "--fs-size", "64KB", - "--compact", "--no-pad", - ] + # Extract and verify all files survived compaction + extract_dir = tmp_path / "extracted" + extract_argv = [ + "littlefs", "extract", str(image_file), str(extract_dir), + "--block-size", "512", + ] + assert main(extract_argv) == 0 - assert main(create_argv) == 0 - assert image_file.exists() - assert image_file.stat().st_size < 64 * 1024 + for i in range(num_files): + extracted_file = extract_dir / f"file_{i}.txt" + assert extracted_file.exists(), f"file_{i}.txt missing from compact image" + assert extracted_file.read_text() == f"content_{i}_" + "x" * file_size