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
13 changes: 12 additions & 1 deletion src/littlefs/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
from contextlib import suppress
import os
from pathlib import Path
import sys
import textwrap
Expand Down Expand Up @@ -51,6 +52,16 @@ def size_parser(size_str):
return int(size_str, base)


def _walk_all(root):
"""Recursively yield all paths under root, following symlinks."""
for dirpath, dirnames, filenames in os.walk(root, followlinks=True):
dirpath = Path(dirpath)
for dirname in dirnames:
yield dirpath / dirname
for filename in filenames:
yield dirpath / filename


def create(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int:
"""Create LittleFS image from file/directory contents."""
# fs_size OR block_count may be populated; make them consistent.
Expand All @@ -72,7 +83,7 @@ def create(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int:

source = Path(args.source).absolute()
if source.is_dir():
sources = source.rglob("*", recurse_symlinks=True)
sources = _walk_all(source)
root = source
else:
sources = [source]
Expand Down
60 changes: 60 additions & 0 deletions test/cli/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from pathlib import Path

from littlefs.__main__ import _walk_all


def test_walk_all_basic(tmp_path):
"""Test basic directory traversal."""
(tmp_path / "file1.txt").write_text("hello")
(tmp_path / "subdir").mkdir()
(tmp_path / "subdir" / "file2.txt").write_text("world")

paths = list(_walk_all(tmp_path))
rel_paths = {p.relative_to(tmp_path).as_posix() for p in paths}

assert rel_paths == {"file1.txt", "subdir", "subdir/file2.txt"}


def test_walk_all_follows_symlinks(tmp_path):
"""Test that symlinks to directories are followed."""
# Create a real directory with files
real_dir = tmp_path / "real_dir"
real_dir.mkdir()
(real_dir / "file1.txt").write_text("hello")
(real_dir / "nested").mkdir()
(real_dir / "nested" / "file2.txt").write_text("world")

# Create a symlink to the real directory
symlink_dir = tmp_path / "symlink_dir"
symlink_dir.symlink_to(real_dir)

paths = list(_walk_all(tmp_path))
rel_paths = {p.relative_to(tmp_path).as_posix() for p in paths}

# Should include both the real directory contents and symlink contents
assert "real_dir" in rel_paths
assert "real_dir/file1.txt" in rel_paths
assert "real_dir/nested" in rel_paths
assert "real_dir/nested/file2.txt" in rel_paths
assert "symlink_dir" in rel_paths
assert "symlink_dir/file1.txt" in rel_paths
assert "symlink_dir/nested" in rel_paths
assert "symlink_dir/nested/file2.txt" in rel_paths


def test_walk_all_empty_directory(tmp_path):
"""Test traversal of empty directory."""
paths = list(_walk_all(tmp_path))
assert paths == []


def test_walk_all_nested_directories(tmp_path):
"""Test deeply nested directory traversal."""
nested = tmp_path / "a" / "b" / "c"
nested.mkdir(parents=True)
(nested / "deep.txt").write_text("deep")

paths = list(_walk_all(tmp_path))
rel_paths = {p.relative_to(tmp_path).as_posix() for p in paths}

assert rel_paths == {"a", "a/b", "a/b/c", "a/b/c/deep.txt"}
Loading