From 49272941fb3979e9b5a7e470e7e8352b9f17f674 Mon Sep 17 00:00:00 2001 From: Ivan Derevianko Date: Sat, 13 Jun 2015 01:34:53 +0200 Subject: [PATCH] Fix #222: Allow environment variables with the same name but in different cases. --- ...EnvironmentVariablesConfigurationSource.cs | 16 +++++++----- ...ronmentVariablesConfigurationSourceTest.cs | 26 ++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.Framework.Configuration.EnvironmentVariables/EnvironmentVariablesConfigurationSource.cs b/src/Microsoft.Framework.Configuration.EnvironmentVariables/EnvironmentVariablesConfigurationSource.cs index 7fb3b02..e236463 100644 --- a/src/Microsoft.Framework.Configuration.EnvironmentVariables/EnvironmentVariablesConfigurationSource.cs +++ b/src/Microsoft.Framework.Configuration.EnvironmentVariables/EnvironmentVariablesConfigurationSource.cs @@ -37,14 +37,18 @@ namespace Microsoft.Framework.Configuration.EnvironmentVariables internal void Load(IDictionary envVariables) { - Data = envVariables + Data = new Dictionary(StringComparer.OrdinalIgnoreCase); + + var filteredEnvVariables = envVariables .Cast() .SelectMany(AzureEnvToAppEnv) - .Where(entry => ((string)entry.Key).StartsWith(_prefix, StringComparison.OrdinalIgnoreCase)) - .ToDictionary( - entry => ((string)entry.Key).Substring(_prefix.Length), - entry => (string)entry.Value, - StringComparer.OrdinalIgnoreCase); + .Where(entry => ((string)entry.Key).StartsWith(_prefix, StringComparison.OrdinalIgnoreCase)); + + foreach (var envVariable in filteredEnvVariables) + { + var key = ((string)envVariable.Key).Substring(_prefix.Length); + Data[key] = (string)envVariable.Value; + } } private static IEnumerable AzureEnvToAppEnv(DictionaryEntry entry) diff --git a/test/Microsoft.Framework.Configuration.EnvironmentVariables.Test/EnvironmentVariablesConfigurationSourceTest.cs b/test/Microsoft.Framework.Configuration.EnvironmentVariables.Test/EnvironmentVariablesConfigurationSourceTest.cs index f635558..cda4da6 100644 --- a/test/Microsoft.Framework.Configuration.EnvironmentVariables.Test/EnvironmentVariablesConfigurationSourceTest.cs +++ b/test/Microsoft.Framework.Configuration.EnvironmentVariables.Test/EnvironmentVariablesConfigurationSourceTest.cs @@ -102,7 +102,7 @@ namespace Microsoft.Framework.Configuration.EnvironmentVariables.Test } [Fact] - public void ThrowExceptionWhenKeyIsDuplicatedInAzureEnvironment() + public void LastVariableAddedWhenKeyIsDuplicatedInAzureEnvironment() { var dict = new Hashtable() { @@ -111,13 +111,27 @@ namespace Microsoft.Framework.Configuration.EnvironmentVariables.Test }; var envConfigSrc = new EnvironmentVariablesConfigurationSource(); - // On mono the exception message is different - var isMono = Type.GetType("Mono.Runtime") != null; - var expectedMsg = isMono ? "An element with the same key already exists in the dictionary." : "An item with the same key has already been added."; + envConfigSrc.Load(dict); - var exception = Assert.Throws(() => envConfigSrc.Load(dict)); + Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("Data:db2:ConnectionString"))); + Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("Data:db2:ProviderName")); + } - Assert.Equal(expectedMsg, exception.Message); + [Fact] + public void LastVariableAddedWhenMultipleEnvironmentVariablesWithSameNameButDifferentCaseExist() + { + var dict = new Hashtable() + { + {"CommonEnv", "CommonEnvValue1"}, + {"commonenv", "commonenvValue2"}, + {"cOMMonEnv", "commonenvValue3"}, + }; + var envConfigSrc = new EnvironmentVariablesConfigurationSource(); + + envConfigSrc.Load(dict); + + Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("cOMMonEnv"))); + Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("CommonEnv"))); } } }