diff --git a/azure-pipeline-PR.yml b/azure-pipeline-PR.yml index 833fb53..6e800d5 100644 --- a/azure-pipeline-PR.yml +++ b/azure-pipeline-PR.yml @@ -1,7 +1,7 @@ variables: Major: 10 Minor: 0 - Patch: 1 + Patch: 2 BuildConfiguration: Release name: $(Major).$(Minor).$(Patch).$(rev:r) @@ -17,7 +17,7 @@ steps: displayName: 'Use .NET 10.0 sdk' inputs: packageType: sdk - version: 10.0.100 + version: 10.0.101 installationPath: $(Agent.ToolsDirectory)/dotnet - script: echo Started restoring the source code - task: DotNetCoreCLI@2 diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 2f2b462..355a521 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,7 +1,7 @@ variables: Major: 10 Minor: 0 - Revision: 1 + Revision: 2 BuildConfiguration: Release name: $(Major).$(Minor).$(Revision) @@ -25,7 +25,7 @@ steps: displayName: 'Use .NET 10.0 sdk' inputs: packageType: sdk - version: 10.0.100 + version: 10.0.101 installationPath: $(Agent.ToolsDirectory)/dotnet - script: echo Started restoring the source code - task: DotNetCoreCLI@2 diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs index 4a6de58..01b392b 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs @@ -5,7 +5,8 @@ public class ConfigurationTests : TestBed private const string Key = "CONFIG_KEY"; private const string Value = "Value"; - public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : base(testOutputHelper, fixture) + public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) + : base(testOutputHelper, fixture) { Environment.SetEnvironmentVariable(Key, Value); } diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTestsWithoutAppsettings.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTestsWithoutAppsettings.cs new file mode 100644 index 0000000..43199b8 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTestsWithoutAppsettings.cs @@ -0,0 +1,21 @@ +namespace Xunit.Microsoft.DependencyInjection.ExampleTests; + +public class ConfigurationTestsWithoutAppsettings : TestBed +{ + private const string Key = "CONFIG_KEY"; + private const string Value = "Value"; + + public ConfigurationTestsWithoutAppsettings(ITestOutputHelper testOutputHelper, TestProjectFixtureWithoutAppsettings fixture) + : base(testOutputHelper, fixture) + { + Environment.SetEnvironmentVariable(Key, Value); + } + + [Fact] + public void EnvironmentVariablesViaConstructorAreAvailable() + { + _fixture.GetServiceProvider(_testOutputHelper); + var value = _fixture.Configuration!.GetValue(Key); + Assert.Equal(Value, value); + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs index bf91c0b..c740ee8 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs @@ -2,7 +2,7 @@ public class TestProjectFixture : TestBedFixture { - protected override void AddServices(IServiceCollection services, IConfiguration? configuration) + protected override void AddServices(IServiceCollection services, IConfiguration configuration) => services // Transient services - new instance for each injection .AddTransient() @@ -23,8 +23,8 @@ protected override void AddServices(IServiceCollection services, IConfiguration? .AddTransient>(provider => () => provider.GetService()!) // Configure options - .Configure(config => configuration?.GetSection("Options").Bind(config)) - .Configure(config => configuration?.GetSection(nameof(SecretValues)).Bind(config)); + .Configure(config => configuration.GetSection("Options").Bind(config)) + .Configure(config => configuration.GetSection(nameof(SecretValues)).Bind(config)); protected override ValueTask DisposeAsyncCore() => new(); diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixtureWithoutAppsettings.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixtureWithoutAppsettings.cs new file mode 100644 index 0000000..5241fdf --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixtureWithoutAppsettings.cs @@ -0,0 +1,10 @@ +namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures; + +public class TestProjectFixtureWithoutAppsettings : TestBedFixture +{ + protected override void AddServices(IServiceCollection services, IConfiguration configuration) + { + } + + protected override ValueTask DisposeAsyncCore() => new(); +} \ No newline at end of file diff --git a/src/Abstracts/TestBedFixture.cs b/src/Abstracts/TestBedFixture.cs index 22f64d9..145764f 100644 --- a/src/Abstracts/TestBedFixture.cs +++ b/src/Abstracts/TestBedFixture.cs @@ -123,12 +123,12 @@ public async ValueTask DisposeAsync() /// /// Adds services to the service collection. Called once before building the provider. /// - protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration); + protected abstract void AddServices(IServiceCollection services, IConfiguration configuration); /// /// Returns the test application settings descriptors (JSON files) to include. /// - protected abstract IEnumerable GetTestAppSettings(); + protected virtual IEnumerable GetTestAppSettings() => []; /// /// Override to asynchronously clean up resources created by the fixture. @@ -147,18 +147,15 @@ protected virtual ILoggingBuilder AddLoggingProvider(ILoggingBuilder loggingBuil /// protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder) { } - private IConfigurationRoot? GetConfigurationRoot() + private IConfigurationRoot GetConfigurationRoot() { var testAppSettings = GetTestAppSettings(); - return - testAppSettings.All(setting => !string.IsNullOrEmpty(setting.Filename)) - ? GetConfigurationRoot(testAppSettings) - : default; + return GetConfigurationRoot(testAppSettings); } private IConfigurationRoot GetConfigurationRoot(IEnumerable configurationFiles) { - foreach (var configurationFile in configurationFiles) + foreach (var configurationFile in configurationFiles.Where(appSetting => !string.IsNullOrEmpty(appSetting.Filename))) { ConfigurationBuilder.AddJsonFile(configurationFile.Filename!, optional: configurationFile.IsOptional); }