From 2a942b8a94761ed279c96367b756e3d8d14866dd Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 19 Mar 2018 19:00:33 -0700 Subject: [PATCH 1/4] use TryAdd to add to providerTable --- .../namespaces/EnvironmentProvider.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs index 6be3c301435..fc5952b6459 100644 --- a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs +++ b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs @@ -198,7 +198,15 @@ internal override IDictionary GetSessionStateTable() 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; From e9c23a70a4a7cacb33567ada8673aeb5b86cb39a Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 5 Mar 2018 17:17:20 -0800 Subject: [PATCH 2/4] Add get-item tests for environment to verify EXISTING behavior --- .../Get-Item.Tests.ps1 | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 e34271cc4b9..174d66b17e2 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,31 @@ 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" { + $env:testVar | should -BeExactly "a" + } + + It "get-item is case-sensitive/insensitive as appropriate" { + $expectedValue = "b" + if($IsWindows) + { + $expectedValue = "a" + } + + $env:testvar | should -BeExactly $expectedValue + } + + } } From bb9836a40bdc87051ab63151d415e5dec219794a Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Thu, 8 Mar 2018 12:15:17 -0800 Subject: [PATCH 3/4] Address PR comments --- .../Microsoft.PowerShell.Management/Get-Item.Tests.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 174d66b17e2..554b8dbb507 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 @@ -129,7 +129,7 @@ Describe "Get-Item" -Tags "CI" { } It "get-item testVar" { - $env:testVar | should -BeExactly "a" + $env:testVar | Should -BeExactly "a" } It "get-item is case-sensitive/insensitive as appropriate" { @@ -139,8 +139,7 @@ Describe "Get-Item" -Tags "CI" { $expectedValue = "a" } - $env:testvar | should -BeExactly $expectedValue + $env:testvar | Should -BeExactly $expectedValue } - } } From 198271d6b36b1fd6ddc78c1a089e1db1d1a503dc Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 19 Mar 2018 18:28:13 -0700 Subject: [PATCH 4/4] fix test to catch error --- .../Microsoft.PowerShell.Management/Get-Item.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 554b8dbb507..c1a8bce69cc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 @@ -129,7 +129,7 @@ Describe "Get-Item" -Tags "CI" { } It "get-item testVar" { - $env:testVar | Should -BeExactly "a" + (get-item env:\testVar).Value | Should -BeExactly "a" } It "get-item is case-sensitive/insensitive as appropriate" { @@ -139,7 +139,7 @@ Describe "Get-Item" -Tags "CI" { $expectedValue = "a" } - $env:testvar | Should -BeExactly $expectedValue + (get-item env:\testvar).Value | Should -BeExactly $expectedValue } } }