Skip to content

Commit

Permalink
use setter for stats, handling main color
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jan 6, 2020
1 parent c91ad61 commit 7a33368
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def __init__(self, config: Config, file=None) -> None:
self._showfspath = None

self.stats = {} # type: Dict[str, List[Any]]
self._main_color = None # type: Optional[str]
self._known_types = None # type: Optional[List]
self.startdir = config.invocation_dir
if file is None:
file = sys.stdout
Expand Down Expand Up @@ -366,6 +368,13 @@ def section(self, title, sep="=", **kw):
def line(self, msg, **kw):
self._tw.line(msg, **kw)

def _add_stats(self, category: str, items: List) -> None:
if category not in self.stats:
self.stats[category] = items
self._set_main_color()
else:
self.stats[category].extend(items)

def pytest_internalerror(self, excrepr):
for line in str(excrepr).split("\n"):
self.write_line("INTERNALERROR> " + line)
Expand All @@ -375,15 +384,14 @@ def pytest_warning_captured(self, warning_message, item):
# from _pytest.nodes import get_fslocation_from_item
from _pytest.warnings import warning_record_to_str

warnings = self.stats.setdefault("warnings", [])
fslocation = warning_message.filename, warning_message.lineno
message = warning_record_to_str(warning_message)

nodeid = item.nodeid if item is not None else ""
warning_report = WarningReport(
fslocation=fslocation, message=message, nodeid=nodeid
)
warnings.append(warning_report)
self._add_stats("warnings", [warning_report])

def pytest_plugin_registered(self, plugin):
if self.config.option.traceconfig:
Expand All @@ -394,7 +402,7 @@ def pytest_plugin_registered(self, plugin):
self.write_line(msg)

def pytest_deselected(self, items):
self.stats.setdefault("deselected", []).extend(items)
self._add_stats("deselected", items)

def pytest_runtest_logstart(self, nodeid, location):
# ensure that the path is printed before the
Expand All @@ -415,7 +423,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
word, markup = word
else:
markup = None
self.stats.setdefault(category, []).append(rep)
self._add_stats(category, [rep])
if not letter and not word:
# probably passed setup/teardown
return
Expand Down Expand Up @@ -529,9 +537,9 @@ def pytest_collection(self):

def pytest_collectreport(self, report: CollectReport) -> None:
if report.failed:
self.stats.setdefault("error", []).append(report)
self._add_stats("error", [report])
elif report.skipped:
self.stats.setdefault("skipped", []).append(report)
self._add_stats("skipped", [report])
items = [x for x in report.result if isinstance(x, pytest.Item)]
self._numcollected += len(items)
if self.isatty:
Expand Down Expand Up @@ -1011,6 +1019,11 @@ def show_skipped(lines: List[str]) -> None:
self.write_line(line)

def _get_main_color(self) -> Tuple[str, List[str]]:
if self._main_color is None or self._known_types is None:
self._main_color, self._known_types = self._set_main_color()
return self._main_color, self._known_types

def _set_main_color(self) -> Tuple[str, List[str]]:
stats = self.stats
known_types = (
"failed passed skipped deselected xfailed xpassed warnings error".split()
Expand All @@ -1031,10 +1044,9 @@ def _get_main_color(self) -> Tuple[str, List[str]]:
main_color = "green"
else:
main_color = "yellow"

return main_color, known_types

def build_summary_stats_line(self):
def build_summary_stats_line(self) -> Tuple[List[Tuple[str, Dict[str, bool]]], str]:
main_color, known_types = self._get_main_color()

parts = []
Expand Down
3 changes: 3 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,9 @@ def test_summary_stats(tr, exp_line, exp_color, stats_arg):
tr._testscollected = 0
assert tr._is_last_item

# Reset cache.
tr._main_color = None

print("Based on stats: %s" % stats_arg)
print('Expect summary: "{}"; with color "{}"'.format(exp_line, exp_color))
(line, color) = tr.build_summary_stats_line()
Expand Down

0 comments on commit 7a33368

Please sign in to comment.