forked from jrast/littlefs-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_create_and_extract.py
More file actions
146 lines (124 loc) · 4.42 KB
/
test_create_and_extract.py
File metadata and controls
146 lines (124 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from pathlib import Path
import filecmp
import pytest
from littlefs.__main__ import main
def test_create_and_extract(tmp_path):
"""Test creating a filesystem image and extracting it."""
# Create test directory with files
source_dir = tmp_path / "source"
source_dir.mkdir()
(source_dir / "file1.txt").write_text("hello world")
(source_dir / "subdir").mkdir()
(source_dir / "subdir" / "file2.txt").write_text("test content")
# Create filesystem image
image_file = tmp_path / "test_image.bin"
create_argv = [
"littlefs",
"create",
str(source_dir),
str(image_file),
"--block-size",
"512",
"--fs-size",
"64KB",
]
assert main(create_argv) == 0
assert image_file.exists()
# Extract filesystem image
extract_dir = tmp_path / "extracted"
extract_argv = [
"littlefs",
"extract",
str(image_file),
str(extract_dir),
"--block-size",
"512",
]
assert main(extract_argv) == 0
assert extract_dir.exists()
# Compare directories
comparison = filecmp.dircmp(source_dir, extract_dir)
assert not comparison.diff_files
assert not comparison.left_only
assert not comparison.right_only
# Verify file contents
assert (extract_dir / "file1.txt").read_text() == "hello world"
assert (extract_dir / "subdir" / "file2.txt").read_text() == "test content"
@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()
# 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)
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",
]
assert main(create_argv) == 0
assert image_file.exists()
assert image_file.stat().st_size < 64 * 1024
# 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
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
def _make_small_source(tmp_path):
"""Create a small source tree (one dir, two small files) for config option tests."""
source_dir = tmp_path / "source"
source_dir.mkdir()
(source_dir / "a.txt").write_text("hello")
(source_dir / "b.txt").write_text("world")
return source_dir
@pytest.mark.parametrize(
"option_name, create_extra, extract_extra",
[
("inline_max", ["--inline-max", "64"], ["--inline-max", "64"]),
("attr_max", ["--attr-max", "64"], ["--attr-max", "64"]),
("file_max", ["--file-max", "32"], ["--file-max", "32"]),
],
)
def test_create_extract_roundtrip_with_config_option(tmp_path, option_name, create_extra, extract_extra):
"""Create image with a config option, extract with same option, compare to source."""
source_dir = _make_small_source(tmp_path)
image_file = tmp_path / "image.bin"
extract_dir = tmp_path / "extracted"
create_argv = [
"littlefs", "create", str(source_dir), str(image_file),
"--block-size", "1024", "--fs-size", "64KB",
] + create_extra
assert main(create_argv) == 0
assert image_file.exists()
extract_argv = [
"littlefs", "extract", str(image_file), str(extract_dir),
"--block-size", "1024",
] + extract_extra
assert main(extract_argv) == 0
assert extract_dir.exists()
comparison = filecmp.dircmp(source_dir, extract_dir)
assert not comparison.diff_files
assert not comparison.left_only
assert not comparison.right_only
assert (extract_dir / "a.txt").read_text() == "hello"
assert (extract_dir / "b.txt").read_text() == "world"