|
9 | 9 | pytest tests/test_cli_args.py -v |
10 | 10 | """ |
11 | 11 |
|
| 12 | +import ast |
12 | 13 | import sys |
13 | 14 | import os |
14 | 15 | import importlib |
|
23 | 24 | from scripts.export import build_parser |
24 | 25 |
|
25 | 26 |
|
| 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 | + |
26 | 64 | # --------------------------------------------------------------------------- |
27 | 65 | # export.py argument tests |
28 | 66 | # --------------------------------------------------------------------------- |
@@ -249,14 +287,14 @@ def test_debug_explicit_true(self): |
249 | 287 | assert args.debug is True |
250 | 288 |
|
251 | 289 | 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.""" |
253 | 291 | 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) |
260 | 298 |
|
261 | 299 | def test_port_default(self): |
262 | 300 | parser = self._build_parser() |
|
0 commit comments