Skip to content

Commit 4935ed4

Browse files
committed
use setter for stats, handling main color
1 parent 41bdfab commit 4935ed4

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/_pytest/terminal.py

Lines changed: 23 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
@@ -525,9 +533,9 @@ def pytest_collection(self):
525533

526534
def pytest_collectreport(self, report: CollectReport) -> None:
527535
if report.failed:
528-
self.stats.setdefault("error", []).append(report)
536+
self._add_stats("error", [report])
529537
elif report.skipped:
530-
self.stats.setdefault("skipped", []).append(report)
538+
self._add_stats("skipped", [report])
531539
items = [x for x in report.result if isinstance(x, pytest.Item)]
532540
self._numcollected += len(items)
533541
if self.isatty:
@@ -1012,6 +1020,13 @@ def show_skipped(lines: List[str]) -> None:
10121020
self.write_line(line)
10131021

10141022
def _get_main_color(self) -> Tuple[str, List[str]]:
1023+
if self._main_color is None or self._known_types is None:
1024+
self._set_main_color()
1025+
assert self._main_color
1026+
assert self._known_types
1027+
return self._main_color, self._known_types
1028+
1029+
def _set_main_color(self) -> Tuple[str, List[str]]:
10151030
stats = self.stats
10161031
known_types = (
10171032
"failed passed skipped deselected xfailed xpassed warnings error".split()
@@ -1032,10 +1047,10 @@ def _get_main_color(self) -> Tuple[str, List[str]]:
10321047
main_color = "green"
10331048
else:
10341049
main_color = "yellow"
1035-
1050+
self._main_color, self._known_types = main_color, known_types
10361051
return main_color, known_types
10371052

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

10411056
parts = []

testing/test_terminal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,9 @@ class fake_session:
15411541
tr._session = fake_session
15421542
assert tr._is_last_item
15431543

1544+
# Reset cache.
1545+
tr._main_color = None
1546+
15441547
print("Based on stats: %s" % stats_arg)
15451548
print('Expect summary: "{}"; with color "{}"'.format(exp_line, exp_color))
15461549
(line, color) = tr.build_summary_stats_line()
@@ -1556,11 +1559,13 @@ class DummyReport(BaseReport):
15561559
r1 = DummyReport()
15571560
r2 = DummyReport()
15581561
tr.stats = {"failed": (r1, r2)}
1562+
tr._main_color = None
15591563
res = tr.build_summary_stats_line()
15601564
assert res == ([("2 failed", {"bold": True, "red": True})], "red")
15611565

15621566
r1.count_towards_summary = False
15631567
tr.stats = {"failed": (r1, r2)}
1568+
tr._main_color = None
15641569
res = tr.build_summary_stats_line()
15651570
assert res == ([("1 failed", {"bold": True, "red": True})], "red")
15661571

0 commit comments

Comments
 (0)