From 69f9285d9f3632ab4af903da8e905ac802e7c604 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 12 Jul 2022 15:32:08 -0700 Subject: [PATCH 1/2] Add PSModulePath paths when retreiving resource paths --- src/code/Utils.cs | 52 +++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/code/Utils.cs b/src/code/Utils.cs index af3687a1f..3464fb34a 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -602,16 +602,17 @@ public static string GetInstalledPackageName(string pkgPath) // expecting the full version module path // ex: ./PowerShell/Modules/TestModule/1.0.0 return new DirectoryInfo(pkgPath).Parent.Name; - } - - public static List GetAllResourcePaths( + } + + // Find all potential resource paths + public static List GetPathsFromEnvVarAndScope( PSCmdlet psCmdlet, - ScopeType? scope = null) - { - GetStandardPlatformPaths( - psCmdlet, - out string myDocumentsPath, - out string programFilesPath); + ScopeType? scope) + { + GetStandardPlatformPaths( + psCmdlet, + out string myDocumentsPath, + out string programFilesPath); List resourcePaths = new List(); @@ -634,6 +635,15 @@ public static List GetAllResourcePaths( resourcePaths.Add(Path.Combine(programFilesPath, "Scripts")); } + return resourcePaths; + } + + public static List GetAllResourcePaths( + PSCmdlet psCmdlet, + ScopeType? scope = null) + { + List resourcePaths = GetPathsFromEnvVarAndScope(psCmdlet, scope); + // resourcePaths should now contain, eg: // ./PowerShell/Scripts // ./PowerShell/Modules @@ -680,26 +690,10 @@ public static List GetAllResourcePaths( // Find all potential installation paths given a scope public static List GetAllInstallationPaths( PSCmdlet psCmdlet, - ScopeType scope) - { - GetStandardPlatformPaths( - psCmdlet, - out string myDocumentsPath, - out string programFilesPath); - - // The default user scope is CurrentUser - var installationPaths = new List(); - if (scope == ScopeType.AllUsers) - { - installationPaths.Add(Path.Combine(programFilesPath, "Modules")); - installationPaths.Add(Path.Combine(programFilesPath, "Scripts")); - } - else - { - installationPaths.Add(Path.Combine(myDocumentsPath, "Modules")); - installationPaths.Add(Path.Combine(myDocumentsPath, "Scripts")); - } - + ScopeType? scope) + { + List installationPaths = GetPathsFromEnvVarAndScope(psCmdlet, scope); + installationPaths = installationPaths.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList(); installationPaths.ForEach(dir => psCmdlet.WriteVerbose(string.Format("All paths to search: '{0}'", dir))); From 8f70e4076dc68eae0220a65afe40db9a761e19c9 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 12 Jul 2022 18:02:18 -0700 Subject: [PATCH 2/2] Add test --- test/InstallPSResource.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index e2666efb0..bef8111ae 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -173,6 +173,13 @@ Describe 'Test Install-PSResource for Module' { $pkg.Version | Should -Be "5.0.0.0" } + It "Install resource under specified in PSModulePath" { + Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository + $pkg = Get-PSResource $testModuleName + $pkg.Name | Should -Be $testModuleName + ($env:PSModulePath).Contains($pkg.InstalledLocation) + } + # Windows only It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope CurrentUser