From 66a817e9dd0d08b18eae122198ce0c23fe282f90 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 10 Jan 2020 09:53:49 -0800 Subject: [PATCH 1/3] Support expanding ~ in $env:PATH when doing command discovery --- .../engine/CommandDiscovery.cs | 11 ++++- .../Environment-Variables.Tests.ps1 | 41 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandDiscovery.cs b/src/System.Management.Automation/engine/CommandDiscovery.cs index 2dc02351489..3333a3f81f8 100644 --- a/src/System.Management.Automation/engine/CommandDiscovery.cs +++ b/src/System.Management.Automation/engine/CommandDiscovery.cs @@ -888,7 +888,7 @@ internal static void AutoloadModulesWithJobSourceAdapters(System.Management.Auto It attempts to load modules from a fixed ModulesWithJobSourceAdapters list that currently has only `PSScheduledJob` module that is not PS-Core compatible. Because this function does not check the result of a (currently failing) `PSScheduledJob` module autoload, it provides no value. After discussion it was decided to comment out this code as it may be useful if ModulesWithJobSourceAdapters list changes in the future. - + if (!context.IsModuleWithJobSourceAdapterLoaded) { PSModuleAutoLoadingPreference moduleAutoLoadingPreference = GetCommandDiscoveryPreference(context, SpecialVariables.PSModuleAutoLoadingPreferenceVarPath, "PSModuleAutoLoadingPreference"); @@ -1328,6 +1328,15 @@ internal LookupPathCollection GetLookupDirectoryPaths() foreach (string directory in tokenizedPath) { string tempDir = directory.TrimStart(); + if (tempDir.EqualsOrdinalIgnoreCase("~")) + { + tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + } + else if (tempDir.StartsWith("~" + Path.DirectorySeparatorChar)) + { + tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + Path.DirectorySeparatorChar + tempDir.Substring(2); + } + _cachedPath.Add(tempDir); result.Add(tempDir); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 index 0d79efbe85b..4e96c8cd77e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 @@ -42,5 +42,44 @@ Describe "Environment-Variables" -Tags "CI" { $ENV:TESTENVIRONMENTVARIABLE | Should -Not -BeNullOrEmpty $ENV:TESTENVIRONMENTVARIABLE | Should -Be $expected - } + } + + Context "~ in PATH" { + AfterEach { + $env:PATH = $oldPath + } + + BeforeAll { + $oldPath = $env:PATH + $pwsh = (Get-Command pwsh | Select-Object -First 1).Source + if ($IsWindows) { + $pwsh2 = "pwsh2.exe" + } + else { + $pwsh2 = "pwsh2" + } + + Copy-Item -Path $pwsh -Destination "~/$pwsh2" + $testPath = Join-Path -Path "~" -ChildPath (New-Guid) + New-Item -Path $testPath -ItemType Directory + Copy-Item -Path $pwsh -Destination "$testPath/$pwsh2" + } + + AfterAll { + Remove-Item -Path "~/pwsh2" -Force + Remove-Item -Path $testPath -Recurse -Force + } + + It "Should be able to resolve ~ in PATH" { + $env:PATH = "~" + [System.IO.Path]::PathSeparator + $env:PATH + $out = Get-Command pwsh2 + $out.Source | Should -BeExactly (Join-Path -Path ([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)) -ChildPath $pwsh2) + } + + It "Should be able to resolve ~/folder in PATH" { + $env:PATH = $testPath + [System.IO.Path]::PathSeparator + $env:PATH + $out = Get-Command pwsh2 + $out.Source | Should -BeExactly (Join-Path -Path (Resolve-Path $testPath) -ChildPath $pwsh2) + } + } } From 28ed0a4f582cdaaf07de2a87a5ff96448b818d29 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 13 Jan 2020 06:44:12 -0800 Subject: [PATCH 2/3] reformat file --- .../Environment-Variables.Tests.ps1 | 81 +++++++++---------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 index 4e96c8cd77e..81d432c77c4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 @@ -3,83 +3,76 @@ Describe "Environment-Variables" -Tags "CI" { It "Should have environment variables" { - Get-Item ENV: | Should -Not -BeNullOrEmpty + Get-Item ENV: | Should -Not -BeNullOrEmpty } It "Should have a nonempty PATH" { - $ENV:PATH | Should -Not -BeNullOrEmpty + $ENV:PATH | Should -Not -BeNullOrEmpty } It "Should contain /bin in the PATH" { - if ($IsWindows) - { - $ENV:PATH | Should -Match "C:" - } - else - { - $ENV:PATH | Should -Match "/bin" - } + if ($IsWindows) { + $ENV:PATH | Should -Match "C:" + } else { + $ENV:PATH | Should -Match "/bin" + } } It "Should have the correct HOME" { - if ($IsWindows) - { - # \Windows\System32 is found as $env:HOMEPATH for temporary profiles - $expected = "\Users", "\Windows" - Split-Path $ENV:HOMEPATH -Parent | Should -BeIn $expected - } - else - { - $expected = /bin/bash -c "cd ~ && pwd" - $ENV:HOME | Should -Be $expected - } + if ($IsWindows) { + # \Windows\System32 is found as $env:HOMEPATH for temporary profiles + $expected = "\Users", "\Windows" + Split-Path $ENV:HOMEPATH -Parent | Should -BeIn $expected + } else { + $expected = /bin/bash -c "cd ~ && pwd" + $ENV:HOME | Should -Be $expected + } } It "Should be able to set the environment variables" { - $expected = "this is a test environment variable" - { $ENV:TESTENVIRONMENTVARIABLE = $expected } | Should -Not -Throw + $expected = "this is a test environment variable" + { $ENV:TESTENVIRONMENTVARIABLE = $expected } | Should -Not -Throw - $ENV:TESTENVIRONMENTVARIABLE | Should -Not -BeNullOrEmpty - $ENV:TESTENVIRONMENTVARIABLE | Should -Be $expected + $ENV:TESTENVIRONMENTVARIABLE | Should -Not -BeNullOrEmpty + $ENV:TESTENVIRONMENTVARIABLE | Should -Be $expected - } + } - Context "~ in PATH" { + Context "~ in PATH" { AfterEach { $env:PATH = $oldPath } - BeforeAll { + BeforeAll { $oldPath = $env:PATH - $pwsh = (Get-Command pwsh | Select-Object -First 1).Source - if ($IsWindows) { - $pwsh2 = "pwsh2.exe" - } - else { - $pwsh2 = "pwsh2" - } + $pwsh = (Get-Command pwsh | Select-Object -First 1).Source + if ($IsWindows) { + $pwsh2 = "pwsh2.exe" + } else { + $pwsh2 = "pwsh2" + } Copy-Item -Path $pwsh -Destination "~/$pwsh2" $testPath = Join-Path -Path "~" -ChildPath (New-Guid) New-Item -Path $testPath -ItemType Directory Copy-Item -Path $pwsh -Destination "$testPath/$pwsh2" - } + } - AfterAll { + AfterAll { Remove-Item -Path "~/pwsh2" -Force Remove-Item -Path $testPath -Recurse -Force - } + } - It "Should be able to resolve ~ in PATH" { + It "Should be able to resolve ~ in PATH" { $env:PATH = "~" + [System.IO.Path]::PathSeparator + $env:PATH $out = Get-Command pwsh2 $out.Source | Should -BeExactly (Join-Path -Path ([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)) -ChildPath $pwsh2) - } + } - It "Should be able to resolve ~/folder in PATH" { + It "Should be able to resolve ~/folder in PATH" { $env:PATH = $testPath + [System.IO.Path]::PathSeparator + $env:PATH $out = Get-Command pwsh2 - $out.Source | Should -BeExactly (Join-Path -Path (Resolve-Path $testPath) -ChildPath $pwsh2) - } - } + $out.Source | Should -BeExactly (Join-Path -Path (Resolve-Path $testPath) -ChildPath $pwsh2) + } + } } From e476a33269f42e7b1ff7de751de719f00dc68c10 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 13 Jan 2020 06:52:13 -0800 Subject: [PATCH 3/3] address Ilya's feedback reformatting doc and redirect new-item to $null --- .../Environment-Variables.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 index 81d432c77c4..3760489ebef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Environment-Variables.Tests.ps1 @@ -54,7 +54,7 @@ Describe "Environment-Variables" -Tags "CI" { Copy-Item -Path $pwsh -Destination "~/$pwsh2" $testPath = Join-Path -Path "~" -ChildPath (New-Guid) - New-Item -Path $testPath -ItemType Directory + New-Item -Path $testPath -ItemType Directory > $null Copy-Item -Path $pwsh -Destination "$testPath/$pwsh2" }