diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 8390cb2ae7..0685018c10 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -96,6 +96,9 @@ private IEnumerable get_hook_scripts(ChocolateyConfiguration configurati // If skipping hook scripts, return an empty list if (configuration.SkipHookScripts) return hookScriptPaths; + // If hooks directory doesn't exist, return an empty list to prevent directory not exist warnings + if (!_fileSystem.directory_exists(ApplicationParameters.HooksLocation)) return hookScriptPaths; + string filenameBase; if (isPreHook) diff --git a/tests/chocolatey-tests/Hooks.Tests.ps1 b/tests/chocolatey-tests/Hooks.Tests.ps1 new file mode 100644 index 0000000000..2087bdbe5b --- /dev/null +++ b/tests/chocolatey-tests/Hooks.Tests.ps1 @@ -0,0 +1,93 @@ +Import-Module helpers/common-helpers + +Describe "hooks tests" -Tag Chocolatey, Hooks { + BeforeDiscovery { + $Flags = @( + @{ Flag = '' ; RunsHooks = $true ; Command = 'install' } + @{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'install' } + @{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'install' } + + @{ Flag = '' ; RunsHooks = $true ; Command = 'uninstall' } + @{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'uninstall' } + @{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'uninstall' } + + @{ Flag = '' ; RunsHooks = $true ; Command = 'upgrade' } + @{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'upgrade' } + @{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'upgrade' } + + ) + } + BeforeAll { + Initialize-ChocolateyTestInstall + # Add the hooks package to the test install so that it is available in each snapshot. + $null = Invoke-Choco install scriptpackage.hook --version 1.0.0 + New-ChocolateyInstallSnapshot + } + + AfterAll { + Remove-ChocolateyTestInstall + } + + Context ' hooks with flag: ' -ForEach $Flags { + BeforeAll { + Restore-ChocolateyInstallSnapshot + $Package = 'upgradepackage' + + if ($Command -ne 'install') { + $ver = if ($Command -eq 'upgrade') { + '1.0.0' + } + else { + '1.1.0' + } + $null = Invoke-Choco install $Package --version $ver --no-progress + } + + $Output = Invoke-Choco $Command $Package $Flag --no-progress + } + + # Uninstall/Upgrade exit -1: https://github.com/chocolatey/choco/issues/2822 + It "Exits with Success (0,-1)" { + $Output.ExitCode | Should -BeIn @(0, -1) -Because $Output.String + } + + It "Should execute hooks ()" { + $Version = '1.1.0' + + $Messages = @( + if ($Command -eq 'uninstall') { + "pre-uninstall-all.ps1 hook ran for $Package $Version" + "post-uninstall-all.ps1 hook ran for $Package $Version" + "pre-uninstall-$Package.ps1 hook ran for $Package $Version" + "post-uninstall-$Package.ps1 hook ran for $Package $Version" + "pre-beforemodify-all.ps1 hook ran for $Package $Version" + "post-beforemodify-all.ps1 hook ran for $Package $Version" + "pre-beforemodify-$Package.ps1 hook ran for $Package $Version" + "post-beforemodify-$Package.ps1 hook ran for $Package $Version" + } + else { + "pre-install-all.ps1 hook ran for $Package $Version" + "post-install-all.ps1 hook ran for $Package $Version" + "pre-install-$Package.ps1 hook ran for $Package $Version" + "post-install-$Package.ps1 hook ran for $Package $Version" + } + + if ($Command -eq 'upgrade') { + "pre-beforemodify-all.ps1 hook ran for $Package 1.0.0" + "pre-beforemodify-$Package.ps1 hook ran for $Package 1.0.0" + } + ) + + foreach ($msg in $Messages) { + if ($RunsHooks) { + $Output.Lines | Should -Contain $msg -Because $Output.String + } + else { + $Output.Lines | Should -Not -Contain $msg -Because $Output.String + } + } + + $Output.Lines | Should -Not -Contain "pre-$Command-doesnotexist.ps1 hook ran for $Package $Version" -Because $Output.String + } + } +} diff --git a/tests/chocolatey-tests/chocolatey.Tests.ps1 b/tests/chocolatey-tests/chocolatey.Tests.ps1 index b89d2d0ccb..de3a47c88a 100644 --- a/tests/chocolatey-tests/chocolatey.Tests.ps1 +++ b/tests/chocolatey-tests/chocolatey.Tests.ps1 @@ -139,13 +139,7 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat # This is FossOnly for now as there are some undetermined errors here that do not seem to present inside of Chocolatey. https://gitlab.com/chocolatey/build-automation/chocolatey-test-kitchen/-/issues/39 It "Should be able to run the script in AllSigned mode" -Skip:($_ -notin $PowerShellFiles) -Tag FossOnly { - # The chocolateyScriptRunner expects some passed in values and results in two errors if they're not there. This accounts for that. - $expectedErrors = if ($FileUnderTest.Name -eq 'chocolateyScriptRunner.ps1') { - 2 - } - else { - 0 - } + $expectedErrors = 0 $command = "Import-Module $FileUnderTest -ErrorAction SilentlyContinue; exit `$error.count" & powershell.exe -noprofile -ExecutionPolicy AllSigned -command $command 2>$null $LastExitCode | Should -BeExactly $expectedErrors