Include filename in exception when loading a json file fails (#111802)

* Include filename in exception when loading a json file fails

* fix
This commit is contained in:
J. Nick Koston
2024-02-29 05:30:29 -10:00
committed by GitHub
parent 9512fb420d
commit f59268b2ee
3 changed files with 11 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
"""Test Home Assistant json utility functions."""
from pathlib import Path
import re
import orjson
import pytest
@@ -21,11 +22,11 @@ TEST_BAD_SERIALIED = "THIS IS NOT JSON\n"
def test_load_bad_data(tmp_path: Path) -> None:
"""Test error from trying to load unserialisable data."""
"""Test error from trying to load unserializable data."""
fname = tmp_path / "test5.json"
with open(fname, "w") as fh:
fh.write(TEST_BAD_SERIALIED)
with pytest.raises(HomeAssistantError) as err:
with pytest.raises(HomeAssistantError, match=re.escape(str(fname))) as err:
load_json(fname)
assert isinstance(err.value.__cause__, ValueError)
@@ -33,7 +34,7 @@ def test_load_bad_data(tmp_path: Path) -> None:
def test_load_json_os_error() -> None:
"""Test trying to load JSON data from a directory."""
fname = "/"
with pytest.raises(HomeAssistantError) as err:
with pytest.raises(HomeAssistantError, match=re.escape(str(fname))) as err:
load_json(fname)
assert isinstance(err.value.__cause__, OSError)