From 41b316183015ccc2e7b301a3969ea7228684b4ab Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Wed, 21 Mar 2018 12:30:49 -0700 Subject: [PATCH] Fix error in windows provider when the environment as an existing set of variables name that only differs by case (#6320) - make the provider storage for the environment on windows ignore duplicates - add tests to verify existing environment get-item behavior --- .../namespaces/EnvironmentProvider.cs | 10 ++++++- .../Get-Item.Tests.ps1 | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs index 6be3c30143..fc5952b645 100644 --- a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs +++ b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs @@ -198,7 +198,15 @@ namespace Microsoft.PowerShell.Commands IDictionary environmentTable = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry entry in environmentTable) { - providerTable.Add((string)entry.Key, entry); + // Windows only: duplicate key (variable name that differs only in case) + // NOTE: Even though this shouldn't happen, it can, e.g. when npm + // creates duplicate environment variables that differ only in case - + // see https://github.com/PowerShell/PowerShell/issues/6305. + // However, because retrieval *by name* later is invariably + // case-Insensitive, in effect only a *single* variable exists. + // We simply ask Environment.GetEnvironmentVariable() which value is + // the effective one, and use that. + providerTable.TryAdd((string)entry.Key, entry); } return providerTable; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 index e5427b0338..65f8256c26 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 @@ -116,4 +116,30 @@ Describe "Get-Item" -Tags "CI" { ${result} | Should -BeOfType "Microsoft.Win32.RegistryKey" } } + + Context "Environment provider" -tag "CI" { + BeforeAll { + $env:testvar="b" + $env:testVar="a" + } + + AfterAll { + Clear-Item -Path env:testvar -ErrorAction SilentlyContinue + Clear-Item -Path env:testVar -ErrorAction SilentlyContinue + } + + It "get-item testVar" { + (get-item env:\testVar).Value | Should -BeExactly "a" + } + + It "get-item is case-sensitive/insensitive as appropriate" { + $expectedValue = "b" + if($IsWindows) + { + $expectedValue = "a" + } + + (get-item env:\testvar).Value | Should -BeExactly $expectedValue + } + } }