-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes appsettings.[EnvironmentName].json (#5421)
* Updated ConfigurationBuilderExtensions to take HostBuilderContext so it can get the EnviornmentName and load the propper appsettings. Fixed bug in loading appsettings.EnvironmentName.json Added base unit tests * Updated Adaptive.Runtime project to target netstandard 2.0 and net core 3.1 * Added conditional references based on target framework * Forced adding ignored files so we can test.
- Loading branch information
Showing
8 changed files
with
152 additions
and
30 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
3 changes: 3 additions & 0 deletions
3
...s.Adaptive.Runtime.Tests/Extensions/ConfigurationBuilderExtensionsFiles/RootDialog.dialog
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,3 @@ | ||
{ | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...ests/Extensions/ConfigurationBuilderExtensionsFiles/settings/appsettings.Development.json
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,3 @@ | ||
{ | ||
"testSetting": "DevEnvironmentValue" | ||
} |
3 changes: 3 additions & 0 deletions
3
...ntime.Tests/Extensions/ConfigurationBuilderExtensionsFiles/settings/appsettings.Test.json
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,3 @@ | ||
{ | ||
"testSetting": "TestEnvironmentValue" | ||
} |
3 changes: 3 additions & 0 deletions
3
...ve.Runtime.Tests/Extensions/ConfigurationBuilderExtensionsFiles/settings/appsettings.json
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,3 @@ | ||
{ | ||
"testSetting": "NoEnvironmentValue" | ||
} |
82 changes: 82 additions & 0 deletions
82
....Builder.Dialogs.Adaptive.Runtime.Tests/Extensions/ConfigurationBuilderExtensionsTests.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,82 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.Bot.Builder.Dialogs.Adaptive.Runtime.Extensions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
|
||
namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Runtime.Tests.Extensions | ||
{ | ||
public class ConfigurationBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void ParameterValidation() | ||
{ | ||
var sut = new ConfigurationBuilder(); | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
sut.AddBotRuntimeConfiguration(null, "blah"); | ||
}); | ||
|
||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
sut.AddBotRuntimeConfiguration(new HostBuilderContext(new Dictionary<object, object>()), null); | ||
}); | ||
|
||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
sut.AddBotRuntimeConfiguration(new HostBuilderContext(new Dictionary<object, object>()), string.Empty); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "NoEnvironmentValue")] | ||
[InlineData("Development", "DevEnvironmentValue")] | ||
[InlineData("Test", "TestEnvironmentValue")] | ||
[InlineData("NotThere", "NoEnvironmentValue")] | ||
public void LoadsAppSettings(string environmentName, string expectedSetting) | ||
{ | ||
// Arrange | ||
var mockBuilderContext = new HostBuilderContext(new Dictionary<object, object>()) | ||
{ | ||
HostingEnvironment = new TestHostingEnvironment { EnvironmentName = environmentName } | ||
}; | ||
var applicationRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extensions", "ConfigurationBuilderExtensionsFiles"); | ||
|
||
// Act | ||
var sut = new ConfigurationBuilder(); | ||
sut.AddBotRuntimeConfiguration(mockBuilderContext, applicationRoot, "settings"); | ||
var actualConfig = sut.Build(); | ||
|
||
// Assert runtime properties. | ||
Assert.Equal(applicationRoot, actualConfig["applicationRoot"]); | ||
Assert.Equal("RootDialog.dialog", actualConfig["defaultRootDialog"]); | ||
|
||
// Assert appsettings values are loaded based on EnvironmentName. | ||
Assert.Equal(expectedSetting, actualConfig["testSetting"]); | ||
} | ||
|
||
#if NETCOREAPP2_1 | ||
/// <summary> | ||
/// Help implementation of <see cref="Microsoft.Extensions.Hosting.IHostEnvironment"/> use for testing. | ||
/// </summary> | ||
private class TestHostingEnvironment : Microsoft.Extensions.Hosting.IHostingEnvironment | ||
#else | ||
private class TestHostingEnvironment : IHostEnvironment | ||
#endif | ||
{ | ||
public string EnvironmentName { get; set; } | ||
|
||
public string ApplicationName { get; set; } | ||
|
||
public string ContentRootPath { get; set; } | ||
|
||
public IFileProvider ContentRootFileProvider { get; set; } | ||
} | ||
} | ||
} |
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