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

Add hook package for pester test validation #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/pester-validation.hook/hook/helpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Get-ChocolateyPackageToolsFolder {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[String]
$PackageName
)

process {
$packageFolder = Get-ChocolateyPath -PathType 'package'
$toolsDir = Join-Path $packageFolder -ChildPath 'tools'
return $toolsDir
}
}
47 changes: 47 additions & 0 deletions src/pester-validation.hook/hook/post-install-all.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
$ErrorActionPreference = 'Stop'
$toolsDir = Split-Path $MyInvocation.MyCommand.Definition
$pp = Get-PackageParameters
$helpers = Join-Path $toolsDir -ChildPath 'helpers.ps1'
. $helpers

$toolsDir = Get-ChocolateyPackageToolsFolder -PackageName $env:ChocolateyPackagename

if (Test-Path (Join-Path $toolsDir -ChildPath 'tests')) {
$testDir = Join-Path $toolsDIr -ChildPath 'tests'
}

if ($testDir) {
if (-not $pp['SkipPackageValidation']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When writing the hooks, I had not really considered usage of package parameters. This was due to concerns of conflicts between parameters for packages, and parameters for hooks. There are some packages on CCR where the parsing of parameters is not ideal, and they could potentially have issues with unexpected parameters, who knows how many internal packages could have issues. Also, if a package parameter has an arbitrary name, it could set something in both a hook and the package, where only one was intended.

Currently, there is the --skip-hooks, which I had though should take care of disabling hooks when needed. Then for configuration, after discussion with @gep13, using the Chocolatey configuration file to store configuration values was the best option for configuring hooks. Use the choco config command to set, and use a helper to read the values (PR here chocolatey/choco#2952).

I had thought that most hooks would be "install and forget", or "configure once and forget", but it looks like that might not be the case. Maybe we need to add a switch to Chocolatey to skip individual hooks, or this hook could be switched to use a configuration value? I'd welcome other thoughts on this.

$files = (Get-ChildItem $testDir -Recurse -Filter *.ps1).FullName

$containers = $files | Foreach-Object {
New-PesterContainer -Path $_
}

$configuration = [PesterConfiguration]@{
Run = @{
Container = $containers
Passthru = $true
}
Output = @{
Verbosity = 'Detailed'
}
}

$results = Invoke-Pester -Configuration $configuration

if ($results.FailedCount -gt 0) {
$results
throw "Package validation failed. Please check results"
}
else {
$results
}
}
else {
Write-Host "Package validation has been skipped via package parameter: SkipPackageValidation"
}
}
else {
Write-Host "No tests exist for $($env:ChocolateyPackageName), not validating installation"
}
46 changes: 46 additions & 0 deletions src/pester-validation.hook/pester-validation.hook.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Do not remove this test for UTF-8: if “Ω” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. -->
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>pester-validation.hook</id>
<version>0.1.0-alpha-20231801</version>
<packageSourceUrl>https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook</packageSourceUrl>
<owners>Chocolatey Community, steviecoaster</owners>
<title>Pester Validation (Hook)</title>
<authors>Chocolatey Community, steviecoaster</authors>
<projectUrl>https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook</projectUrl>
<!--<iconUrl>http://rawcdn.githack.com/__REPLACE_YOUR_REPO__/master/icons/cleanup-desktop-shortcuts.hook.png</iconUrl>-->
<copyright>2023 Chocolatey Community</copyright>
<licenseUrl>https://github.com/chocolatey-community/chocolatey-hooks/blob/main/LICENSE</licenseUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<projectSourceUrl>https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook</projectSourceUrl>
<!--<docsUrl>TODO</docsUrl>-->
<!--<mailingListUrl>TODO</mailingListUrl>-->
<bugTrackerUrl>https://github.com/chocolatey-community/chocolatey-hooks/issues</bugTrackerUrl>
<tags>Pester Validation Hook</tags>
<summary>Execute pester tests against package post install</summary>
<description>This hook package adds scripts that execute Pester tests for a package after the execution of `chocolateyInstall.ps1`.
To run validation include pester tests inside a `tests` folder inside the package.

## Package Parameters:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be read as package parameters available while installing pester-validation.hook, not package parameters available when the hook is already installed.


- `SkipPackageValidation`: skips the execution of pester tests after the installation of a package

## Examples:

`choco install testpackage -y`
This example will execute any pester tests included in the tests folder of the package

`choco install testpackage -y --package-parameters="'/SkipPackageValidation'"
This example will skip the execution of any pester tests included in the tests folder of the package
</description>
<releaseNotes>0.1.0-alpha-20231801: Initial Release</releaseNotes>
<dependencies>
<dependency id="chocolatey" version="1.2.0" />
<dependency id="pester" version="5.4.0" />
</dependencies>
</metadata>
<files>
<file src="hook\**" target="hook" />
</files>
</package>