Skip to content

Commit

Permalink
Manual DI for speed and to remove extra dependency (#639)
Browse files Browse the repository at this point in the history
* Manual DI for speed and to remove extra dependency

* Fix
  • Loading branch information
thomhurst committed Sep 25, 2024
1 parent fe1915d commit 2a2ce5d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" VersionOverride="4.10.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers">
Expand Down
52 changes: 0 additions & 52 deletions TUnit.Engine/Extensions/ServiceCollectionExtensions.cs

This file was deleted.

76 changes: 76 additions & 0 deletions TUnit.Engine/Framework/TUnitServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Messages;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Services;
using TUnit.Core;
using TUnit.Core.Helpers;
using TUnit.Engine.Hooks;
using TUnit.Engine.Logging;
using TUnit.Engine.Services;

namespace TUnit.Engine.Framework;

internal class TUnitServiceProvider : IAsyncDisposable
{
public ILoggerFactory LoggerFactory;
public IOutputDevice OutputDevice;
public ICommandLineOptions CommandLineOptions;

public TUnitFrameworkLogger Logger { get; }
public TUnitInitializer Initializer { get; }
public StandardOutConsoleInterceptor StandardOutConsoleInterceptor { get; }
public StandardErrorConsoleInterceptor StandardErrorConsoleInterceptor { get; }
public TUnitTestDiscoverer TestDiscoverer { get; }
public TestsExecutor TestsExecutor { get; }
public OnEndExecutor OnEndExecutor { get; }

public TUnitServiceProvider(IExtension extension,
IMessageBus messageBus,
IServiceProvider frameworkServiceProvider)
{
LoggerFactory = frameworkServiceProvider.GetLoggerFactory();

OutputDevice = frameworkServiceProvider.GetOutputDevice();

CommandLineOptions = frameworkServiceProvider.GetCommandLineOptions();

Logger = new TUnitFrameworkLogger(extension, OutputDevice, LoggerFactory);

Initializer = new TUnitInitializer(CommandLineOptions);

StandardOutConsoleInterceptor = new StandardOutConsoleInterceptor(CommandLineOptions);

StandardErrorConsoleInterceptor = new StandardErrorConsoleInterceptor(CommandLineOptions);

var testsLoader = new TestsLoader(LoggerFactory);
var testFilterService = new TestFilterService(LoggerFactory);

TestDiscoverer = new TUnitTestDiscoverer(testsLoader, testFilterService, LoggerFactory);

var testGrouper = new TestGrouper();
var disposer = new Disposer(Logger);
var cancellationTokenSource = EngineCancellationToken.CancellationTokenSource;
var testInvoker = new TestInvoker();
var explicitFilterService = new ExplicitFilterService();
var parallelLimitProvider = new ParallelLimitProvider();
var hookMessagePublisher = new HookMessagePublisher(extension, messageBus);
var globalStaticTestHookOrchestrator = new GlobalStaticTestHookOrchestrator(hookMessagePublisher);
var assemblyHookOrchestrator =
new AssemblyHookOrchestrator(hookMessagePublisher, globalStaticTestHookOrchestrator);
var classHookOrchestrator = new ClassHookOrchestrator(hookMessagePublisher, globalStaticTestHookOrchestrator);
var singleTestExecutor = new SingleTestExecutor(extension, disposer, cancellationTokenSource, testInvoker,
explicitFilterService, parallelLimitProvider, assemblyHookOrchestrator, classHookOrchestrator, Logger);

TestsExecutor = new TestsExecutor(singleTestExecutor, testGrouper, Logger, CommandLineOptions);

OnEndExecutor = new OnEndExecutor(CommandLineOptions, Logger);
}

public async ValueTask DisposeAsync()
{
await StandardOutConsoleInterceptor.DisposeAsync();
await StandardErrorConsoleInterceptor.DisposeAsync();
}
}
122 changes: 49 additions & 73 deletions TUnit.Engine/Framework/TUnitTestFramework.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Requests;
using Microsoft.Testing.Platform.Services;
using TUnit.Core;
using TUnit.Engine.Extensions;
using TUnit.Engine.Hooks;
using TUnit.Engine.Logging;
using TUnit.Engine.Models;
using TUnit.Engine.Services;

namespace TUnit.Engine.Framework;

internal sealed class TUnitTestFramework : ITestFramework, IDataProducer
{
private readonly IExtension _extension;
private readonly ITestFrameworkCapabilities _capabilities;
private readonly ServiceProvider _serviceProvider;
private readonly TUnitFrameworkLogger _logger;
private readonly TUnitTestDiscoverer _testDiscover;
private readonly TestsExecutor _testsExecutor;
private readonly TUnitInitializer _initializer;
private readonly OnEndExecutor _onEndExecutor;
private readonly StandardOutConsoleInterceptor _standardOutInterceptor;
private readonly StandardErrorConsoleInterceptor _standardErrorInterceptor;
private readonly TUnitServiceProvider _serviceProvider;

public TUnitTestFramework(IExtension extension,
IServiceProvider frameworkServiceProvider,
ITestFrameworkCapabilities capabilities)
{
_extension = extension;
_capabilities = capabilities;

_serviceProvider = new ServiceCollection()
.AddTestEngineServices(frameworkServiceProvider, extension)
.AddFromFrameworkServiceProvider(frameworkServiceProvider, extension)
.BuildServiceProvider();

_logger = _serviceProvider.GetRequiredService<TUnitFrameworkLogger>();
_testDiscover = _serviceProvider.GetRequiredService<TUnitTestDiscoverer>();
_testsExecutor = _serviceProvider.GetRequiredService<TestsExecutor>();
_initializer = _serviceProvider.GetRequiredService<TUnitInitializer>();
_standardOutInterceptor = _serviceProvider.GetRequiredService<StandardOutConsoleInterceptor>();
_standardErrorInterceptor = _serviceProvider.GetRequiredService<StandardErrorConsoleInterceptor>();
_onEndExecutor = _serviceProvider.GetRequiredService<OnEndExecutor>();

_serviceProvider = new TUnitServiceProvider(extension, frameworkServiceProvider.GetMessageBus(), frameworkServiceProvider);
}

public Task<bool> IsEnabledAsync() => _extension.IsEnabledAsync();
Expand All @@ -71,71 +50,68 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)

EngineCancellationToken.Initialise(context.CancellationToken);

_standardOutInterceptor.Initialize();
_standardErrorInterceptor.Initialize();
_serviceProvider.StandardOutConsoleInterceptor.Initialize();
_serviceProvider.StandardErrorConsoleInterceptor.Initialize();

await using (_serviceProvider)
try
{
try
{
_initializer.Initialize();
_serviceProvider.Initializer.Initialize();

await GlobalStaticTestHookOrchestrator.ExecuteBeforeHooks(new BeforeTestDiscoveryContext());
await GlobalStaticTestHookOrchestrator.ExecuteBeforeHooks(new BeforeTestDiscoveryContext());

var discoveredTests = _testDiscover.DiscoverTests(context.Request as TestExecutionRequest, context.CancellationToken);
var discoveredTests = _serviceProvider.TestDiscoverer.DiscoverTests(context.Request as TestExecutionRequest, context.CancellationToken);

var failedToInitializeTests = _testDiscover.GetFailedToInitializeTests();
var failedToInitializeTests = _serviceProvider.TestDiscoverer.GetFailedToInitializeTests();

await GlobalStaticTestHookOrchestrator.ExecuteAfterHooks(new TestDiscoveryContext(AssemblyHookOrchestrator.GetAllAssemblyHookContexts()));
await GlobalStaticTestHookOrchestrator.ExecuteAfterHooks(new TestDiscoveryContext(AssemblyHookOrchestrator.GetAllAssemblyHookContexts()));

switch (context.Request)
switch (context.Request)
{
case DiscoverTestExecutionRequest:
{
case DiscoverTestExecutionRequest:
foreach (var testNode in discoveredTests.Select(testInformation => testInformation.ToTestNode()))
{
foreach (var testNode in discoveredTests.Select(testInformation => testInformation.ToTestNode()))
{
testNode.Properties.Add(DiscoveredTestNodeStateProperty.CachedInstance);

await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: context.Request.Session.SessionUid,
testNode: testNode)
);
}

await NotifyFailedTests(context, failedToInitializeTests, true);
break;
testNode.Properties.Add(DiscoveredTestNodeStateProperty.CachedInstance);

await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: context.Request.Session.SessionUid,
testNode: testNode)
);
}
case RunTestExecutionRequest runTestExecutionRequest:
await NotifyFailedTests(context, failedToInitializeTests, false);

var testSessionContext = new TestSessionContext(AssemblyHookOrchestrator.GetAllAssemblyHookContexts());
await NotifyFailedTests(context, failedToInitializeTests, true);
break;
}
case RunTestExecutionRequest runTestExecutionRequest:
await NotifyFailedTests(context, failedToInitializeTests, false);

