Skip to content

Commit 45d1c34

Browse files
authored
Fix 1794 by handling if prerelease is supplied as boolean too (#1843)
1 parent cfb6180 commit 45d1c34

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/code/InstallPSResource.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,14 @@ protected override void ProcessRecord()
290290
break;
291291

292292
case InputObjectParameterSet:
293-
foreach (var inputObj in InputObject) {
293+
foreach (var inputObj in InputObject)
294+
{
294295
string normalizedVersionString = Utils.GetNormalizedVersionString(inputObj.Version.ToString(), inputObj.Prerelease);
295296
ProcessInstallHelper(
296297
pkgNames: new string[] { inputObj.Name },
297298
pkgVersion: normalizedVersionString,
298299
pkgPrerelease: inputObj.IsPrerelease,
299-
pkgRepository: new string[]{ inputObj.Repository },
300+
pkgRepository: new string[] { inputObj.Repository },
300301
pkgCredential: Credential,
301302
reqResourceParams: null);
302303
}
@@ -430,8 +431,10 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
430431
WriteDebug("In InstallPSResource::RequiredResourceHelper()");
431432
foreach (DictionaryEntry entry in reqResourceHash)
432433
{
433-
InstallPkgParams pkgParams = new InstallPkgParams();
434+
InstallPkgParams pkgParams = new();
434435
PSCredential pkgCredential = Credential;
436+
string pkgVersion = String.Empty;
437+
bool isPrerelease = false;
435438

436439
// The package name will be the key for the inner hashtable and is present for all scenarios,
437440
// including the scenario where only package name is specified
@@ -450,8 +453,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
450453
return;
451454
}
452455

453-
string pkgVersion = String.Empty;
454-
if (!(entry.Value is Hashtable pkgInstallInfo))
456+
if (entry.Value is not Hashtable pkgInstallInfo)
455457
{
456458
var requiredResourceHashtableInputFormatError = new ErrorRecord(
457459
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)
467469
// Install-PSResource -RequiredResource @ { MyPackage = @{ version = '1.2.3', repository = 'PSGallery' } }
468470
if (pkgInstallInfo.Count != 0)
469471
{
470-
var pkgParamNames = pkgInstallInfo.Keys;
472+
ICollection pkgParamNames = pkgInstallInfo.Keys;
471473

472474
foreach (string paramName in pkgParamNames)
473475
{
@@ -491,13 +493,27 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
491493
}
492494

493495
pkgVersion = pkgInstallInfo["version"] == null ? String.Empty : pkgInstallInfo["version"].ToString();
496+
497+
// Prerelease - Handle both string and boolean
498+
object prereleaseObj = pkgInstallInfo.ContainsKey("prerelease") ? pkgInstallInfo["prerelease"] : null;
499+
if (prereleaseObj != null)
500+
{
501+
if (prereleaseObj is bool b)
502+
{
503+
isPrerelease = b;
504+
}
505+
else if (prereleaseObj is string s)
506+
{
507+
isPrerelease = s.Equals("true", StringComparison.OrdinalIgnoreCase);
508+
}
509+
}
494510
}
495511

496512
ProcessInstallHelper(
497513
pkgNames: new string[] { pkgName },
498514
pkgVersion: pkgVersion,
499-
pkgPrerelease: pkgParams.Prerelease,
500-
pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[]{},
515+
pkgPrerelease: isPrerelease,
516+
pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[] { },
501517
pkgCredential: pkgCredential,
502518
reqResourceParams: pkgParams);
503519
}
@@ -506,7 +522,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
506522
private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkgPrerelease, string[] pkgRepository, PSCredential pkgCredential, InstallPkgParams reqResourceParams)
507523
{
508524
WriteDebug("In InstallPSResource::ProcessInstallHelper()");
509-
var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard);
525+
var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard);
510526
if (nameContainsWildcard)
511527
{
512528
WriteError(new ErrorRecord(
@@ -549,7 +565,7 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
549565
this));
550566
}
551567

552-
var installedPkgs = _installHelper.BeginInstallPackages(
568+
IEnumerable<PSResourceInfo> installedPkgs = _installHelper.BeginInstallPackages(
553569
names: pkgNames,
554570
versionRange: versionRange,
555571
nugetVersion: nugetVersion,

test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
3131

3232
AfterEach {
3333
Uninstall-PSResource "test_module", "test_module2", "test_script", "TestModule99", "testModuleWithlicense", `
34-
"TestFindModule","ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", `
34+
"TestFindModule", "ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", `
3535
"TestModuleWithDependency", "TestModuleWithPrereleaseDep", "PrereleaseModule" -SkipDependencyCheck -ErrorAction SilentlyContinue
3636
}
3737

@@ -40,9 +40,9 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
4040
}
4141

4242
$testCases = [array](
43-
@{Name="*"; ErrorId="NameContainsWildcard"},
44-
@{Name="Test_Module*"; ErrorId="NameContainsWildcard"},
45-
@{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"}
43+
@{Name = "*"; ErrorId = "NameContainsWildcard" },
44+
@{Name = "Test_Module*"; ErrorId = "NameContainsWildcard" },
45+
@{Name = "Test?Module", "Test[Module"; ErrorId = "ErrorFilteringNamesForUnsupportedWildcards" }
4646
)
4747

4848
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' {
388388
$res.Name | Should -Contain $testModuleName
389389
}
390390

391+
# -RequiredResource
391392
It "Install modules using -RequiredResource with hashtable" {
392393
$rrHash = @{
393394
test_module = @{
@@ -418,6 +419,16 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
418419
$res3.Version | Should -Be "0.0.93"
419420
}
420421

422+
It "Install module using -RequiredResource with hashtable, and prerelease is boolean true" {
423+
Install-PSResource -TrustRepository -RequiredResource @{
424+
'TestModule99' = @{
425+
'repository' = 'PSGallery'
426+
'prerelease' = $true
427+
}
428+
}
429+
(Get-InstalledPSResource -Name 'TestModule99').'Prerelease' | Should -Be 'beta2'
430+
}
431+
421432
It "Install modules using -RequiredResource with JSON string" {
422433
$rrJSON = "{
423434
'test_module': {

0 commit comments

Comments
 (0)