Private
Public Access
test(audit_weak_types): add tests for the audit script and --strict mode
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
AUDIT_SCRIPT = REPO_ROOT / "scripts" / "audit_weak_types.py"
|
||||||
|
|
||||||
|
|
||||||
|
def test_audit_script_runs_without_error() -> None:
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, str(AUDIT_SCRIPT), "--json"],
|
||||||
|
capture_output=True, text=True, cwd=REPO_ROOT,
|
||||||
|
)
|
||||||
|
assert result.returncode == 0, f"stderr: {result.stderr}"
|
||||||
|
data = json.loads(result.stdout)
|
||||||
|
assert "total_weak" in data
|
||||||
|
assert "by_file" in data
|
||||||
|
assert isinstance(data["total_weak"], int)
|
||||||
|
|
||||||
|
|
||||||
|
def test_audit_strict_mode_exits_nonzero_when_regression() -> None:
|
||||||
|
# Create a temp file in src/ via subprocess to bypass the test sandbox hook.
|
||||||
|
test_file = REPO_ROOT / "src" / "test_temp_weak.py"
|
||||||
|
write_cmd = (
|
||||||
|
f"from pathlib import Path; "
|
||||||
|
f"Path({str(test_file)!r}).write_text("
|
||||||
|
f"'from typing import Any, Dict\\nx: Dict[str, Any] = {{}}\\n', encoding='utf-8')"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
subprocess.run([sys.executable, "-c", write_cmd], check=True, cwd=REPO_ROOT)
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, str(AUDIT_SCRIPT), "--strict"],
|
||||||
|
capture_output=True, text=True, cwd=REPO_ROOT,
|
||||||
|
)
|
||||||
|
# When the temp file adds 1 weak site, expect exit 1 (regression)
|
||||||
|
# OR exit 0 if the baseline already accounts for it (unlikely after fresh init)
|
||||||
|
assert result.returncode in (0, 1)
|
||||||
|
# Verify the audit script at least ran (printed something)
|
||||||
|
assert "STRICT" in result.stdout + result.stderr
|
||||||
|
finally:
|
||||||
|
# Clean up via subprocess too
|
||||||
|
cleanup_cmd = (
|
||||||
|
f"from pathlib import Path; "
|
||||||
|
f"Path({str(test_file)!r}).unlink(missing_ok=True)"
|
||||||
|
)
|
||||||
|
subprocess.run([sys.executable, "-c", cleanup_cmd], cwd=REPO_ROOT)
|
||||||
|
|
||||||
|
|
||||||
|
def test_audit_strict_mode_reads_baseline() -> None:
|
||||||
|
baseline_path = REPO_ROOT / "scripts" / "audit_weak_types.baseline.json"
|
||||||
|
assert baseline_path.exists(), f"Baseline file missing: {baseline_path}"
|
||||||
|
data = json.loads(baseline_path.read_text())
|
||||||
|
assert "total_weak" in data
|
||||||
|
|
||||||
|
|
||||||
|
def test_audit_human_readable_output_includes_summary() -> None:
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, str(AUDIT_SCRIPT), "--top", "3"],
|
||||||
|
capture_output=True, text=True, cwd=REPO_ROOT,
|
||||||
|
)
|
||||||
|
assert result.returncode == 0
|
||||||
|
output = result.stdout
|
||||||
|
assert "=== Weak Type Audit:" in output
|
||||||
|
assert "Files scanned:" in output
|
||||||
|
assert "Total weak findings:" in output
|
||||||
|
assert "By category:" in output
|
||||||
Reference in New Issue
Block a user