Skip to content

Commit

Permalink
(chocolatey#1760) 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
unset a config value that doesn't exist, or try to set a config value
where the value is already set to that value, 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 d14c8af commit 876d433
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion tests/pester-tests/commands/choco-config.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ Describe "choco config" -Tag Chocolatey, ConfigCommand {
}
}

Context "Setting a configuration setting (cacheLocation) with same value that already exists" {
BeforeAll {
$null = Invoke-Choco config set cacheLocation "C:\temp\choco"

$Output = Invoke-Choco config set cacheLocation "C:\temp\choco"
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Changes Nothing" {
$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 config set cacheLocation "C:\temp\choco"
}

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 "Setting a configuration setting not available by default (newConfiguration)" {
BeforeAll {
$Output = Invoke-Choco config set --name newConfiguration --value some-value
Expand Down Expand Up @@ -261,6 +293,7 @@ Describe "choco config" -Tag Chocolatey, ConfigCommand {

Context "Unsetting a configuration that doesn't exist" {
BeforeAll {
$null = Disable-ChocolateyFeature -Name "useEnhancedExitCodes"
$Output = Invoke-Choco config unset not-existing

[xml]$ConfigFileContent = Get-Content $env:ChocolateyInstall\config\chocolatey.config
Expand All @@ -275,14 +308,30 @@ Describe "choco config" -Tag Chocolatey, ConfigCommand {
$Output.Lines | Should -Contain $expectedLicenseHeader
}

It "Displays config value Added" {
It "Changes nothing" {
$Output.Lines | Should -Contain "Nothing to change. Config already set."
}

It "No value is added to file" {
$value = $configs.Where{ $_.key -eq "not-existing" }
$value | Should -HaveCount 0
}

Context "when using enhanced exit codes" {
BeforeAll {
$null = Enable-ChocolateyFeature -Name "useEnhancedExitCodes"

$Output = Invoke-Choco config unset 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."
}
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Expand Down

0 comments on commit 876d433

Please sign in to comment.