Merge pull request #6107 from MarcoGorelli/color-percentage-indicator

Color percentage indicator
This commit is contained in:
Daniel Hahler 2019-11-06 00:28:45 +01:00 committed by GitHub
commit 00f67494e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 5 deletions

View File

@ -0,0 +1 @@
The "[XXX%]" indicator in the test summary is colored according to the final (new) multi-colored line's main color.

View File

@ -15,6 +15,7 @@ from typing import List
from typing import Mapping from typing import Mapping
from typing import Optional from typing import Optional
from typing import Set from typing import Set
from typing import Tuple
import attr import attr
import pluggy import pluggy
@ -458,18 +459,20 @@ class TerminalReporter:
else: else:
progress_length = len(" [100%]") progress_length = len(" [100%]")
main_color, _ = _get_main_color(self.stats)
self._progress_nodeids_reported.add(nodeid) self._progress_nodeids_reported.add(nodeid)
is_last_item = ( is_last_item = (
len(self._progress_nodeids_reported) == self._session.testscollected len(self._progress_nodeids_reported) == self._session.testscollected
) )
if is_last_item: if is_last_item:
self._write_progress_information_filling_space() self._write_progress_information_filling_space(color=main_color)
else: else:
w = self._width_of_current_line w = self._width_of_current_line
past_edge = w + progress_length + 1 >= self._screen_width past_edge = w + progress_length + 1 >= self._screen_width
if past_edge: if past_edge:
msg = self._get_progress_information_message() msg = self._get_progress_information_message()
self._tw.write(msg + "\n", cyan=True) self._tw.write(msg + "\n", **{main_color: True})
def _get_progress_information_message(self): def _get_progress_information_message(self):
collected = self._session.testscollected collected = self._session.testscollected
@ -486,11 +489,13 @@ class TerminalReporter:
return " [{:3d}%]".format(progress) return " [{:3d}%]".format(progress)
return " [100%]" return " [100%]"
def _write_progress_information_filling_space(self): def _write_progress_information_filling_space(self, color=None):
if not color:
color, _ = _get_main_color(self.stats)
msg = self._get_progress_information_message() msg = self._get_progress_information_message()
w = self._width_of_current_line w = self._width_of_current_line
fill = self._tw.fullwidth - w - 1 fill = self._tw.fullwidth - w - 1
self.write(msg.rjust(fill), cyan=True) self.write(msg.rjust(fill), **{color: True})
@property @property
def _width_of_current_line(self): def _width_of_current_line(self):
@ -1075,7 +1080,7 @@ def _make_plural(count, noun):
return count, noun + "s" if count != 1 else noun return count, noun + "s" if count != 1 else noun
def build_summary_stats_line(stats): def _get_main_color(stats) -> Tuple[str, List[str]]:
known_types = ( known_types = (
"failed passed skipped deselected xfailed xpassed warnings error".split() "failed passed skipped deselected xfailed xpassed warnings error".split()
) )
@ -1096,6 +1101,12 @@ def build_summary_stats_line(stats):
else: else:
main_color = "yellow" main_color = "yellow"
return main_color, known_types
def build_summary_stats_line(stats):
main_color, known_types = _get_main_color(stats)
parts = [] parts = []
for key in known_types: for key in known_types:
reports = stats.get(key, None) reports = stats.get(key, None)

View File

@ -21,6 +21,10 @@ from _pytest.terminal import getreportopt
from _pytest.terminal import TerminalReporter from _pytest.terminal import TerminalReporter
DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"]) DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"])
RED = r"\x1b\[31m"
GREEN = r"\x1b\[32m"
YELLOW = r"\x1b\[33m"
RESET = r"\x1b\[0m"
class Option: class Option:
@ -1487,6 +1491,43 @@ class TestProgressOutputStyle:
] ]
) )
def test_colored_progress(self, testdir, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
testdir.makepyfile(
test_bar="""
import pytest
@pytest.mark.parametrize('i', range(10))
def test_bar(i): pass
""",
test_foo="""
import pytest
import warnings
@pytest.mark.parametrize('i', range(5))
def test_foo(i):
warnings.warn(DeprecationWarning("collection"))
pass
""",
test_foobar="""
import pytest
@pytest.mark.parametrize('i', range(5))
def test_foobar(i): raise ValueError()
""",
)
output = testdir.runpytest()
output.stdout.re_match_lines(
[
r"test_bar.py ({green}\.{reset}){{10}}{green} \s+ \[ 50%\]{reset}".format(
green=GREEN, reset=RESET
),
r"test_foo.py ({green}\.{reset}){{5}}{yellow} \s+ \[ 75%\]{reset}".format(
green=GREEN, reset=RESET, yellow=YELLOW
),
r"test_foobar.py ({red}F{reset}){{5}}{red} \s+ \[100%\]{reset}".format(
reset=RESET, red=RED
),
]
)
def test_count(self, many_tests_files, testdir): def test_count(self, many_tests_files, testdir):
testdir.makeini( testdir.makeini(
""" """