From 3a4efa1d18a3692a0cf92c3783d3edf4d9187a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Thu, 16 Jul 2020 17:42:03 +0200 Subject: [PATCH 1/2] Strip ANSI escape sequences when ansi2html is missing Fixes: #314 --- pytest_html/plugin.py | 6 ++++++ testing/test_pytest_html.py | 23 +++++++++++++++++++++++ tox.ini | 3 +++ 3 files changed, 32 insertions(+) diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index b92236a9..716975fd 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 @@ -279,9 +281,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 290ffe98..faabe523 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -830,6 +830,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 e85d0b3b..bbd93584 100644 --- a/tox.ini +++ b/tox.ini @@ -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 From b9f4e8959a44e8968ba8cafe3329134b6fde1f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Thu, 16 Jul 2020 19:19:13 +0200 Subject: [PATCH 2/2] fix typo --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index bbd93584..362a4965 100644 --- a/tox.ini +++ b/tox.ini @@ -24,7 +24,7 @@ commands = pre-commit run --all-files --show-diff-on-failure [flake8] max-line-length = 88 exclude = .eggs,.tox -# rational here: +# rationale here: # https://github.com/psf/black/blob/master/docs/the_black_code_style.md#slices extend-ignore = E203