diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index d8af185f0..59317837b 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -290,13 +290,14 @@ protected override void ProcessRecord() break; case InputObjectParameterSet: - foreach (var inputObj in InputObject) { + foreach (var inputObj in InputObject) + { string normalizedVersionString = Utils.GetNormalizedVersionString(inputObj.Version.ToString(), inputObj.Prerelease); ProcessInstallHelper( pkgNames: new string[] { inputObj.Name }, pkgVersion: normalizedVersionString, pkgPrerelease: inputObj.IsPrerelease, - pkgRepository: new string[]{ inputObj.Repository }, + pkgRepository: new string[] { inputObj.Repository }, pkgCredential: Credential, reqResourceParams: null); } @@ -430,8 +431,10 @@ private void RequiredResourceHelper(Hashtable reqResourceHash) WriteDebug("In InstallPSResource::RequiredResourceHelper()"); foreach (DictionaryEntry entry in reqResourceHash) { - InstallPkgParams pkgParams = new InstallPkgParams(); + InstallPkgParams pkgParams = new(); PSCredential pkgCredential = Credential; + string pkgVersion = String.Empty; + bool isPrerelease = false; // The package name will be the key for the inner hashtable and is present for all scenarios, // including the scenario where only package name is specified @@ -450,8 +453,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash) return; } - string pkgVersion = String.Empty; - if (!(entry.Value is Hashtable pkgInstallInfo)) + if (entry.Value is not Hashtable pkgInstallInfo) { var requiredResourceHashtableInputFormatError = new ErrorRecord( new ArgumentException($"The RequiredResource input with name '{pkgName}' does not have a valid value, the value must be a hashtable."), @@ -467,7 +469,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash) // Install-PSResource -RequiredResource @ { MyPackage = @{ version = '1.2.3', repository = 'PSGallery' } } if (pkgInstallInfo.Count != 0) { - var pkgParamNames = pkgInstallInfo.Keys; + ICollection pkgParamNames = pkgInstallInfo.Keys; foreach (string paramName in pkgParamNames) { @@ -491,13 +493,27 @@ private void RequiredResourceHelper(Hashtable reqResourceHash) } pkgVersion = pkgInstallInfo["version"] == null ? String.Empty : pkgInstallInfo["version"].ToString(); + + // Prerelease - Handle both string and boolean + object prereleaseObj = pkgInstallInfo.ContainsKey("prerelease") ? pkgInstallInfo["prerelease"] : null; + if (prereleaseObj != null) + { + if (prereleaseObj is bool b) + { + isPrerelease = b; + } + else if (prereleaseObj is string s) + { + isPrerelease = s.Equals("true", StringComparison.OrdinalIgnoreCase); + } + } } ProcessInstallHelper( pkgNames: new string[] { pkgName }, pkgVersion: pkgVersion, - pkgPrerelease: pkgParams.Prerelease, - pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[]{}, + pkgPrerelease: isPrerelease, + pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[] { }, pkgCredential: pkgCredential, reqResourceParams: pkgParams); } @@ -506,7 +522,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash) private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkgPrerelease, string[] pkgRepository, PSCredential pkgCredential, InstallPkgParams reqResourceParams) { WriteDebug("In InstallPSResource::ProcessInstallHelper()"); - var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard); + var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard); if (nameContainsWildcard) { WriteError(new ErrorRecord( @@ -549,7 +565,7 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg this)); } - var installedPkgs = _installHelper.BeginInstallPackages( + IEnumerable installedPkgs = _installHelper.BeginInstallPackages( names: pkgNames, versionRange: versionRange, nugetVersion: nugetVersion, diff --git a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 index 0b03a984b..bf97bb96d 100644 --- a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 @@ -31,7 +31,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { AfterEach { Uninstall-PSResource "test_module", "test_module2", "test_script", "TestModule99", "testModuleWithlicense", ` - "TestFindModule","ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", ` + "TestFindModule", "ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", ` "TestModuleWithDependency", "TestModuleWithPrereleaseDep", "PrereleaseModule" -SkipDependencyCheck -ErrorAction SilentlyContinue } @@ -40,9 +40,9 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } $testCases = [array]( - @{Name="*"; ErrorId="NameContainsWildcard"}, - @{Name="Test_Module*"; ErrorId="NameContainsWildcard"}, - @{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} + @{Name = "*"; ErrorId = "NameContainsWildcard" }, + @{Name = "Test_Module*"; ErrorId = "NameContainsWildcard" }, + @{Name = "Test?Module", "Test[Module"; ErrorId = "ErrorFilteringNamesForUnsupportedWildcards" } ) It "Should not install resource with wildcard in name" -TestCases $testCases { @@ -388,6 +388,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { $res.Name | Should -Contain $testModuleName } + # -RequiredResource It "Install modules using -RequiredResource with hashtable" { $rrHash = @{ test_module = @{ @@ -418,6 +419,16 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { $res3.Version | Should -Be "0.0.93" } + It "Install module using -RequiredResource with hashtable, and prerelease is boolean true" { + Install-PSResource -TrustRepository -RequiredResource @{ + 'TestModule99' = @{ + 'repository' = 'PSGallery' + 'prerelease' = $true + } + } + (Get-InstalledPSResource -Name 'TestModule99').'Prerelease' | Should -Be 'beta2' + } + It "Install modules using -RequiredResource with JSON string" { $rrJSON = "{ 'test_module': {