Skip to content

Commit 3a4efa1

Browse files
committed
Strip ANSI escape sequences when ansi2html is missing
Fixes: #314
1 parent b5a02fb commit 3a4efa1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

pytest_html/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
from . import extras
3131
from . import __version__, __pypi_url__
3232

33+
from _pytest.logging import _remove_ansi_escape_sequences
34+
3335

3436
def pytest_addhooks(pluginmanager):
3537
from . import hooks
@@ -279,9 +281,13 @@ def append_log_html(self, report, additional_html):
279281
header, content = map(escape, section)
280282
log.append(f" {header:-^80} ")
281283
log.append(html.br())
284+
282285
if ANSI:
283286
converter = Ansi2HTMLConverter(inline=False, escaped=False)
284287
content = converter.convert(content, full=False)
288+
else:
289+
content = _remove_ansi_escape_sequences(content)
290+
285291
log.append(raw(content))
286292
log.append(html.br())
287293

testing/test_pytest_html.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,29 @@ def test_ansi():
830830
else:
831831
assert content not in html
832832

833+
def test_ansi_escape_sequence_removed(self, testdir):
834+
testdir.makeini(
835+
r"""
836+
[pytest]
837+
log_cli = 1
838+
log_cli_level = INFO
839+
"""
840+
)
841+
testdir.makepyfile(
842+
r"""
843+
import logging
844+
logging.basicConfig()
845+
LOGGER = logging.getLogger()
846+
def test_ansi():
847+
LOGGER.info("ANSI removed")
848+
"""
849+
)
850+
result, html = run(
851+
testdir, "report.html", "--self-contained-html", "--color=yes"
852+
)
853+
assert result.ret == 0
854+
assert not re.search(r"\[[\d;]+m", html)
855+
833856
@pytest.mark.parametrize("content", [("'foo'"), ("u'\u0081'")])
834857
def test_utf8_longrepr(self, testdir, content):
835858
testdir.makeconftest(

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ commands = pre-commit run --all-files --show-diff-on-failure
2424
[flake8]
2525
max-line-length = 88
2626
exclude = .eggs,.tox
27+
# rational here:
28+
# https://github.com/psf/black/blob/master/docs/the_black_code_style.md#slices
29+
extend-ignore = E203
2730

2831
[pytest]
2932
testpaths = testing

0 commit comments

Comments
 (0)