Skip to content

Commit 1400538

Browse files
committed
use setter for stats, handling main color
1 parent c91ad61 commit 1400538

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/_pytest/terminal.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def __init__(self, config: Config, file=None) -> None:
248248
self._showfspath = None
249249

250250
self.stats = {} # type: Dict[str, List[Any]]
251+
self._main_color = None # type: Optional[str]
252+
self._known_types = None # type: Optional[List]
251253
self.startdir = config.invocation_dir
252254
if file is None:
253255
file = sys.stdout
@@ -366,6 +368,13 @@ def section(self, title, sep="=", **kw):
366368
def line(self, msg, **kw):
367369
self._tw.line(msg, **kw)
368370

371+
def _add_stats(self, category: str, items: List) -> None:
372+
if category not in self.stats:
373+
self.stats[category] = items
374+
self._set_main_color()
375+
else:
376+
self.stats[category].extend(items)
377+
369378
def pytest_internalerror(self, excrepr):
370379
for line in str(excrepr).split("\n"):
371380
self.write_line("INTERNALERROR> " + line)
@@ -375,15 +384,14 @@ def pytest_warning_captured(self, warning_message, item):
375384
# from _pytest.nodes import get_fslocation_from_item
376385
from _pytest.warnings import warning_record_to_str
377386

378-
warnings = self.stats.setdefault("warnings", [])
379387
fslocation = warning_message.filename, warning_message.lineno
380388
message = warning_record_to_str(warning_message)
381389

382390
nodeid = item.nodeid if item is not None else ""
383391
warning_report = WarningReport(
384392
fslocation=fslocation, message=message, nodeid=nodeid
385393
)
386-
warnings.append(warning_report)
394+
self._add_stats("warnings", [warning_report])
387395

388396
def pytest_plugin_registered(self, plugin):
389397
if self.config.option.traceconfig:
@@ -394,7 +402,7 @@ def pytest_plugin_registered(self, plugin):
394402
self.write_line(msg)
395403

396404
def pytest_deselected(self, items):
397-
self.stats.setdefault("deselected", []).extend(items)
405+
self._add_stats("deselected", items)
398406

399407
def pytest_runtest_logstart(self, nodeid, location):
400408
# ensure that the path is printed before the
@@ -415,7 +423,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
415423
word, markup = word
416424
else:
417425
markup = None
418-
self.stats.setdefault(category, []).append(rep)
426+
self._add_stats(category, [rep])
419427
if not letter and not word:
420428
# probably passed setup/teardown
421429
return
@@ -529,9 +537,9 @@ def pytest_collection(self):
529537

530538
def pytest_collectreport(self, report: CollectReport) -> None:
531539
if report.failed:
532-
self.stats.setdefault("error", []).append(report)
540+
self._add_stats("error", [report])
533541
elif report.skipped:
534-
self.stats.setdefault("skipped", []).append(report)
542+
self._add_stats("skipped", [report])
535543
items = [x for x in report.result if isinstance(x, pytest.Item)]
536544
self._numcollected += len(items)
537545
if self.isatty:
@@ -1011,6 +1019,11 @@ def show_skipped(lines: List[str]) -> None:
10111019
self.write_line(line)
10121020

10131021
def _get_main_color(self) -> Tuple[str, List[str]]:
1022+
if self._main_color is None or self._known_types is None:
1023+
self._main_color, self._known_types = self._set_main_color()
1024+
return self._main_color, self._known_types
1025+
1026+
def _set_main_color(self) -> Tuple[str, List[str]]:
10141027
stats = self.stats
10151028
known_types = (
10161029
"failed passed skipped deselected xfailed xpassed warnings error".split()
@@ -1031,10 +1044,9 @@ def _get_main_color(self) -> Tuple[str, List[str]]:
10311044
main_color = "green"
10321045
else:
10331046
main_color = "yellow"
1034-
10351047
return main_color, known_types
10361048

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

10401052
parts = []

testing/test_terminal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,9 @@ def test_summary_stats(tr, exp_line, exp_color, stats_arg):
14411441
tr._testscollected = 0
14421442
assert tr._is_last_item
14431443

1444+
# Reset cache.
1445+
tr._main_color = None
1446+
14441447
print("Based on stats: %s" % stats_arg)
14451448
print('Expect summary: "{}"; with color "{}"'.format(exp_line, exp_color))
14461449
(line, color) = tr.build_summary_stats_line()
@@ -1456,11 +1459,13 @@ class DummyReport(BaseReport):
14561459
r1 = DummyReport()
14571460
r2 = DummyReport()
14581461
tr.stats = {"failed": (r1, r2)}
1462+
tr._main_color = None
14591463
res = tr.build_summary_stats_line()
14601464
assert res == ([("2 failed", {"bold": True, "red": True})], "red")
14611465

14621466
r1.count_towards_summary = False
14631467
tr.stats = {"failed": (r1, r2)}
1468+
tr._main_color = None
14641469
res = tr.build_summary_stats_line()
14651470
assert res == ([("1 failed", {"bold": True, "red": True})], "red")
14661471

0 commit comments

Comments
 (0)