Skip to content

Commit

Permalink
(#3088) Don't loop over null hook script variables
Browse files Browse the repository at this point in the history
PowerShell 2 had the behaviour that foreach would iterate over a null
variable
(https://devblogs.microsoft.com/powershell/new-v3-language-features/).
This has resulted in an issue if no hook scripts are passed in, then
chocolateyScriptRunner would cause an error. This update changes this to
do a nullcheck like we already had for the packageScript parameter.
  • Loading branch information
corbob authored and gep13 committed May 9, 2023
1 parent 392a154 commit 2e95fa3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Invoke-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $TestsLocation = Join-Path $PSScriptRoot tests
$MaxFileNameLength = 110
$LongFiles = Get-ChildItem $TestsLocation -Recurse |
Where-Object { ($_.FullName.Length - $TestsLocation.Length) -gt $MaxFileNameLength } |
Select-Object -Property @{Name = 'RelativePath' ; Expression = { $_.FullName.Replace($TestsLocation, [string]::Empty)}}, @{ Name = 'ReductionNeeded' ; Expression = { $_.FullName.Length - $TestsLocation.Length - $MaxFileNameLength } }
Select-Object -Property @{Name = 'RelativePath' ; Expression = { $_.FullName.Replace($TestsLocation, [string]::Empty) } }, @{ Name = 'ReductionNeeded' ; Expression = { $_.FullName.Length - $TestsLocation.Length - $MaxFileNameLength } }

if ($LongFiles) {
Write-Host "Tests' file paths may be too long for Test Kitchen use. Please shorten file names or paths:"
Expand Down Expand Up @@ -118,6 +118,9 @@ try {
}
)
}
Should = @{
ErrorAction = 'Continue'
}
}

Invoke-Pester -Configuration $PesterConfiguration
Expand Down
6 changes: 3 additions & 3 deletions src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $7zip = Join-Path $chocoTools '7z.exe'
$ShimGen = Join-Path $chocoTools 'shimgen.exe'
$checksumExe = Join-Path $chocoTools 'checksum.exe'

if ($PSBoundParameters.ContainsKey('preRunHookScripts')) {
if ($preRunHookScripts) {
foreach ($prehookscript in $preRunHookScripts) {
Write-Debug "Running Pre-Run Hook '$prehookscript'";
& "$prehookscript"
Expand Down Expand Up @@ -82,7 +82,7 @@ if ($exitCode -ne $null -and $exitCode -ne '' -and $exitCode -ne 0) {
Set-PowerShellExitCode $exitCode
}

if ($PSBoundParameters.ContainsKey('postRunHookScripts')) {
if ($postRunHookScripts) {
foreach ($posthookscript in $postRunHookScripts) {
Write-Debug "Running Post-Run Hook '$posthookscript'";
& "$posthookscript"
Expand All @@ -91,4 +91,4 @@ if ($PSBoundParameters.ContainsKey('postRunHookScripts')) {

Write-Debug '----------------------------------------------------------------------'

Exit $exitCode
Exit $exitCode
40 changes: 40 additions & 0 deletions tests/chocolatey-tests/chocolatey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,46 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat
& powershell.exe -Version 2 -noprofile -command $command
$LastExitCode | Should -BeExactly 0
}

Context "chocolateyScriptRunner.ps1" {
BeforeAll {
$Command = @'
& "$env:ChocolateyInstall\helpers\chocolateyScriptRunner.ps1" -packageScript '{0}' -installArguments '' -packageParameters '' -preRunHookScripts '{1}' -postRunHookScripts '{2}'
exit $error.count
'@
'Write-Host "packageScript"' > packageScript.ps1
'Write-Host "preRunHookScript"' > preRunHookScript.ps1
'Write-Host "postRunHookScript"' > postRunHookScript.ps1
}

It "Handles just a packageScript" {
$commandToExecute = $Command -f "$PWD/packageScript.ps1", $null, $null
$output = & powershell.exe -Version 2 -noprofile -command $commandToExecute
$LastExitCode | Should -BeExactly 0 -Because ($output -join ([Environment]::NewLine))
$output | Should -Be @('packageScript') -Because ($output -join ([Environment]::NewLine))
}

It "Handles a packageScript with a preRunHookScript" {
$commandToExecute = $Command -f "$PWD/packageScript.ps1", "$PWD/preRunHookScript.ps1", $null
$output = & powershell.exe -Version 2 -noprofile -command $commandToExecute
$LastExitCode | Should -BeExactly 0 -Because ($output -join ([Environment]::NewLine))
$output | Should -Be @('preRunHookScript','packageScript') -Because ($output -join ([Environment]::NewLine))
}

It "Handles a packageScript with a preRunHookScript and postRunHookScript" {
$commandToExecute = $Command -f "$PWD/packageScript.ps1", "$PWD/preRunHookScript.ps1", "$PWD/postRunHookScript.ps1"
$output = & powershell.exe -Version 2 -noprofile -command $commandToExecute
$LastExitCode | Should -BeExactly 0 -Because ($output -join ([Environment]::NewLine))
$output | Should -Be @('preRunHookScript','packageScript', 'postRunHookScript') -Because ($output -join ([Environment]::NewLine))
}

It "Handles a packageScript with and postRunHookScript" {
$commandToExecute = $Command -f "$PWD/packageScript.ps1", $null, "$PWD/postRunHookScript.ps1"
$output = & powershell.exe -Version 2 -noprofile -command $commandToExecute
$LastExitCode | Should -BeExactly 0 -Because ($output -join ([Environment]::NewLine))
$output | Should -Be @('packageScript', 'postRunHookScript') -Because ($output -join ([Environment]::NewLine))
}
}
}

# This is skipped when not run in CI because it modifies the local system.
Expand Down

0 comments on commit 2e95fa3

Please sign in to comment.