forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BindConfiguration extension method for OptionsBuilder (dotnet#39825)
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/libraries/Microsoft.Extensions.Options.ConfigurationExtensions/tests/FakeOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Microsoft.Extensions.Options.ConfigurationExtensions.Tests | ||
{ | ||
public class FakeOptions | ||
{ | ||
public string? Message { get; set; } | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...urationExtensions/tests/Microsoft.Extensions.Options.ConfigurationExtensions.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
<TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks> | ||
<EnableDefaultItems>true</EnableDefaultItems> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\src\Microsoft.Extensions.Configuration.csproj"/> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection\src\Microsoft.Extensions.DependencyInjection.csproj"/> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options\src\Microsoft.Extensions.Options.csproj"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Microsoft.Extensions.Options.ConfigurationExtensions.csproj" SkipUseReferenceAssembly="true" /> | ||
</ItemGroup> | ||
|
||
</Project> |
106 changes: 106 additions & 0 deletions
106
...sions.Options.ConfigurationExtensions/tests/OptionsBuidlerConfigurationExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Options.ConfigurationExtensions.Tests | ||
{ | ||
public class OptionsBuidlerConfigurationExtensionsTests | ||
{ | ||
[Fact] | ||
public static void BindConfiguration_ThrowsForNullBuilder() | ||
{ | ||
OptionsBuilder<FakeOptions> optionsBuilder = null!; | ||
|
||
Assert.Throws<ArgumentNullException>("optionsBuilder", () => | ||
{ | ||
optionsBuilder.BindConfiguration("test"); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public static void BindConfiguration_ThrowsForNullConfigurationSectionPath() | ||
{ | ||
var services = new ServiceCollection(); | ||
var optionsBuilder = new OptionsBuilder<FakeOptions>(services, Options.DefaultName); | ||
string configSectionPath = null!; | ||
|
||
Assert.Throws<ArgumentNullException>("configSectionPath", () => | ||
{ | ||
optionsBuilder.BindConfiguration(configSectionPath); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public static void BindConfiguration_ReturnsSameBuilderInstance() | ||
{ | ||
var services = new ServiceCollection(); | ||
var optionsBuilder = new OptionsBuilder<FakeOptions>(services, Options.DefaultName); | ||
|
||
var returnedBuilder = optionsBuilder.BindConfiguration("Test"); | ||
|
||
Assert.Same(optionsBuilder, returnedBuilder); | ||
} | ||
|
||
[Fact] | ||
public static void BindConfiguration_OptionsMaterializationThrowsIfNoConfigurationInDI() | ||
{ | ||
var services = new ServiceCollection(); | ||
var optionsBuilder = services.AddOptions<FakeOptions>(); | ||
|
||
_ = optionsBuilder.BindConfiguration("Test"); | ||
using ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
Assert.ThrowsAny<InvalidOperationException>(() => | ||
{ | ||
_ = serviceProvider.GetRequiredService<IOptions<FakeOptions>>(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public static void BindConfiguration_UsesConfigurationSectionPath() | ||
{ | ||
const string configSectionName = "Test"; | ||
const string messageValue = "This is a test"; | ||
var configEntries = new Dictionary<string, string> | ||
{ | ||
[ConfigurationPath.Combine(configSectionName, nameof(FakeOptions.Message))] = messageValue | ||
}; | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder() | ||
.AddInMemoryCollection(configEntries) | ||
.Build()); | ||
var optionsBuilder = services.AddOptions<FakeOptions>(); | ||
|
||
_ = optionsBuilder.BindConfiguration(configSectionName); | ||
|
||
using ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<FakeOptions>>().Value; | ||
|
||
Assert.Equal(messageValue, options.Message); | ||
} | ||
|
||
[Fact] | ||
public static void BindConfiguration_UsesConfigurationRootIfSectionNameIsEmptyString() | ||
{ | ||
const string messageValue = "This is a test"; | ||
var configEntries = new Dictionary<string, string> | ||
{ | ||
[nameof(FakeOptions.Message)] = messageValue | ||
}; | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder() | ||
.AddInMemoryCollection(configEntries) | ||
.Build()); | ||
var optionsBuilder = services.AddOptions<FakeOptions>(); | ||
|
||
_ = optionsBuilder.BindConfiguration(configSectionPath: ""); | ||
|
||
using ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<FakeOptions>>().Value; | ||
|
||
Assert.Equal(messageValue, options.Message); | ||
} | ||
} | ||
} |