Skip to content

Commit

Permalink
(chocolatey#3318) Add -WhatIf unit tests for helper commands
Browse files Browse the repository at this point in the history
Update-SessionEnvironment, Set-EnvironmentVariable, and
(Un)Install-ChocolateyPath implement ShouldProcess, so we can use
-WhatIf to verify their behaviour with unit tests.
  • Loading branch information
vexx32 committed Oct 25, 2024
1 parent 8034274 commit 707db73
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
29 changes: 29 additions & 0 deletions tests/helpers/common/Get-WhatIfResult.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-WhatIfResult {
<#
.SYNOPSIS
Runs a $Command in a new powershell.exe process, and then returns *only*
the output lines that are prefixed with 'What if:' which are written as
console output.
#>
[CmdletBinding()]
param(
# The script to execute in the new process.
[Parameter(Mandatory)]
[scriptblock]
$Command,

# Any setup scripts that are required for running. All output from this
# script block will be suppressed, if possible.
[Parameter()]
[scriptblock]
$Preamble
)

$commandString = @"
. {{ {0} }} *>&1 > `$null
& {{ {1} }}
"@ -f $Preamble, $Command

powershell -NoProfile -NonInteractive -Command $commandString |
Where-Object { $_ -like "What if:*" }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Describe 'Install-ChocolateyPath helper function tests' -Tags Cmdlets {
Describe 'Install-ChocolateyPath helper function tests' -Tags InstallChocolateyPath, Cmdlets {
BeforeAll {
Initialize-ChocolateyTestInstall

Expand All @@ -10,7 +10,37 @@
Remove-Module "chocolateyInstaller" -Force
}

Context 'Adding and removing PATH values' -ForEach @(
Context 'Unit tests' -Tags WhatIf -ForEach @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
Context 'Path "<_>"' -ForEach @("C:\test", "C:\tools") {
BeforeAll {
$Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'")
}

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 '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'
}
}
}

Context 'Adding and removing PATH values' -Tag VMOnly -ForEach @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
Expand Down Expand Up @@ -55,7 +85,7 @@
}
}

Describe 'Install-ChocolateyPath end-to-end tests with add-path package modifying <Scope> PATH' -Tags Cmdlet -ForEach @(
Describe 'Install-ChocolateyPath end-to-end tests with add-path package modifying <Scope> PATH' -Tags Cmdlet, UninstallChocolateyPath, VMOnly -ForEach @(
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
Describe 'Set-EnvironmentVariable helper function tests' -Tags Cmdlets {
Describe 'Set-EnvironmentVariable helper function tests' -Tags SetEnvironmentVariable, Cmdlets {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Unit tests' -Tags WhatIf -ForEach @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
It 'Sets an environment variable value at the target <Scope>' {
$testVariableName = 'testVariable'
$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""."

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".'
}
}
}

Context 'Sets an environment variable value at the target <Scope>' -ForEach @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
Describe 'Uninstall-ChocolateyPath helper function tests' -Tags Cmdlets {
Describe 'Uninstall-ChocolateyPath helper function tests' -Tags UninstallChocolateyPath, Cmdlets {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Adding and removing PATH values' -ForEach @(
Context 'Unit tests' -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
$Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'")
$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""."

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 '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'
}
}

Context 'Adding and removing PATH values' -Tags VMOnly -ForEach @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
Expand Down Expand Up @@ -40,7 +66,7 @@
}
}

Describe 'Uninstall-ChocolateyPath end-to-end tests with add-path package' -Tags Cmdlet -ForEach @(
Describe 'Uninstall-ChocolateyPath end-to-end tests with add-path package' -Tags Cmdlet, UninstallChocolateyPath, VMOnly -ForEach @(
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
Describe 'Update-SessionEnvironment helper function tests' -Tag Cmdlets {
Describe 'Update-SessionEnvironment helper function tests' -Tag UpdateSessionEnvironment, Cmdlets {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Unit tests' -Tag WhatIf {
It 'refreshes the current session environment variables' {
$Preamble = [scriptblock]::Create("Import-Module '$testLocation\helpers\chocolateyInstaller.psm1'")
$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".'
}
}

Context 'Refreshing environment' {
BeforeAll {
Expand Down

0 comments on commit 707db73

Please sign in to comment.