| 1 | import argparse |
| 2 | import importlib.util |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import shutil |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[1] |
| 13 | TESTS = ROOT / "tests" |
| 14 | SECURITY = TESTS / "security" |
| 15 | BUILD_TIMEOUT = 120 |
| 16 | CORE_TIMEOUT = 60 |
| 17 | |
| 18 | |
| 19 | def python_checks(): |
| 20 | suite = unittest.TestSuite() |
| 21 | for index, path in enumerate(sorted(TESTS.rglob("check_*.py"))): |
| 22 | if any(part in (".work", "bin", "obj", "__pycache__") for part in path.relative_to(TESTS).parts): |
| 23 | continue |
| 24 | spec = importlib.util.spec_from_file_location(f"zipelo_checks_{index}", path) |
| 25 | module = importlib.util.module_from_spec(spec) |
| 26 | spec.loader.exec_module(module) |
| 27 | suite.addTests(unittest.defaultTestLoader.loadTestsFromModule(module)) |
| 28 | return unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful() |
| 29 | |
| 30 | |
| 31 | def check_work_path(path): |
| 32 | work = SECURITY / ".work" |
| 33 | if ".." in path.parts or (path != work and not path.is_relative_to(work)): |
| 34 | raise ValueError("프로젝트 안의 테스트 임시 영역이 필요함") |
| 35 | for ancestor in (path, *path.parents): |
| 36 | if ancestor.is_symlink() or (hasattr(ancestor, "is_junction") and ancestor.is_junction()): |
| 37 | raise ValueError("테스트 작업 경로의 링크를 허용하지 않음") |
| 38 | |
| 39 | |
| 40 | def core_checks(): |
| 41 | return csharp_checks("security", "Zipelo.SecurityTests", CORE_TIMEOUT) |
| 42 | |
| 43 | |
| 44 | def runtime_checks(): |
| 45 | return csharp_checks("runtime", "Zipelo.RuntimeTests", 120) |
| 46 | |
| 47 | |
| 48 | def csharp_checks(folder, assembly_name, timeout): |
| 49 | dotnet = shutil.which("dotnet") |
| 50 | if dotnet is None: |
| 51 | print("미실행: .NET 10 SDK가 PATH에 없어 C# 검사를 빌드하거나 실행하지 못함", flush=True) |
| 52 | return 2 |
| 53 | |
| 54 | work = SECURITY / ".work" |
| 55 | check_work_path(work) |
| 56 | work.mkdir(exist_ok=True) |
| 57 | |
| 58 | # 빌드 결과, SDK 캐시와 실행 입력을 이번 테스트 소유 폴더에만 둠. |
| 59 | with tempfile.TemporaryDirectory(prefix="run-", dir=work) as temporary: |
| 60 | sandbox = Path(temporary) |
| 61 | check_work_path(sandbox) |
| 62 | environment = os.environ.copy() |
| 63 | for variable, child in ( |
| 64 | ("DOTNET_CLI_HOME", "dotnet-home"), |
| 65 | ("NUGET_PACKAGES", "nuget-packages"), |
| 66 | ("NUGET_HTTP_CACHE_PATH", "nuget-http-cache"), |
| 67 | ("TMPDIR", "temp"), |
| 68 | ("TMP", "temp"), |
| 69 | ("TEMP", "temp"), |
| 70 | ): |
| 71 | location = sandbox / child |
| 72 | location.mkdir(exist_ok=True) |
| 73 | environment[variable] = str(location) |
| 74 | environment["DOTNET_CLI_TELEMETRY_OPTOUT"] = "1" |
| 75 | environment["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "1" |
| 76 | environment["DOTNET_CLI_WORKLOAD_UPDATE_NOTIFY_DISABLE"] = "true" |
| 77 | binary_directory = sandbox / "bin" |
| 78 | build = [ |
| 79 | dotnet, |
| 80 | "build", |
| 81 | str(TESTS / folder / f"{assembly_name}.csproj"), |
| 82 | "--configuration", "Release", |
| 83 | "--artifacts-path", str(sandbox / "artifacts"), |
| 84 | "--output", str(binary_directory), |
| 85 | "--configfile", str(SECURITY / "NuGet.Config"), |
| 86 | "--disable-build-servers", |
| 87 | "-p:UseAppHost=false", |
| 88 | ] |
| 89 | print(f"C# 테스트 빌드: tests/{folder}/{assembly_name}.csproj", flush=True) |
| 90 | try: |
| 91 | built = subprocess.run( |
| 92 | build, cwd=sandbox, env=environment, check=False, timeout=BUILD_TIMEOUT |
| 93 | ) |
| 94 | if built.returncode != 0: |
| 95 | return built.returncode |
| 96 | |
| 97 | assembly = binary_directory / f"{assembly_name}.dll" |
| 98 | check_work_path(assembly) |
| 99 | if not assembly.is_file(): |
| 100 | print("실행 실패: 빌드된 테스트 DLL이 없음", flush=True) |
| 101 | return 2 |
| 102 | |
| 103 | if folder == "runtime": |
| 104 | for name in ("Zipelo.Worker.dll", "Zipelo.Worker.deps.json", "Zipelo.Worker.runtimeconfig.json"): |
| 105 | artifact = binary_directory / name |
| 106 | check_work_path(artifact) |
| 107 | if not artifact.is_file(): |
| 108 | print(f"실행 실패: 작업자 빌드 파일이 없음: {name}", flush=True) |
| 109 | return 2 |
| 110 | |
| 111 | # 빌드 옵션을 앱 인수로 넘기지 않고 루트와 임시 경로만 전달함. |
| 112 | command = [dotnet, str(assembly), str(ROOT), str(sandbox)] |
| 113 | print(f"{assembly_name} DLL 실행", flush=True) |
| 114 | completed = subprocess.run( |
| 115 | command, cwd=sandbox, env=environment, check=False, timeout=timeout |
| 116 | ) |
| 117 | return completed.returncode |
| 118 | except subprocess.TimeoutExpired: |
| 119 | print("실행 실패: C# 빌드 또는 테스트 제한 시간을 초과함", flush=True) |
| 120 | return 124 |
| 121 | |
| 122 | |
| 123 | def main(): |
| 124 | parser = argparse.ArgumentParser(description="프로젝트 루트 tests/의 검사를 실행함") |
| 125 | parser.add_argument("--core", action="store_true", help=".NET 10 코어 회귀 검사도 실행함") |
| 126 | parser.add_argument("--runtime", action="store_true", help="실제 ZIP 작업자와 프로토콜 회귀 검사도 실행함") |
| 127 | options = parser.parse_args() |
| 128 | sys.dont_write_bytecode = True |
| 129 | if not python_checks(): |
| 130 | return 1 |
| 131 | for enabled, run in ((options.core, core_checks), (options.runtime, runtime_checks)): |
| 132 | if enabled: |
| 133 | code = run() |
| 134 | if code != 0: |
| 135 | return code |
| 136 | if not options.core: |
| 137 | print("미실행: 코어 회귀 검사. .NET 10 SDK 환경에서 --core로 실행해야 함", flush=True) |
| 138 | if not options.runtime: |
| 139 | print("미실행: 런타임 회귀 검사. .NET 10 SDK 환경에서 --runtime으로 실행해야 함", flush=True) |
| 140 | return 0 |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | raise SystemExit(main()) |