Skip to content

Commit c012d38

Browse files
authored
WhatIf parameter should respect provided value instead of simply checking presence (#1925)
1 parent 80d1ef0 commit c012d38

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

src/code/InstallHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private List<PSResourceInfo> ProcessRepositories(
342342
allPkgsInstalled.AddRange(installedPkgs);
343343
}
344344

345-
if (!_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") && _pkgNamesToInstall.Count > 0)
345+
if ((!_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") || (SwitchParameter)_cmdletPassedIn.MyInvocation.BoundParameters["WhatIf"] == false) && _pkgNamesToInstall.Count > 0)
346346
{
347347
string repositoryWording = repositoryNamesToSearch.Count > 1 ? "registered repositories" : "repository";
348348
_cmdletPassedIn.WriteError(new ErrorRecord(
@@ -624,7 +624,7 @@ private List<PSResourceInfo> InstallPackages(
624624
}
625625

626626
// If -WhatIf is passed in, early out.
627-
if (_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf"))
627+
if (_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") && (SwitchParameter)_cmdletPassedIn.MyInvocation.BoundParameters["WhatIf"] == true)
628628
{
629629
return pkgsSuccessfullyInstalled;
630630
}

src/code/RegisterPSResourceRepository.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,20 @@ protected override void ProcessRecord()
186186
break;
187187

188188
case PSGalleryParameterSet:
189-
try
190-
{
191-
items.Add(PSGalleryParameterSetHelper(Priority, Trusted));
192-
}
193-
catch (Exception e)
189+
if (PSGallery)
194190
{
195-
ThrowTerminatingError(new ErrorRecord(
196-
new PSInvalidOperationException(e.Message),
197-
"ErrorInPSGalleryParameterSet",
198-
ErrorCategory.InvalidArgument,
199-
this));
191+
try
192+
{
193+
items.Add(PSGalleryParameterSetHelper(Priority, Trusted));
194+
}
195+
catch (Exception e)
196+
{
197+
ThrowTerminatingError(new ErrorRecord(
198+
new PSInvalidOperationException(e.Message),
199+
"ErrorInPSGalleryParameterSet",
200+
ErrorCategory.InvalidArgument,
201+
this));
202+
}
200203
}
201204
break;
202205

test/InstallPSResourceTests/InstallPSResourceRepositorySearching.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,12 @@ Describe 'Test Install-PSResource for searching and looping through repositories
137137
$err | Should -HaveCount 0
138138
$warningVar | Should -Not -BeNullOrEmpty
139139
}
140+
141+
It "install resources from repository should respect WhatIf value of false" {
142+
# Package "test_module" exists in the following repositories (in this order): localRepo, PSGallery, NuGetGallery
143+
$res = Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -SkipDependencyCheck -PassThru -WhatIf:$false
144+
$res | Should -Not -BeNullOrEmpty
145+
$res.Name | Should -Be $testModuleName
146+
$res.Repository | Should -Be $PSGalleryName
147+
}
140148
}

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
8585
$res.Priority | Should -Be 50
8686
}
8787

88+
It "register repository with PSGallery switch parameter value of false (PSGalleryParameterSet)" {
89+
Unregister-PSResourceRepository -Name $PSGalleryName
90+
$res = Register-PSResourceRepository -PSGallery:$false -PassThru
91+
$res | Should -BeNullOrEmpty
92+
}
93+
8894
It "register repository with PSGallery, Trusted parameters (PSGalleryParameterSet)" {
8995
Unregister-PSResourceRepository -Name $PSGalleryName
9096
$res = Register-PSResourceRepository -PSGallery -Trusted -PassThru

0 commit comments

Comments
 (0)