Skip to content

Commit db708c6

Browse files
committed
Additional exceptions tests for bad args and failing .NET runtime
1 parent 113393b commit db708c6

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
import pythonnet
5+
6+
17
def pytest_addoption(parser):
28
parser.addoption("--test-file", action="store", default=None)
39
parser.addoption("--test-verbose", action="store_true", default=False)
10+
11+
12+
@pytest.fixture
13+
def path_is_file_fails(monkeypatch):
14+
"""Make Path.is_file return False so load_runtime will raise FileNotFoundError."""
15+
monkeypatch.setattr(Path, "is_file", lambda self: False)
16+
17+
18+
@pytest.fixture
19+
def pythonnet_load_raises(monkeypatch):
20+
"""Make pythonnet.load raise RuntimeError while keeping the DLL present."""
21+
monkeypatch.setattr(Path, "is_file", lambda self: True)
22+
23+
def _raise(runtime) -> None:
24+
_ = runtime
25+
msg = "failed to init runtime"
26+
raise RuntimeError(msg)
27+
28+
monkeypatch.setattr(pythonnet, "load", _raise)

tests/test_formatter.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
import sys
13
from pathlib import Path
24

35
import pytest
@@ -98,3 +100,45 @@ def test_exceptions():
98100
match=r"Invalid value 'EolStyle\.CRLF' for option comment_policy",
99101
):
100102
_ = FracturedJsonOptions(comment_policy=EolStyle.CRLF)
103+
formatter = Formatter()
104+
with pytest.raises(TypeError, match="json_text must be a str"):
105+
formatter.reformat(None) # type: ignore[arg-type]
106+
107+
with pytest.raises(TypeError, match="json_text must be a str"):
108+
formatter.minify(b"{}") # type: ignore[arg-type]
109+
110+
with pytest.raises(TypeError, match="Must be callable"):
111+
formatter.string_length_func = 123 # type: ignore[assignment]
112+
113+
114+
def test_dll_missing(path_is_file_fails): # noqa: ARG001
115+
if "fractured_json" in sys.modules:
116+
del sys.modules["fractured_json"]
117+
118+
with pytest.raises(FileNotFoundError) as exc:
119+
importlib.import_module("fractured_json")
120+
121+
assert "FracturedJson.dll not found" in str(exc.value)
122+
123+
124+
def test_load_runtime_fails(pythonnet_load_raises): # noqa: ARG001
125+
if "fractured_json" in sys.modules:
126+
del sys.modules["fractured_json"]
127+
128+
with pytest.raises(RuntimeError) as exc:
129+
importlib.import_module("fractured_json")
130+
131+
assert "Failed to load pythonnet runtime" in str(exc.value)
132+
assert "coreclr" in str(exc.value)
133+
134+
135+
def test_string_length_property():
136+
formatter = Formatter()
137+
138+
def double_len(s: str) -> int:
139+
return len(s) * 2
140+
141+
formatter.string_length_func = double_len
142+
getter = formatter.string_length_func
143+
assert callable(getter)
144+
assert getter("abc") == 6

0 commit comments

Comments
 (0)