Skip to content

Commit

Permalink
Merge pull request #3554 from vexx32/fix-whatif-tests
Browse files Browse the repository at this point in the history
(#3318) Fix -WhatIf tests
  • Loading branch information
corbob authored Nov 8, 2024
2 parents 475f80b + c0e6e79 commit 8e7582f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
8 changes: 6 additions & 2 deletions tests/helpers/common/Get-WhatIfResult.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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:*" })
}
}
4 changes: 2 additions & 2 deletions tests/pester-tests/features/PageSize.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
}
) {
BeforeAll {
Disable-ChocolateySource
Enable-ChocolateySource -Name hermes
$Output = Invoke-Choco $Command --page-size $ProvidedSize
}

Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Unit tests' -Tags WhatIf -ForEach @(
Context 'Unit tests (Scope: <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)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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".'
}
}

Expand Down

0 comments on commit 8e7582f

Please sign in to comment.