from __future__ import annotations from pathlib import Path import subprocess import sys import unittest REPOSITORY_ROOT = Path(__file__).resolve().parents[2] CHECK_CODE_CALLS = REPOSITORY_ROOT / "scripts" / "check_code_calls.py" FIXTURES = REPOSITORY_ROOT / "scripts" / "tests" / "fixtures" / "code-calls" def run_check(target: Path) -> subprocess.CompletedProcess[str]: return subprocess.run( [sys.executable, str(CHECK_CODE_CALLS), str(target)], cwd=REPOSITORY_ROOT, text=True, capture_output=True, check=False, ) class CheckCodeCallsTest(unittest.TestCase): def test_accepts_valid_fixture_and_current_p0_tree(self) -> None: valid_fixture = run_check(FIXTURES / "valid") current_p0 = run_check(REPOSITORY_ROOT / "backend" / "src" / "ploop") self.assertEqual(valid_fixture.returncode, 0, valid_fixture.stdout + valid_fixture.stderr) self.assertIn("violations=0", valid_fixture.stdout) self.assertEqual(current_p0.returncode, 0, current_p0.stdout + current_p0.stderr) def test_reports_re_evaluate_runner_dependency(self) -> None: result = run_check(FIXTURES / "violations" / "call001") self.assertNotEqual(result.returncode, 0) self.assertIn("CALL001", result.stdout) self.assertIn("api/re_evaluate.py", result.stdout) self.assertRegex(result.stdout, r"\.py:\d+:") self.assertIn(";", result.stdout) def test_reports_api_and_worker_direct_process_or_redis_calls(self) -> None: for fixture_name in ("call002-api", "call002-worker"): with self.subTest(fixture_name=fixture_name): result = run_check(FIXTURES / "violations" / fixture_name) self.assertNotEqual(result.returncode, 0) self.assertIn("CALL002", result.stdout) self.assertRegex(result.stdout, r"\.py:\d+:") self.assertIn(";", result.stdout) if __name__ == "__main__": unittest.main()