forked from ChelseaKR/tods-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_report_extras.py
More file actions
107 lines (79 loc) · 3.66 KB
/
Copy pathtest_report_extras.py
File metadata and controls
107 lines (79 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Reporting additions: by-rule grouping, root-cause hints, SARIF, HTML."""
from tods_validate.findings import Finding, Severity
from tods_validate.report import (
REPORT_SCHEMA_VERSION,
by_rule,
render_html,
render_markdown,
render_sarif,
render_text,
)
def _findings(rule_id: str, n: int, severity: Severity = Severity.ERROR) -> list[Finding]:
return [
Finding(rule_id=rule_id, severity=severity, file="run_events.txt", row=i, message=f"m{i}")
for i in range(2, 2 + n)
]
def test_by_rule_counts() -> None:
findings = _findings("TODS-E307", 3) + _findings("TODS-E308", 1)
counts = by_rule(findings)
assert counts["TODS-E307"] == 3
assert counts["TODS-E308"] == 1
def test_text_shows_breakdown_and_path_to_green() -> None:
text = render_text(_findings("TODS-E307", 2), "feed/")
assert "By rule: TODS-E307 ×2" in text
assert "away from a clean run" in text
def test_text_shows_root_cause_hint_when_clustered() -> None:
text = render_text(_findings("TODS-E307", 6), "feed/")
assert "hint:" in text
assert "stale" in text
def test_text_hint_includes_worked_example_when_clustered() -> None:
text = render_text(_findings("TODS-E307", 6), "feed/")
assert "Before:" in text
assert "After:" in text
def test_markdown_hint_includes_worked_example_when_clustered() -> None:
markdown = render_markdown(_findings("TODS-E307", 6), "feed/")
assert "hint:" in markdown
assert "Before:" in markdown
assert "After:" in markdown
def test_max_findings_hides_overflow() -> None:
text = render_text(_findings("TODS-E307", 10), "feed/", max_findings=3)
assert "7 more finding(s) not shown" in text
def test_sarif_lists_rules_and_results() -> None:
import json
sarif = json.loads(render_sarif(_findings("TODS-E307", 2), "feed/"))
driver = sarif["runs"][0]["tool"]["driver"]
assert {r["id"] for r in driver["rules"]} == {"TODS-E307"}
assert len(sarif["runs"][0]["results"]) == 2
loc = sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]
assert loc["artifactLocation"]["uri"] == "run_events.txt"
def test_html_escapes_and_is_standalone() -> None:
nasty = [Finding(rule_id="TODS-E999", severity=Severity.ERROR, message="<script>x</script>")]
out = render_html(nasty, "feed/")
assert "<script>x</script>" not in out # escaped
assert "<script>" in out
def test_html_report_is_accessible() -> None:
out = render_html(_findings("TODS-E307", 2), "feed/")
# Declared language and a responsive viewport so the page reflows on zoom.
assert "<html lang='en'>" in out
assert "name='viewport'" in out
# Document landmarks give assistive tech an outline.
assert "<header>" in out
assert "<main>" in out
# The findings table is navigable: a caption plus column-scoped headers.
assert "<caption>" in out
assert out.count("scope='col'") == 4
# Severity reaches a screen reader as a word, not color alone.
assert ">ERROR<" in out
def test_html_severity_colors_clear_contrast() -> None:
# The info green was lightened away from the original low-contrast #0b5 so
# all three severities clear WCAG AA (4.5:1) on the white background.
out = render_html([], "feed/")
assert "#0b5" not in out
assert ".sev-info{color:#0a7d3f}" in out
def test_markdown_stamp_is_optional() -> None:
plain = render_markdown(_findings("TODS-E307", 1), "feed/")
stamped = render_markdown(_findings("TODS-E307", 1), "feed/", stamp=True)
assert "Generated by tods-validate" not in plain
assert "Generated by tods-validate" in stamped
def test_report_schema_version_is_set() -> None:
assert REPORT_SCHEMA_VERSION