Skip to content
Merged
4 changes: 2 additions & 2 deletions azure-pipeline-PR.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variables:
Major: 10
Minor: 0
Patch: 1
Patch: 2
BuildConfiguration: Release

name: $(Major).$(Minor).$(Patch).$(rev:r)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variables:
Major: 10
Minor: 0
Revision: 1
Revision: 2
BuildConfiguration: Release

name: $(Major).$(Minor).$(Revision)
Expand All @@ -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
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisDoernen Can you please restore the original implementation of ConfigurationTersts class and instead add a new configuration tests class to verify TestProjectFixtureWithoutAppsettings's implementation?

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public class ConfigurationTests : TestBed<TestProjectFixture>
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;

public class ConfigurationTestsWithoutAppsettings : TestBed<TestProjectFixtureWithoutAppsettings>
{
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<string>(Key);
Assert.Equal(Value, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICalculator, Calculator>()
Expand All @@ -23,8 +23,8 @@ protected override void AddServices(IServiceCollection services, IConfiguration?
.AddTransient<Func<ISingletonService>>(provider => () => provider.GetService<ISingletonService>()!)

// Configure options
.Configure<Options>(config => configuration?.GetSection("Options").Bind(config))
.Configure<SecretValues>(config => configuration?.GetSection(nameof(SecretValues)).Bind(config));
.Configure<Options>(config => configuration.GetSection("Options").Bind(config))
.Configure<SecretValues>(config => configuration.GetSection(nameof(SecretValues)).Bind(config));

protected override ValueTask DisposeAsyncCore()
=> new();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
13 changes: 5 additions & 8 deletions src/Abstracts/TestBedFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ public async ValueTask DisposeAsync()
/// <summary>
/// Adds services to the service collection. Called once before building the provider.
/// </summary>
protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration);
protected abstract void AddServices(IServiceCollection services, IConfiguration configuration);

/// <summary>
/// Returns the test application settings descriptors (JSON files) to include.
/// </summary>
protected abstract IEnumerable<TestAppSettings> GetTestAppSettings();
protected virtual IEnumerable<TestAppSettings> GetTestAppSettings() => [];

/// <summary>
/// Override to asynchronously clean up resources created by the fixture.
Expand All @@ -147,18 +147,15 @@ protected virtual ILoggingBuilder AddLoggingProvider(ILoggingBuilder loggingBuil
/// </summary>
protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder) { }

private IConfigurationRoot? GetConfigurationRoot()
private IConfigurationRoot GetConfigurationRoot()
{
var testAppSettings = GetTestAppSettings();
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisDoernen what is the reason for removing the condition?

testAppSettings.All(setting => !string.IsNullOrEmpty(setting.Filename))
? GetConfigurationRoot(testAppSettings)
: default;
return GetConfigurationRoot(testAppSettings);
}

private IConfigurationRoot GetConfigurationRoot(IEnumerable<TestAppSettings> configurationFiles)
{
foreach (var configurationFile in configurationFiles)
foreach (var configurationFile in configurationFiles.Where(appSetting => !string.IsNullOrEmpty(appSetting.Filename)))
{
ConfigurationBuilder.AddJsonFile(configurationFile.Filename!, optional: configurationFile.IsOptional);
}
Expand Down