|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] |
| 5 | +Param() |
| 6 | + |
| 7 | +$ProgressPreference = "SilentlyContinue" |
| 8 | +$modPath = "$psscriptroot/PSGetTestUtils.psm1" |
| 9 | +Import-Module $modPath -Force |
| 10 | + |
| 11 | +Describe 'PSUserContentPath/PSContentPath - End-to-End Install Location' -Tags 'CI' { |
| 12 | + BeforeAll { |
| 13 | + $script:originalPSModulePath = $env:PSModulePath |
| 14 | + $script:actualConfigPath = Join-Path $env:LOCALAPPDATA "PowerShell\powershell.config.json" |
| 15 | + $script:configBackup = $null |
| 16 | + |
| 17 | + # Detect if Get-PSContentPath cmdlet is available (requires PSContentPath experimental feature) |
| 18 | + $script:getPSContentPathAvailable = $false |
| 19 | + $script:isPSContentPathEnabled = $false |
| 20 | + $script:sessionContentPath = $null |
| 21 | + try { |
| 22 | + # Check if Get-PSContentPath cmdlet exists |
| 23 | + $null = Get-Command Get-PSContentPath -ErrorAction Stop |
| 24 | + $script:getPSContentPathAvailable = $true |
| 25 | + |
| 26 | + # Get the actual session path |
| 27 | + $script:sessionContentPath = Get-PSContentPath |
| 28 | + $documentsPath = [Environment]::GetFolderPath('MyDocuments') |
| 29 | + $documentsPS = Join-Path $documentsPath "PowerShell" |
| 30 | + |
| 31 | + # If Get-PSContentPath returns something other than Documents, the feature is enabled |
| 32 | + $script:isPSContentPathEnabled = $script:sessionContentPath -ne $documentsPS |
| 33 | + } catch { |
| 34 | + # Get-PSContentPath not available (feature disabled) |
| 35 | + } |
| 36 | + |
| 37 | + # Backup existing config if it exists |
| 38 | + if (Test-Path $script:actualConfigPath) { |
| 39 | + $script:configBackup = Get-Content $script:actualConfigPath -Raw |
| 40 | + } |
| 41 | + |
| 42 | + $localRepo = "psgettestlocal" |
| 43 | + $testModuleName = "PSContentPathTestModule" |
| 44 | + Get-NewPSResourceRepositoryFile |
| 45 | + Register-LocalRepos |
| 46 | + |
| 47 | + # Create a test module |
| 48 | + New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @() |
| 49 | + } |
| 50 | + |
| 51 | + AfterEach { |
| 52 | + # Restore PSModulePath |
| 53 | + $env:PSModulePath = $script:originalPSModulePath |
| 54 | + # Clean up installed test modules |
| 55 | + Uninstall-PSResource $testModuleName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue |
| 56 | + # Clear testing hooks |
| 57 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::ClearPSContentPathHooks() |
| 58 | + } |
| 59 | + |
| 60 | + AfterAll { |
| 61 | + # Restore original config |
| 62 | + if ($null -ne $script:configBackup) { |
| 63 | + Set-Content -Path $script:actualConfigPath -Value $script:configBackup -Force |
| 64 | + } |
| 65 | + Get-RevertPSResourceRepositoryFile |
| 66 | + } |
| 67 | + |
| 68 | + Context "PSResourceGet behavior on PS 7.7+" { |
| 69 | + It "Should use Get-PSContentPath when available, Legacy when not" { |
| 70 | + Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository |
| 71 | + |
| 72 | + $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") |
| 73 | + $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") |
| 74 | + |
| 75 | + if ($script:getPSContentPathAvailable) { |
| 76 | + # When Get-PSContentPath cmdlet exists, PSResourceGet should use it |
| 77 | + $pathSource | Should -Be "Get-PSContentPath" |
| 78 | + $pathUsed | Should -Be $script:sessionContentPath |
| 79 | + } else { |
| 80 | + # When Get-PSContentPath cmdlet doesn't exist, PSResourceGet should use legacy path |
| 81 | + $pathSource | Should -Be "Legacy" |
| 82 | + $documentsPath = [Environment]::GetFolderPath('MyDocuments') |
| 83 | + $pathUsed | Should -BeLike "*$documentsPath*PowerShell" |
| 84 | + } |
| 85 | + |
| 86 | + # Module should be installed |
| 87 | + $res = Get-InstalledPSResource -Name $testModuleName |
| 88 | + $res.Name | Should -Be $testModuleName |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Context "When PSContentPath feature is enabled in session (PS >= 7.7)" { |
| 93 | + It "Should install to custom LocalAppData path (not Documents)" { |
| 94 | + if (-not $script:getPSContentPathAvailable -or -not $script:isPSContentPathEnabled) { |
| 95 | + Set-ItResult -Skipped -Because "PSContentPath feature not enabled in this session" |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository |
| 100 | + |
| 101 | + $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") |
| 102 | + $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") |
| 103 | + |
| 104 | + # PSResourceGet should call Get-PSContentPath |
| 105 | + $pathSource | Should -Be "Get-PSContentPath" |
| 106 | + |
| 107 | + # Path should NOT be Documents (feature enabled means custom path) |
| 108 | + $documentsPath = [Environment]::GetFolderPath('MyDocuments') |
| 109 | + $pathUsed | Should -Not -BeLike "*$documentsPath*" |
| 110 | + |
| 111 | + # Module should be installed in custom path |
| 112 | + $res = Get-InstalledPSResource -Name $testModuleName |
| 113 | + $res.Name | Should -Be $testModuleName |
| 114 | + $res.InstalledLocation | Should -Not -BeLike "*$documentsPath*" |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Context "PSResourceGet correctly delegates path resolution (PS >= 7.7)" { |
| 119 | + It "Should always defer to Get-PSContentPath when cmdlet is available" { |
| 120 | + if (-not $script:getPSContentPathAvailable) { |
| 121 | + Set-ItResult -Skipped -Because "Get-PSContentPath cmdlet not available" |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + $beforePath = Get-PSContentPath |
| 126 | + |
| 127 | + Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository |
| 128 | + |
| 129 | + $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") |
| 130 | + $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") |
| 131 | + |
| 132 | + # PSResourceGet should call Get-PSContentPath |
| 133 | + $pathSource | Should -Be "Get-PSContentPath" |
| 134 | + |
| 135 | + # Path should match what Get-PSContentPath returns |
| 136 | + $pathUsed | Should -Be $beforePath |
| 137 | + |
| 138 | + # Module should be installed |
| 139 | + $res = Get-InstalledPSResource -Name $testModuleName |
| 140 | + $res.Name | Should -Be $testModuleName |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + Context "AllUsers scope should not be affected by PSContentPath/PSUserContentPath" { |
| 145 | + BeforeAll { |
| 146 | + if (!$IsWindows -or !(Test-IsAdmin)) { return } |
| 147 | + } |
| 148 | + It "Should install to Program Files (AllUsers not affected by PSContentPath)" { |
| 149 | + if (!$IsWindows -or !(Test-IsAdmin)) { |
| 150 | + Set-ItResult -Skipped -Because "Test requires Windows and Administrator privileges" |
| 151 | + return |
| 152 | + } |
| 153 | + Install-PSResource -Name $testModuleName -Repository $localRepo -Scope AllUsers -TrustRepository |
| 154 | + $programFilesPath = [Environment]::GetFolderPath('ProgramFiles') |
| 155 | + $expectedPath = Join-Path $programFilesPath "PowerShell\Modules\$testModuleName" |
| 156 | + Test-Path $expectedPath | Should -BeTrue |
| 157 | + $res = Get-InstalledPSResource -Name $testModuleName |
| 158 | + $res.Name | Should -Be $testModuleName |
| 159 | + $res.InstalledLocation | Should -BeLike "*Program Files*PowerShell*Modules*" |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function Test-IsAdmin { |
| 165 | + if ($IsWindows) { |
| 166 | + $identity = [Security.Principal.WindowsIdentity]::GetCurrent() |
| 167 | + $principal = New-Object Security.Principal.WindowsPrincipal($identity) |
| 168 | + return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
| 169 | + } |
| 170 | + return $false |
| 171 | +} |
0 commit comments