-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Support expanding ~ in $env:PATH when doing command discovery #11552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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("~")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should only check the very first directory, right? I don't know if tilda can be in the middle...
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd expect that we expand tilde for all elements. There are other applications which add their paths.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My intent here is to only handle the beginning case which is the most common case. |
||
| { | ||
| 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); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,44 +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" { | ||
| AfterEach { | ||
| $env:PATH = $oldPath | ||
| } | ||
|
|
||
| BeforeAll { | ||
| $oldPath = $env:PATH | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it is tabs here and below. Please fix indentations.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed the file was mixed formatting, I'll reformat the entire file. |
||
| $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 > $null | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.