sickdays: a non-UTF-8 pool file fails closed instead of raising

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xfj1sxKH9VBPqpcAni8Jry
This commit is contained in:
Krzysztof Rudnicki 2026-10-16 08:25:00 +02:00
parent 894d76f811
commit af8c6c78bf
2 changed files with 12 additions and 1 deletions

View File

@ -36,7 +36,7 @@ def _read(path: Path) -> Log:
text = path.read_text(encoding="utf-8")
except FileNotFoundError:
return {}
except OSError as exc:
except (OSError, UnicodeDecodeError) as exc:
msg = f"the sick-day pool {path} cannot be read: {exc}"
raise UnreadablePoolError(msg) from exc
try:

View File

@ -74,3 +74,14 @@ def test_writers_refuse_rather_than_overwrite_a_corrupt_pool(state_dir: Path) ->
with pytest.raises(UnreadablePoolError):
import_legacy([TODAY])
assert pool.read_text(encoding="utf-8") == "{not json"
def test_a_non_utf8_pool_fails_closed_and_refuses_writers(
state_dir: Path, caplog: pytest.LogCaptureFixture
) -> None:
(state_dir / "sick_days.json").write_bytes(b"\xff\xfe\x00junk")
with caplog.at_level("WARNING"):
assert not is_sick_day(TODAY)
assert "cannot be read" in caplog.text
with pytest.raises(UnreadablePoolError):
add_sick_day(TODAY, now=TODAY)