Skip to content

Commit cbfc38d

Browse files
authored
Consolidate tox environments (#328)
1 parent 8a2dded commit cbfc38d

File tree

4 files changed

+30
-42
lines changed

4 files changed

+30
-42
lines changed

.travis.yml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ jobs:
2727
python: 3.6
2828
env: TOXENV=py36
2929

30-
-
31-
python: 3.6
32-
env: TOXENV=py36-ansi2html
33-
3430
-
3531
python: 3.7
3632
dist: xenial
@@ -41,29 +37,15 @@ jobs:
4137
python: 3.8
4238
env: TOXENV=py38
4339

44-
-
45-
python: 3.7
46-
dist: xenial
47-
sudo: required
48-
env: TOXENV=py37-ansi2html
49-
5040
- name: devel
5141
python: 3.8
5242
dist: xenial
5343
env: TOXENV=devel
5444

55-
-
56-
python: 3.8
57-
env: TOXENV=py38-ansi2html
58-
5945
-
6046
python: pypy3
6147
env: TOXENV=pypy3
6248

63-
-
64-
python: pypy3
65-
env: TOXENV=pypy3-ansi2html
66-
6749
- stage: deploy
6850
python: 3.7
6951
dist: xenial

pytest_html/plugin.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from base64 import b64encode, b64decode
66
from collections import OrderedDict
7+
from functools import lru_cache
8+
import importlib
79
from os.path import isfile
810
import datetime
911
import json
@@ -17,14 +19,6 @@
1719
from html import escape
1820
import pytest
1921

20-
try:
21-
from ansi2html import Ansi2HTMLConverter, style
22-
23-
ANSI = True
24-
except ImportError:
25-
# ansi2html is not installed
26-
ANSI = False
27-
2822
from py.xml import html, raw
2923

3024
from . import extras
@@ -33,6 +27,16 @@
3327
from _pytest.logging import _remove_ansi_escape_sequences
3428

3529

30+
@lru_cache()
31+
def ansi_support():
32+
try:
33+
# from ansi2html import Ansi2HTMLConverter, style # NOQA
34+
return importlib.import_module("ansi2html")
35+
except ImportError:
36+
# ansi2html is not installed
37+
pass
38+
39+
3640
def pytest_addhooks(pluginmanager):
3741
from . import hooks
3842

@@ -290,8 +294,10 @@ def append_log_html(self, report, additional_html):
290294
log.append(f" {header:-^80} ")
291295
log.append(html.br())
292296

293-
if ANSI:
294-
converter = Ansi2HTMLConverter(inline=False, escaped=False)
297+
if ansi_support():
298+
converter = ansi_support().Ansi2HTMLConverter(
299+
inline=False, escaped=False
300+
)
295301
content = converter.convert(content, full=False)
296302
else:
297303
content = _remove_ansi_escape_sequences(content)
@@ -413,13 +419,13 @@ def _generate_report(self, session):
413419
__name__, os.path.join("resources", "style.css")
414420
).decode("utf-8")
415421

416-
if ANSI:
422+
if ansi_support():
417423
ansi_css = [
418424
"\n/******************************",
419425
" * ANSI2HTML STYLES",
420426
" ******************************/\n",
421427
]
422-
ansi_css.extend([str(r) for r in style.get_styles()])
428+
ansi_css.extend([str(r) for r in ansi_support().style.get_styles()])
423429
self.style_css += "\n".join(ansi_css)
424430

425431
# <DF> Add user-provided CSS

testing/test_pytest_html.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -809,14 +809,14 @@ def test_foo(val):
809809
assert result.ret == 0
810810
assert_results(html, passed=1)
811811

812-
def test_ansi_color(self, testdir):
813-
try:
814-
import ansi2html # NOQA
815-
816-
ANSI = True
817-
except ImportError:
818-
# ansi2html is not installed
819-
ANSI = False
812+
@pytest.mark.parametrize(
813+
"with_ansi", [True, False],
814+
)
815+
def test_ansi_color(self, testdir, mocker, with_ansi):
816+
if not with_ansi:
817+
mock_ansi_support = mocker.patch("pytest_html.plugin.ansi_support")
818+
mock_ansi_support.return_value = None
819+
820820
pass_content = [
821821
'<span class="ansi31">RCOLOR',
822822
'<span class="ansi32">GCOLOR',
@@ -834,7 +834,7 @@ def test_ansi():
834834
result, html = run(testdir, "report.html", "--self-contained-html")
835835
assert result.ret == 0
836836
for content in pass_content:
837-
if ANSI:
837+
if with_ansi:
838838
assert content in html
839839
else:
840840
assert content not in html

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# Tox (http://tox.testrun.org/) is a tool for running tests
1+
# Tox (https://tox.readthedocs.io) is a tool for running tests
22
# in multiple virtualenvs. This configuration file will run the
33
# test suite on all supported python versions. To use it, "pip install tox"
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py{36,37,38,py3}{,-ansi2html}, linting
7+
envlist = py{36,37,38,py3}, linting
88

99
[testenv]
1010
setenv = PYTHONDONTWRITEBYTECODE=1
1111
deps =
1212
pytest-xdist
1313
pytest-rerunfailures
1414
pytest-mock
15-
py{36,37,38,py3}-ansi2html: ansi2html
15+
ansi2html # soft-dependency
1616
commands = pytest -v -r a --color=yes --html={envlogdir}/report.html --self-contained-html {posargs}
1717

1818
[testenv:linting]

0 commit comments

Comments
 (0)