Skip to content

Commit

Permalink
Fix PSModule path with multiple paths
Browse files Browse the repository at this point in the history
  • Loading branch information
giggio committed Mar 13, 2024
1 parent ad2bf78 commit 90e2da2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Expand Up @@ -1275,11 +1275,13 @@ private static string UpdatePath(string path, string pathToAdd, ref int insertIn
if (!string.IsNullOrEmpty(pathToAdd))
{
path = AddToPath(path, pathToAdd, insertIndex);
insertIndex = path.IndexOf(Path.PathSeparator, PathContainsSubstring(path, pathToAdd));
if (insertIndex != -1)
foreach (string addedSubPath in pathToAdd.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
// advance past the path separator
insertIndex++;
int newInsertIndex = PathContainsSubstring(path, addedSubPath) + addedSubPath.Length + 1;
if (newInsertIndex > insertIndex)
{
insertIndex = newInsertIndex;
}
}
}
return path;
Expand Down
30 changes: 20 additions & 10 deletions test/powershell/engine/Module/ModulePath.Tests.ps1
Expand Up @@ -3,25 +3,22 @@
Describe "SxS Module Path Basic Tests" -tags "CI" {

BeforeAll {
if ($IsWindows)
{
if ($IsWindows) {
$powershell = "$PSHOME\pwsh.exe"
$ProductName = "WindowsPowerShell"
if ($IsCoreCLR -and ($PSHOME -notlike "*Windows\System32\WindowsPowerShell\v1.0"))
{
$ProductName = "PowerShell"
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
{
} 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"
}
$pwshUserDir = Split-Path $PROFILE.CurrentUserCurrentHost
if (!(Test-Path $pwshUserDir)) { New-Item -ItemType Directory -Path $pwshUserDir }
$userConfigPath = Join-Path $pwshUserDir powershell.config.json

$userConfigExists = $false
if (Test-Path $userConfigPath) {
Expand Down Expand Up @@ -201,6 +198,19 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
$out = & $powershell -noprofile -command '$env:PSModulePath'
$out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike $validation
}

It 'PSModulePath set in powershell.config.json allows for multiple values' -Skip:(!$IsCoreCLR -or $skipNoPwsh) {
try {
$userConfig = '{ "PSModulePath": "' + "a$([System.IO.Path]::PathSeparator)b" + '"}'
Set-Content -Path $userConfigPath -Value $userConfig -Force
$out = & $powershell -noprofile -command 'powershell.exe -noprofile -command `$env:PSModulePath'
$psModulePaths = $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries)
$psModulePaths | Should -Contain a
$psModulePaths | Should -Contain b
} finally {
Remove-Item -Path $userConfigPath -Force
}
}
}

Describe "ModuleIntrinsics.GetPSModulePath API tests" -tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {
Expand Down

0 comments on commit 90e2da2

Please sign in to comment.