| |
| |
| Describe "SxS Module Path Basic Tests" -tags "CI" { |
|
|
| BeforeAll { |
| if ($IsWindows) |
| { |
| $powershell = "$PSHOME\pwsh.exe" |
| $ProductName = "WindowsPowerShell" |
| if ($IsCoreCLR -and ($PSHOME -notlike "*Windows\System32\WindowsPowerShell\v1.0")) |
| { |
| $ProductName = "PowerShell" |
| } |
| $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' |
|
|
| |
| $skipNoPwsh = -not (Test-Path $powershell) |
|
|
| if ($IsWindows) |
| { |
| $expectedWindowsPowerShellPSHomePath = Join-Path $env:windir "System32" "WindowsPowerShell" "v1.0" "Modules" |
| } |
|
|
| |
| $fakePSHome = Join-Path -Path $TestDrive -ChildPath 'FakePSHome' |
| $fakePSHomeModuleDir = Join-Path -Path $fakePSHome -ChildPath 'Modules' |
| $fakePowerShell = Join-Path -Path $fakePSHome -ChildPath (Split-Path -Path $powershell -Leaf) |
| $fakePSDepsFile = Join-Path -Path $fakePSHome -ChildPath "pwsh.deps.json" |
|
|
| New-Item -Path $fakePSHome -ItemType Directory > $null |
| 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 |
| } |
|
|
| AfterEach { |
| $env:PSModulePath = $originalModulePath |
| } |
|
|
| It "validate sxs module path" -Skip:$skipNoPwsh { |
|
|
| $env:PSModulePath = $null |
| $defaultModulePath = & $powershell -nopro -c '$env:PSModulePath' |
| $pathSeparator = [System.IO.Path]::PathSeparator |
|
|
| $paths = $defaultModulePath.Replace("$pathSeparator$pathSeparator", "$pathSeparator") -split $pathSeparator |
|
|
| if ($IsWindows) |
| { |
| $expectedPaths = 3 |
| $userPaths = [System.Environment]::GetEnvironmentVariable("PSModulePath", [System.EnvironmentVariableTarget]::User) |
| $expectedPaths += $userPaths ? $userPaths.Split($pathSeparator).Count : 0 |
| $machinePaths = [System.Environment]::GetEnvironmentVariable("PSModulePath", [System.EnvironmentVariableTarget]::Machine) |
| $expectedPaths += $machinePaths ? $machinePaths.Split($pathSeparator).Count : 0 |
|
|
| $paths.Count | Should -Be $expectedPaths |
| } |
| else |
| { |
| $paths.Count | Should -Be 3 |
| } |
|
|
| $paths[0].TrimEnd([System.IO.Path]::DirectorySeparatorChar) | Should -Be $expectedUserPath |
| $paths[1].TrimEnd([System.IO.Path]::DirectorySeparatorChar) | Should -Be $expectedSharedPath |
| $paths[2].TrimEnd([System.IO.Path]::DirectorySeparatorChar) | Should -Be $expectedSystemPath |
| $defaultModulePath | Should -BeLike "*$expectedWindowsPowerShellPSHomePath*" |
| } |
|
|
| It "Works with pshome module path derived from a different PowerShell instance" -Skip:(!$IsCoreCLR -or $skipNoPwsh) { |
|
|
| |
| |
| New-Item -Path $fakePowerShell -ItemType File -Force > $null |
| New-Item -Path $fakePSDepsFile -ItemType File -Force > $null |
|
|
| try { |
|
|
| |
| $env:PSModulePath = $fakePSHomeModuleDir |
| $newModulePath = & $powershell -nopro -c '$env:PSModulePath' |
| $paths = $newModulePath -split [System.IO.Path]::PathSeparator |
| $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 |
| } finally { |
|
|
| |
| Remove-Item -Path $fakePowerShell -Force -ErrorAction SilentlyContinue |
| Remove-Item -Path $fakePSDepsFile -Force -ErrorAction SilentlyContinue |
| } |
| } |
|
|
| It "keep non-pshome module path derived from PowerShell instance parent" -Skip:(!$IsCoreCLR -or $skipNoPwsh) { |
|
|
| |
| $customeModules = Join-Path -Path $TestDrive -ChildPath 'CustomModules' |
| $env:PSModulePath = $fakePSHomeModuleDir, $customeModules -join ([System.IO.Path]::PathSeparator) |
| $newModulePath = & $powershell -nopro -c '$env:PSModulePath' |
| $paths = $newModulePath -split [System.IO.Path]::PathSeparator |
| $paths.Count | Should -Be 5 |
| $paths -contains $fakePSHomeModuleDir | Should -BeTrue |
| $paths -contains $customeModules | Should -BeTrue |
| } |
|
|
| It 'Ensures $PSHOME\Modules is inserted correctly when launched from a different version of PowerShell' -Skip:(!($IsCoreCLR -and $IsWindows) -or $skipNoPwsh) { |
| |
| |
| |
| $winpwshModulePath = [System.IO.Path]::Combine([System.Environment]::SystemDirectory, "WindowsPowerShell", "v1.0", "Modules"); |
| $pwshModulePath = Join-Path -Path $PSHOME -ChildPath 'Modules' |
|
|
| |
| $fakeHome = Join-Path -Path $TestDrive -ChildPath 'fakepwsh' |
| $fakeModulePath = Join-Path -Path $fakeHome -ChildPath 'Modules' |
|
|
| $null = New-Item -Path $fakeHome -ItemType Directory |
| $null = New-Item -Path $fakeModulePath -ItemType Directory |
|
|
| |
| Set-Content -Path "$fakeHome\pwsh.exe" -Value "fake pwsh.exe" |
| Set-Content -Path "$fakeHome\pwsh.deps.json" -Value 'fake pwsh.deps.json' |
|
|
| |
| $fakeModulePath = $env:PSModulePath.Replace($pwshModulePath, $fakeModulePath, [StringComparison]::OrdinalIgnoreCase) |
|
|
| $newModulePath = & $powershell -nopro -c '$env:PSModulePath' |
| $pwshIndex = $newModulePath.IndexOf($pwshModulePath, [StringComparison]::OrdinalIgnoreCase) |
| $wpshIndex = $newModulePath.IndexOf($winpwshModulePath, [StringComparison]::OrdinalIgnoreCase) |
| |
| $pwshIndex | Should -Not -Be -1 |
| $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 -BeLike "*$expectedUserPath*" |
| $out | Should -Not -BeLike "*$expectedSharedPath*" |
| $out | Should -Not -BeLike "*$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) { |
| try { |
| $userConfig = '{ "PSModulePath": "myUserPath" }' |
| Set-Content -Path $userConfigPath -Value $userConfig -Force |
| $out = & $powershell -noprofile -command 'powershell.exe -noprofile -command `$env:PSModulePath' |
| $out | Should -Not -BeLike 'myUserPath;*' |
| } |
| finally { |
| Remove-Item -Path $userConfigPath -Force |
| } |
| } |
|
|
| It 'User PSModulePath has trailing separator' { |
| if ($IsWindows) { |
| $validation = "*\$env:SystemDrive\*" |
| } |
| else { |
| $validation = "*//*" |
| } |
|
|
| $newUserPath = Join-Path $expectedUserPath ([System.IO.Path]::DirectorySeparatorChar) |
| $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $newUserPath).Replace($expectedSharedPath,"") |
| $out = & $powershell -noprofile -command '$env:PSModulePath' |
| $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike $validation |
| } |
| } |
|
|
| Describe "ModuleIntrinsics.GetPSModulePath API tests" -tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { |
| BeforeAll { |
| |
| $localSourceName = [Guid]::NewGuid().ToString("n") |
| $localSourceLocation = Join-Path $PSScriptRoot assets |
| Register-PSRepository -Name $localSourceName -SourceLocation $localSourceLocation -InstallationPolicy Trusted -ErrorAction SilentlyContinue |
| Install-Module -Force -Scope AllUsers -Name PowerShell.TestPackage -Repository $localSourceName -ErrorAction SilentlyContinue |
| Install-Module -Force -Scope CurrentUser -Name PowerShell.TestPackage -Repository $localSourceName -ErrorAction SilentlyContinue |
|
|
| $testCases = @( |
| @{ Name = "User" ; Expected = $IsWindows ? |
| (Resolve-Path ([Environment]::GetFolderPath("Personal") + "\PowerShell\Modules")).Path : |
| (Resolve-Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory("USER_MODULES"))).Path |
| } |
| @{ Name = "Machine" ; Expected = $IsWindows ? |
| [Environment]::GetFolderPath("ProgramFiles") + "\PowerShell\Modules" : |
| (Resolve-Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory("SHARED_MODULES"))).Path |
| } |
| @{ Name = "Builtin" ; Expected = (Resolve-Path (Join-Path $PSHOME Modules)).Path } |
| ) |
| |
| $currentModulePathElements = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Foreach-Object { (Resolve-Path $_).Path } |
| } |
|
|
| AfterAll { |
| Unregister-PSRepository -Name $localSourceName -ErrorAction SilentlyContinue |
| } |
|
|
| It "The value '<Name>' should return the proper value" -testcase $testCases { |
| param ( $Name, $Expected ) |
| $result = [System.Management.Automation.ModuleIntrinsics]::GetPSModulePath($name) |
| $result | Should -not -BeNullOrEmpty |
| |
| if ( $name -eq "PSHOME") { |
| $result | Should -Be $Expected |
| } |
| } |
|
|
| It "The current module path should contain the expected paths for '<Name>'" -testcase $testCases { |
| param ( $Name, $Expected ) |
| $mPath = (Resolve-Path ([System.Management.Automation.ModuleIntrinsics]::GetPSModulePath($name))).Path |
| $currentModulePathElements | Should -Contain $mPath |
| } |
| } |
|
|
| Describe 'ModuleIntrinsics.GetModulePath environment interaction' -tag @('CI') { |
|
|
| It "GetModulePath does not crash when MyDocuments special folder is empty" { |
| try { |
| [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('SetMyDocumentsSpecialFolderToBlank', $true) |
| [System.Management.Automation.ModuleIntrinsics]::GetModulePath($null, $null, $null) |
| } |
| finally { |
| [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('SetMyDocumentsSpecialFolderToBlank', $false) |
| } |
| } |
|
|
| It "GetModulePath does not crash when MyDocuments special folder is not empty" { |
| [System.Management.Automation.ModuleIntrinsics]::GetModulePath($null, $null, $null) |
| } |
|
|
| } |
|
|