Skip to content

Commit

Permalink
Merge pull request #3452 from gep13/add-pester-tests
Browse files Browse the repository at this point in the history
Add Pester tests to cover functionality added in Chocolatey CLI 2.3.0
  • Loading branch information
corbob committed May 30, 2024
2 parents 5959d7e + 0d7da1a commit 7e3326d
Show file tree
Hide file tree
Showing 25 changed files with 954 additions and 58 deletions.
2 changes: 1 addition & 1 deletion nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ function Invoke-Chocolatey-Initial {
try {
$chocoInstallationFolder = Get-ChocolateyInstallFolder
$chocoExe = Join-Path -Path $chocoInstallationFolder -ChildPath "choco.exe"
$runResult = & $chocoExe -v *>&1
$runResult = & $chocoExe -v 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Debug "Chocolatey CLI execution completed successfully."
}
Expand Down
14 changes: 12 additions & 2 deletions src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ public static void SetVariable(PSCmdlet cmdlet, string name, EnvironmentVariable

cmdlet.WriteDebug($"Registry type for {name} is/will be {registryType}");

registryKey.SetValue(name, value, registryType);
if (string.IsNullOrEmpty(value))
{
registryKey.DeleteValue(name, throwOnMissingValue: false);
}
else
{
registryKey.SetValue(name, value, registryType);
}
}

try
Expand Down Expand Up @@ -219,7 +226,10 @@ public static void UpdateSession(PSCmdlet cmdlet)
foreach (var name in GetVariableNames(scope))
{
var value = GetVariable(cmdlet, name, scope);
SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value);
if (!string.IsNullOrEmpty(value))
{
SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value);
}
}
}

Expand Down
39 changes: 22 additions & 17 deletions src/chocolatey.resources/helpers/chocolateyInstaller.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,31 @@ if (Test-Path $extensionsPath) {
# In effect we ensure that any command calls that match the name of one of our commands
# will resolve to _our_ commands (preferring licensed cmdlets in the case of a name collision),
# preventing packages from overriding them with their own commands and potentially breaking things.
$ExecutionContext.InvokeCommand.PreCommandLookupAction = {
param($command, $eventArgs)
#
# This functionality is only available in v3 and later, so using this in v2 will not
# work; check for the property before trying to set it.
if ($ExecutionContext.InvokeCommand.PreCommandLookupAction) {
$ExecutionContext.InvokeCommand.PreCommandLookupAction = {
param($command, $eventArgs)

# Don't run this handler for stuff PowerShell is looking up internally
if ($eventArgs.CommandOrigin -eq 'Runspace') {
$resolvedCommand = if ($command -in $chocolateyCmdlets.Licensed) {
Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction Ignore |
Where-Object { $_.Name -match "^$command(Cmdlet)?$" } |
Select-Object -First 1
}
elseif ($command -in $chocolateyCmdlets.Default) {
Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction Ignore
}
# Don't run this handler for stuff PowerShell is looking up internally
if ($eventArgs.CommandOrigin -eq 'Runspace') {
$resolvedCommand = if ($chocolateyCmdlets.Licensed -contains $command) {
Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction SilentlyContinue |
Where-Object { @($command, "$($command)Cmdlet") -contains $_.Name } |
Select-Object -First 1
}
elseif ($chocolateyCmdlets.Default -contains $command) {
Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction SilentlyContinue
}

if ($resolvedCommand) {
$eventArgs.Command = $resolvedCommand
$eventArgs.StopSearch = $true
if ($resolvedCommand) {
$eventArgs.Command = $resolvedCommand
$eventArgs.StopSearch = $true
}
}
}
}.GetNewClosure()
}.GetNewClosure()
}

# todo: explore removing this for a future version
Export-ModuleMember -Function * -Alias * -Cmdlet *
Expand Down
14 changes: 0 additions & 14 deletions tests/pester-tests/chocolatey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,6 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat
# $null = Invoke-Choco install MicrosoftWindowsPowerShellV2 -s windowsfeatures
}

# This is Foss only as PowerShell running under version 2 doesn't have .net available and can't import the Licensed DLL.
# Tests on Windows 7 show no issues with running Chocolatey under Windows 7 with PowerShell v2 aside from issues surrounding TLS versions that we cannot resolve without an upgrade to Windows 7.
It "Imports ChocolateyInstaller module successfully in PowerShell v2" -Tag FossOnly {
$command = 'try { $ErrorActionPreference = ''Stop''; Import-Module $env:ChocolateyInstall\helpers\chocolateyInstaller.psm1 } catch { $_ ; exit 1 }'
$result = & powershell.exe -Version 2 -noprofile -command $command
$LastExitCode | Should -BeExactly 0 -Because $result
}

It "Imports ChocolateyProfile module successfully in PowerShell v2" {
$command = 'try { $ErrorActionPreference = ''Stop''; Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1 } catch { $_ ; exit 1 }'
$result = & powershell.exe -Version 2 -noprofile -command $command
$LastExitCode | Should -BeExactly 0 -Because $result
}

Context "chocolateyScriptRunner.ps1" {
BeforeAll {
$Command = @'
Expand Down
35 changes: 35 additions & 0 deletions tests/pester-tests/commands/choco-apikey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,41 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, ApiKeyCommand {
}
}

Context "Adding an apikey when it is already added" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

# Ensure that the apikey is indeed set
$null = Invoke-Choco apikey add --source "https://somewhere.out/there/" --api-key "123-4567-89"

$Output = Invoke-Choco apikey add --source "https://somewhere.out/there/" --api-key "123-4567-89"
}

It "Exits with ExitCode 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 apikey add --source "https://somewhere.out/there/" --api-key "123-4567-89"
}

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.
Test-NuGetPaths
}
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
70 changes: 70 additions & 0 deletions tests/pester-tests/commands/choco-feature.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,76 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, FeatureCommand {
}
}

