From 385749e3bd2494c5811ae5ccbad8ef330e0f4eba Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Thu, 23 May 2024 03:05:31 -0700 Subject: [PATCH] (#2200) Add test for new enhanced exit code Now, when you have useEnhancedExitCodes turned on, if you try to upgrade a package, whether directly, or via the all keyword, and there are no upgrade available, the exit code will change to be a 2, giving a clear indication that no action was taken. This commit adds a Pester test to verify that this works as expected. --- .../commands/choco-upgrade.Tests.ps1 | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/pester-tests/commands/choco-upgrade.Tests.ps1 b/tests/pester-tests/commands/choco-upgrade.Tests.ps1 index 1521a0bf7c..8346d9dde8 100644 --- a/tests/pester-tests/commands/choco-upgrade.Tests.ps1 +++ b/tests/pester-tests/commands/choco-upgrade.Tests.ps1 @@ -73,6 +73,77 @@ } } + Context "Attempt to upgrade a package when there isn't an upgrade available" -Tag Internal { + BeforeAll { + Restore-ChocolateyInstallSnapshot + + Enable-ChocolateySource -Name hermes-setup + $null = Invoke-Choco install wget + + $Output = Invoke-Choco upgrade wget + } + + It 'Exits with Success (0)' { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It 'Displays that upgrade was attempted but wasnt required' { + $Output.Lines | Should -Contain "Chocolatey upgraded 0/1 packages." -Because $Output.String + } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco upgrade wget + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It 'Displays that upgrade was attempted but wasnt required' { + $Output.Lines | Should -Contain "Chocolatey upgraded 0/1 packages." -Because $Output.String + } + } + } + + Context "Attempt to run upgrade all when there isn't any upgrade available" -Tag Internal { + BeforeAll { + Restore-ChocolateyInstallSnapshot + + Enable-ChocolateySource -Name hermes-setup + $null = Invoke-Choco install wget + $null = Invoke-Choco install curl + + $Output = Invoke-Choco upgrade all + } + + It 'Exits with Success (0)' { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It 'Displays that upgrade was attempted but wasnt required' { + $Output.Lines | Should -Contain "Chocolatey upgraded 0/2 packages." -Because $Output.String + } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco upgrade all + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It 'Displays that upgrade was attempted but wasnt required' { + $Output.Lines | Should -Contain "Chocolatey upgraded 0/2 packages." -Because $Output.String + } + } + } + # We exclude this test when running CCM, as it will install and remove # the firefox package which is used through other tests that will be affected. Context "Upgrading packages while remembering arguments with multiple packages using arguments" -Tag CCMExcluded, Internal, VMOnly {