From 67da1b20cbf46f642643a2ccfe018252e826b929 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Wed, 18 Jan 2023 10:54:05 -0500 Subject: [PATCH] (#7) Add hoook for executing pester tests This commit adds a hook package to this repository which executes any pester tests that have been included in a tests folder inside the package. Package parameters are provided to allow skipping of execution of the pester tests to allow for other hooks that may be required to run as opposed to using the global --skip-hooks parameter. --- src/pester-validation.hook/hook/helpers.ps1 | 14 ++++++ .../hook/post-install-all.ps1 | 47 +++++++++++++++++++ .../pester-validation.hook.nuspec | 46 ++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/pester-validation.hook/hook/helpers.ps1 create mode 100644 src/pester-validation.hook/hook/post-install-all.ps1 create mode 100644 src/pester-validation.hook/pester-validation.hook.nuspec diff --git a/src/pester-validation.hook/hook/helpers.ps1 b/src/pester-validation.hook/hook/helpers.ps1 new file mode 100644 index 0000000..7adf4c3 --- /dev/null +++ b/src/pester-validation.hook/hook/helpers.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/src/pester-validation.hook/hook/post-install-all.ps1 b/src/pester-validation.hook/hook/post-install-all.ps1 new file mode 100644 index 0000000..4400689 --- /dev/null +++ b/src/pester-validation.hook/hook/post-install-all.ps1 @@ -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']) { + $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" +} diff --git a/src/pester-validation.hook/pester-validation.hook.nuspec b/src/pester-validation.hook/pester-validation.hook.nuspec new file mode 100644 index 0000000..a0bf7a8 --- /dev/null +++ b/src/pester-validation.hook/pester-validation.hook.nuspec @@ -0,0 +1,46 @@ + + + + + pester-validation.hook + 0.1.0-alpha-20231801 + https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook + Chocolatey Community, steviecoaster + Pester Validation (Hook) + Chocolatey Community, steviecoaster + https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook + + 2023 Chocolatey Community + https://github.com/chocolatey-community/chocolatey-hooks/blob/main/LICENSE + true + https://github.com/chocolatey-community/chocolatey-hooks/tree/main/src/pester-validation.hook + + + https://github.com/chocolatey-community/chocolatey-hooks/issues + Pester Validation Hook + Execute pester tests against package post install + 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: + + - `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 + + 0.1.0-alpha-20231801: Initial Release + + + + + + + + +