E586 Remove ANSI escape sequences from HTML report · pytest-dev/pytest-html@a04e63a · GitHub
[go: up one dir, main page]

Skip to content

Commit a04e63a

Browse files
committed
Remove ANSI escape sequences from HTML report
Fixes: #314
1 parent b5a02fb commit a04e63a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ deps = pre-commit
2222
commands = pre-commit run --all-files --show-diff-on-failure
2323

2424
[flake8]
25+
ignore = E203
2526
max-line-length = 88
2627
exclude = .eggs,.tox
2728

0 commit comments

Comments
 (0)
0