Skip to content

Commit d3f9087

Browse files
committed
Refactoring and changes from code review
1 parent 471c8dd commit d3f9087

23 files changed

+571
-426
lines changed

package-tests.cake

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ StandardRunnerTests.Add(new PackageTest(1, "Net462Test")
5757
AddToBothLists(new PackageTest(1, "Net80Test")
5858
{
5959
Description = "Run mock-assembly.dll under .NET 8.0",
60-
Arguments = "testdata/net8.0/mock-assembly.dll --trace:Debug",
60+
Arguments = "testdata/net8.0/mock-assembly.dll",
6161
ExpectedResult = new MockAssemblyExpectedResult("netcore-8.0")
6262
});
6363

@@ -293,14 +293,14 @@ StandardRunnerTests.Add(new PackageTest(1, "Net80WindowsFormsTest")
293293
AddToBothLists(new PackageTest(1, "Net60WPFTest")
294294
{
295295
Description = "Run test using WPF under .NET 6.0",
296-
Arguments = "testdata/net6.0-windows/WpfTest.dll --trace=Debug",
296+
Arguments = "testdata/net6.0-windows/WpfTest.dll",
297297
ExpectedResult = new ExpectedResult("Passed") { Assemblies = new[] { new ExpectedAssemblyResult("WpfTest.dll", "netcore-6.0") } }
298298
});
299299

300300
AddToBothLists(new PackageTest(1, "Net80WPFTest")
301301
{
302302
Description = "Run test using WPF under .NET 8.0",
303-
Arguments = "testdata/net8.0-windows/WpfTest.dll --trace=Debug",
303+
Arguments = "testdata/net8.0-windows/WpfTest.dll",
304304
ExpectedResult = new ExpectedResult("Passed") { Assemblies = new[] { new ExpectedAssemblyResult("WpfTest.dll", "netcore-8.0") } }
305305
});
306306

@@ -382,7 +382,7 @@ StandardRunnerTests.Add(new PackageTest(1, "V2ResultWriterTest_Net462")
382382
//StandardRunnerTests.Add(new PackageTest(1, "VSProjectLoaderTest_Solution")
383383
//{
384384
// Description = "Run mock-assembly using the .sln file",
385-
// Arguments = "../../src/TestData/TestData.sln --config=Release --trace=Debug",
385+
// Arguments = "../../src/TestData/TestData.sln --config=Release",
386386
// ExpectedResult = new ExpectedResult("Failed")
387387
// {
388388
// Total = 37 * 5,

src/NUnitCommon/nunit.agent.core/Drivers/NUnitFrameworkApi2009.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
5454

5555
_testAssemblyPath = testAssemblyPath;
5656

57-
// Normally, the caller should check for an invalid requested runtime, but we make sure here
57+
// Normally, the caller should check for an invalid requested runtime, but to be sure,
58+
// we check it. The setting value is only used for an error message.
5859
settings.TryGetValue(EnginePackageSettings.RequestedRuntimeFramework, out object? requestedRuntime);
5960

6061
var idPrefix = _driverId + "-";

src/NUnitEngine/nunit.engine.tests/Services/ProcessUtilsTests.cs renamed to src/NUnitCommon/nunit.common.tests/ProcessUtilsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using NUnit.Framework;
2525
using System.Text;
2626

27-
namespace NUnit.Engine.Services
27+
namespace NUnit.Common
2828
{
2929
public static class ProcessUtilsTests
3030
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

src/NUnitEngine/nunit.engine/Services/ProcessUtils.cs renamed to src/NUnitCommon/nunit.common/ProcessUtils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
using System.Text;
2525

26-
namespace NUnit.Engine.Services
26+
namespace NUnit.Common
2727
{
2828
public static class ProcessUtils
2929
{

src/NUnitCommon/nunit.common/nunit.common.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
<ItemGroup>
1717
<ProjectReference Include="..\..\NUnitEngine\nunit.engine.api\nunit.engine.api.csproj" />
1818
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Compile Update="AgentProcess.cs">
22+
<SubType>Component</SubType>
23+
</Compile>
24+
</ItemGroup>
1925

2026
</Project>

src/NUnitConsole/nunit4-console/ConsoleRunner.cs

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using NUnit.TextDisplay;
1313
using System.Runtime.InteropServices;
1414
using System.Text;
15+
using System.Data;
1516

1617
namespace NUnit.ConsoleRunner
1718
{
@@ -438,7 +439,11 @@ public static TestPackage MakeTestPackage(ConsoleOptions options)
438439
TestPackage package = new TestPackage(options.InputFiles);
439440

440441
if (options.RuntimeFrameworkSpecified)
442+
{
443+
// Temporarily use both settings until RuntimeFramework is retired
441444
package.AddSetting(EnginePackageSettings.RequestedRuntimeFramework, options.RuntimeFramework);
445+
package.AddSetting(EnginePackageSettings.RequestedFrameworkName, MakeFrameworkName(options.RuntimeFramework));
446+
}
442447

443448
if (options.RunAsX86)
444449
package.AddSetting(EnginePackageSettings.RunAsX86, true);
@@ -516,6 +521,33 @@ public static TestPackage MakeTestPackage(ConsoleOptions options)
516521
return package;
517522
}
518523

524+
private static string MakeFrameworkName(string runtimeFramework)
525+
{
526+
var parts = runtimeFramework.Split('-');
527+
528+
if (parts.Length == 2)
529+
{
530+
string runtime = parts[0];
531+
Version version = new Version(parts[1]);
532+
string? identifier = null;
533+
534+
switch (runtime)
535+
{
536+
case "netcore":
537+
identifier = FrameworkIdentifiers.NetCoreApp;
538+
break;
539+
case "net":
540+
identifier = version.Major > 4 ? FrameworkIdentifiers.NetCoreApp : FrameworkIdentifiers.NetFramework;
541+
break;
542+
}
543+
544+
if (identifier is not null)
545+
return $"{identifier},Version=v{version}";
546+
}
547+
548+
throw new ArgumentException($"Invalid RuntimeFramework: {runtimeFramework}", nameof(runtimeFramework));
549+
}
550+
519551
/// <summary>
520552
/// Sets test parameters, handling backwards compatibility.
521553
/// </summary>

src/NUnitEngine/nunit.engine.api/EnginePackageSettings.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

33
using System;
4+
using System.Runtime.Versioning;
45

56
namespace NUnit
67
{
@@ -72,21 +73,20 @@ public static class EnginePackageSettings
7273
/// are strings like "net-4.5", "mono-4.0", etc. Default is to
7374
/// use the target framework for which an assembly was built.
7475
/// </summary>
75-
[Obsolete("Use 'RequestedRuntimeFramework' instead.")]
76-
public const string RuntimeFramework = "RequestedRuntimeFramework";
76+
public const string RequestedRuntimeFramework = "RequestedRuntimeFramework";
7777

7878
/// <summary>
7979
/// Indicates the desired runtime to use for the tests. Values
8080
/// are strings like "net-4.5", "mono-4.0", etc. Default is to
8181
/// use the target framework for which an assembly was built.
8282
/// </summary>
83-
public const string RequestedRuntimeFramework = "RequestedRuntimeFramework";
83+
public const string RequestedFrameworkName = "RequestedFrameworkName";
8484

8585
/// <summary>
8686
/// Indicates the Target runtime selected for use by the engine,
8787
/// based on the requested runtime and assembly metadata.
8888
/// </summary>
89-
public const string TargetRuntimeFramework = "TargetRuntimeFramework";
89+
public const string TargetFrameworkName = "TargetFrameworkName";
9090

9191
/// <summary>
9292
/// Indicates the name of the agent requested by the user.

src/NUnitEngine/nunit.engine.api/TestAgentInfo.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ namespace NUnit.Engine
88
/// The TestAgentInfo struct provides information about an
99
/// available agent for use by a runner.
1010
/// </summary>
11-
public struct TestAgentInfo
11+
public readonly struct TestAgentInfo
1212
{
1313
/// <summary>
1414
/// The name of this agent
1515
/// </summary>
16-
public string AgentName;
16+
public readonly string AgentName;
1717

1818
/// <summary>
1919
/// The agent type: InProcess, LocalProcess or RemoteProcess
2020
/// </summary>
21-
public TestAgentType AgentType;
21+
public readonly TestAgentType AgentType;
2222

2323
/// <summary>
2424
/// The target runtime used by this agent
2525
/// </summary>
26-
public FrameworkName TargetRuntime;
26+
public readonly FrameworkName TargetRuntime;
2727

2828
/// <summary>
2929
/// Construct a TestAgent Info

src/NUnitEngine/nunit.engine.tests/Services/AgentProcessTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

3-
#if NETFRAMEWORK
3+
#if NETFRAMEWORK && false
44
using System;
55
using System.IO;
66
using System.Diagnostics;

0 commit comments

Comments
 (0)