Allow pwsh to inherit $env:PSModulePath and enable powershell.exe to start correctly (#11057)

This commit is contained in:
Steve Lee
2019-11-15 17:29:04 -08:00
committed by Dongbo Wang
parent fe712f8de2
commit 2e553038c5
6 changed files with 118 additions and 164 deletions
@@ -3,7 +3,6 @@
Describe "SxS Module Path Basic Tests" -tags "CI" {
BeforeAll {
if ($IsWindows)
{
$powershell = "$PSHOME\pwsh.exe"
@@ -14,13 +13,22 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
}
$expectedUserPath = Join-Path -Path $HOME -ChildPath "Documents\$ProductName\Modules"
$expectedSharedPath = Join-Path -Path $env:ProgramFiles -ChildPath "$ProductName\Modules"
$userConfigPath = "~/Documents/powershell/powershell.config.json"
}
else
{
$powershell = "$PSHOME/pwsh"
$expectedUserPath = [System.Management.Automation.Platform]::SelectProductNameForDirectory("USER_MODULES")
$expectedSharedPath = [System.Management.Automation.Platform]::SelectProductNameForDirectory("SHARED_MODULES")
$userConfigPath = "~/.config/powershell/powershell.config.json"
}
$userConfigExists = $false
if (Test-Path $userConfigPath) {
$userConfigExists = $true
Copy-Item $userConfigPath "$userConfigPath.backup" -Force -ErrorAction Ignore
}
$expectedSystemPath = Join-Path -Path $PSHOME -ChildPath 'Modules'
# Skip these tests in cases when there is no 'pwsh' executable (e.g. when framework dependent PS package is used)
@@ -41,6 +49,15 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
New-Item -Path $fakePSHomeModuleDir -ItemType Directory > $null
}
AfterAll {
if ($userConfigExists) {
Move-Item "$userConfigPath.backup" $userConfigPath -Force -ErrorAction Ignore
}
else {
Remove-Item "$userConfigPath" -Force -ErrorAction Ignore
}
}
BeforeEach {
$originalModulePath = $env:PSModulePath
}
@@ -74,7 +91,7 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
}
}
It "ignore pshome module path derived from a different PowerShell instance" -Skip:(!$IsCoreCLR -or $skipNoPwsh) {
It "Works with pshome module path derived from a different PowerShell instance" -Skip:(!$IsCoreCLR -or $skipNoPwsh) {
## Create 'powershell' and 'pwsh.deps.json' in the fake PSHome folder,
## so that the module path calculation logic would believe it's real.
@@ -88,21 +105,15 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
$newModulePath = & $powershell -nopro -c '$env:PSModulePath'
$paths = $newModulePath -split [System.IO.Path]::PathSeparator
if ($IsWindows)
{
$paths.Count | Should -Be 4
}
else
{
$paths.Count | Should -Be 3
}
$paths.Count | Should -Be 4
$paths[0] | Should -Be $expectedUserPath
$paths[1] | Should -Be $expectedSharedPath
$paths[2] | Should -Be $expectedSystemPath
$paths[3] | Should -Be $fakePSHomeModuleDir
if ($IsWindows)
{
$paths[3].TrimEnd([System.IO.Path]::DirectorySeparatorChar) | Should -Be $expectedWindowsPowerShellPSHomePath
$expectedWindowsPowerShellPSHomePath | Should -Not -BeIn $paths
}
} finally {
@@ -120,15 +131,7 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
$env:PSModulePath = $fakePSHomeModuleDir, $customeModules -join ([System.IO.Path]::PathSeparator)
$newModulePath = & $powershell -nopro -c '$env:PSModulePath'
$paths = $newModulePath -split [System.IO.Path]::PathSeparator
if ($IsWindows)
{
$paths.Count | Should -Be 6
}
else
{
$paths.Count | Should -Be 5
}
$paths.Count | Should -Be 5
$paths -contains $fakePSHomeModuleDir | Should -BeTrue
$paths -contains $customeModules | Should -BeTrue
}
@@ -162,4 +165,24 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
$wpshIndex | Should -Not -Be -1
$pwshIndex | Should -BeLessThan $wpshIndex
}
It 'Windows PowerShell does not inherit PowerShell paths' -Skip:(!$IsWindows) {
$out = powershell.exe -noprofile -command '$env:PSModulePath'
$out | Should -Not -Contain $expectedUserPath
$out | Should -Not -Contain $expectedSharedPath
$out | Should -Not -Contain $expectedSystemPath
}
It 'Windows PowerShell inherits user added paths' -Skip:(!$IsWindows) {
$env:PSModulePath += ";myPath"
$out = powershell.exe -noprofile -command '$env:PSModulePath'
$out | Should -BeLike '*;myPath'
}
It 'Windows PowerShell does not inherit path defined in powershell.config.json' -Skip:(!$IsWindows) {
$userConfig = '{ "PSModulePath": "myUserPath" }'
Set-Content -Path $userConfigPath -Value $userConfig -Force
$out = pwsh -noprofile -command 'powershell.exe -noprofile -command $env:PSModulePath'
$out | Should -Not -BeLike 'myUserPath;*'
}
}