Skip to content

Commit cf71e38

Browse files
Respect -WhatIf in PowerShell package commands (#6588)
## 📖 Description Fixes #4622. Package commands run their work on an MTA thread. The existing `PowerShellCmdlet.ShouldProcess` implementation returned `true` from that thread instead of evaluating PowerShell's `PSCmdlet.ShouldProcess`, so `-WhatIf` could permit real package operations. Dispatch the decision through the existing PowerShell-thread bridge and return the actual `ShouldProcess` result. Add regression coverage for install, update, uninstall, MSIX repair, and export. Add a release note. ## 🔗 References Fixes #4622
1 parent add0d63 commit cf71e38

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎.github/actions/spelling/allow.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,4 @@ Xlang
450450
XResource
451451
XTOKEN
452452
xunit
453+
whatif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Bug Fixes
2+
3+
### Minor Bug Fixes
4+
5+
* Fixed PowerShell package commands ignoring `-WhatIf` and performing package operations during preview.

‎src/PowerShell/CommonFiles/PowerShellCmdlet.cs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,12 @@ internal void SetVariable(string variableName, object value)
502502
/// <returns>If the operation should continue.</returns>
503503
internal bool ShouldProcess(string target)
504504
{
505-
// If not on the main thread just continue.
506-
if (this.pwshThread != Thread.CurrentThread)
505+
bool shouldProcess = false;
506+
this.ExecuteInPowerShellThread(() =>
507507
{
508-
return true;
509-
}
510-
511-
return this.psCmdlet.ShouldProcess(target);
508+
shouldProcess = this.psCmdlet.ShouldProcess(target);
509+
});
510+
return shouldProcess;
512511
}
513512

514513
private void Complete()

‎src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,34 @@ Describe 'Install|Update|Uninstall-WinGetPackage' {
298298
}
299299
}
300300

301+
It 'Does not install with WhatIf' {
302+
$result = Install-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0' -WhatIf
303+
304+
$result | Should -BeNullOrEmpty
305+
(Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller -ErrorAction SilentlyContinue) | Should -BeNullOrEmpty
306+
}
307+
301308
It 'Install by Id' {
302309
$result = Install-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0'
303310
Validate-WinGetPackageOperationResult $result $expectedExeInstallerResult 'install'
304311
}
305312

313+
It 'Does not update with WhatIf' {
314+
$installedVersion = (Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller).InstalledVersion
315+
$result = Update-WinGetPackage -Id AppInstallerTest.TestExeInstaller -WhatIf
316+
317+
$result | Should -BeNullOrEmpty
318+
(Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller).InstalledVersion | Should -Be $installedVersion
319+
}
320+
321+
It 'Does not uninstall with WhatIf' {
322+
$installedVersion = (Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller).InstalledVersion
323+
$result = Uninstall-WinGetPackage -Id AppInstallerTest.TestExeInstaller -WhatIf
324+
325+
$result | Should -BeNullOrEmpty
326+
(Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller).InstalledVersion | Should -Be $installedVersion
327+
}
328+
306329
It 'Install by exact Name and Version' {
307330
$result = Install-WinGetPackage -Name TestPortableExe -Version '2.0.0.0' -MatchOption Equals
308331
Validate-WinGetPackageOperationResult $result $expectedPortableInstallerResult 'install'
@@ -369,6 +392,14 @@ Describe 'Install|Repair|Uninstall-WinGetPackage' {
369392
Validate-WinGetPackageOperationResult $result $expectedResult 'install'
370393
}
371394

395+
It 'Does not repair MSIX with WhatIf' {
396+
$installedVersion = (Get-WinGetPackage -Id AppInstallerTest.TestMsixInstaller).InstalledVersion
397+
$result = Repair-WinGetPackage -Id AppInstallerTest.TestMsixInstaller -WhatIf
398+
399+
$result | Should -BeNullOrEmpty
400+
(Get-WinGetPackage -Id AppInstallerTest.TestMsixInstaller).InstalledVersion | Should -Be $installedVersion
401+
}
402+
372403
It 'Repair MSIX By Id' {
373404
$result = Repair-WinGetPackage -Id AppInstallerTest.TestMsixInstaller
374405
Validate-WinGetPackageOperationResult $result $expectedResult 'repair'
@@ -594,6 +625,14 @@ Describe 'Export-WinGetPackage' {
594625
AddTestSource
595626
}
596627

628+
It 'Does not download with WhatIf' {
629+
$testDirectory = GetRandomTestDirectory
630+
$result = Export-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0' -DownloadDirectory $testDirectory -WhatIf
631+
632+
$result | Should -BeNullOrEmpty
633+
Test-Path -Path $testDirectory | Should -Be $false
634+
}
635+
597636
It 'Download by Id' {
598637
$testDirectory = GetRandomTestDirectory
599638
$result = Export-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0' -DownloadDirectory $testDirectory

0 commit comments

Comments
 (0)