From eb369dfd03630663a9c8f78ec1270075a989a562 Mon Sep 17 00:00:00 2001 From: Michael Mortensen Date: Mon, 23 Sep 2024 22:27:38 +0200 Subject: [PATCH] :recycle: better DX with less cognitive load for AddXunitTestLogging --- .../LoggerExtensions.cs | 4 +- .../ServiceCollectionExtensions.cs | 52 ++++++++----------- .../AspNetCoreHostTestTest.cs | 2 +- .../MvcAspNetCoreHostTestTest.cs | 2 +- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs index 7a7e13b..90943b9 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs +++ b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs @@ -2,9 +2,7 @@ using System.Collections; using System.Linq; using Cuemon; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Xunit.Abstractions; namespace Codebelt.Extensions.Xunit.Hosting { @@ -14,7 +12,7 @@ namespace Codebelt.Extensions.Xunit.Hosting public static class LoggerExtensions { /// - /// Returns the associated that is provided when settings up services from either or . + /// Returns the associated that is provided when settings up services from . /// /// The from which to retrieve the . /// Returns an implementation of with all logged entries expressed as . diff --git a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs index f935e08..3f6a6ac 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs +++ b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Cuemon; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -25,38 +26,31 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelper output, LogLevel minimumLevel = LogLevel.Trace) { Validator.ThrowIfNull(services); - Validator.ThrowIfNull(output); - services.AddLogging(builder => - { - builder.SetMinimumLevel(minimumLevel); - builder.AddProvider(new XunitTestLoggerProvider(output)); - }); + Validator.ThrowIfNull(output); + if (services.Any(sd => sd.ServiceType == typeof(ITestOutputHelperAccessor))) + { + services.AddLogging(builder => + { + builder.SetMinimumLevel(minimumLevel); + builder.Services.AddSingleton(provider => + { + var accessor = provider.GetRequiredService(); + accessor.TestOutput = output; + return new XunitTestLoggerProvider(accessor); + }); + }); + } + else + { + services.AddLogging(builder => + { + builder.SetMinimumLevel(minimumLevel); + builder.AddProvider(new XunitTestLoggerProvider(output)); + }); + } return services; } - /// - /// Adds a unit test optimized implementation of output logging to the collection. - /// - /// The to extend. - /// The that provides access to the output for the logging. - /// The that specifies the minimum level to include for the logging. - /// A reference to so that additional configuration calls can be chained. - /// - /// cannot be null -or- - /// cannot be null. - /// - public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelperAccessor accessor, LogLevel minimumLevel = LogLevel.Trace) - { - Validator.ThrowIfNull(services); - Validator.ThrowIfNull(accessor); - services.AddLogging(builder => - { - builder.SetMinimumLevel(minimumLevel); - builder.AddProvider(new XunitTestLoggerProvider(accessor)); - }); - return services; - } - /// /// Adds a default implementation of to the collection. /// diff --git a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs index de7e2cf..559e90a 100644 --- a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs +++ b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs @@ -78,7 +78,7 @@ public override void ConfigureServices(IServiceCollection services) o.E = true; }); services.AddXunitTestLoggingOutputHelperAccessor(); - services.AddXunitTestLogging(new TestOutputHelperAccessor(TestOutput)); + services.AddXunitTestLogging(TestOutput); } } } diff --git a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs index ed459e5..bbaeb66 100644 --- a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs +++ b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs @@ -46,7 +46,7 @@ public override void ConfigureServices(IServiceCollection services) .AddApplicationPart(typeof(FakeController).Assembly); services.AddXunitTestLoggingOutputHelperAccessor(); - services.AddXunitTestLogging(new TestOutputHelperAccessor(TestOutput)); + services.AddXunitTestLogging(TestOutput); } public override void ConfigureApplication(IApplicationBuilder app)