forked from ChelseaKR/tods-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_severity_remap.py
More file actions
305 lines (224 loc) · 10.8 KB
/
Copy pathtest_severity_remap.py
File metadata and controls
305 lines (224 loc) · 10.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""The `[severity]` config remap table and its mandatory disclosure.
TODS-W101 is a WARNING-band rule (structure.py); TODS-E201 is an ERROR-band
rule (fields.py). Both have dedicated fixtures under tests/fixtures/invalid/.
"""
from pathlib import Path
import pytest
from click.testing import CliRunner
from conftest import FIXTURES
from tods_validate.cli import main
from tods_validate.config import Config, ConfigError, load_config
from tods_validate.findings import Finding, Severity
from tods_validate.report import (
render_github,
render_html,
render_markdown,
render_sarif,
render_text,
)
from tods_validate.runner import run
def invoke(*args: str):
return CliRunner().invoke(main, list(args))
# --- config parsing -------------------------------------------------------
def test_severity_table_upgrades_warning_to_error(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
config = load_config(path)
assert config.severity_remap == (("TODS-W101", "ERROR"),)
assert config.severity_acknowledged == frozenset()
def test_severity_table_rejects_unknown_rule_id(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('[severity]\n"TODS-E999" = "warning"\n', encoding="utf-8")
with pytest.raises(ConfigError, match="unknown rule ID"):
load_config(path)
def test_severity_table_rejects_bad_level(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('[severity]\n"TODS-W101" = "critical"\n', encoding="utf-8")
with pytest.raises(ConfigError, match="level must be one of"):
load_config(path)
def test_severity_table_must_be_a_table(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text("severity = 3\n", encoding="utf-8")
with pytest.raises(ConfigError, match="must be a table"):
load_config(path)
def test_downgrading_error_rule_without_acknowledged_raises(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('[severity]\n"TODS-E201" = "warning"\n', encoding="utf-8")
with pytest.raises(ConfigError, match="acknowledged"):
load_config(path)
def test_downgrading_error_rule_with_acknowledged_succeeds(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text(
'[severity]\n"TODS-E201" = {level = "warning", acknowledged = true}\n', encoding="utf-8"
)
config = load_config(path)
assert config.severity_remap == (("TODS-E201", "WARNING"),)
assert config.severity_acknowledged == frozenset({"TODS-E201"})
def test_severity_table_inline_table_rejects_unknown_key(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('[severity]\n"TODS-W101" = {level = "info", bogus = true}\n', encoding="utf-8")
with pytest.raises(ConfigError, match="unknown key"):
load_config(path)
def test_merge_severity_remap_override_wins(tmp_path: Path) -> None:
from tods_validate.config import _merge, _parse_data
base = _parse_data({"severity": {"TODS-W101": "info"}}, "base")
override = _parse_data({"severity": {"TODS-W101": "error"}}, "override")
merged = _merge(base, override)
assert merged.severity_remap == (("TODS-W101", "ERROR"),)
def test_config_equality_still_works() -> None:
assert Config() == Config()
# --- runner application ----------------------------------------------------
def test_runner_upgrades_warning_and_records_original() -> None:
_, findings = run(FIXTURES / "invalid" / "TODS-W101", severity_remap={"TODS-W101": "ERROR"})
hit = [f for f in findings if f.rule_id == "TODS-W101"]
assert hit
for f in hit:
assert f.severity is Severity.ERROR
assert f.severity_original is Severity.WARNING
def test_runner_leaves_unmapped_rules_alone() -> None:
_, findings = run(FIXTURES / "invalid" / "TODS-W101", severity_remap={"TODS-E999": "INFO"})
hit = [f for f in findings if f.rule_id == "TODS-W101"]
assert hit
for f in hit:
assert f.severity_original is None
def test_runner_no_remap_when_mapped_severity_matches_original() -> None:
_, findings = run(FIXTURES / "invalid" / "TODS-W101", severity_remap={"TODS-W101": "WARNING"})
hit = [f for f in findings if f.rule_id == "TODS-W101"]
assert hit
for f in hit:
assert f.severity_original is None
# --- CLI end-to-end: exit code changes with an upgrade remap --------------
def test_cli_upgrade_warning_to_error_changes_exit_code(tmp_path: Path) -> None:
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
result = invoke(
str(FIXTURES / "invalid" / "TODS-W101"), "--config", str(config), "--fail-on", "error"
)
assert result.exit_code == 1, result.output
assert "ERROR TODS-W101" in result.output
def test_cli_without_remap_warning_does_not_fail_on_error(tmp_path: Path) -> None:
result = invoke(str(FIXTURES / "invalid" / "TODS-W101"), "--fail-on", "error")
assert result.exit_code == 0, result.output
def test_cli_unknown_severity_rule_id_exits_two(tmp_path: Path) -> None:
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-E999" = "warning"\n', encoding="utf-8")
result = invoke(str(FIXTURES / "valid" / "tods"), "--config", str(config))
assert result.exit_code == 2
assert "TODS-E999" in result.output
# --- disclosure across every output format ---------------------------------
FINDING = Finding(
rule_id="TODS-W101",
severity=Severity.ERROR,
file="run_events.txt",
row=3,
message="Example message.",
severity_original=Severity.WARNING,
)
ACK_FINDING = Finding(
rule_id="TODS-E201",
severity=Severity.WARNING,
file="run_events.txt",
row=4,
message="Example message two.",
severity_original=Severity.ERROR,
)
def test_text_report_discloses_remap() -> None:
text = render_text([FINDING], "feed/")
assert "Local policy: 1 severity remapped" in text
assert "TODS-W101: WARNING -> ERROR" in text
assert "(spec: WARNING)" in text
def test_text_report_discloses_multiple_remaps_pluralized() -> None:
text = render_text([FINDING, ACK_FINDING], "feed/")
assert "Local policy: 2 severities remapped" in text
assert "TODS-E201: ERROR -> WARNING (acknowledged)" in text
def test_markdown_report_discloses_remap() -> None:
out = render_markdown([FINDING], "feed/")
assert "Local policy: 1 severity remapped" in out
assert "(spec: WARNING)" in out
def test_markdown_stamp_output_discloses_remap() -> None:
out = render_markdown([FINDING], "feed/", stamp=True)
assert "Local policy: 1 severity remapped" in out
assert "_Generated by tods-validate" in out
def test_json_to_dict_carries_severity_original() -> None:
payload = FINDING.to_dict()
assert payload["severity_original"] == "WARNING"
clean = Finding(rule_id="TODS-I102", severity=Severity.INFO, message="x")
assert clean.to_dict()["severity_original"] is None
def test_github_annotations_disclose_remap() -> None:
out = render_github([FINDING], "feed/")
assert "(spec: WARNING)" in out
assert "Local policy: 1 severity remapped" in out
def test_sarif_discloses_remap() -> None:
import json
payload = json.loads(render_sarif([FINDING], "feed/"))
result = payload["runs"][0]["results"][0]
assert result["properties"]["severityOriginal"] == "WARNING"
assert payload["runs"][0]["properties"]["severityRemapDisclosure"]
def test_html_report_discloses_remap() -> None:
out = render_html([FINDING], "feed/")
assert "Local policy: 1 severity remapped" in out
def test_no_disclosure_when_nothing_remapped() -> None:
clean = Finding(rule_id="TODS-I102", severity=Severity.INFO, message="ok")
assert "Local policy" not in render_text([clean], "feed/")
assert "Local policy" not in render_markdown([clean], "feed/")
assert "Local policy" not in render_github([clean], "feed/")
assert "Local policy" not in render_html([clean], "feed/")
def test_cli_json_report_carries_severity_original_field(tmp_path: Path) -> None:
import json
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
result = invoke(
str(FIXTURES / "invalid" / "TODS-W101"), "--config", str(config), "--format", "json"
)
payload = json.loads(result.output)
hits = [f for f in payload["findings"] if f["rule_id"] == "TODS-W101"]
assert hits
assert all(f["severity_original"] == "WARNING" for f in hits)
def test_cli_stamp_output_discloses_remap(tmp_path: Path) -> None:
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
result = invoke(
str(FIXTURES / "invalid" / "TODS-W101"),
"--config",
str(config),
"--format",
"markdown",
"--stamp",
)
assert "Local policy: " in result.output
assert "severity remapped" in result.output or "severities remapped" in result.output
# --- diff/batch honor the same [severity] policy as validate ----------------
def test_batch_applies_remap_to_gate(tmp_path: Path) -> None:
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
fixture = str(FIXTURES / "invalid" / "TODS-W101")
without = invoke("batch", fixture, "--fail-on", "error")
assert without.exit_code == 0, without.output
with_remap = invoke("batch", fixture, "--config", str(config), "--fail-on", "error")
assert with_remap.exit_code == 1, with_remap.output
def test_diff_applies_remap_to_gate(tmp_path: Path) -> None:
config = tmp_path / "tods-validate.toml"
config.write_text('[severity]\n"TODS-W101" = "error"\n', encoding="utf-8")
old = str(FIXTURES / "valid" / "tods")
new = str(FIXTURES / "invalid" / "TODS-W101")
without = invoke("diff", old, new, "--fail-on", "error")
assert without.exit_code == 0, without.output
with_remap = invoke("diff", old, new, "--config", str(config), "--fail-on", "error")
assert with_remap.exit_code == 1, with_remap.output
# --- disclosure stays bounded when one rule fires many times ----------------
def test_disclosure_dedupes_repeated_rule_lines() -> None:
repeats = [
Finding(
rule_id="TODS-W101",
severity=Severity.ERROR,
file="run_events.txt",
row=row,
message=f"Example message {row}.",
severity_original=Severity.WARNING,
)
for row in range(2, 5)
]
text = render_text(repeats, "feed/")
assert "Local policy: 3 severities remapped:" in text
assert text.count("TODS-W101: WARNING -> ERROR") == 1
assert "TODS-W101: WARNING -> ERROR ×3" in text