|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import io |
| 4 | +from pathlib import Path |
| 5 | +import textwrap |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from maf_starter.critic import ( |
| 10 | + check_bare_except, |
| 11 | + check_file_size_limit, |
| 12 | + check_missing_telemetry, |
| 13 | + parse_unified_diff, |
| 14 | + run_critic, |
| 15 | +) |
| 16 | +from maf_starter.critic_cli import build_parser, main |
| 17 | + |
| 18 | + |
| 19 | +DIRTY_DIFF = textwrap.dedent( |
| 20 | + """\ |
| 21 | + diff --git a/app.py b/app.py |
| 22 | + +++ b/app.py |
| 23 | + @@ |
| 24 | + +try: |
| 25 | + + do_work() |
| 26 | + +except Exception: |
| 27 | + + pass |
| 28 | + """ |
| 29 | +) |
| 30 | + |
| 31 | +DIRTY_DIFF_WITH_TELEMETRY_GAP = textwrap.dedent( |
| 32 | + """\ |
| 33 | + diff --git a/app.py b/app.py |
| 34 | + +++ b/app.py |
| 35 | + @@ |
| 36 | + +try: |
| 37 | + + do_work() |
| 38 | + +except ValueError: |
| 39 | + + recover() |
| 40 | + """ |
| 41 | +) |
| 42 | + |
| 43 | +CLEAN_DIFF = textwrap.dedent( |
| 44 | + """\ |
| 45 | + diff --git a/app.py b/app.py |
| 46 | + +++ b/app.py |
| 47 | + @@ |
| 48 | + +try: |
| 49 | + + do_work() |
| 50 | + +except Exception as exc: |
| 51 | + + logger.error("failed", exc_info=exc) |
| 52 | + + raise RuntimeError("typed failure") from exc |
| 53 | + """ |
| 54 | +) |
| 55 | + |
| 56 | + |
| 57 | +def test_parse_unified_diff_returns_changed_files_and_added_lines() -> None: |
| 58 | + diff_text = textwrap.dedent( |
| 59 | + """\ |
| 60 | + diff --git a/app.py b/app.py |
| 61 | + +++ b/app.py |
| 62 | + @@ |
| 63 | + +first |
| 64 | + diff --git a/worker.py b/worker.py |
| 65 | + +++ b/worker.py |
| 66 | + @@ |
| 67 | + +second |
| 68 | + """ |
| 69 | + ) |
| 70 | + |
| 71 | + files = parse_unified_diff(diff_text) |
| 72 | + |
| 73 | + assert len(files) == 2 |
| 74 | + assert files[0].path == "app.py" |
| 75 | + assert files[0].added_lines == ["first"] |
| 76 | + assert files[1].path == "worker.py" |
| 77 | + assert files[1].added_lines == ["second"] |
| 78 | + |
| 79 | + |
| 80 | +def test_check_bare_except_blocks_untyped_unlogged_handler() -> None: |
| 81 | + findings = check_bare_except("app.py", ["except Exception:", " pass"]) |
| 82 | + |
| 83 | + assert len(findings) == 1 |
| 84 | + assert findings[0].check == "bare-except" |
| 85 | + assert findings[0].severity == "blocking" |
| 86 | + |
| 87 | + |
| 88 | +def test_check_bare_except_allows_logged_typed_reraise() -> None: |
| 89 | + findings = check_bare_except( |
| 90 | + "app.py", |
| 91 | + [ |
| 92 | + "except Exception as exc:", |
| 93 | + ' logger.error("boom", exc_info=exc)', |
| 94 | + ' raise RuntimeError("typed failure") from exc', |
| 95 | + ], |
| 96 | + ) |
| 97 | + |
| 98 | + assert findings == [] |
| 99 | + |
| 100 | + |
| 101 | +def test_check_missing_telemetry_returns_advisory_only() -> None: |
| 102 | + findings = check_missing_telemetry("app.py", ["except ValueError:", " recover()"]) |
| 103 | + |
| 104 | + assert len(findings) == 1 |
| 105 | + assert findings[0].check == "missing-telemetry" |
| 106 | + assert findings[0].severity == "advisory" |
| 107 | + |
| 108 | + |
| 109 | +def test_check_file_size_limit_flags_large_files() -> None: |
| 110 | + findings = check_file_size_limit("big.py", 401, limit=400) |
| 111 | + |
| 112 | + assert len(findings) == 1 |
| 113 | + assert findings[0].check == "file-size-limit" |
| 114 | + assert findings[0].severity == "advisory" |
| 115 | + |
| 116 | + |
| 117 | +def test_run_critic_blocks_dirty_diff() -> None: |
| 118 | + report = run_critic(DIRTY_DIFF) |
| 119 | + |
| 120 | + assert report.exit_code == 1 |
| 121 | + assert report.blocking_count == 1 |
| 122 | + assert any(finding.check == "bare-except" for finding in report.findings) |
| 123 | + |
| 124 | + |
| 125 | +def test_run_critic_passes_clean_diff() -> None: |
| 126 | + report = run_critic(CLEAN_DIFF) |
| 127 | + |
| 128 | + assert report.exit_code == 0 |
| 129 | + assert report.blocking_count == 0 |
| 130 | + assert report.advisory_count == 0 |
| 131 | + |
| 132 | + |
| 133 | +def test_run_critic_returns_advisory_error_on_malformed_input() -> None: |
| 134 | + report = run_critic("not a diff at all") |
| 135 | + |
| 136 | + assert report.exit_code == 0 |
| 137 | + assert report.blocking_count == 0 |
| 138 | + assert report.advisory_count == 1 |
| 139 | + assert report.findings[0].check == "critic-error" |
| 140 | + |
| 141 | + |
| 142 | +def test_build_parser_defaults_to_blocking_gate() -> None: |
| 143 | + args = build_parser().parse_args(["--diff", "-"]) |
| 144 | + |
| 145 | + assert args.diff == "-" |
| 146 | + assert args.severity_gate == "blocking" |
| 147 | + |
| 148 | + |
| 149 | +def test_main_returns_zero_for_clean_diff_file(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 150 | + diff_path = tmp_path / "clean.diff" |
| 151 | + diff_path.write_text(CLEAN_DIFF, encoding="utf-8") |
| 152 | + |
| 153 | + exit_code = main(["--diff", str(diff_path)]) |
| 154 | + |
| 155 | + out = capsys.readouterr().out |
| 156 | + assert exit_code == 0 |
| 157 | + assert "critic: 0 blocking, 0 advisory" in out |
| 158 | + |
| 159 | + |
| 160 | +def test_main_returns_one_for_blocking_diff_file(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 161 | + diff_path = tmp_path / "dirty.diff" |
| 162 | + diff_path.write_text(DIRTY_DIFF, encoding="utf-8") |
| 163 | + |
| 164 | + exit_code = main(["--diff", str(diff_path)]) |
| 165 | + |
| 166 | + out = capsys.readouterr().out |
| 167 | + assert exit_code == 1 |
| 168 | + assert "[blocking] bare-except" in out |
| 169 | + |
| 170 | + |
| 171 | +def test_main_returns_two_for_missing_diff_path(capsys: pytest.CaptureFixture[str]) -> None: |
| 172 | + exit_code = main(["--diff", "missing.diff"]) |
| 173 | + |
| 174 | + err = capsys.readouterr().err |
| 175 | + assert exit_code == 2 |
| 176 | + assert "unable to read diff" in err |
| 177 | + |
| 178 | + |
| 179 | +def test_main_reads_diff_from_stdin_and_supports_advisory_gate( |
| 180 | + monkeypatch: pytest.MonkeyPatch, |
| 181 | + capsys: pytest.CaptureFixture[str], |
| 182 | +) -> None: |
| 183 | + monkeypatch.setattr("sys.stdin", io.StringIO(DIRTY_DIFF_WITH_TELEMETRY_GAP)) |
| 184 | + |
| 185 | + exit_code = main(["--diff", "-", "--severity-gate", "advisory"]) |
| 186 | + |
| 187 | + out = capsys.readouterr().out |
| 188 | + assert exit_code == 1 |
| 189 | + assert "critic: 0 blocking, 1 advisory" in out |
0 commit comments