var testSessionContext = new TestSessionContext(AssemblyHookOrchestrator.GetAllAssemblyHookContexts());

await GlobalStaticTestHookOrchestrator.ExecuteBeforeHooks(testSessionContext);
await GlobalStaticTestHookOrchestrator.ExecuteBeforeHooks(testSessionContext);

await _testsExecutor.ExecuteAsync(discoveredTests, runTestExecutionRequest.Filter, context);
await _serviceProvider.TestsExecutor.ExecuteAsync(discoveredTests, runTestExecutionRequest.Filter, context);

await GlobalStaticTestHookOrchestrator.ExecuteAfterHooks(testSessionContext);
await GlobalStaticTestHookOrchestrator.ExecuteAfterHooks(testSessionContext);

foreach (var artifact in testSessionContext.Artifacts)
{
await context.MessageBus.PublishAsync(this, new SessionFileArtifact(context.Request.Session.SessionUid, artifact.File, artifact.DisplayName, artifact.Description));
}
foreach (var artifact in testSessionContext.Artifacts)
{
await context.MessageBus.PublishAsync(this, new SessionFileArtifact(context.Request.Session.SessionUid, artifact.File, artifact.DisplayName, artifact.Description));
}

break;
default:
throw new ArgumentOutOfRangeException(nameof(context.Request), context.Request.GetType().Name);
}
}
catch (Exception e) when (e is TaskCanceledException or OperationCanceledException && context.CancellationToken.IsCancellationRequested)
{
await _logger.LogErrorAsync("The test run was cancelled.");
break;
default:
throw new ArgumentOutOfRangeException(nameof(context.Request), context.Request.GetType().Name);
}
finally
{
await _onEndExecutor.ExecuteAsync();
}
catch (Exception e) when (e is TaskCanceledException or OperationCanceledException && context.CancellationToken.IsCancellationRequested)
{
await _serviceProvider.Logger.LogErrorAsync("The test run was cancelled.");
}
finally
{
await _serviceProvider.OnEndExecutor.ExecuteAsync();

context.Complete();
}
context.Complete();
}
}

Expand Down
1 change: 0 additions & 1 deletion TUnit.Engine/TUnit.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<ItemGroup>
<PackageReference Include="EnumerableAsyncProcessor" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport.Abstractions" />
<PackageReference Include="Microsoft.Testing.Platform" />
</ItemGroup>
Expand Down

0 comments on commit 2a2ce5d

Please sign in to comment.