-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_changed_file_syntax_gate.py
More file actions
194 lines (146 loc) · 7.38 KB
/
Copy pathtest_changed_file_syntax_gate.py
File metadata and controls
194 lines (146 loc) · 7.38 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
"""Tests for the changed-file syntax gate."""
from __future__ import annotations
import runpy
import subprocess
import sys
import pytest
from scripts.ci import changed_file_syntax_gate as gate
def write(tmp_path, name, content):
"""Write a file under tmp_path and return its path."""
path = tmp_path / name
path.write_text(content, encoding="utf-8")
return path
def test_check_python_accepts_valid_and_rejects_invalid(tmp_path):
"""Valid Python parses; a syntax error is reported with its line."""
good = write(tmp_path, "good.py", "def f():\n return 1\n")
bad = write(tmp_path, "bad.py", "def f(:\n return 1\n")
assert gate.check_python(good) == (gate.OK, "")
result, detail = gate.check_python(bad)
assert result == gate.FAILED
assert "line" in detail
def test_check_with_command_skips_when_tool_missing(tmp_path, monkeypatch):
"""A missing toolchain skips (never fails) the file."""
monkeypatch.setattr(gate.shutil, "which", lambda tool: None)
result, detail = gate.check_with_command("node", ["node", "--check", "x"], tmp_path)
assert result == gate.SKIPPED
assert "not available" in detail
def test_check_with_command_reports_ok_and_failure(tmp_path, monkeypatch):
"""A present tool returns ok on success and failed on non-zero exit."""
monkeypatch.setattr(gate.shutil, "which", lambda tool: "/usr/bin/" + tool)
def fake_run_ok(cmd, **kwargs):
assert kwargs["timeout"] == gate.COMMAND_TIMEOUT_SECONDS
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(gate.subprocess, "run", fake_run_ok)
assert gate.check_with_command("node", ["node"], tmp_path) == (gate.OK, "")
def fake_run_fail(cmd, **kwargs):
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="Unexpected token }")
monkeypatch.setattr(gate.subprocess, "run", fake_run_fail)
result, detail = gate.check_with_command("node", ["node"], tmp_path)
assert result == gate.FAILED
assert "Unexpected token" in detail
def test_check_with_command_skips_when_tool_times_out(tmp_path, monkeypatch):
"""A hung syntax tool is skipped with an explicit reason."""
monkeypatch.setattr(gate.shutil, "which", lambda tool: "/usr/bin/" + tool)
def fake_timeout(cmd, **kwargs):
raise subprocess.TimeoutExpired(cmd, kwargs["timeout"])
monkeypatch.setattr(gate.subprocess, "run", fake_timeout)
result, detail = gate.check_with_command("bash", ["bash"], tmp_path)
assert result == gate.SKIPPED
assert "timed out after" in detail
def test_check_with_command_detail_fallbacks(tmp_path, monkeypatch):
"""Failure detail falls back to stdout, then a generic message."""
monkeypatch.setattr(gate.shutil, "which", lambda tool: "/usr/bin/" + tool)
def stdout_only(cmd, **kwargs):
return subprocess.CompletedProcess(cmd, 2, stdout="parse error here", stderr="")
monkeypatch.setattr(gate.subprocess, "run", stdout_only)
_, detail = gate.check_with_command("bash", ["bash"], tmp_path)
assert detail == "parse error here"
def empty_output(cmd, **kwargs):
return subprocess.CompletedProcess(cmd, 2, stdout="", stderr="")
monkeypatch.setattr(gate.subprocess, "run", empty_output)
_, detail = gate.check_with_command("bash", ["bash"], tmp_path)
assert "reported a syntax error" in detail
def test_check_shell_uses_bash_n(tmp_path, monkeypatch):
"""A real bash -n check flags a broken shell script when bash exists."""
bad = write(tmp_path, "bad.sh", "if then fi\n")
bash = gate.shutil.which("bash")
if bash is None: # pragma: no cover - CI always has bash
pytest.skip("bash not available")
monkeypatch.setattr(gate, "COMMAND_TIMEOUT_SECONDS", 3)
result, detail = gate.check_shell(bad)
if result == gate.SKIPPED and "timed out after" in detail:
pytest.skip(detail)
assert result == gate.FAILED
good = write(tmp_path, "good.sh", "echo hello\n")
assert gate.check_shell(good) == (gate.OK, "")
def test_check_javascript_dispatch(tmp_path, monkeypatch):
"""check_javascript routes through node --check."""
calls = {}
def fake(tool, command, path):
calls["tool"] = tool
calls["command"] = command
return (gate.OK, "")
monkeypatch.setattr(gate, "check_with_command", fake)
assert gate.check_javascript(tmp_path / "a.js") == (gate.OK, "")
assert calls["tool"] == "node"
assert calls["command"][:2] == ["node", "--check"]
def test_check_changed_file_skips_missing_and_unknown(tmp_path):
"""Deleted paths and unrecognized extensions are skipped, not failed."""
missing, _ = gate.check_changed_file(tmp_path / "gone.py")
assert missing == gate.SKIPPED
unknown = write(tmp_path, "notes.rs", "fn main() {")
result, detail = gate.check_changed_file(unknown)
assert result == gate.SKIPPED
assert "no unambiguous" in detail
def test_check_changed_file_dispatches_python(tmp_path):
"""A known extension dispatches to its checker."""
bad = write(tmp_path, "bad.py", "x = (")
result, _ = gate.check_changed_file(bad)
assert result == gate.FAILED
def test_read_changed_files_ignores_blank_lines(tmp_path):
"""Blank and whitespace-only lines are dropped."""
listing = write(tmp_path, "changed.txt", "a.py\n\n \n b.py \n")
assert gate.read_changed_files(listing) == ["a.py", "b.py"]
def test_run_gate_aggregates_results(tmp_path):
"""run_gate returns failures plus checked and skipped counts."""
good = write(tmp_path, "good.py", "x = 1\n")
bad = write(tmp_path, "bad.py", "x = (\n")
skipped = write(tmp_path, "data.rs", "??")
failures, checked, skipped_count = gate.run_gate(
[str(good), str(bad), str(skipped), str(tmp_path / "missing.py")]
)
assert checked == 2
assert skipped_count == 2
assert len(failures) == 1
assert failures[0][0] == str(bad)
def test_format_report_lists_failures():
"""The report names each failing file and the counts."""
report = gate.format_report([("bad.py", "invalid syntax (line 1)")], 3, 1)
assert "3 checked, 1 skipped, 1 failed" in report
assert "SYNTAX ERROR bad.py" in report
clean = gate.format_report([], 2, 0)
assert "0 failed" in clean
def test_main_returns_zero_when_clean(tmp_path, capsys):
"""main exits 0 when no changed file has a syntax error."""
good = write(tmp_path, "good.py", "x = 1\n")
listing = write(tmp_path, "changed.txt", f"{good}\n")
assert gate.main(["--changed-files-file", str(listing)]) == 0
assert "0 failed" in capsys.readouterr().out
def test_main_returns_one_on_syntax_error(tmp_path, capsys):
"""main exits 1 and reports the offending file on a syntax error."""
bad = write(tmp_path, "bad.py", "def broken(:\n")
listing = write(tmp_path, "changed.txt", f"{bad}\n")
assert gate.main(["--changed-files-file", str(listing)]) == 1
assert "SYNTAX ERROR" in capsys.readouterr().out
def test_module_entrypoint_invokes_main(tmp_path, monkeypatch):
"""Exercise the __main__ entrypoint used by the workflow step."""
good = write(tmp_path, "good.py", "x = 1\n")
listing = write(tmp_path, "changed.txt", f"{good}\n")
monkeypatch.setattr(
sys,
"argv",
["changed_file_syntax_gate.py", "--changed-files-file", str(listing)],
)
with pytest.raises(SystemExit) as excinfo:
runpy.run_path(gate.__file__, run_name="__main__")
assert excinfo.value.code == 0