Skip to content

Commit

Permalink
Improve test summary
Browse files Browse the repository at this point in the history
* Group failures together at the end
* Align summary with `fmt` and `lint` style

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano committed Aug 17, 2020
1 parent 5da01a1 commit c225382
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
36 changes: 24 additions & 12 deletions src/python/pants/core/goals/test.py
Expand Up @@ -7,7 +7,7 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePath
from typing import Dict, Iterable, List, Optional, Tuple, Type, TypeVar, cast
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, Union, cast

from pants.core.util_rules.filter_empty_sources import (
FieldSetsWithSources,
Expand Down Expand Up @@ -136,6 +136,19 @@ def message(self) -> str:
output = f"{output.rstrip()}\n\n"
return f"{message}{output}"

def __lt__(self, other: Union[Any, "EnrichedTestResult"]) -> bool:
"""We sort first by status (skipped vs failed vs succeeded), then alphanumerically within
each group."""
if not isinstance(other, EnrichedTestResult):
return NotImplemented
if self.exit_code == other.exit_code:
return self.address.spec < other.address.spec
if self.exit_code is None:
return True
if other.exit_code is None:
return False
return self.exit_code < other.exit_code


@dataclass(frozen=True)
class TestDebugRequest:
Expand Down Expand Up @@ -371,22 +384,21 @@ async def run_tests(
Get(EnrichedTestResult, TestFieldSet, field_set) for field_set in field_sets_with_sources
)

# Print summary
# Print summary.
exit_code = 0
if results:
console.print_stderr("")
for result in results:
for result in sorted(results):
if result.skipped:
continue
result_desc = console.green("SUCCESS") if result.exit_code == 0 else console.red("FAILURE")
# The right-align logic sees the color control codes as characters, so we have
# to account for that. In f-strings the alignment field widths must be literals,
# so we have to indirect via a call to .format().
right_align = 19 if console.use_colors else 10
format_str = f"{{addr:80}}.....{{result:>{right_align}}}"
console.print_stderr(format_str.format(addr=result.address.spec, result=result_desc))
if result.exit_code is not None and result.exit_code != 0:
exit_code = result.exit_code
if result.exit_code == 0:
sigil = console.green("✓")
status = "succeeded"
else:
sigil = console.red("𐄂")
status = "failed"
exit_code = cast(int, result.exit_code)
console.print_stderr(f"{sigil} {result.address} {status}.")

merged_xml_results = await Get(
Digest, MergeDigests(result.xml_results for result in results if result.xml_results),
Expand Down
26 changes: 23 additions & 3 deletions src/python/pants/core/goals/test_test.py
Expand Up @@ -217,10 +217,10 @@ def test_summary(self) -> None:
)
assert exit_code == ConditionallySucceedsFieldSet.exit_code(bad_address)
assert stderr == dedent(
f"""\
"""\
{good_address} ..... SUCCESS
{bad_address} ..... FAILURE
✓ //:good succeeded.
𐄂 //:bad failed.
"""
)

Expand All @@ -242,6 +242,26 @@ def test_coverage(self) -> None:
assert stderr.strip().endswith(f"Ran coverage on {addr1.spec}, {addr2.spec}")


def sort_results() -> None:
create_test_result = partial(
EnrichedTestResult, stdout="", stderr="", output_setting=ShowOutput.ALL
)
skip1 = create_test_result(exit_code=None, address=Address("t1"))
skip2 = create_test_result(exit_code=None, address=Address("t2"))
success1 = create_test_result(exit_code=0, address=Address("t1"))
success2 = create_test_result(exit_code=0, address=Address("t2"))
fail1 = create_test_result(exit_code=1, address=Address("t1"))
fail2 = create_test_result(exit_code=1, address=Address("t2"))
assert sorted([fail2, success2, skip2, fail1, success1, skip1]) == [
skip1,
skip2,
success1,
success2,
fail1,
fail2,
]


def assert_streaming_output(
*,
exit_code: Optional[int],
Expand Down

0 comments on commit c225382

Please sign in to comment.