Skip to content

Commit 31e61c8

Browse files
author
Evgeny Torbin
committed
feat: Add optional path to ChecksumsManager dump method
1 parent 2d0e940 commit 31e61c8

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

idf_component_tools/hash_tools/checksums.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def __init__(self, path: Path):
4646
def exists(self) -> bool:
4747
"""Check if checksums file exists.
4848
49-
:return: True if checksums file exists, False otherwise
49+
:return: True if checksums file exists in the component directory, False otherwise.
5050
"""
5151
return (self.path / CHECKSUMS_FILENAME).is_file()
5252

53-
def dump(self) -> None:
53+
def dump(self, path: t.Optional[Path] = None) -> None:
5454
"""Writes checksums to a file.
5555
5656
Format of the file is:
@@ -68,8 +68,13 @@ def dump(self) -> None:
6868
...
6969
]
7070
}
71+
72+
:param path: Path to directory where checksums file will be created.
73+
If None, uses the component path provided during initialization.
7174
"""
7275

76+
checksums_path = (path if path is not None else self.path) / CHECKSUMS_FILENAME
77+
7378
checksums = ChecksumsModel(
7479
files=[
7580
FileField(
@@ -82,7 +87,7 @@ def dump(self) -> None:
8287
]
8388
)
8489

85-
(self.path / CHECKSUMS_FILENAME).write_text(checksums.model_dump_json())
90+
checksums_path.write_text(checksums.model_dump_json())
8691

8792
def load(self) -> ChecksumsModel:
8893
"""Load file with checksums.

tests/idf_component_tools/hash_tools/test_checksums.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ def test_checksums_dump(tmp_path, checksums_model):
7575
assert stored_checksums == expected_checksums
7676

7777

78+
def test_checksums_dump_to_other_path(tmp_path, checksums_model):
79+
component_path = tmp_path / 'component'
80+
component_path.mkdir()
81+
(component_path / 'file1.txt').write_text('file1')
82+
checksums_manager = ChecksumsManager(component_path)
83+
84+
checksums_model.files.append(
85+
create_file_field(
86+
'file1.txt', 5, 'c147efcfc2d7ea666a9e4f5187b115c90903f0fc896a56df9a6ef5d8f3fc9f31'
87+
)
88+
)
89+
90+
external_path = tmp_path / 'ext'
91+
external_path.mkdir()
92+
checksums_path = external_path / CHECKSUMS_FILENAME
93+
94+
checksums_manager.dump(external_path)
95+
assert checksums_path.is_file()
96+
97+
stored_checksums = json.loads(checksums_path.read_text())
98+
expected_checksums = checksums_model.model_dump()
99+
100+
normalize_checksums(stored_checksums)
101+
normalize_checksums(expected_checksums)
102+
103+
assert stored_checksums == expected_checksums
104+
105+
78106
def test_load_checksums(tmp_path, checksums_model):
79107
checksums_path = tmp_path / CHECKSUMS_FILENAME
80108
checksums_manager = ChecksumsManager(tmp_path)

0 commit comments

Comments
 (0)