From 99a9d650cf398e3e74fc6bb3b58a0d319758b95d Mon Sep 17 00:00:00 2001 From: maisiesadler Date: Wed, 30 Nov 2022 08:10:58 +0000 Subject: [PATCH 1/2] Test workaround --- src/Demo.Api/MinimalApiTestConfiguration.cs | 31 +++++++++++++++++++++ src/Demo.Api/Program.cs | 2 ++ test/Demo.Test/TestServerFixture.cs | 23 ++++++++------- 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 src/Demo.Api/MinimalApiTestConfiguration.cs diff --git a/src/Demo.Api/MinimalApiTestConfiguration.cs b/src/Demo.Api/MinimalApiTestConfiguration.cs new file mode 100644 index 0000000..7d8571c --- /dev/null +++ b/src/Demo.Api/MinimalApiTestConfiguration.cs @@ -0,0 +1,31 @@ +namespace Microsoft.Extensions.Configuration; + +public static class MinimalApiTestConfiguration +{ + // This async local is set in from tests and it flows to main + internal static readonly AsyncLocal?> _current = new(); + + /// + /// Adds the current test configuration to the application in the "right" place + /// + /// The configuration builder + /// The modified + public static IConfigurationBuilder AddTestConfiguration(this IConfigurationBuilder configurationBuilder) + { + if (_current.Value is { } configure) + { + configure(configurationBuilder); + } + + return configurationBuilder; + } + + /// + /// Unit tests can use this to flow state to the main program and change configuration + /// + /// + public static void Configure(Action action) + { + _current.Value = action; + } +} diff --git a/src/Demo.Api/Program.cs b/src/Demo.Api/Program.cs index 5904373..f1ca89d 100644 --- a/src/Demo.Api/Program.cs +++ b/src/Demo.Api/Program.cs @@ -1,5 +1,7 @@ var builder = WebApplication.CreateBuilder(args); +builder.Configuration.AddTestConfiguration(); + builder.Services.AddSingleton(new Options(builder.Configuration["AppSettings:Options"])); var app = builder.Build(); diff --git a/test/Demo.Test/TestServerFixture.cs b/test/Demo.Test/TestServerFixture.cs index 3d2ec54..ebf2fcc 100644 --- a/test/Demo.Test/TestServerFixture.cs +++ b/test/Demo.Test/TestServerFixture.cs @@ -10,20 +10,19 @@ public class TestServerFixture : WebApplicationFactory { internal Dictionary OverrideConfiguration = new(); - protected override void ConfigureWebHost(IWebHostBuilder builder) + protected override IWebHostBuilder? CreateWebHostBuilder() { - base.ConfigureWebHost(builder); + MinimalApiTestConfiguration.Configure(builder => + { + System.Console.WriteLine("configure test"); + var testDir = Path.GetDirectoryName(GetType().Assembly.Location); + var configLocation = Path.Combine(testDir!, "testsettings.json"); - builder - .ConfigureAppConfiguration((ctx, builder) => - { - System.Console.WriteLine("configure test"); - var testDir = Path.GetDirectoryName(GetType().Assembly.Location); - var configLocation = Path.Combine(testDir!, "testsettings.json"); + builder.Sources.Clear(); + builder.AddJsonFile(configLocation); + builder.AddInMemoryCollection(OverrideConfiguration); + }); - builder.Sources.Clear(); - builder.AddJsonFile(configLocation); - builder.AddInMemoryCollection(OverrideConfiguration); - }); + return base.CreateWebHostBuilder(); } } From 24638af8fbf7fdd568cf94a88e19ba681eb776c0 Mon Sep 17 00:00:00 2001 From: maisiesadler Date: Wed, 30 Nov 2022 08:12:34 +0000 Subject: [PATCH 2/2] on push/PR --- .github/workflows/run-tests.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index ee65349..d79ff62 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,8 +1,5 @@ name: Run tests -on: - push: - branches: - - main +on: [push, pull_request] jobs: runtests: