Skip to content

Commit 992e3de

Browse files
authored
Merge pull request #315 from BeyondEvil/fix-ansi-in-report
2 parents 4dfec1c + 955eb44 commit 992e3de

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
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
@@ -287,9 +289,13 @@ def append_log_html(self, report, additional_html):
287289
header, content = map(escape, section)
288290
log.append(f" {header:-^80} ")
289291
log.append(html.br())
292+
290293
if ANSI:
291294
converter = Ansi2HTMLConverter(inline=False, escaped=False)
292295
content = converter.convert(content, full=False)
296+
else:
297+
content = _remove_ansi_escape_sequences(content)
298+
293299
log.append(raw(content))
294300
log.append(html.br())
295301

testing/test_pytest_html.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,29 @@ def test_ansi():
839839
else:
840840
assert content not in html
841841

842+
def test_ansi_escape_sequence_removed(self, testdir):
843+
testdir.makeini(
844+
r"""
845+
[pytest]
846+
log_cli = 1
847+
log_cli_level = INFO
848+
"""
849+
)
850+
testdir.makepyfile(
851+
r"""
852+
import logging
853+
logging.basicConfig()
854+
LOGGER = logging.getLogger()
855+
def test_ansi():
856+
LOGGER.info("ANSI removed")
857+
"""
858+
)
859+
result, html = run(
860+
testdir, "report.html", "--self-contained-html", "--color=yes"
861+
)
862+
assert result.ret == 0
863+
assert not re.search(r"\[[\d;]+m", html)
864+
842865
@pytest.mark.parametrize("content", [("'foo'"), ("u'\u0081'")])
843866
def test_utf8_longrepr(self, testdir, content):
844867
testdir.makeconftest(

tox.ini

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

2931
[pytest]
3032
testpaths = testing

0 commit comments

Comments
 (0)