mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add a public API for getting locations of PSModulePath elements (#19422)
This commit is contained in:
@@ -202,3 +202,48 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
|
||||
$out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike $validation
|
||||
}
|
||||
}
|
||||
|
||||
Describe "ModuleIntrinsics.GetPSModulePath API tests" -tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {
|
||||
BeforeAll {
|
||||
# create a local repostory and install a module
|
||||
$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 }
|
||||
)
|
||||
# resolve the paths to ensure they are in the correct format
|
||||
$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
|
||||
# spot check pshome, the user and shared paths may not be present
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user