Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#798, #2787, #1185) Add or update tests for side by side installs, pin during install/upgrade, hook scripts #2823

Merged
merged 6 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
}

# Issue: https://github.com/chocolatey/chocolatey-licensed-issues/issues/284
Context "Installing a Package with Embedded Zip Archive and specifying destination <Name>" -Foreach @(
Context "Installing a Package with Embedded Zip Archive and specifying destination <Name>" -ForEach @(
@{ Name = 'Root of UNC share' ; Path = '\\localhost\c$\' }
@{ Name = 'UNC share path' ; Path = '\\localhost\c$\temp\' }
@{ Name = 'Root of drive with trailing slash' ; Path = 'C:\' }
Expand Down Expand Up @@ -1393,6 +1393,37 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
}
}

Context "Install '<Package>' package with (<Command>) specified" -ForEach @(
@{ Command = '--pin' ; Package = 'installpackage' ; Contains = $true }
@{ Command = '' ; Package = 'installpackage' ; Contains = $false }
@{ Command = '' ; Package = 'packages.config' ; Contains = $true }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot

if ($Package -eq 'packages.config') {
@"
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="installpackage" pinPackage="true" />
</packages>
"@ | Set-Content $PWD/packages.config
}

$null = Invoke-Choco install $Package $Command --confirm
$Output = Invoke-Choco pin list
}

It "Output should include pinned package" {
if ($Contains) {
$Output.String | Should -Match "installpackage|1.0.0"
}
else {
$Output.String | Should -Not -Match "installpackage|1.0.0"
}
}
}

Context "Installing package that extracts local zip archive while disabling logging" {
BeforeAll {
Restore-ChocolateyInstallSnapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ Describe "choco upgrade" -Tag Chocolatey, UpgradeCommand {
}
}

Context "Upgrade package with (<Command>) specified" -ForEach @(
@{ Command = '--pin' ; Contains = $true }
@{ Command = '' ; Contains = $false }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$Package = 'upgradepackage'
$null = Invoke-Choco install $Package --version 1.0.0 --confirm
$null = Invoke-Choco upgrade $Package $Command --confirm
$Output = Invoke-Choco pin list
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0
}

It "Output should include pinned package" {
if ($Contains) {
$Output.String | Should -Match "$Package|1.1.0"
}
else {
$Output.String | Should -Not -Match "$Package|1.1.0"
}
}
}

Context "Upgrading packages while remembering arguments with only one package using arguments" -Tag Internal {
BeforeAll {
Restore-ChocolateyInstallSnapshot
Expand Down
93 changes: 93 additions & 0 deletions tests/chocolatey-tests/features/Hooks.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Import-Module helpers/common-helpers

Describe "choco hooks tests" -Tag Chocolatey, HooksFeature {
BeforeDiscovery {
$Flags = @(
@{ Flag = '' ; RunsHooks = $true ; Command = 'install' ; ExitCode = 0 }
@{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'install' ; ExitCode = 0 }
@{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'install' ; ExitCode = 0 }

@{ Flag = '' ; RunsHooks = $true ; Command = 'uninstall' ; ExitCode = 0 }
@{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'uninstall' ; ExitCode = 0 }
@{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'uninstall' ; ExitCode = 0 }

@{ Flag = '' ; RunsHooks = $true ; Command = 'upgrade' ; ExitCode = -1 }
@{ Flag = '--skip-powershell' ; RunsHooks = $false ; Command = 'upgrade' ; ExitCode = 0 }
@{ Flag = '--skip-hooks' ; RunsHooks = $false ; Command = 'upgrade' ; ExitCode = -1 }

)
}
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 expected value (<ExitCode>)" {
$Output.ExitCode | Should -Be $ExitCode -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
}
}
}