INTERNAL ERROR with arbitrary "<XXX>" strings in message
See original GitHub issueWe are using loguru
to capture py.test
errors and redirect them to one or several files. So far, we have code similar to the following:
@pytest.fixture(autouse=True)
def init_logger(request, workspace):
log_path = workspace.logdir / (request.node.name + ".log")
# 1. log to file with ansi color codes
logger.add(
log_path.with_suffix(".color.log"),
colorize=True)
# 2. log to file with plain text
logger.add(log_path,
colorize=False)
def pytest_runtest_logreport(report):
"""
Pytest hook called after a test has completed.
"""
def get_status(report):
TestReport = namedtuple('TestReport', ['color', 'status'])
was_xfail = hasattr(report, "wasxfail")
if report.passed and not was_xfail:
return TestReport("green", "PASSED")
elif report.passed and was_xfail:
return TestReport("yellow", "PASSED")
elif report.failed:
return TestReport("red", "FAILED")
elif report.skipped:
return TestReport("yellow", "SKIPPED")
else:
raise ValueError("Test report has unknown state")
if report.when == "call":
c, s = get_status(report)
extra = (":\n" + f"{report.longrepr}" if report.failed else "")
logger.opt(colors=True).info(f"<{c}>TEST {s}</{c}> {extra}")
The problem lies in the last line if extra
contains something similar to loguru
color tags. In our case, for the offending test py.test
produces a failure message that points to the function below, which gets stored in extra
:
def test():
"""Check if a file exists using `if [[ -e <file> ]]`."""
# do stuff
Since py.test
shows the function code up until the point of failure, the <file>
text in the docstring is wrongly parsed as a color tag by loguru
and the following internal error is produced:
INTERNALERROR> ValueError: Tag "<file>" does not corespond to any known ansi directive, make sure you did not misspelled it (or prepend '\' to escape it)
Even if we could escape this particular <file>
string to avoid the error, there’s no way to guarantee that a stack dump produced by py.test
doesn’t include some other string that might be misinterpreted as a color tag. Is there any way to prevent loguru
from raising the internal error and just ignore the “tag” if it’s not recognized as a color?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Great, that’s was easy for once! 😄
I’m closing this issue then. 😉
You are right, thanks, I added a note about that in the documentation. 😉