mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-11 18:14:00 +01:00
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:
parent
894d76f811
commit
af8c6c78bf
@ -36,7 +36,7 @@ def _read(path: Path) -> Log:
|
|||||||
text = path.read_text(encoding="utf-8")
|
text = path.read_text(encoding="utf-8")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return {}
|
return {}
|
||||||
except OSError as exc:
|
except (OSError, UnicodeDecodeError) as exc:
|
||||||
msg = f"the sick-day pool {path} cannot be read: {exc}"
|
msg = f"the sick-day pool {path} cannot be read: {exc}"
|
||||||
raise UnreadablePoolError(msg) from exc
|
raise UnreadablePoolError(msg) from exc
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -74,3 +74,14 @@ def test_writers_refuse_rather_than_overwrite_a_corrupt_pool(state_dir: Path) ->
|
|||||||
with pytest.raises(UnreadablePoolError):
|
with pytest.raises(UnreadablePoolError):
|
||||||
import_legacy([TODAY])
|
import_legacy([TODAY])
|
||||||
assert pool.read_text(encoding="utf-8") == "{not json"
|
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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user