From ee8f7f1fb538f055e6e94468d7e92605e0f5d93b Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 6 Oct 2021 12:54:59 -0700 Subject: [PATCH 1/7] Fix bug with Pester installing as a script instead of a module --- src/code/InstallHelper.cs | 81 ++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 15998e30e..670cd2a15 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -376,9 +376,11 @@ private List InstallPackage(IEnumerable pkgsToInstall, s var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString(); string moduleManifestVersion = string.Empty; var scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1")); - var isScript = File.Exists(scriptPath) ? true : false; + var modulePath = Path.Combine(tempDirNameVersion, (p.Name + ".psd1")); + // Check if the package is a module or a script + var isModule = File.Exists(modulePath) ? true : false; - if (!isScript) + if (isModule) { var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1"); if (!File.Exists(moduleManifest)) @@ -420,16 +422,16 @@ private List InstallPackage(IEnumerable pkgsToInstall, s /// ./Modules /// ./Scripts /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list - installPath = isScript ? _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)) - : _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)); + installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)) + : _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)); } if (_includeXML) { - CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isScript); + CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule); } - MoveFilesIntoInstallPath(p, isScript, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath); + MoveFilesIntoInstallPath(p, isModule, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath); _cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", p.Name, installPath)); pkgsSuccessfullyInstalled.Add(p.Name); @@ -535,12 +537,12 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t return success; } - private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isScript) + private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isModule) { // Script will have a metadata file similar to: "TestScript_InstalledScriptInfo.xml" // Modules will have the metadata file: "PSGetModuleInfo.xml" - var metadataXMLPath = isScript ? Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml")) - : Path.Combine(dirNameVersion, "PSGetModuleInfo.xml"); + var metadataXMLPath = isModule ? Path.Combine(dirNameVersion, "PSGetModuleInfo.xml") + : Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml")); pkg.InstalledDate = DateTime.Now; pkg.InstalledLocation = installPath; @@ -627,7 +629,7 @@ private bool TryDeleteDirectory( private void MoveFilesIntoInstallPath( PSResourceInfo p, - bool isScript, + bool isModule, bool isLocalRepo, string dirNameVersion, string tempInstallPath, @@ -639,18 +641,42 @@ private void MoveFilesIntoInstallPath( string scriptPath) { // Creating the proper installation path depending on whether pkg is a module or script - var newPathParent = isScript ? installPath : Path.Combine(installPath, p.Name); - var finalModuleVersionDir = isScript ? installPath : Path.Combine(installPath, p.Name, moduleManifestVersion); // versionWithoutPrereleaseTag + var newPathParent = isModule ? Path.Combine(installPath, p.Name) : installPath; + var finalModuleVersionDir = isModule ? Path.Combine(installPath, p.Name, moduleManifestVersion) : installPath; // versionWithoutPrereleaseTag // If script, just move the files over, if module, move the version directory over - var tempModuleVersionDir = (isScript || isLocalRepo) ? dirNameVersion + var tempModuleVersionDir = (!isModule || isLocalRepo) ? dirNameVersion : Path.Combine(tempInstallPath, p.Name.ToLower(), newVersion); _cmdletPassedIn.WriteVerbose(string.Format("Installation source path is: '{0}'", tempModuleVersionDir)); - _cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir)); + _cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir)); - if (isScript) + if (isModule) { + // If new path does not exist + if (!Directory.Exists(newPathParent)) + { + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); + Directory.CreateDirectory(newPathParent); + Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); + } + else + { + _cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir)); + + // At this point if + if (Directory.Exists(finalModuleVersionDir)) + { + // Delete the directory path before replacing it with the new module + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir)); + Directory.Delete(finalModuleVersionDir, true); + } + + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); + Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); + } + } + else { if (!_savePkg) { // Need to delete old xml files because there can only be 1 per script @@ -677,31 +703,6 @@ private void MoveFilesIntoInstallPath( _cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1"))); Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1")); } - else - { - // If new path does not exist - if (!Directory.Exists(newPathParent)) - { - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); - Directory.CreateDirectory(newPathParent); - Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); - } - else - { - _cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir)); - - // At this point if - if (Directory.Exists(finalModuleVersionDir)) - { - // Delete the directory path before replacing it with the new module - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir)); - Directory.Delete(finalModuleVersionDir, true); - } - - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); - Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); - } - } } } } From 5186c6582062df34527316536f503714f8d48ad5 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 6 Oct 2021 13:06:15 -0700 Subject: [PATCH 2/7] 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 4a61f1549..d6e8e4f99 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -243,6 +243,13 @@ Describe 'Test Install-PSResource for Module' { $res = Get-Module "TestModule" -ListAvailable $res | Should -BeNullOrEmpty } + + It "Validate that Pester is installing under Modules path" { + Install-PSResource -Name "Pester" -Repository PSGallery + + $res = Get-Module "Pester" -ListAvailable + $res.Path.Contains("Modules") | Should -Be $true + } } <# Temporarily commented until -Tag is implemented for this Describe block From 79f2bcef5d1c2cc305c8ef7f21fbe9a6f93c25e1 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 6 Oct 2021 15:50:24 -0700 Subject: [PATCH 3/7] Update repo name in test --- test/InstallPSResource.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index d6e8e4f99..8317ab4c4 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -245,7 +245,7 @@ Describe 'Test Install-PSResource for Module' { } It "Validate that Pester is installing under Modules path" { - Install-PSResource -Name "Pester" -Repository PSGallery + Install-PSResource -Name "Pester" -Repository $PSGalleryName $res = Get-Module "Pester" -ListAvailable $res.Path.Contains("Modules") | Should -Be $true From 398a458b218b7aed1cb6a979799f03d3290bf087 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 6 Oct 2021 19:52:41 -0700 Subject: [PATCH 4/7] Add debug logging --- test/InstallPSResource.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 8317ab4c4..57d025a90 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -245,9 +245,11 @@ Describe 'Test Install-PSResource for Module' { } It "Validate that Pester is installing under Modules path" { + write-Output $PSGalleryName Install-PSResource -Name "Pester" -Repository $PSGalleryName $res = Get-Module "Pester" -ListAvailable + Write-Output ($res) $res.Path.Contains("Modules") | Should -Be $true } } From 3fd4e61d61a1794cfa634a57794eca0a85f74ebb Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 6 Oct 2021 20:53:46 -0700 Subject: [PATCH 5/7] Reference newly created test pkg in test gallery --- test/InstallPSResource.Tests.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 57d025a90..76c8764c0 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -245,11 +245,9 @@ Describe 'Test Install-PSResource for Module' { } It "Validate that Pester is installing under Modules path" { - write-Output $PSGalleryName - Install-PSResource -Name "Pester" -Repository $PSGalleryName + Install-PSResource -Name "testModuleWithScript" -Repository $TestGalleryName - $res = Get-Module "Pester" -ListAvailable - Write-Output ($res) + $res = Get-Module "testModuleWithScript" -ListAvailable $res.Path.Contains("Modules") | Should -Be $true } } From 6a3803f1cde38d353bb97162756474c43d63460d Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:04:19 -0700 Subject: [PATCH 6/7] Update test/InstallPSResource.Tests.ps1 Co-authored-by: Paul Higinbotham --- test/InstallPSResource.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 76c8764c0..9dcfa1f30 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -244,7 +244,8 @@ Describe 'Test Install-PSResource for Module' { $res | Should -BeNullOrEmpty } - It "Validate that Pester is installing under Modules path" { + It "Validates that a module with module-name script files (like Pester) installs under Modules path" { + Install-PSResource -Name "testModuleWithScript" -Repository $TestGalleryName $res = Get-Module "testModuleWithScript" -ListAvailable From 2b5d7707a798b420c4179763ed646a1a2389d611 Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:04:33 -0700 Subject: [PATCH 7/7] Update src/code/InstallHelper.cs Co-authored-by: Anam Navied --- src/code/InstallHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 670cd2a15..ed5d89a47 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -378,7 +378,8 @@ private List InstallPackage(IEnumerable pkgsToInstall, s var scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1")); var modulePath = Path.Combine(tempDirNameVersion, (p.Name + ".psd1")); // Check if the package is a module or a script - var isModule = File.Exists(modulePath) ? true : false; + var isModule = File.Exists(modulePath); + if (isModule) {