From 9bf0093283eee15f75832269858531bf95b489c5 Mon Sep 17 00:00:00 2001 From: Rain Sallow Date: Thu, 7 Nov 2024 11:27:33 -0500 Subject: [PATCH 1/2] (#3318) Fix -WhatIf tests A couple of small errors in the -WhatIf tests showed up on teamcity, so this should hopefully fix those. --- tests/helpers/common/Get-WhatIfResult.ps1 | 8 +++- .../Install-ChocolateyPath.Tests.ps1 | 37 +++++++++---------- .../Set-EnvironmentVariable.Tests.ps1 | 8 ++-- .../Uninstall-ChocolateyPath.Tests.ps1 | 20 ++++++---- .../Update-SessionEnvironment.Tests.ps1 | 2 +- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/tests/helpers/common/Get-WhatIfResult.ps1 b/tests/helpers/common/Get-WhatIfResult.ps1 index 53e38590d..a38d8d29b 100644 --- a/tests/helpers/common/Get-WhatIfResult.ps1 +++ b/tests/helpers/common/Get-WhatIfResult.ps1 @@ -24,6 +24,10 @@ & {{ {1} }} '@ -f $Preamble, $Command - powershell -NoProfile -NonInteractive -Command $commandString | - Where-Object { $_ -like "What if:*" } + $results = @(powershell -NoProfile -NonInteractive -Command $commandString) + + [pscustomobject]@{ + Output = $results + WhatIf = @($results | Where-Object { $_ -like "What if:*" }) + } } \ No newline at end of file diff --git a/tests/pester-tests/powershell-commands/Install-ChocolateyPath.Tests.ps1 b/tests/pester-tests/powershell-commands/Install-ChocolateyPath.Tests.ps1 index d6e4d094c..5f3ad5272 100644 --- a/tests/pester-tests/powershell-commands/Install-ChocolateyPath.Tests.ps1 +++ b/tests/pester-tests/powershell-commands/Install-ChocolateyPath.Tests.ps1 @@ -15,29 +15,28 @@ @{ Scope = 'User' } @{ Scope = 'Machine' } ) { - Context 'Path "<_>"' -ForEach @("C:\test", "C:\tools") { - BeforeAll { - $Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'") - } + BeforeAll { + $Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1' -Global") + } - It 'stores the value in the desired PATH scope' { - $Command = [scriptblock]::Create("Install-ChocolateyPath -Path '$_' -Scope $Scope -WhatIf") - - $results = @( Get-WhatIfResult -Preamble $Preamble -Command $Command ) - $results[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""PATH""." - - if ($Scope -ne 'Process') { - $results[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' - $results[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' - } - } + It 'stores the value <_> in the desired PATH scope' -TestCases @("C:\test", "C:\tools") { + $Command = [scriptblock]::Create("Install-ChocolateyPath -Path '$_' -Scope $Scope -WhatIf") + + $results = Get-WhatIfResult -Preamble $Preamble -Command $Command + $results.WhatIf[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""PATH""." -Because $results.Output - It 'skips adding the value if it is already present' { - $targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -First 1 - $Command = [scriptblock]::Create("Install-ChocolateyPath -Path '$targetPathEntry' -Scope $Scope -WhatIf") - Get-WhatIfResult -Preamble $Preamble -Command $Command | Should -BeNullOrEmpty -Because 'we should skip adding values that already exist' + if ($Scope -ne 'Process') { + $results.WhatIf[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' -Because $results.Output + $results.WhatIf[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' -Because $results.Output } } + + It 'skips adding the value if it is already present' { + $targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -Last 1 + $Command = [scriptblock]::Create("Install-ChocolateyPath -Path '$targetPathEntry' -Scope $Scope -WhatIf") + $result = Get-WhatIfResult -Preamble $Preamble -Command $Command + $result.WhatIf | Should -BeNullOrEmpty -Because "we should skip adding values that already exist. Output:`n$($result.Output)" + } } Context 'Adding and removing PATH values' -Tag VMOnly -ForEach @( diff --git a/tests/pester-tests/powershell-commands/Set-EnvironmentVariable.Tests.ps1 b/tests/pester-tests/powershell-commands/Set-EnvironmentVariable.Tests.ps1 index f750e75aa..6dbe0a913 100644 --- a/tests/pester-tests/powershell-commands/Set-EnvironmentVariable.Tests.ps1 +++ b/tests/pester-tests/powershell-commands/Set-EnvironmentVariable.Tests.ps1 @@ -16,12 +16,12 @@ Describe 'Set-EnvironmentVariable helper function tests' -Tags SetEnvironmentVar $Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'") $Command = [scriptblock]::Create("Set-EnvironmentVariable -Name $testVariableName -Value 'TEST' -Scope $Scope -WhatIf") - $results = @( Get-WhatIfResult -Preamble $Preamble -Command $Command ) - $results[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""testVariable""." + $results = Get-WhatIfResult -Preamble $Preamble -Command $Command + $results.WhatIf[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""testVariable""." -Because $results.Output if ($Scope -ne 'Process') { - $results[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' - $results[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' + $results.WhatIf[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' -Because $results.Output + $results.WhatIf[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' -Because $results.Output } } } diff --git a/tests/pester-tests/powershell-commands/Uninstall-ChocolateyPath.Tests.ps1 b/tests/pester-tests/powershell-commands/Uninstall-ChocolateyPath.Tests.ps1 index 489a170fb..238a40585 100644 --- a/tests/pester-tests/powershell-commands/Uninstall-ChocolateyPath.Tests.ps1 +++ b/tests/pester-tests/powershell-commands/Uninstall-ChocolateyPath.Tests.ps1 @@ -6,29 +6,33 @@ Import-Module "$testLocation\helpers\chocolateyInstaller.psm1" } - Context 'Unit tests' -Tags WhatIf -ForEach @( + Context 'Unit tests (Scope: )' -Tags WhatIf -ForEach @( @{ Scope = 'Process' } @{ Scope = 'User' } @{ Scope = 'Machine' } ) { - It 'removes a stored PATH value in the desired PATH scope' { - $targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -First 1 + BeforeAll { $Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'") + } + + It 'removes a stored PATH value in the desired PATH scope' { + $targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -Last 1 $Command = [scriptblock]::Create("Uninstall-ChocolateyPath -Path '$targetPathEntry' -Scope $Scope -WhatIf") - $results = @( Get-WhatIfResult -Preamble $Preamble -Command $Command ) - $results[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""PATH""." + $results = Get-WhatIfResult -Preamble $Preamble -Command $Command + $results.WhatIf[0] | Should -BeExactly "What if: Performing the operation ""Set $Scope environment variable"" on target ""PATH""." -Because $results.Output if ($Scope -ne 'Process') { - $results[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' - $results[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' + $results.WhatIf[1] | Should -BeExactly 'What if: Performing the operation "Notify system of changes" on target "Environment variables".' -Because $results.Output + $results.WhatIf[2] | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' -Because $results.Output } } It 'skips removing the value if it is not present' { $targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -First 1 $Command = [scriptblock]::Create("Uninstall-ChocolateyPath -Path 'C:\ThisShouldNotBePresent' -Scope $Scope -WhatIf") - Get-WhatIfResult -Preamble $Preamble -Command $Command | Should -BeNullOrEmpty -Because 'we should skip removing a value that does not exist' + $result = Get-WhatIfResult -Preamble $Preamble -Command $Command + $result.WhatIf | Should -BeNullOrEmpty -Because "we should skip removing a value that does not exist. Output:`n$($result.Output)" } } diff --git a/tests/pester-tests/powershell-commands/Update-SessionEnvironment.Tests.ps1 b/tests/pester-tests/powershell-commands/Update-SessionEnvironment.Tests.ps1 index 7a1c081a1..67cd0acf0 100644 --- a/tests/pester-tests/powershell-commands/Update-SessionEnvironment.Tests.ps1 +++ b/tests/pester-tests/powershell-commands/Update-SessionEnvironment.Tests.ps1 @@ -12,7 +12,7 @@ $Command = [scriptblock]::Create("Update-SessionEnvironment -WhatIf") $results = Get-WhatIfResult -Preamble $Preamble -Command $Command - $results | Should -BeExactly 'What if: Performing the operation "refresh all environment variables" on target "current process".' + $results.WhatIf | Should -BeExactly 'What if: Performing the operation "Refresh all environment variables" on target "Current process".' } } From c0e6e79b31222708629b468297eca1850a8018bf Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Thu, 7 Nov 2024 12:13:48 -0800 Subject: [PATCH 2/2] (#3502) Update tests to disable extra sources The Test Kitchen environment has both `hermes` and `hermes-all` enabled. This results in a duplication of the warning message. This disables all the sources and then enables just the `hermes` source. # Please enter the commit message for your changes. Lines starting --- tests/pester-tests/features/PageSize.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/pester-tests/features/PageSize.Tests.ps1 b/tests/pester-tests/features/PageSize.Tests.ps1 index a0b846522..f6bedb51d 100644 --- a/tests/pester-tests/features/PageSize.Tests.ps1 +++ b/tests/pester-tests/features/PageSize.Tests.ps1 @@ -34,6 +34,8 @@ } ) { BeforeAll { + Disable-ChocolateySource + Enable-ChocolateySource -Name hermes $Output = Invoke-Choco $Command --page-size $ProvidedSize } @@ -55,8 +57,6 @@ } } foreach ($message in $ExpectedMessage) { - # The output here may contain duplicated line for the warning about non-30 page size. - # We have been unable to reproduce this output in any scenario other than in these tests. $Output.Lines | Should -Contain $message -Because $Output.String } }