Skip to content

Commit b5a208a

Browse files
authored
Merge pull request #43 from jkoelker/fix/issue-42-json-trailing-newline
fix: add trailing newline to generated JSON files
2 parents beea622 + d8ec17d commit b5a208a

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/oneiro/civitai.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def _save_metadata(self) -> None:
226226
}
227227
with open(self.metadata_file, "w") as f:
228228
json.dump(data, f, indent=2)
229+
f.write("\n")
229230

230231
def get(self, sha256: str) -> Path | None:
231232
"""Get cached file path by SHA256 hash."""

src/oneiro/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def set(self, *keys: str, value: Any) -> None:
129129
self.state_path.parent.mkdir(parents=True, exist_ok=True)
130130
with open(self.state_path, "w") as f:
131131
json.dump(self._state, f, indent=2)
132+
f.write("\n")
132133

133134
print(f"State updated: {'.'.join(keys)} = {value}")
134135

tests/test_civitai.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ def test_persistence(self, tmp_path):
290290

291291
assert cache2.get("abc123") == test_file
292292

293+
def test_metadata_file_has_trailing_newline(self, tmp_path):
294+
"""Metadata file should end with a newline."""
295+
cache = CivitaiCache(tmp_path)
296+
test_file = tmp_path / "test.bin"
297+
test_file.write_bytes(b"test content")
298+
299+
cache.add(
300+
file_path=test_file,
301+
sha256="abc123",
302+
model_id=1,
303+
version_id=2,
304+
filename="test.bin",
305+
size_kb=100,
306+
)
307+
308+
content = cache.metadata_file.read_text()
309+
assert content.endswith("\n"), "metadata.json should end with a newline"
310+
293311
def test_total_size(self, tmp_path):
294312
"""total_size_kb returns sum of all cached file sizes."""
295313
cache = CivitaiCache(tmp_path)

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ def test_set_creates_parent_dirs(self, tmp_path):
212212
config.set("key", value="value")
213213
assert state.exists()
214214

215+
def test_set_writes_trailing_newline(self, tmp_path):
216+
"""Set writes state file with trailing newline."""
217+
base = tmp_path / "config.toml"
218+
state = tmp_path / "state.json"
219+
base.write_text("")
220+
config = Config(base, state_path=state)
221+
config.load()
222+
config.set("key", value="value")
223+
content = state.read_text()
224+
assert content.endswith("\n"), "state.json should end with a newline"
225+
215226

216227
class TestConfigDeepMerge:
217228
"""Tests for Config._deep_merge()."""

0 commit comments

Comments
 (0)