From 1a9b6155e9dc45112ced0cfa619ed01999b3a93a Mon Sep 17 00:00:00 2001 From: Keith Kjer Date: Mon, 10 Feb 2020 16:54:04 -0800 Subject: [PATCH] Change flag to memory data source. Change content root default instead of runtime default. --- src/Hosting/Hosting/test/HostTests.cs | 34 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Hosting/Hosting/test/HostTests.cs b/src/Hosting/Hosting/test/HostTests.cs index 302dc3f5848..ff2f26846dc 100644 --- a/src/Hosting/Hosting/test/HostTests.cs +++ b/src/Hosting/Hosting/test/HostTests.cs @@ -85,18 +85,26 @@ public void CreateDefaultBuilder_EnablesValidateOnBuild() [Fact] public void CreateDefaultBuilder_ConfigJsonDoesNotReload() { - Environment.SetEnvironmentVariable("DOTNET_HOSTBUILDER_CONFIG_RELOAD", "false"); - Environment.CurrentDirectory = Path.GetTempPath(); + var reloadFlagConfig = new Dictionary() {{ "HOSTBUILDER_CONFIG_RELOAD", "false" }}; + var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json"); string SaveRandomConfig() { var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}"; - File.WriteAllText(Path.Combine(Path.GetTempPath(), "appsettings.json"), $"{{ \"Hello\": \"{newMessage}\" }}"); + File.WriteAllText(appSettingsPath, $"{{ \"Hello\": \"{newMessage}\" }}"); return newMessage; } var dynamicConfigMessage1 = SaveRandomConfig(); - var host = Host.CreateDefaultBuilder().Build(); + + var host = Host.CreateDefaultBuilder() + .UseContentRoot(Path.GetDirectoryName(appSettingsPath)) + .ConfigureHostConfiguration(builder => + { + builder.AddInMemoryCollection(reloadFlagConfig); + }) + .Build(); + var config = host.Services.GetRequiredService(); Assert.Equal(dynamicConfigMessage1, config["Hello"]); @@ -110,18 +118,26 @@ string SaveRandomConfig() [Fact] public void CreateDefaultBuilder_ConfigJsonDoesReload() { - Environment.SetEnvironmentVariable("DOTNET_HOSTBUILDER_CONFIG_RELOAD", "true"); - Environment.CurrentDirectory = Path.GetTempPath(); + var reloadFlagConfig = new Dictionary() { { "HOSTBUILDER_CONFIG_RELOAD", "true" } }; + var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json"); string SaveRandomConfig() { var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}"; - File.WriteAllText(Path.Combine(Path.GetTempPath(), "appsettings.json"), $"{{ \"Hello\": \"{newMessage}\" }}"); + File.WriteAllText(appSettingsPath, $"{{ \"Hello\": \"{newMessage}\" }}"); return newMessage; } var dynamicConfigMessage1 = SaveRandomConfig(); - var host = Host.CreateDefaultBuilder().Build(); + + var host = Host.CreateDefaultBuilder() + .UseContentRoot(Path.GetDirectoryName(appSettingsPath)) + .ConfigureHostConfiguration(builder => + { + builder.AddInMemoryCollection(reloadFlagConfig); + }) + .Build(); + var config = host.Services.GetRequiredService(); Assert.Equal(dynamicConfigMessage1, config["Hello"]); @@ -129,7 +145,7 @@ string SaveRandomConfig() var dynamicConfigMessage2 = SaveRandomConfig(); Task.Delay(1000).Wait(); // Give reload time to fire if it's going to. Assert.NotEqual(dynamicConfigMessage1, dynamicConfigMessage2); // Messages are different. - Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload. + Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload from disk } internal class ServiceA { }