Skip to content

Commit

Permalink
(#1185) Add tests for hook scripts
Browse files Browse the repository at this point in the history
Add tests that hook scripts execute in scenarios where they're expected
to run.
  • Loading branch information
corbob committed Sep 21, 2022
1 parent 3008724 commit 4431f83
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ private IEnumerable<string> 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)
Expand Down
93 changes: 93 additions & 0 deletions tests/chocolatey-tests/Hooks.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 '<Command> hooks with flag: <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 (<RunsHooks>)" {
$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
}
}
}
8 changes: 1 addition & 7 deletions tests/chocolatey-tests/chocolatey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4431f83

Please sign in to comment.