diff --git a/sickdays/sickdays/_store.py b/sickdays/sickdays/_store.py index eb9a2d1..7acdae5 100644 --- a/sickdays/sickdays/_store.py +++ b/sickdays/sickdays/_store.py @@ -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: diff --git a/sickdays/sickdays/tests/test_store.py b/sickdays/sickdays/tests/test_store.py index 79644da..bb4e754 100644 --- a/sickdays/sickdays/tests/test_store.py +++ b/sickdays/sickdays/tests/test_store.py @@ -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)