forked from ChelseaKR/ceqa-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
413 lines (336 loc) · 13.1 KB
/
Copy pathtest_cli.py
File metadata and controls
413 lines (336 loc) · 13.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""CLI smoke tests."""
import json
from pathlib import Path
from typing import Any
import pytest
from pypdf import PdfWriter
from typer.testing import CliRunner
from ceqa_preflight import __version__
from ceqa_preflight.cli import app
runner = CliRunner()
def _reported_rule_ids(stdout: str) -> tuple[set[str], set[str]]:
"""Return the rule identifiers a JSON report ran, and the ones it says did not run."""
report: dict[str, Any] = json.loads(stdout)
ran = {finding["rule_id"] for finding in report["findings"] + report["manual_review"]}
not_run = {skipped["rule_id"] for skipped in report["not_run"]}
return ran, not_run
def test_version_option() -> None:
result = runner.invoke(app, ["--version"])
assert result.exit_code == 0
assert result.stdout == f"{__version__}\n"
def test_version_command() -> None:
result = runner.invoke(app, ["version"])
assert result.exit_code == 0
assert result.stdout == f"{__version__}\n"
def test_help_discloses_advisory_scope() -> None:
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "does not provide legal advice" in result.stdout
def test_check_writes_json_report(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
document = package / "notice.pdf"
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with document.open("wb") as output:
writer.write(output)
output_directory = tmp_path / "reports"
result = runner.invoke(
app,
[
"check",
str(package),
"--filing-type",
"NOE",
"--format",
"json",
"--output",
str(output_directory),
],
)
assert result.exit_code == 0
assert "Wrote advisory report" in result.stdout
assert '"filing_type": "NOE"' in (output_directory / "report.json").read_text(encoding="utf-8")
def test_check_writes_html_report(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice.pdf").open("wb") as output:
writer.write(output)
output_directory = tmp_path / "reports"
result = runner.invoke(
app,
[
"check",
str(package),
"--filing-type",
"NOE",
"--format",
"html",
"--output",
str(output_directory),
],
)
assert result.exit_code == 0
assert '<html lang="en">' in (output_directory / "report.html").read_text(encoding="utf-8")
def test_rules_list_and_show_are_source_cited() -> None:
listed = runner.invoke(app, ["rules", "list", "--filing-type", "NOE"])
shown = runner.invoke(app, ["rules", "show", "noe-001"])
missing = runner.invoke(app, ["rules", "show", "missing-001"])
assert listed.exit_code == 0
assert "NOE-001" in listed.stdout
assert "NOD-001" not in listed.stdout
assert shown.exit_code == 0
assert '"url": "https://lci.ca.gov/sch/faq/"' in shown.stdout
assert missing.exit_code == 2
assert "Unknown rule identifier" in missing.stderr
def test_check_excludes_filing_pilot_rules_unless_explicitly_requested(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice.pdf").open("wb") as output:
writer.write(output)
manifest = tmp_path / "package.yaml"
manifest.write_text(
"filing_type: NOE\nproject:\n title: Example Project\ndocuments:\n - path: notice.pdf\n"
" category: Notice of Exemption\n primary: true\n",
encoding="utf-8",
)
base_arguments = [
"check",
str(package),
"--filing-type",
"NOE",
"--manifest",
str(manifest),
"--format",
"json",
]
default = runner.invoke(app, base_arguments)
experimental = runner.invoke(app, [*base_arguments, "--include-experimental"])
assert default.exit_code == 0
default_ran, default_not_run = _reported_rule_ids(default.stdout)
assert "NOE-001" not in default_ran
# The skipped filing rules are named in the report itself, not left to be inferred.
assert {"NOE-001", "NOE-002", "NOE-003"} <= default_not_run
assert experimental.exit_code == 0
experimental_ran, experimental_not_run = _reported_rule_ids(experimental.stdout)
assert "NOE-001" in experimental_ran
assert experimental_not_run == set()
def test_init_creates_a_non_overwriting_template(tmp_path: Path) -> None:
directory = tmp_path / "new-package"
created = runner.invoke(app, ["init", str(directory), "--filing-type", "NOD"])
existing = runner.invoke(app, ["init", str(directory), "--filing-type", "NOD"])
assert created.exit_code == 0
assert "Created manifest template" in created.stdout
assert "filing_type: NOD" in (directory / "package.yaml").read_text(encoding="utf-8")
assert existing.exit_code == 2
assert "refusing to overwrite" in existing.stderr
def test_pilot_commands_create_and_summarize_controlled_label_files(tmp_path: Path) -> None:
result = runner.invoke(app, ["pilot", "init", str(tmp_path)])
reviews = tmp_path / "finding-review.csv"
baseline = tmp_path / "manual-baseline.csv"
reviews.write_text(
"package_id,filing_type,rule_id,finding_status,disposition,severity,elapsed_seconds\n"
"PKG_001,NOE,NOE-001,warning,true_positive,medium,60\n",
encoding="utf-8",
)
baseline.write_text(
"package_id,filing_type,severity,was_missed\nPKG_001,NOE,high,false\n",
encoding="utf-8",
)
summary = runner.invoke(
app,
[
"pilot",
"summarize",
"--reviews",
str(reviews),
"--baseline",
str(baseline),
"--format",
"json",
],
)
assert result.exit_code == 0
assert summary.exit_code == 0
assert '"go_no_go": "go"' in summary.stdout
def test_synth_creates_a_checkable_package_and_refuses_reuse(tmp_path: Path) -> None:
directory = tmp_path / "synthetic"
created = runner.invoke(
app,
["synth", str(directory), "--filing-type", "NOE", "--defect", "scanned"],
)
reused = runner.invoke(app, ["synth", str(directory), "--filing-type", "NOE"])
assert created.exit_code == 0
assert "synthetic NOE package" in created.stdout
assert (directory / "package.yaml").exists()
assert reused.exit_code == 2
assert "refusing to write" in reused.stderr
def test_check_supports_batch_sources_with_rollup_summary(tmp_path: Path) -> None:
for name in ("one", "two"):
package = tmp_path / name
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / f"{name}_notice_document.pdf").open("wb") as output:
writer.write(output)
output_directory = tmp_path / "reports"
result = runner.invoke(
app,
[
"check",
str(tmp_path / "one"),
str(tmp_path / "two"),
"--filing-type",
"NOE",
"--format",
"json",
"--output",
str(output_directory),
],
)
assert result.exit_code == 0
assert "Batch summary" in result.stdout
assert (output_directory / "report-01-one.json").exists()
assert (output_directory / "report-02-two.json").exists()
def test_check_rejects_manifest_with_multiple_sources(tmp_path: Path) -> None:
for name in ("one", "two"):
(tmp_path / name).mkdir()
result = runner.invoke(
app,
[
"check",
str(tmp_path / "one"),
str(tmp_path / "two"),
"--filing-type",
"NOE",
"--manifest",
str(tmp_path / "package.yaml"),
],
)
assert result.exit_code == 2
def test_check_rule_selection_flags(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice.pdf").open("wb") as output:
writer.write(output)
base = ["check", str(package), "--filing-type", "NOE", "--format", "json"]
only = runner.invoke(app, [*base, "--rules", "core-001"])
excluded = runner.invoke(app, [*base, "--exclude-rules", "CORE-001"])
unknown = runner.invoke(app, [*base, "--rules", "NOPE-001"])
empty = runner.invoke(app, [*base, "--rules", " , "])
assert only.exit_code == 0
only_ran, only_not_run = _reported_rule_ids(only.stdout)
assert only_ran == {"CORE-001"}
assert "PDF-001" in only_not_run
assert excluded.exit_code == 0
excluded_ran, excluded_not_run = _reported_rule_ids(excluded.stdout)
assert "CORE-001" not in excluded_ran
assert "CORE-001" in excluded_not_run
assert unknown.exit_code == 2
assert "unknown rule identifier" in unknown.stderr
assert empty.exit_code == 2
experimental_only = runner.invoke(app, [*base, "--rules", "NOE-001"])
assert experimental_only.exit_code == 2
assert "--include-experimental" in experimental_only.stderr
@pytest.mark.parametrize("output_format", ["console", "json", "html", "checklist"])
def test_no_output_format_reports_a_clean_pass_while_hiding_a_skipped_check(
tmp_path: Path, output_format: str
) -> None:
"""A clean-looking report in any format must still name the checks that did not run.
Excluding a rule leaves a report with no failures and exit code 0. Without the
disclosure below, that report is indistinguishable from one where every check ran,
and the reader's next step is submission.
"""
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice_of_exemption.pdf").open("wb") as output:
writer.write(output)
result = runner.invoke(
app,
[
"check",
str(package),
"--filing-type",
"NOE",
"--format",
output_format,
"--exclude-rules",
"PDF-003",
],
)
assert result.exit_code == 0
assert "PDF-003" in result.stdout
assert "did not run" in result.stdout
if output_format == "json":
_, not_run = _reported_rule_ids(result.stdout)
assert not_run == {"PDF-003", "NOE-001", "NOE-002", "NOE-003"} | {
"NOE-M001",
"NOE-M002",
"NOE-M003",
}
else:
assert "6 check(s) not run" not in result.stdout # PDF-003 plus the six pilot rules
assert "7 check(s) not run" in result.stdout
def test_a_report_with_no_skipped_checks_says_so(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice_of_exemption.pdf").open("wb") as output:
writer.write(output)
result = runner.invoke(
app,
["check", str(package), "--filing-type", "NOE", "--include-experimental"],
)
assert result.exit_code == 0
assert "0 check(s) not run" in result.stdout
assert "Every check that applies to this filing type ran." in result.stdout
def test_check_renders_a_printable_checklist(tmp_path: Path) -> None:
package = tmp_path / "package"
package.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (package / "notice.pdf").open("wb") as output:
writer.write(output)
result = runner.invoke(
app,
["check", str(package), "--filing-type", "NOE", "--format", "checklist"],
)
assert result.exit_code == 0
assert "CEQA Preflight pre-submission checklist" in result.stdout
assert "Resolve before submission" in result.stdout
def test_init_from_package_prepopulates_existing_pdfs(tmp_path: Path) -> None:
directory = tmp_path / "existing-package"
directory.mkdir()
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with (directory / "NOD_project_form.pdf").open("wb") as output:
writer.write(output)
populated = runner.invoke(
app, ["init", str(directory), "--filing-type", "NOD", "--from-package"]
)
empty_directory = tmp_path / "empty-package"
empty_directory.mkdir()
empty = runner.invoke(
app, ["init", str(empty_directory), "--filing-type", "NOD", "--from-package"]
)
assert populated.exit_code == 0
manifest_text = (directory / "package.yaml").read_text(encoding="utf-8")
assert "NOD_project_form.pdf" in manifest_text
assert "REPLACE_WITH_FORM" not in manifest_text
assert empty.exit_code == 2
assert "no PDF files were found" in empty.stderr
def test_rules_list_supports_json_output() -> None:
result = runner.invoke(app, ["rules", "list", "--format", "json"])
invalid = runner.invoke(app, ["rules", "list", "--format", "yaml"])
assert result.exit_code == 0
assert '"id": "CORE-001"' in result.stdout
assert invalid.exit_code == 2