| 1 | using Zipelo.Runtime; |
| 2 | |
| 3 | namespace Zipelo.RuntimeTests; |
| 4 | |
| 5 | internal static class TestWorkerLaunch |
| 6 | { |
| 7 | private static string ExecutableSuffix => OperatingSystem.IsWindows() ? ".exe" : ""; |
| 8 | private static StringComparison PathComparison => OperatingSystem.IsWindows() |
| 9 | ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| 10 | |
| 11 | public static ZipWorkerClient Worker => Create("Zipelo.Worker"); |
| 12 | public static ZipWorkerClient Peer => Create("Zipelo.RuntimeTests"); |
| 13 | |
| 14 | public static string? DotnetHost |
| 15 | { |
| 16 | get |
| 17 | { |
| 18 | var processPath = Environment.ProcessPath; |
| 19 | if (processPath is null || !Path.IsPathFullyQualified(processPath) || !File.Exists(processPath)) |
| 20 | { |
| 21 | throw new InvalidOperationException("현재 테스트 실행파일의 절대 경로를 확인할 수 없음"); |
| 22 | } |
| 23 | |
| 24 | // 현재 실행 중인 muxer만 재사용하며 PATH에서 호스트를 찾지 않음. |
| 25 | if (string.Equals(Path.GetFileName(processPath), "dotnet" + ExecutableSuffix, PathComparison)) |
| 26 | { |
| 27 | if (!string.Equals(typeof(Program).Assembly.Location, |
| 28 | OutputFile("Zipelo.RuntimeTests.dll"), PathComparison)) |
| 29 | { |
| 30 | throw new InvalidOperationException("테스트 DLL이 고정 출력 폴더와 일치하지 않음"); |
| 31 | } |
| 32 | return processPath; |
| 33 | } |
| 34 | |
| 35 | if (!string.Equals(processPath, OutputFile("Zipelo.RuntimeTests" + ExecutableSuffix), PathComparison)) |
| 36 | { |
| 37 | throw new InvalidOperationException("알 수 없는 테스트 앱호스트임"); |
| 38 | } |
| 39 | return null; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private static ZipWorkerClient Create(string name) |
| 44 | { |
| 45 | var host = DotnetHost; |
| 46 | var path = OutputFile(name + (host is null ? ExecutableSuffix : ".dll")); |
| 47 | // 앱호스트에는 DLL 인수를 붙이지 않아 자기 앱의 무인수 peer 모드를 유지함. |
| 48 | return new ZipWorkerClient(path, host); |
| 49 | } |
| 50 | |
| 51 | private static string OutputFile(string name) |
| 52 | { |
| 53 | var path = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, name)); |
| 54 | if (!File.Exists(path)) { throw new FileNotFoundException("필수 테스트 출력 파일이 없음", path); } |
| 55 | if ((File.GetAttributes(path) & (FileAttributes.Directory | FileAttributes.ReparsePoint)) != 0) |
| 56 | { |
| 57 | throw new InvalidOperationException("테스트 출력 실행파일의 링크를 허용하지 않음"); |
| 58 | } |
| 59 | return path; |
| 60 | } |
| 61 | } |