Context "Enabling a feature when it is already enabled" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

# Ensure that the feature is already enabled
$null = Invoke-Choco feature enable --name allowGlobalConfirmation

$Output = Invoke-Choco feature enable --name allowGlobalConfirmation
}

It "Exits with ExitCode 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 feature enable --name allowGlobalConfirmation
}

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 a feature when it is already disabled" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

# Ensure that the feature is already disabled
$null = Invoke-Choco feature disable --name allowGlobalConfirmation

$Output = Invoke-Choco feature disable --name allowGlobalConfirmation
}

It "Exits with ExitCode 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 feature disable --name allowGlobalConfirmation
}

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 usePackageRepositoryOptimizations" {
BeforeAll {
Restore-ChocolateyInstallSnapshot
Expand Down
43 changes: 41 additions & 2 deletions tests/pester-tests/commands/choco-info.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@
Remove-ChocolateyTestInstall
}

Context "Should include remembered arguments (including redacted) when using option --local-only" {
BeforeAll {
Initialize-ChocolateyTestInstall -Source $PSScriptRoot\testpackages

$Setup = Invoke-Choco install installpackage --package-parameters="bob" --password="secure-password" --confirm

$Setup.ExitCode | Should -Be 0 -Because $Setup.String

$Output = Invoke-Choco info installpackage --local-only
}

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

It "Should contain a summary" {
$Output.Lines | Should -Contain "Remembered Package Arguments:" -Because $Output.String
$Output.Lines | Should -Contain "--package-parameters='bob'" -Because $Output.String
$Output.Lines | Should -Contain "--password=[REDACTED ARGUMENT]" -Because $Output.String
}
}

Context "Should include configured sources" {
BeforeAll {
Initialize-ChocolateyTestInstall -Source $PSScriptRoot\testpackages
Invoke-Choco install installpackage --confirm

$Output = Invoke-Choco info installpackage --include-configured-sources --debug
}

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

It "Should mention that configured sources have been included" {
$Output.Lines | Should -Contain "Including sources from chocolatey.config file." -Because $Output.String
}
}

Context "Listing package information when package can be found" {
BeforeDiscovery {
$infoItems = @(
Expand Down Expand Up @@ -187,11 +226,11 @@
}

It 'Outputs warning about unable to load service index' {
$Output.Lines | Should -Contain "Unable to load the service index for source $InvalidSource."
$Output.Lines | Should -Contain "Unable to load the service index for source $InvalidSource." -Because $Output.String
}

It 'Output information about the package' {
$Output.String | Should -Match "Title: Chocolatey "
$Output.String | Should -Match "Title: Chocolatey " -Because $Output.String
}
}

Expand Down
Loading

0 comments on commit 7e3326d

Please sign in to comment.