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
This commit is contained in:
Travis Plunk
2018-03-21 12:30:49 -07:00
committed by GitHub
parent 1730d8c496
commit 41b3161830
2 changed files with 35 additions and 1 deletions
@@ -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;
@@ -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
}
}
}