|
| 1 | +"""CLI and portable reports share one level-failure projection.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from trace_tests import cli as cli_module |
| 8 | +from trace_tests import report as report_module |
| 9 | +from trace_tests.cli import _print_report |
| 10 | +from trace_tests.modules.unverified import finding_counts_as_level_failure |
| 11 | +from trace_tests.report import _tally |
| 12 | +from trace_tests.result import Finding, Status |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("level", [0, 1, 2]) |
| 16 | +@pytest.mark.parametrize("status", list(Status)) |
| 17 | +def test_cli_report_and_policy_agree_for_every_status_and_level( |
| 18 | + status: Status, |
| 19 | + level: int, |
| 20 | +) -> None: |
| 21 | + finding = Finding("TR-NEW-001", status, "synthetic") |
| 22 | + expected = finding_counts_as_level_failure(finding, level) |
| 23 | + results = {"TR-X": [finding]} |
| 24 | + |
| 25 | + report_failures, _ = _tally(results, level) |
| 26 | + cli_exit = _print_report("record.json", "trace", level, results) |
| 27 | + |
| 28 | + assert bool(report_failures) is expected |
| 29 | + assert bool(cli_exit) is expected |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + ("status", "forced_contribution"), |
| 34 | + [ |
| 35 | + (Status.PASS, True), |
| 36 | + (Status.FAIL, False), |
| 37 | + (Status.SKIP, True), |
| 38 | + (Status.UNVERIFIED, False), |
| 39 | + ], |
| 40 | +) |
| 41 | +def test_each_projection_delegates_once_even_for_wrong_branch_differentials( |
| 42 | + monkeypatch: pytest.MonkeyPatch, |
| 43 | + status: Status, |
| 44 | + forced_contribution: bool, |
| 45 | +) -> None: |
| 46 | + finding = Finding("TR-X-001", status, "synthetic") |
| 47 | + results = {"TR-X": [finding]} |
| 48 | + cli_calls: list[tuple[str, int]] = [] |
| 49 | + report_calls: list[tuple[str, int]] = [] |
| 50 | + |
| 51 | + def cli_policy(item: Finding, level: int) -> bool: |
| 52 | + cli_calls.append((item.code, level)) |
| 53 | + return forced_contribution |
| 54 | + |
| 55 | + def report_policy(item: Finding, level: int) -> bool: |
| 56 | + report_calls.append((item.code, level)) |
| 57 | + return forced_contribution |
| 58 | + |
| 59 | + monkeypatch.setattr(cli_module, "finding_counts_as_level_failure", cli_policy) |
| 60 | + monkeypatch.setattr(report_module, "finding_counts_as_level_failure", report_policy) |
| 61 | + |
| 62 | + assert bool(_print_report("record.json", "trace", 2, results)) is forced_contribution |
| 63 | + assert bool(_tally(results, 2)[0]) is forced_contribution |
| 64 | + assert cli_calls == [("TR-X-001", 2)] |
| 65 | + assert report_calls == [("TR-X-001", 2)] |
0 commit comments