diff --git a/.gitignore b/.gitignore index e5d7ce1..2079720 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ # mergetool output *.orig +# API Approvals +*.received.txt + # Build dependencies and artifacts .dotnet/ .build/ diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt b/src/ServiceComposer.AspNetCore.Testing.Tests/ApiApprovals.Approve_API.verified.txt similarity index 64% rename from src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt rename to src/ServiceComposer.AspNetCore.Testing.Tests/ApiApprovals.Approve_API.verified.txt index 992ad5c..78409d2 100644 --- a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/ApiApprovals.Approve_API.verified.txt @@ -1,6 +1,4 @@ -[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/ServiceComposer/ServiceComposer.AspNetCore.Testing")] -[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName=".NET 10.0")] -namespace ServiceComposer.AspNetCore.Testing +namespace ServiceComposer.AspNetCore.Testing { public class SelfContainedWebApplicationFactoryWithHost : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory where TEntryPoint : class @@ -17,14 +15,6 @@ namespace ServiceComposer.AspNetCore.Testing public SelfContainedWebApplicationFactoryWithWebHost(System.Action configureServices, System.Action configure) { } public System.Action BuilderCustomization { get; set; } protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { } - protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { } - } - public class WebApplicationFactoryWithWebHost : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory - where TStartup : class - { - public WebApplicationFactoryWithWebHost() { } - public System.Action BuilderCustomization { get; set; } - protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { } - protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { } + protected override Microsoft.Extensions.Hosting.IHostBuilder CreateHostBuilder() { } } } \ No newline at end of file diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs index 027da55..5bb7aa4 100644 --- a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs @@ -3,12 +3,16 @@ namespace ServiceComposer.AspNetCore.Testing.Tests; -public class PublicApiApprovalTests +public class ApiApprovals { [Fact] - public Task Public_api_is_approved() + public Task Approve_API() { - var publicApi = ApiGenerator.GeneratePublicApi(typeof(WebApplicationFactoryWithWebHost<>).Assembly); + var publicApi = typeof(SelfContainedWebApplicationFactoryWithHost<>).Assembly.GeneratePublicApi(new ApiGeneratorOptions + { + ExcludeAttributes = ["System.Runtime.Versioning.TargetFrameworkAttribute", "System.Reflection.AssemblyMetadataAttribute"] + }) + ; return Verifier.Verify(publicApi); } } diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs b/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs index c4ab1ce..c01d3ad 100644 --- a/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs @@ -46,22 +46,6 @@ public async Task SelfContainedFactoryWithWebHost_invokes_builder_customization( Assert.True(builderCustomizationCalled); } - [Fact] - public async Task WebApplicationFactoryWithWebHost_invokes_builder_customization() - { - var builderCustomizationCalled = false; - await using var factory = new WebApplicationFactoryWithWebHost - { - BuilderCustomization = _ => builderCustomizationCalled = true - }; - - var client = factory.CreateClient(); - var response = await client.GetStringAsync("/"); - - Assert.Equal("startup", response); - Assert.True(builderCustomizationCalled); - } - class TestEntryPoint; class Startup diff --git a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs index 8de198e..6b33753 100644 --- a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs +++ b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using System; namespace ServiceComposer.AspNetCore.Testing @@ -30,45 +31,45 @@ public SelfContainedWebApplicationFactoryWithWebHost(Action _configureServices = configureServices; _configure = configure; } - + public SelfContainedWebApplicationFactoryWithWebHost(Action configureServices, Action configure) { _configureServicesWithWebHostBuilderContext = configureServices; _configureWithWebHostBuilderContext = configure; } - protected override IWebHostBuilder CreateWebHostBuilder() + protected override IHostBuilder CreateHostBuilder() { - var host = new WebHostBuilder().UseKestrel(); - - if (_configureServices != null) - { - host.ConfigureServices(_configureServices); - } + return Host.CreateDefaultBuilder(Array.Empty()) + .ConfigureWebHostDefaults(webBuilder => + { + if (_configureServices != null) + { + webBuilder.ConfigureServices(_configureServices); + } - if (_configureServicesWithWebHostBuilderContext != null) - { - host.ConfigureServices(_configureServicesWithWebHostBuilderContext); - } + if (_configureServicesWithWebHostBuilderContext != null) + { + webBuilder.ConfigureServices(_configureServicesWithWebHostBuilderContext); + } - if (_configure != null) - { - host.Configure(_configure); - } + if (_configure != null) + { + webBuilder.Configure(_configure); + } - if (_configureWithWebHostBuilderContext != null) - { - host.Configure(_configureWithWebHostBuilderContext); - } - - return host; + if (_configureWithWebHostBuilderContext != null) + { + webBuilder.Configure(_configureWithWebHostBuilderContext); + } + }); } protected override void ConfigureWebHost(IWebHostBuilder builder) { base.ConfigureWebHost(builder); - + BuilderCustomization?.Invoke(builder); } } -} +} \ No newline at end of file diff --git a/src/ServiceComposer.AspNetCore.Testing/ServiceComposer.AspNetCore.Testing.csproj b/src/ServiceComposer.AspNetCore.Testing/ServiceComposer.AspNetCore.Testing.csproj index 0864748..62824f1 100644 --- a/src/ServiceComposer.AspNetCore.Testing/ServiceComposer.AspNetCore.Testing.csproj +++ b/src/ServiceComposer.AspNetCore.Testing/ServiceComposer.AspNetCore.Testing.csproj @@ -1,7 +1,7 @@ - net8.0;net9.0;net10.0 + net10.0 @@ -20,6 +20,10 @@ $(NoWarn);NU5105 + + 4.0 + + @@ -28,15 +32,6 @@ - - - - - - - - - diff --git a/src/ServiceComposer.AspNetCore.Testing/WebApplicationFactoryWithWebHost.cs b/src/ServiceComposer.AspNetCore.Testing/WebApplicationFactoryWithWebHost.cs deleted file mode 100644 index a3a07df..0000000 --- a/src/ServiceComposer.AspNetCore.Testing/WebApplicationFactoryWithWebHost.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; - -namespace ServiceComposer.AspNetCore.Testing -{ - public class WebApplicationFactoryWithWebHost : - WebApplicationFactory - where TStartup : class - { - public Action BuilderCustomization { get; set; } - - protected override IWebHostBuilder CreateWebHostBuilder() - { - var host = new WebHostBuilder() - .UseKestrel() - .UseStartup(); - - return host; - } - - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - base.ConfigureWebHost(builder); - - BuilderCustomization?.Invoke(builder); - } - } -}