Skip to content

Commit

Permalink
(chocolatey#1764) Add test for new enhanced exit code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gep13 committed May 24, 2024
1 parent 876d433 commit bc6c8ec
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions tests/pester-tests/commands/choco-source.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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" {
Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand All @@ -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.
Expand Down

0 comments on commit bc6c8ec

Please sign in to comment.