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/choco-info.Tests.ps1 b/tests/chocolatey-tests/choco-info.Tests.ps1 index 81038b7432..1356d18aa9 100644 --- a/tests/chocolatey-tests/choco-info.Tests.ps1 +++ b/tests/chocolatey-tests/choco-info.Tests.ps1 @@ -145,6 +145,7 @@ Describe "choco info" -Tag Chocolatey, InfoCommand { Context "Listing package information about local side by side installed package" { BeforeAll { + Restore-ChocolateyInstallSnapshot $null = Invoke-Choco install 'isdependency' --confirm --sxs $Output = Invoke-Choco info 'isdependency' --local-only diff --git a/tests/chocolatey-tests/choco-upgrade.Tests.ps1 b/tests/chocolatey-tests/choco-upgrade.Tests.ps1 index 98edba3d67..4eccff9827 100644 --- a/tests/chocolatey-tests/choco-upgrade.Tests.ps1 +++ b/tests/chocolatey-tests/choco-upgrade.Tests.ps1 @@ -123,16 +123,22 @@ Describe "choco upgrade" -Tag Chocolatey, UpgradeCommand { Context "Upgrade package with () specified" -ForEach @( @{ Command = '--pin' ; Contains = $true } @{ Command = '' ; Contains = $false } - ) -Tag Mine { + ) -tag mine { BeforeAll { Restore-ChocolateyInstallSnapshot -SetWorkDir $Package = 'upgradepackage' $null = Invoke-Choco install $Package --version 1.0.0 --confirm - $null = Invoke-Choco upgrade $Package $Command --confirm + $hmmm = Invoke-Choco upgrade $Package $Command --confirm + write-host $hmmm.string + write-host $hmmm.ExitCode $Output = Invoke-Choco pin list } + It "Exits with Success (0)" { + $Output.ExitCode | Should -Be 0 + } + It "Output should include pinned package" { if ($Contains) { $Output.Lines | Should -Contain "$Package|1.1.0"