-
Notifications
You must be signed in to change notification settings - Fork 100
Add PSContentPath Standard Platform Paths #1912
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
Open
jshigetomi
wants to merge
9
commits into
PowerShell:master
Choose a base branch
from
jshigetomi:PSContentPath
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+265
−8
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f00798
Add experimental feature check and add PSContentPath to standard plat…
jshigetomi 7d03376
Use Json.net to parse instead
jshigetomi bbb7148
Add support in Linux, MacOS and refactor
jshigetomi d53dd08
Use PowerShell GetPSModulePath API instead
jshigetomi a454cb3
Use runspace instead of reflection
jshigetomi 94a1563
Use current runspace and only get PSVersion once
jshigetomi 26c5bf8
Add tests and test hooks
jshigetomi 536b1ed
Use pwsh variable PSContentPath instead of cmdlet Get-PSContentPath
jshigetomi eafe88b
remove try catch for auomatic variable
jshigetomi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] | ||
| Param() | ||
|
|
||
| $ProgressPreference = "SilentlyContinue" | ||
| $modPath = "$psscriptroot/PSGetTestUtils.psm1" | ||
| Import-Module $modPath -Force | ||
|
|
||
| Describe 'PSUserContentPath/PSContentPath - End-to-End Install Location' -Tags 'CI' { | ||
| BeforeAll { | ||
| $script:originalPSModulePath = $env:PSModulePath | ||
| $script:actualConfigPath = Join-Path $env:LOCALAPPDATA "PowerShell\powershell.config.json" | ||
| $script:configBackup = $null | ||
|
|
||
| # Detect if Get-PSContentPath cmdlet is available (requires PSContentPath experimental feature) | ||
| $script:getPSContentPathAvailable = $false | ||
| $script:isPSContentPathEnabled = $false | ||
| $script:sessionContentPath = $null | ||
| try { | ||
| # Check if Get-PSContentPath cmdlet exists | ||
| $null = Get-Command Get-PSContentPath -ErrorAction Stop | ||
| $script:getPSContentPathAvailable = $true | ||
|
|
||
| # Get the actual session path | ||
| $script:sessionContentPath = Get-PSContentPath | ||
| $documentsPath = [Environment]::GetFolderPath('MyDocuments') | ||
| $documentsPS = Join-Path $documentsPath "PowerShell" | ||
|
|
||
| # If Get-PSContentPath returns something other than Documents, the feature is enabled | ||
| $script:isPSContentPathEnabled = $script:sessionContentPath -ne $documentsPS | ||
| } catch { | ||
| # Get-PSContentPath not available (feature disabled) | ||
| } | ||
|
|
||
| # Backup existing config if it exists | ||
| if (Test-Path $script:actualConfigPath) { | ||
| $script:configBackup = Get-Content $script:actualConfigPath -Raw | ||
| } | ||
|
|
||
| $localRepo = "psgettestlocal" | ||
| $testModuleName = "PSContentPathTestModule" | ||
| Get-NewPSResourceRepositoryFile | ||
| Register-LocalRepos | ||
|
|
||
| # Create a test module | ||
| New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @() | ||
| } | ||
|
|
||
| AfterEach { | ||
| # Restore PSModulePath | ||
| $env:PSModulePath = $script:originalPSModulePath | ||
| # Clean up installed test modules | ||
| Uninstall-PSResource $testModuleName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue | ||
| # Clear testing hooks | ||
| [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::ClearPSContentPathHooks() | ||
| } | ||
|
|
||
| AfterAll { | ||
| # Restore original config | ||
| if ($null -ne $script:configBackup) { | ||
| Set-Content -Path $script:actualConfigPath -Value $script:configBackup -Force | ||
| } | ||
| Get-RevertPSResourceRepositoryFile | ||
| } | ||
|
|
||
| Context "PSResourceGet behavior on PS 7.7+" { | ||
| It "Should use Get-PSContentPath when available, Legacy when not" { | ||
| Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository | ||
|
|
||
| $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") | ||
| $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") | ||
|
|
||
| if ($script:getPSContentPathAvailable) { | ||
| # When Get-PSContentPath cmdlet exists, PSResourceGet should use it | ||
| $pathSource | Should -Be "Get-PSContentPath" | ||
| $pathUsed | Should -Be $script:sessionContentPath | ||
| } else { | ||
| # When Get-PSContentPath cmdlet doesn't exist, PSResourceGet should use legacy path | ||
| $pathSource | Should -Be "Legacy" | ||
| $documentsPath = [Environment]::GetFolderPath('MyDocuments') | ||
| $pathUsed | Should -BeLike "*$documentsPath*PowerShell" | ||
| } | ||
|
|
||
| # Module should be installed | ||
| $res = Get-InstalledPSResource -Name $testModuleName | ||
| $res.Name | Should -Be $testModuleName | ||
| } | ||
| } | ||
|
|
||
| Context "When PSContentPath feature is enabled in session (PS >= 7.7)" { | ||
| It "Should install to custom LocalAppData path (not Documents)" { | ||
| if (-not $script:getPSContentPathAvailable -or -not $script:isPSContentPathEnabled) { | ||
| Set-ItResult -Skipped -Because "PSContentPath feature not enabled in this session" | ||
| return | ||
| } | ||
|
|
||
| Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository | ||
|
|
||
| $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") | ||
| $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") | ||
|
|
||
| # PSResourceGet should call Get-PSContentPath | ||
| $pathSource | Should -Be "Get-PSContentPath" | ||
|
|
||
| # Path should NOT be Documents (feature enabled means custom path) | ||
| $documentsPath = [Environment]::GetFolderPath('MyDocuments') | ||
| $pathUsed | Should -Not -BeLike "*$documentsPath*" | ||
|
|
||
| # Module should be installed in custom path | ||
| $res = Get-InstalledPSResource -Name $testModuleName | ||
| $res.Name | Should -Be $testModuleName | ||
| $res.InstalledLocation | Should -Not -BeLike "*$documentsPath*" | ||
| } | ||
| } | ||
|
|
||
| Context "PSResourceGet correctly delegates path resolution (PS >= 7.7)" { | ||
| It "Should always defer to Get-PSContentPath when cmdlet is available" { | ||
| if (-not $script:getPSContentPathAvailable) { | ||
| Set-ItResult -Skipped -Because "Get-PSContentPath cmdlet not available" | ||
| return | ||
| } | ||
|
|
||
| $beforePath = Get-PSContentPath | ||
|
|
||
| Install-PSResource -Name $testModuleName -Repository $localRepo -Scope CurrentUser -TrustRepository | ||
|
|
||
| $pathSource = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPathSource") | ||
| $pathUsed = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetTestHook("LastUserContentPath") | ||
|
|
||
| # PSResourceGet should call Get-PSContentPath | ||
| $pathSource | Should -Be "Get-PSContentPath" | ||
|
|
||
| # Path should match what Get-PSContentPath returns | ||
| $pathUsed | Should -Be $beforePath | ||
|
|
||
| # Module should be installed | ||
| $res = Get-InstalledPSResource -Name $testModuleName | ||
| $res.Name | Should -Be $testModuleName | ||
| } | ||
| } | ||
|
|
||
| Context "AllUsers scope should not be affected by PSContentPath/PSUserContentPath" { | ||
| BeforeAll { | ||
| if (!$IsWindows -or !(Test-IsAdmin)) { return } | ||
| } | ||
| It "Should install to Program Files (AllUsers not affected by PSContentPath)" { | ||
| if (!$IsWindows -or !(Test-IsAdmin)) { | ||
| Set-ItResult -Skipped -Because "Test requires Windows and Administrator privileges" | ||
| return | ||
| } | ||
| Install-PSResource -Name $testModuleName -Repository $localRepo -Scope AllUsers -TrustRepository | ||
| $programFilesPath = [Environment]::GetFolderPath('ProgramFiles') | ||
| $expectedPath = Join-Path $programFilesPath "PowerShell\Modules\$testModuleName" | ||
| Test-Path $expectedPath | Should -BeTrue | ||
| $res = Get-InstalledPSResource -Name $testModuleName | ||
| $res.Name | Should -Be $testModuleName | ||
| $res.InstalledLocation | Should -BeLike "*Program Files*PowerShell*Modules*" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function Test-IsAdmin { | ||
| if ($IsWindows) { | ||
| $identity = [Security.Principal.WindowsIdentity]::GetCurrent() | ||
| $principal = New-Object Security.Principal.WindowsPrincipal($identity) | ||
| return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | ||
| } | ||
| return $false | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the PR in the PowerShell repo I & the community really do not want this dragging out until 7.7.0
We can get this in much sooner than that as I mentioned in that PR @jshigetomi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kilasuit This feature is coming out in 7.7.0-preivew.1 which shouldn't be too far away from release.