Strip ANSI escape sequences when ansi2html is missing

Fixes: #314
This commit is contained in:
Jim Brännlund 2020-07-16 17:42:03 +02:00
parent b5a02fb462
commit 3a4efa1d18
3 changed files with 32 additions and 0 deletions

View File

@ -30,6 +30,8 @@ from py.xml import html, raw
from . import extras
from . import __version__, __pypi_url__
from _pytest.logging import _remove_ansi_escape_sequences
def pytest_addhooks(pluginmanager):
from . import hooks
@ -279,9 +281,13 @@ class HTMLReport:
header, content = map(escape, section)
log.append(f" {header:-^80} ")
log.append(html.br())
if ANSI:
converter = Ansi2HTMLConverter(inline=False, escaped=False)
content = converter.convert(content, full=False)
else:
content = _remove_ansi_escape_sequences(content)
log.append(raw(content))
log.append(html.br())

View File

@ -830,6 +830,29 @@ class TestHTML:
else:
assert content not in html
def test_ansi_escape_sequence_removed(self, testdir):
testdir.makeini(
r"""
[pytest]
log_cli = 1
log_cli_level = INFO
"""
)
testdir.makepyfile(
r"""
import logging
logging.basicConfig()
LOGGER = logging.getLogger()
def test_ansi():
LOGGER.info("ANSI removed")
"""
)
result, html = run(
testdir, "report.html", "--self-contained-html", "--color=yes"
)
assert result.ret == 0
assert not re.search(r"\[[\d;]+m", html)
@pytest.mark.parametrize("content", [("'foo'"), ("u'\u0081'")])
def test_utf8_longrepr(self, testdir, content):
testdir.makeconftest(

View File

@ -24,6 +24,9 @@ commands = pre-commit run --all-files --show-diff-on-failure
[flake8]
max-line-length = 88
exclude = .eggs,.tox
# rational here:
# https://github.com/psf/black/blob/master/docs/the_black_code_style.md#slices
extend-ignore = E203
[pytest]
testpaths = testing