diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 2996c344..39f8a677 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -30,6 +30,8 @@ from . import extras from . import __version__, __pypi_url__ +from _pytest.logging import _remove_ansi_escape_sequences + def pytest_addhooks(pluginmanager): from . import hooks @@ -287,9 +289,13 @@ def append_log_html(self, report, additional_html): 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()) diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index cf608327..2d5708db 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -839,6 +839,29 @@ def test_ansi(): 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( diff --git a/tox.ini b/tox.ini index d1464b80..362a4965 100644 --- a/tox.ini +++ b/tox.ini @@ -24,7 +24,9 @@ commands = pre-commit run --all-files --show-diff-on-failure [flake8] max-line-length = 88 exclude = .eggs,.tox -ignore = E203 +# rationale here: +# https://github.com/psf/black/blob/master/docs/the_black_code_style.md#slices +extend-ignore = E203 [pytest] testpaths = testing