diff --git a/Invoke-Tests.ps1 b/Invoke-Tests.ps1 index 525378256c..6dd9d199a0 100644 --- a/Invoke-Tests.ps1 +++ b/Invoke-Tests.ps1 @@ -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:" @@ -118,6 +118,9 @@ try { } ) } + Should = @{ + ErrorAction = 'Continue' + } } Invoke-Pester -Configuration $PesterConfiguration diff --git a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 index 7557d347c9..078290b22e 100644 --- a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 +++ b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 @@ -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" @@ -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" @@ -91,4 +91,4 @@ if ($PSBoundParameters.ContainsKey('postRunHookScripts')) { Write-Debug '----------------------------------------------------------------------' -Exit $exitCode \ No newline at end of file +Exit $exitCode diff --git a/tests/chocolatey-tests/chocolatey.Tests.ps1 b/tests/chocolatey-tests/chocolatey.Tests.ps1 index 5713a6d728..ebac56259d 100644 --- a/tests/chocolatey-tests/chocolatey.Tests.ps1 +++ b/tests/chocolatey-tests/chocolatey.Tests.ps1 @@ -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.