|
| 1 | +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt |
| 2 | + |
| 3 | +#if NETFRAMEWORK |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Runtime.Versioning; |
| 10 | +using System.Text; |
| 11 | +using NUnit.Engine; |
| 12 | + |
| 13 | +namespace NUnit.Common |
| 14 | +{ |
| 15 | + public class AgentProcess : Process |
| 16 | + { |
| 17 | + private static readonly Logger log = InternalTrace.GetLogger(typeof(AgentProcess)); |
| 18 | + |
| 19 | + private readonly TestPackage _package; |
| 20 | + private readonly string _agentPath; |
| 21 | + private readonly FrameworkName _targetRuntime; |
| 22 | + |
| 23 | + public AgentProcess(TestPackage package, string agentPath) |
| 24 | + { |
| 25 | + _package = package; |
| 26 | + _agentPath = agentPath; |
| 27 | + |
| 28 | + // Get target runtime |
| 29 | + string runtimeSetting = package.GetSetting(EnginePackageSettings.TargetFrameworkName, string.Empty); |
| 30 | + log.Debug($"TargetRuntime = {runtimeSetting}"); |
| 31 | + _targetRuntime = new FrameworkName(runtimeSetting); |
| 32 | + |
| 33 | + // Access package settings |
| 34 | + bool debugAgent = package.GetSetting(EnginePackageSettings.DebugAgent, false); |
| 35 | + string traceLevel = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off"); |
| 36 | + string workDirectory = package.GetSetting(EnginePackageSettings.WorkDirectory, string.Empty); |
| 37 | + |
| 38 | + // Set options that need to be in effect before the package |
| 39 | + // is loaded by using the command line. |
| 40 | + if (_targetRuntime.Identifier == FrameworkIdentifiers.NetCoreApp) |
| 41 | + Arguments.Add($"\"{agentPath}\""); |
| 42 | + Arguments.Add($"--pid={Process.GetCurrentProcess().Id}"); |
| 43 | + if (traceLevel != "Off") |
| 44 | + Arguments.Add($"--trace={traceLevel}"); //.EscapeProcessArgument(traceLevel); |
| 45 | + if (debugAgent) |
| 46 | + Arguments.Add("--debug-agent"); |
| 47 | + if (workDirectory != string.Empty) |
| 48 | + Arguments.Add($"--work={workDirectory}"); //.EscapeProcessArgument(workDirectory); |
| 49 | + |
| 50 | + UpdateStartInfo(); |
| 51 | + } |
| 52 | + |
| 53 | + private void UpdateStartInfo() |
| 54 | + { |
| 55 | + bool loadUserProfile = _package.GetSetting(EnginePackageSettings.LoadUserProfile, false); |
| 56 | + bool runAsX86 = _package.GetSetting(EnginePackageSettings.RunAsX86, false); |
| 57 | + |
| 58 | + StartInfo.UseShellExecute = false; |
| 59 | + StartInfo.CreateNoWindow = true; // runUnderAgency; |
| 60 | + StartInfo.WorkingDirectory = Environment.CurrentDirectory; |
| 61 | + StartInfo.LoadUserProfile = loadUserProfile; |
| 62 | + EnableRaisingEvents = true; |
| 63 | + |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + foreach (var arg in Arguments) |
| 66 | + { |
| 67 | + if (sb.Length > 0) |
| 68 | + sb.Append(" "); |
| 69 | + sb.Append(arg.ToString()); |
| 70 | + } |
| 71 | + string arguments = sb.ToString(); |
| 72 | + |
| 73 | + switch (_targetRuntime.Identifier) |
| 74 | + { |
| 75 | + case FrameworkIdentifiers.NetFramework: |
| 76 | + StartInfo.FileName = _agentPath; |
| 77 | + StartInfo.Arguments = arguments; |
| 78 | + StartInfo.LoadUserProfile = loadUserProfile; |
| 79 | + // TODO: Re-integrate mono |
| 80 | + //if (TargetRuntime.Runtime == Runtime.Mono) |
| 81 | + //{ |
| 82 | + // StartInfo.FileName = RuntimeFrameworkService.MonoExePath; |
| 83 | + // string monoOptions = "--runtime=v" + TargetRuntime.FrameworkVersion.ToString(2); |
| 84 | + // monoOptions += " --debug"; |
| 85 | + // StartInfo.Arguments = $"{monoOptions} \"{agentPath}\" {AgentArgs}"; |
| 86 | + //} |
| 87 | + break; |
| 88 | + case FrameworkIdentifiers.NetCoreApp: |
| 89 | + StartInfo.FileName = "dotnet"; |
| 90 | + StartInfo.Arguments = $"\"{_agentPath}\" {arguments}"; |
| 91 | + StartInfo.LoadUserProfile = loadUserProfile; |
| 92 | + |
| 93 | + // TODO: Remove the windows limitation and the use of a hard-coded path. |
| 94 | + if (runAsX86) |
| 95 | + { |
| 96 | + if (Path.DirectorySeparatorChar != '\\') |
| 97 | + throw new Exception("Running .NET Core as X86 is currently only supported on Windows"); |
| 98 | + |
| 99 | + string? installDirectory = DotNet.GetX86InstallDirectory(); |
| 100 | + if (installDirectory is null) |
| 101 | + throw new Exception("The X86 version of dotnet.exe is not installed"); |
| 102 | + |
| 103 | + var x86_dotnet_exe = Path.Combine(installDirectory, "dotnet.exe"); |
| 104 | + if (!File.Exists(x86_dotnet_exe)) |
| 105 | + throw new Exception("The X86 version of dotnet.exe is not installed"); |
| 106 | + |
| 107 | + StartInfo.FileName = x86_dotnet_exe; |
| 108 | + } |
| 109 | + break; |
| 110 | + default: |
| 111 | + StartInfo.FileName = _agentPath; |
| 112 | + StartInfo.Arguments = arguments; |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + log.Debug($"Startinfo.FileName set to {StartInfo.FileName}"); |
| 117 | + log.Debug($"StartInfo.Arguments set to {StartInfo.Arguments}"); |
| 118 | + } |
| 119 | + |
| 120 | + // Public Properties |
| 121 | + public List<string> Arguments { get; } = new List<string>(); |
| 122 | + |
| 123 | + // Public Methods |
| 124 | + public AgentProcess WithArguments(params string[] arguments) |
| 125 | + { |
| 126 | + foreach (string arg in arguments) |
| 127 | + Arguments.Add(arg); |
| 128 | + UpdateStartInfo(); |
| 129 | + return this; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +#endif |
0 commit comments