fix: log append reports an unreadable --body-file as ERROR, not a traceback (#145)
CI / verify (push) Successful in 1m10s
Release / release (push) Successful in 35s

Files changed:
- CHANGES.md
- VERSION
- tools/CONTRACT.md
- tools/chemenu/commands/log_append.py
- tools/chemenu/tests/test_log_append.py
This commit is contained in:
torben committed 2026-09-26 16:03:44 +02:00
1 parent 685fc2e15e
commit a70d904274
5 files changed
+83 -6

No files matched your search

+5 -2
View File
@@ -75,7 +75,7 @@ def ingests_since_last_lint(entries: list[tuple[str, str, str]]) -> int:
reaction="Nothing was written - fix the argument and retry once",
),
cli_contract.Failure(
cause="`--body-file` cannot be read",
cause="`--body-file` is missing, not a readable file, or not valid UTF-8",
reaction="Nothing was written - fix the path and retry once",
),
),
@@ -104,7 +104,10 @@ def log_append(
fail(f"--op must be one of {VALID_OPS}")
text = body
if body_file:
text = body_file.read_text(encoding="utf-8")
try:
text = body_file.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as exc:
fail(f"Cannot read --body-file {body_file}: {exc}")
entry = format_log_entry(op, title, text)
with config.LOG_FILE.open("a", encoding="utf-8") as f:
f.write("\n" + entry)
+61
View File
@@ -1,6 +1,10 @@
import pytest
import typer
from chemenu.commands.log_append import (
format_log_entry,
ingests_since_last_lint,
log_append,
log_status,
parse_log_entries,
)
@@ -75,3 +79,60 @@ def test_log_status_warns_at_the_ten_ingest_threshold(tmp_path, monkeypatch, cap
out = capsys.readouterr().out
assert "Ingests since last lint: 10" in out
assert "Threshold reached" in out
# --- log_append --body-file: unreadable is an ERROR line, never a traceback (Gitea #145) ---
def _make_missing(tmp_path):
return tmp_path / "nonexistent.md"
def _make_directory(tmp_path):
d = tmp_path / "a-directory"
d.mkdir()
return d
def _make_non_utf8(tmp_path):
p = tmp_path / "not-utf8.md"
p.write_bytes(b"\xff\xfe garbage")
return p
@pytest.mark.parametrize(
"make_bad_file",
[_make_missing, _make_directory, _make_non_utf8],
ids=["missing", "directory", "non-utf8"],
)
def test_log_append_reports_an_unreadable_body_file_as_an_error(make_bad_file, tmp_path, monkeypatch, capsys):
import chemenu.config as config
log_file = tmp_path / "log.md"
before = "# Wiki Log\n"
log_file.write_text(before, encoding="utf-8")
monkeypatch.setattr(config, "LOG_FILE", log_file)
bad_file = make_bad_file(tmp_path)
with pytest.raises(typer.Exit) as exc:
log_append(op="ingest", title="raw/notes/a.md", body="", body_file=bad_file)
assert exc.value.exit_code == 1
out = capsys.readouterr().out
assert out.startswith("ERROR ")
assert str(bad_file) in out
assert log_file.read_text(encoding="utf-8") == before
def test_log_append_reads_the_body_from_body_file(tmp_path, monkeypatch):
import chemenu.config as config
log_file = tmp_path / "log.md"
log_file.write_text("# Wiki Log\n", encoding="utf-8")
monkeypatch.setattr(config, "LOG_FILE", log_file)
body_file = tmp_path / "body.md"
body_file.write_text("Created [[Docker]].", encoding="utf-8")
log_append(op="ingest", title="raw/notes/a.md", body="", body_file=body_file)
assert "Created [[Docker]]." in log_file.read_text(encoding="utf-8")