Skip to content

Commit

Permalink
New Feature: Direct Agent Execution
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Feb 7, 2025
1 parent 34a4dc1 commit e00a6c2
Show file tree
Hide file tree
Showing 14 changed files with 515 additions and 8 deletions.
7 changes: 7 additions & 0 deletions NUnitConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "nunit.agent.core", "src\NUn
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "nunit.agent.core.tests", "src\NUnitCommon\nunit.agent.core.tests\nunit.agent.core.tests.csproj", "{89258A3E-5B62-487A-9AE7-D7672CBF61F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectTestAgent", "src\NUnitCommon\DirectTestAgent\DirectTestAgent.csproj", "{0E415863-1621-41DB-9DB0-36B5664DFF35}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -290,6 +292,10 @@ Global
{89258A3E-5B62-487A-9AE7-D7672CBF61F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89258A3E-5B62-487A-9AE7-D7672CBF61F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89258A3E-5B62-487A-9AE7-D7672CBF61F1}.Release|Any CPU.Build.0 = Release|Any CPU
{0E415863-1621-41DB-9DB0-36B5664DFF35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E415863-1621-41DB-9DB0-36B5664DFF35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E415863-1621-41DB-9DB0-36B5664DFF35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E415863-1621-41DB-9DB0-36B5664DFF35}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -336,6 +342,7 @@ Global
{6605DA54-EBF4-4ED1-B5BC-B607AD0CE308} = {3B30D2E5-1587-4D68-B848-1BDDB3C24BFC}
{4FCFAF1C-1579-4A1C-BAF9-9627E39D7CDA} = {3B30D2E5-1587-4D68-B848-1BDDB3C24BFC}
{89258A3E-5B62-487A-9AE7-D7672CBF61F1} = {3B30D2E5-1587-4D68-B848-1BDDB3C24BFC}
{0E415863-1621-41DB-9DB0-36B5664DFF35} = {3B30D2E5-1587-4D68-B848-1BDDB3C24BFC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D8E4FC26-5422-4C51-8BBC-D1AC0A578711}
Expand Down
12 changes: 12 additions & 0 deletions src/NUnitCommon/DirectTestAgent/DirectTestAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

namespace NUnit.Agents
{
public class DirectTestAgent : NUnitAgent<DirectTestAgent>
{
public static void Main(string[] args)
{
Execute(args);
}
}
}
22 changes: 22 additions & 0 deletions src/NUnitCommon/DirectTestAgent/DirectTestAgent.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>NUnit.Agents.Agents</RootNamespace>
<OutputType>Exe</OutputType>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
<OutputPath>..\..\bin\$(Configuration)\direct-test-agent\</OutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\NUnitEngine\nunit.engine.api\nunit.engine.api.csproj" />
<ProjectReference Include="..\nunit.agent.core\nunit.agent.core.csproj" />
<ProjectReference Include="..\nunit.extensibility\nunit.extensibility.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net8.0'">
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions src/NUnitCommon/nunit.agent.core.tests/AgentDirectRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.TestData.Assemblies;

namespace NUnit.Agents
{
public class AgentDirectRunnerTests
{
[Test]
public void RunAgentDirectly()
{
RunTestUnderTestBed(typeof(MockAssembly).Assembly.Location);
}

private void RunTestUnderTestBed(string testAssembly)
{
string agentAssembly = typeof(DirectTestAgent).Assembly.Location;

#if NETFRAMEWORK
string agentExe = agentAssembly;
#else
string agentExe = Path.ChangeExtension(agentAssembly, ".exe");
#endif

var startInfo = new ProcessStartInfo(agentExe);
startInfo.Arguments = testAssembly;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;

Process? process = Process.Start(startInfo);

if (process != null)
{
process.WaitForExit();

string output = process.StandardOutput.ReadToEnd();
int index = output.IndexOf("Test Run Summary");
if (index > 0)
output = output.Substring(index);

Console.WriteLine(output);
Console.WriteLine($"Agent process exited with rc={process.ExitCode}");

if (index < 0)
Assert.Fail("No Summary Report found");
}
}
}
}
6 changes: 6 additions & 0 deletions src/NUnitCommon/nunit.agent.core.tests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test.setting" value="54321"/>
</appSettings>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void CorrectDriverIsUsed(string fileName, bool skipNonTestAssemblies, Typ
new TestCaseData("mock-assembly.pdb", true, typeof(InvalidAssemblyFrameworkDriver)),
new TestCaseData("junk.dll", false, typeof(InvalidAssemblyFrameworkDriver)),
new TestCaseData("junk.dll", true, typeof(InvalidAssemblyFrameworkDriver)),
new TestCaseData("nunit.engine.core.dll", false, typeof(InvalidAssemblyFrameworkDriver)),
new TestCaseData("nunit.engine.core.dll", true, typeof(SkippedAssemblyFrameworkDriver))
new TestCaseData("nunit.agent.core.dll", false, typeof(InvalidAssemblyFrameworkDriver)),
new TestCaseData("nunit.agent.core.dll", true, typeof(SkippedAssemblyFrameworkDriver))
};

[Test]
Expand Down
15 changes: 15 additions & 0 deletions src/NUnitCommon/nunit.agent.core.tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System.Reflection;
using NUnitLite;

namespace NUnit.Engine.Tests
{
class Program
{
static int Main(string[] args)
{
return new TextRunner(typeof(Program).Assembly).Execute(args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ public static class DomainManagerStaticTests
static string path2 = TestPath("/test/bin/debug/test2.dll");
static string path3 = TestPath("/test/utils/test3.dll");

#if NETFRAMEWORK
const string STANDARD_CONFIG_FILE = "nunit.engine.core.tests.exe.config";
#else
const string STANDARD_CONFIG_FILE = "nunit.engine.core.tests.dll.config";
#endif
const string STANDARD_CONFIG_FILE = "nunit.agent.core.tests.exe.config";
const string ALTERNATE_CONFIG_FILE = "alt.config";

[Test]
Expand Down
6 changes: 6 additions & 0 deletions src/NUnitCommon/nunit.agent.core.tests/alt.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test.setting" value="Alternate config used"/>
</appSettings>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<ExposedInternals>true</ExposedInternals>
</PropertyGroup>

Expand All @@ -28,6 +29,7 @@

<ItemGroup>
<ProjectReference Include="..\..\TestData\mock-assembly\mock-assembly.csproj" />
<ProjectReference Include="..\DirectTestAgent\DirectTestAgent.csproj" />
<ProjectReference Include="..\nunit.agent.core\nunit.agent.core.csproj" />
<ProjectReference Include="..\nunit.common\nunit.common.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit e00a6c2

Please sign in to comment.