| | @@ -0,0 +1,115 @@ |
| 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) |