Skip to content

Commit 480bce5

Browse files
authored
Merge pull request #337 from ChrisDoernen/get-configuration-root-regardless
Make configuration work independent of whether appsettings json files are provided
2 parents 41efc0d + 319612d commit 480bce5

File tree

7 files changed

+45
-16
lines changed

7 files changed

+45
-16
lines changed

azure-pipeline-PR.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variables:
22
Major: 10
33
Minor: 0
4-
Patch: 1
4+
Patch: 2
55
BuildConfiguration: Release
66

77
name: $(Major).$(Minor).$(Patch).$(rev:r)
@@ -17,7 +17,7 @@ steps:
1717
displayName: 'Use .NET 10.0 sdk'
1818
inputs:
1919
packageType: sdk
20-
version: 10.0.100
20+
version: 10.0.101
2121
installationPath: $(Agent.ToolsDirectory)/dotnet
2222
- script: echo Started restoring the source code
2323
- task: DotNetCoreCLI@2

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variables:
22
Major: 10
33
Minor: 0
4-
Revision: 1
4+
Revision: 2
55
BuildConfiguration: Release
66

77
name: $(Major).$(Minor).$(Revision)
@@ -25,7 +25,7 @@ steps:
2525
displayName: 'Use .NET 10.0 sdk'
2626
inputs:
2727
packageType: sdk
28-
version: 10.0.100
28+
version: 10.0.101
2929
installationPath: $(Agent.ToolsDirectory)/dotnet
3030
- script: echo Started restoring the source code
3131
- task: DotNetCoreCLI@2

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public class ConfigurationTests : TestBed<TestProjectFixture>
55
private const string Key = "CONFIG_KEY";
66
private const string Value = "Value";
77

8-
public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : base(testOutputHelper, fixture)
8+
public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture)
9+
: base(testOutputHelper, fixture)
910
{
1011
Environment.SetEnvironmentVariable(Key, Value);
1112
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
2+
3+
public class ConfigurationTestsWithoutAppsettings : TestBed<TestProjectFixtureWithoutAppsettings>
4+
{
5+
private const string Key = "CONFIG_KEY";
6+
private const string Value = "Value";
7+
8+
public ConfigurationTestsWithoutAppsettings(ITestOutputHelper testOutputHelper, TestProjectFixtureWithoutAppsettings fixture)
9+
: base(testOutputHelper, fixture)
10+
{
11+
Environment.SetEnvironmentVariable(Key, Value);
12+
}
13+
14+
[Fact]
15+
public void EnvironmentVariablesViaConstructorAreAvailable()
16+
{
17+
_fixture.GetServiceProvider(_testOutputHelper);
18+
var value = _fixture.Configuration!.GetValue<string>(Key);
19+
Assert.Equal(Value, value);
20+
}
21+
}

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class TestProjectFixture : TestBedFixture
44
{
5-
protected override void AddServices(IServiceCollection services, IConfiguration? configuration)
5+
protected override void AddServices(IServiceCollection services, IConfiguration configuration)
66
=> services
77
// Transient services - new instance for each injection
88
.AddTransient<ICalculator, Calculator>()
@@ -23,8 +23,8 @@ protected override void AddServices(IServiceCollection services, IConfiguration?
2323
.AddTransient<Func<ISingletonService>>(provider => () => provider.GetService<ISingletonService>()!)
2424

2525
// Configure options
26-
.Configure<Options>(config => configuration?.GetSection("Options").Bind(config))
27-
.Configure<SecretValues>(config => configuration?.GetSection(nameof(SecretValues)).Bind(config));
26+
.Configure<Options>(config => configuration.GetSection("Options").Bind(config))
27+
.Configure<SecretValues>(config => configuration.GetSection(nameof(SecretValues)).Bind(config));
2828

2929
protected override ValueTask DisposeAsyncCore()
3030
=> new();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures;
2+
3+
public class TestProjectFixtureWithoutAppsettings : TestBedFixture
4+
{
5+
protected override void AddServices(IServiceCollection services, IConfiguration configuration)
6+
{
7+
}
8+
9+
protected override ValueTask DisposeAsyncCore() => new();
10+
}

src/Abstracts/TestBedFixture.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ public async ValueTask DisposeAsync()
123123
/// <summary>
124124
/// Adds services to the service collection. Called once before building the provider.
125125
/// </summary>
126-
protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration);
126+
protected abstract void AddServices(IServiceCollection services, IConfiguration configuration);
127127

128128
/// <summary>
129129
/// Returns the test application settings descriptors (JSON files) to include.
130130
/// </summary>
131-
protected abstract IEnumerable<TestAppSettings> GetTestAppSettings();
131+
protected virtual IEnumerable<TestAppSettings> GetTestAppSettings() => [];
132132

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

150-
private IConfigurationRoot? GetConfigurationRoot()
150+
private IConfigurationRoot GetConfigurationRoot()
151151
{
152152
var testAppSettings = GetTestAppSettings();
153-
return
154-
testAppSettings.All(setting => !string.IsNullOrEmpty(setting.Filename))
155-
? GetConfigurationRoot(testAppSettings)
156-
: default;
153+
return GetConfigurationRoot(testAppSettings);
157154
}
158155

159156
private IConfigurationRoot GetConfigurationRoot(IEnumerable<TestAppSettings> configurationFiles)
160157
{
161-
foreach (var configurationFile in configurationFiles)
158+
foreach (var configurationFile in configurationFiles.Where(appSetting => !string.IsNullOrEmpty(appSetting.Filename)))
162159
{
163160
ConfigurationBuilder.AddJsonFile(configurationFile.Filename!, optional: configurationFile.IsOptional);
164161
}

0 commit comments

Comments
 (0)