Update TestRunnerService.cs

This commit is contained in:
Matt Parker
2025-11-04 18:27:05 +10:00
parent e8e919796b
commit 448764c432

View File

@@ -7,22 +7,32 @@ namespace SharpIDE.Application.Features.Testing;
public class TestRunnerService
{
public async Task<List<TestNode>> DiscoverTests(SharpIdeSolutionModel solutionModel)
{
var testProjects = solutionModel.AllProjects.Where(p => p.IsMtpTestProject).ToList();
List<TestNode> allDiscoveredTestNodes = [];
foreach (var testProject in testProjects)
{
using var client = await GetInitialisedClientAsync(testProject);
List<TestNodeUpdate> testNodeUpdates = [];
var discoveryResponse = await client.DiscoverTestsAsync(Guid.NewGuid(), node =>
{
testNodeUpdates.AddRange(node);
return Task.CompletedTask;
});
await discoveryResponse.WaitCompletionAsync();
await client.ExitAsync();
allDiscoveredTestNodes.AddRange(testNodeUpdates.Select(tn => tn.Node));
}
return allDiscoveredTestNodes;
}
// Assumes it has already been built
public async Task RunTestsAsync(SharpIdeProjectModel project)
{
// get path to executable
var outputDllPath = ProjectEvaluation.GetOutputDllFullPath(project);
var outputExecutablePath = 0 switch
{
_ when OperatingSystem.IsWindows() => outputDllPath!.Replace(".dll", ".exe"),
_ when OperatingSystem.IsLinux() => outputDllPath!.Replace(".dll", ""),
_ when OperatingSystem.IsMacOS() => outputDllPath!.Replace(".dll", ""),
_ => throw new PlatformNotSupportedException("Unsupported OS for running tests.")
};
using var client = await TestingPlatformClientFactory.StartAsServerAndConnectToTheClientAsync(outputExecutablePath);
await client.InitializeAsync();
using var client = await GetInitialisedClientAsync(project);
List<TestNodeUpdate> testNodeUpdates = [];
var discoveryResponse = await client.DiscoverTestsAsync(Guid.NewGuid(), node =>
{
@@ -50,4 +60,20 @@ public class TestRunnerService
Console.WriteLine($"Passed: {passedCount}; Skipped: {skippedCount}; Failed: {failedCount};");
await client.ExitAsync();
}
private async Task<TestingPlatformClient> GetInitialisedClientAsync(SharpIdeProjectModel project)
{
var outputDllPath = ProjectEvaluation.GetOutputDllFullPath(project);
var outputExecutablePath = 0 switch
{
_ when OperatingSystem.IsWindows() => outputDllPath!.Replace(".dll", ".exe"),
_ when OperatingSystem.IsLinux() => outputDllPath!.Replace(".dll", ""),
_ when OperatingSystem.IsMacOS() => outputDllPath!.Replace(".dll", ""),
_ => throw new PlatformNotSupportedException("Unsupported OS for running tests.")
};
var client = await TestingPlatformClientFactory.StartAsServerAndConnectToTheClientAsync(outputExecutablePath);
await client.InitializeAsync();
return client;
}
}