| 1 | import json |
| 2 | import os |
| 3 | import pathlib |
| 4 | import subprocess |
| 5 | import sys |
| 6 | import unittest |
| 7 | |
| 8 | BASE = pathlib.Path(__file__).resolve().parent |
| 9 | CHECKER = BASE / "static_check.py" |
| 10 | GUARD_MESSAGE = "최적화 모드에서는 정적 검사를 실행할 수 없음" |
| 11 | PROBE = """ |
| 12 | import contextlib |
| 13 | import json |
| 14 | import pathlib |
| 15 | import runpy |
| 16 | import sys |
| 17 | from unittest import mock |
| 18 | |
| 19 | namespace = runpy.run_path(sys.argv[1]) |
| 20 | main = namespace["main"] |
| 21 | calls = [] |
| 22 | |
| 23 | def forbidden(name): |
| 24 | def fail(*args, **kwargs): |
| 25 | calls.append(name) |
| 26 | raise RuntimeError("reached:" + name) |
| 27 | return fail |
| 28 | |
| 29 | with contextlib.ExitStack() as stack: |
| 30 | stack.enter_context(mock.patch( |
| 31 | "argparse.ArgumentParser", side_effect=forbidden("argument_parser") |
| 32 | )) |
| 33 | stack.enter_context(mock.patch.object( |
| 34 | pathlib.Path, "read_bytes", side_effect=forbidden("read_bytes") |
| 35 | )) |
| 36 | stack.enter_context(mock.patch.object( |
| 37 | pathlib.Path, "read_text", side_effect=forbidden("read_text") |
| 38 | )) |
| 39 | stack.enter_context(mock.patch.object( |
| 40 | runpy, "run_path", side_effect=forbidden("fixture_generator") |
| 41 | )) |
| 42 | stack.enter_context(mock.patch.dict(main.__globals__, { |
| 43 | "verify_manifest": forbidden("verify_manifest"), |
| 44 | "verify_pin": forbidden("verify_pin"), |
| 45 | })) |
| 46 | try: |
| 47 | main() |
| 48 | except RuntimeError as error: |
| 49 | result = { |
| 50 | "optimize": sys.flags.optimize, |
| 51 | "message": str(error), |
| 52 | "calls": calls, |
| 53 | } |
| 54 | else: |
| 55 | raise RuntimeError("조기 거부가 발생하지 않음") |
| 56 | print(json.dumps(result, ensure_ascii=False)) |
| 57 | """ |
| 58 | |
| 59 | |
| 60 | class OptimizationGuardTests(unittest.TestCase): |
| 61 | def run_child(self, mode, arguments): |
| 62 | environment = os.environ.copy() |
| 63 | environment.pop("PYTHONOPTIMIZE", None) |
| 64 | environment["PYTHONDONTWRITEBYTECODE"] = "1" |
| 65 | command = [sys.executable, "-B"] |
| 66 | if mode == "flag": |
| 67 | command.append("-O") |
| 68 | elif mode == "environment": |
| 69 | environment["PYTHONOPTIMIZE"] = "1" |
| 70 | command.extend(arguments) |
| 71 | return subprocess.run( |
| 72 | command, |
| 73 | cwd=BASE, |
| 74 | env=environment, |
| 75 | capture_output=True, |
| 76 | text=True, |
| 77 | timeout=10, |
| 78 | check=False, |
| 79 | ) |
| 80 | |
| 81 | def check_probe(self, mode, expected_calls, expected_message, optimize): |
| 82 | result = self.run_child(mode, ["-c", PROBE, str(CHECKER)]) |
| 83 | self.assertEqual(result.returncode, 0, result.stderr) |
| 84 | self.assertEqual(result.stderr, "") |
| 85 | self.assertEqual(json.loads(result.stdout), { |
| 86 | "optimize": optimize, |
| 87 | "message": expected_message, |
| 88 | "calls": expected_calls, |
| 89 | }) |
| 90 | |
| 91 | def check_cli(self, mode): |
| 92 | result = self.run_child(mode, [str(CHECKER)]) |
| 93 | self.assertNotEqual(result.returncode, 0) |
| 94 | self.assertEqual(result.stdout, "") |
| 95 | self.assertIn("RuntimeError: " + GUARD_MESSAGE, result.stderr) |
| 96 | self.assertNotIn("the following arguments are required", result.stderr) |
| 97 | |
| 98 | def test_flag_rejected_before_validation_or_fixture_generation(self): |
| 99 | self.check_probe("flag", [], GUARD_MESSAGE, 1) |
| 100 | |
| 101 | def test_environment_rejected_before_validation_or_fixture_generation(self): |
| 102 | self.check_probe("environment", [], GUARD_MESSAGE, 1) |
| 103 | |
| 104 | def test_normal_mode_reaches_argument_parser(self): |
| 105 | self.check_probe("normal", ["argument_parser"], "reached:argument_parser", 0) |
| 106 | |
| 107 | def test_flag_cli_rejected_before_required_arguments(self): |
| 108 | self.check_cli("flag") |
| 109 | |
| 110 | def test_environment_cli_rejected_before_required_arguments(self): |
| 111 | self.check_cli("environment") |
| 112 | |
| 113 | |
| 114 | if __name__ == "__main__": |
| 115 | unittest.main(verbosity=2) |