From 64726556cb47991b303a73bd612b5a3cd1df362d Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Thu, 23 May 2024 02:37:59 -0700 Subject: [PATCH] (#1764) Add test for new enhanced exit code Now, when you have useEnhancedExitCodes turned on, if you try to add an already existing source, remove a source that doesn't exist, disable a source that is already disabled, or enable a source that is already enabled, 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-source.Tests.ps1 | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/tests/pester-tests/commands/choco-source.Tests.ps1 b/tests/pester-tests/commands/choco-source.Tests.ps1 index 701550edad..1ee40fa5bc 100644 --- a/tests/pester-tests/commands/choco-source.Tests.ps1 +++ b/tests/pester-tests/commands/choco-source.Tests.ps1 @@ -67,6 +67,45 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { } } + Context "Add source that already exists" { + BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + + # Ensure source is not available + Invoke-Choco $CurrentCommand add --name "already-exists" --source "https://somewhere/out/there/" + + $Output = Invoke-Choco $CurrentCommand add --name "already-exists" --source "https://somewhere/out/there/" + } + + It "Exits with Success (0)" { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It "Displays chocolatey name with version" { + $Output.Lines | Should -Contain $expectedHeader + } + + It "Displays no change made" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand add --name "already-exists" --source "https://somewhere/out/there/" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } + } + Context "Add single unauthenticated source with priority" { BeforeAll { $Output = Invoke-Choco $CurrentCommand add --name "dummy-long" --source "https://priority.test.com/api" --priority 1 @@ -304,6 +343,8 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { Context "Removing missing source" { BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + # Make sure the source is removed Invoke-Choco $CurrentCommand remove --name "not-existing" @@ -321,6 +362,22 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { It "Displays message about no change made" { $Output.Lines | Should -Contain "Nothing to change. Config already set." } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand remove --name "not-existing" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } } Context "Disabling existing source" { @@ -387,6 +444,8 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { Context "Disabling missing source" { BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + # Ensure source is not available Invoke-Choco $CurrentCommand remove --name "not-existing" @@ -404,10 +463,68 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { It "Displays no change made" { $Output.Lines | Should -Contain "Nothing to change. Config already set." } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand disable --name "not-existing" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } + } + + Context "Disabling source that is already disabled" { + BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + + # Ensure source is not available + Invoke-Choco $CurrentCommand add --name "already-disabled" --source "https://somewhere/out/there/" + Invoke-Choco $CurrentCommand disable --name "already-disabled" + + $Output = Invoke-Choco $CurrentCommand disable --name "already-disabled" + } + + It "Exits with Success (0)" { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It "Displays chocolatey name with version" { + $Output.Lines | Should -Contain $expectedHeader + } + + It "Displays no change made" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand disable --name "already-disabled" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } } Context "Enabling missing source" { BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + # Ensure source is not available Invoke-Choco $CurrentCommand remove --name "not-existing" @@ -425,6 +542,61 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, SourceCommand { It "Displays no change made" { $Output.Lines | Should -Contain "Nothing to change. Config already set." } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand enable --name "not-existing" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } + } + + Context "Enabling source that is already enabled" { + BeforeAll { + $null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes" + + # Ensure source is enable + Invoke-Choco $CurrentCommand add --name "already-enabled" --source "https://somewhere/out/there/" + + $Output = Invoke-Choco $CurrentCommand enable --name "already-enabled" + } + + It "Exits with Success (0)" { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It "Displays chocolatey name with version" { + $Output.Lines | Should -Contain $expectedHeader + } + + It "Displays no change made" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + + Context "when using enhanced exit codes" { + BeforeAll { + $null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes" + + $Output = Invoke-Choco $CurrentCommand enable --name "already-enabled" + } + + It "Exits with ExitCode 2" { + $Output.ExitCode | Should -Be 2 -Because $Output.String + } + + It "Changes Nothing" { + $Output.Lines | Should -Contain "Nothing to change. Config already set." + } + } } # This needs to be the last test in this block, to ensure NuGet configurations aren't being created.