| 1 | import json |
| 2 | import os |
| 3 | import pathlib |
| 4 | import runpy |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import unittest |
| 8 | |
| 9 | BASE = pathlib.Path(__file__).resolve().parent |
| 10 | CHECKER = BASE / "static_check.py" |
| 11 | CODE = """ |
| 12 | import json, pathlib, runpy, sys |
| 13 | from unittest.mock import patch |
| 14 | scope = runpy.run_path(sys.argv[1]) |
| 15 | calls = [] |
| 16 | def blocked(*args, **kwargs): |
| 17 | calls.append("side_effect") |
| 18 | raise RuntimeError("side_effect") |
| 19 | with patch("argparse.ArgumentParser", blocked), \ |
| 20 | patch.object(pathlib.Path, "read_bytes", blocked), \ |
| 21 | patch.object(pathlib.Path, "read_text", blocked), \ |
| 22 | patch.object(runpy, "run_path", blocked): |
| 23 | try: |
| 24 | scope["main"]() |
| 25 | except RuntimeError as error: |
| 26 | print(json.dumps({"message": str(error), "calls": calls, "optimize": sys.flags.optimize})) |
| 27 | """ |
| 28 | |
| 29 | |
| 30 | class StaticHelperTests(unittest.TestCase): |
| 31 | def child(self, environment_mode=False, flag=False, cli=False): |
| 32 | environment = os.environ.copy() |
| 33 | environment.pop("PYTHONOPTIMIZE", None) |
| 34 | environment["PYTHONDONTWRITEBYTECODE"] = "1" |
| 35 | if environment_mode: |
| 36 | environment["PYTHONOPTIMIZE"] = "1" |
| 37 | command = [sys.executable, "-B"] + (["-O"] if flag else []) |
| 38 | command += [str(CHECKER)] if cli else ["-c", CODE, str(CHECKER)] |
| 39 | return subprocess.run(command, env=environment, cwd=BASE, |
| 40 | capture_output=True, text=True, timeout=10) |
| 41 | |
| 42 | def probe(self, **mode): |
| 43 | result = self.child(**mode) |
| 44 | self.assertEqual(result.returncode, 0, result.stderr) |
| 45 | self.assertEqual(result.stderr, "") |
| 46 | self.assertEqual(json.loads(result.stdout), { |
| 47 | "message": "최적화 모드에서는 정적 검사를 실행할 수 없음", |
| 48 | "calls": [], "optimize": 1, |
| 49 | }) |
| 50 | |
| 51 | def test_flag_stops_before_reads_arguments_and_generation(self): |
| 52 | self.probe(flag=True) |
| 53 | |
| 54 | def test_environment_stops_before_reads_arguments_and_generation(self): |
| 55 | self.probe(environment_mode=True) |
| 56 | |
| 57 | def test_normal_mode_reaches_parser(self): |
| 58 | result = self.child() |
| 59 | self.assertEqual(result.returncode, 0, result.stderr) |
| 60 | self.assertEqual(json.loads(result.stdout), { |
| 61 | "message": "side_effect", "calls": ["side_effect"], "optimize": 0, |
| 62 | }) |
| 63 | |
| 64 | def test_flag_cli_fails_before_required_arguments(self): |
| 65 | result = self.child(flag=True, cli=True) |
| 66 | self.assertNotEqual(result.returncode, 0) |
| 67 | self.assertEqual(result.stdout, "") |
| 68 | self.assertIn("RuntimeError: 최적화 모드", result.stderr) |
| 69 | self.assertNotIn("arguments are required", result.stderr) |
| 70 | |
| 71 | def test_environment_cli_fails_before_required_arguments(self): |
| 72 | result = self.child(environment_mode=True, cli=True) |
| 73 | self.assertNotEqual(result.returncode, 0) |
| 74 | self.assertEqual(result.stdout, "") |
| 75 | self.assertIn("RuntimeError: 최적화 모드", result.stderr) |
| 76 | |
| 77 | def reject(self, content, paths): |
| 78 | parse = runpy.run_path(str(CHECKER))["parse_manifest"] |
| 79 | with self.assertRaises(RuntimeError): |
| 80 | parse(content, paths) |
| 81 | |
| 82 | def test_duplicate_manifest_rows_rejected(self): |
| 83 | line = "a" * 64 + " main.rs\n" |
| 84 | self.reject(line + line, {"main.rs"}) |
| 85 | |
| 86 | def test_missing_manifest_path_rejected(self): |
| 87 | self.reject("a" * 64 + " main.rs\n", {"main.rs", "support.rs"}) |
| 88 | |
| 89 | def test_parent_path_rejected(self): |
| 90 | self.reject("a" * 64 + " ../main.rs\n", {"../main.rs"}) |
| 91 | |
| 92 | def test_malformed_digest_rejected(self): |
| 93 | self.reject("g" * 64 + " main.rs\n", {"main.rs"}) |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | unittest.main(verbosity=2) |