fix: log append reports an unreadable --body-file as ERROR, not a traceback (#145)
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:
1 parent
685fc2e15e
commit
a70d904274
5 files changed
+83
-6
No files matched your search
@@ -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")
|
||||
Reference in new issue
Block a user