Skip to content

Commit 6a2b285

Browse files
Fix: Replaced with AST helpers
1 parent f4609bf commit 6a2b285

1 file changed

Lines changed: 45 additions & 7 deletions

File tree

tests/test_cli_args.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pytest tests/test_cli_args.py -v
1010
"""
1111

12+
import ast
1213
import sys
1314
import os
1415
import importlib
@@ -23,6 +24,43 @@
2324
from scripts.export import build_parser
2425

2526

27+
def _is_app_run_call(node: ast.AST) -> bool:
28+
if not isinstance(node, ast.Call):
29+
return False
30+
func = node.func
31+
return (
32+
isinstance(func, ast.Attribute)
33+
and func.attr == "run"
34+
and isinstance(func.value, ast.Name)
35+
and func.value.id == "app"
36+
)
37+
38+
39+
def _call_passes_hardcoded_debug_true(call: ast.Call) -> bool:
40+
"""True if this Call passes literal True for debug (kwarg or positional)."""
41+
for kw in call.keywords:
42+
if kw.arg == "debug" and isinstance(kw.value, ast.Constant) and kw.value.value is True:
43+
return True
44+
for arg in call.args:
45+
if isinstance(arg, ast.Constant) and arg.value is True:
46+
return True
47+
return False
48+
49+
50+
def _debug_kwarg_uses_args(call: ast.Call) -> bool:
51+
for kw in call.keywords:
52+
if kw.arg != "debug":
53+
continue
54+
val = kw.value
55+
return (
56+
isinstance(val, ast.Attribute)
57+
and isinstance(val.value, ast.Name)
58+
and val.value.id == "args"
59+
and val.attr == "debug"
60+
)
61+
return False
62+
63+
2664
# ---------------------------------------------------------------------------
2765
# export.py argument tests
2866
# ---------------------------------------------------------------------------
@@ -249,14 +287,14 @@ def test_debug_explicit_true(self):
249287
assert args.debug is True
250288

251289
def test_app_py_debug_not_hardcoded_true(self):
252-
"""app.run() must not pass debug=True unconditionally."""
290+
"""app.run() must wire debug from args, not a literal True."""
253291
app_path = os.path.join(REPO_ROOT, "app.py")
254-
with open(app_path, "r", encoding="utf-8") as f:
255-
src = f.read()
256-
run_block_start = src.find("app.run(")
257-
assert run_block_start != -1
258-
run_block = src[run_block_start : src.find(")", run_block_start) + 1]
259-
assert "debug=True" not in run_block
292+
with open(app_path, encoding="utf-8") as f:
293+
tree = ast.parse(f.read(), filename=app_path)
294+
app_run_calls = [n for n in ast.walk(tree) if _is_app_run_call(n)]
295+
assert app_run_calls, "expected at least one app.run() call in app.py"
296+
assert not any(_call_passes_hardcoded_debug_true(c) for c in app_run_calls)
297+
assert any(_debug_kwarg_uses_args(c) for c in app_run_calls)
260298

261299
def test_port_default(self):
262300
parser = self._build_parser()

0 commit comments

Comments
 (0)