Skip to content

Commit 138fcaf

Browse files
committed
test: Migrate remainders of errorlog stub tests to runtime
1 parent 8b0dfa0 commit 138fcaf

File tree

2 files changed

+84
-80
lines changed

2 files changed

+84
-80
lines changed

test-rt/test_errorlog.py

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import inspect
4+
import logging
45
from typing import Any, cast
56

67
import _testutils
@@ -10,15 +11,31 @@
1011
ErrorDomains,
1112
ErrorLevels,
1213
ErrorTypes as ErrorTypes,
14+
PyErrorLog,
1315
XMLSyntaxError,
1416
_ListErrorLog,
1517
_LogEntry as _LogEntry,
18+
clear_error_log,
1619
fromstring,
20+
use_global_python_log,
1721
)
1822

1923
reveal_type = getattr(_testutils, "reveal_type_wrapper")
2024

2125

26+
### NOTES
27+
#
28+
# - Not testing manual construction of _ErrorLog; technically
29+
# feasible, but it does not make sense creating a collection
30+
# of error entries out of context
31+
#
32+
# - Not testing _DomainErrorLog as it is completely unused
33+
#
34+
# - Not testing _RotatingErrorLog, which is only used in
35+
# global lxml logging, and doesn't expose any attributes
36+
# other than those already present in _ListErrorLog
37+
38+
2239
def _method_no_kwarg() -> bool:
2340
# Param for some methods (filter_levels, receive)
2441
# is strictly positional in lxml 4.9.
@@ -176,9 +193,71 @@ def test_create_empty_log(self) -> None:
176193
reveal_type(e_copy)
177194

178195

179-
# TODO PyErrorLog, _RotatingErrorLog
180-
# The unused _DomainErrorLog is ignored
196+
class TestModuleFunc:
181197

182-
# Not testing manual construction of _ErrorLog; technically
183-
# feasible, but it does not make sense creating a collection
184-
# of error entries out of context
198+
def test_sig(self) -> None:
199+
sig = inspect.signature(clear_error_log)
200+
param = list(sig.parameters.values())
201+
assert len(param) == 0
202+
del sig, param
203+
204+
sig = inspect.signature(use_global_python_log)
205+
param = list(sig.parameters.values())
206+
assert len(param) == 1
207+
assert param[0].name == "log"
208+
assert param[0].kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
209+
del sig, param
210+
211+
with pytest.raises(
212+
TypeError, match=r"expected lxml\.etree\.PyErrorLog, got int"
213+
):
214+
use_global_python_log(cast(Any, 1))
215+
216+
# exception if used after use_global_python_log
217+
clear_error_log()
218+
219+
pylog = PyErrorLog()
220+
use_global_python_log(pylog)
221+
222+
223+
class TestPyErrorLog:
224+
def test_construct(self) -> None:
225+
pylog = PyErrorLog()
226+
use_global_python_log(pylog)
227+
del pylog
228+
229+
pylog = PyErrorLog("foobar")
230+
use_global_python_log(pylog)
231+
del pylog
232+
233+
with pytest.raises(TypeError, match="logger name must be a string"):
234+
_ = PyErrorLog(cast(Any, 1))
235+
236+
pylog = PyErrorLog(logger_name="foobar")
237+
use_global_python_log(pylog)
238+
del pylog
239+
240+
logger = logging.Logger("foobar")
241+
pylog = PyErrorLog(logger=logger)
242+
use_global_python_log(pylog)
243+
del pylog
244+
245+
with pytest.raises(AttributeError, match="has no attribute 'log'"):
246+
_ = PyErrorLog(logger=cast(Any, "foobar"))
247+
248+
def test_properties(self) -> None:
249+
pylog = PyErrorLog()
250+
use_global_python_log(pylog)
251+
252+
reveal_type(pylog.last_error) # None initially
253+
for mapping in pylog.level_map.items():
254+
reveal_type(mapping[0])
255+
reveal_type(mapping[1])
256+
257+
broken_xml = "<doc><a><b></a>&bar;</doc>"
258+
with pytest.raises(XMLSyntaxError):
259+
_ = fromstring(broken_xml)
260+
261+
assert pylog.last_error is not None
262+
reveal_type(pylog.last_error)
263+
pylog.receive(pylog.last_error)

test-stub/test-errorlog.yml

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)