diff --git a/.github/workflows/module.yml b/.github/workflows/module.yml new file mode 100644 index 0000000..e2ce0a1 --- /dev/null +++ b/.github/workflows/module.yml @@ -0,0 +1,169 @@ +name: Module + +# Tests the kit built as a PowerShell module, and packs it for the PowerShell +# Gallery. Nothing is published: the package is kept with the run. Before a +# release (a version's branch is release/X.Y or version/X.Y) it runs the whole +# harness on the module. On a published release, by hand, and on a pull request +# that changes the build, it runs the sections where loading can matter. Not a +# required check. +on: + push: + branches: ['release/**', 'version/**'] + pull_request: + paths: + - 'Build/**' + - '.github/workflows/module.yml' + release: + types: [published] + workflow_dispatch: + inputs: + tag: + description: Release tag to build (e.g. v4.0). Empty builds the branch it's run on. + required: false + +permissions: + contents: read + +env: + # As in tests.yml: the harness's console-event checks must run here. + PK_HARNESS_NO_SKIP: '1' + # Empty for a version's branch: the whole harness. + SECTIONS: ${{ github.event_name != 'push' && 'MODULE' || '' }} + +jobs: + # Windows PowerShell 5.1. test-pwsh is the same on PowerShell 7: keep the two + # in step. + test: + name: Module + runs-on: windows-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.tag || github.event.release.tag_name || '' }} + + - name: Run the harness on the module + shell: powershell + run: | + $harness = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', '.\ParallelKit.Harness.ps1', '-AsModule') + if ($env:SECTIONS) { $harness += @('-Section', $env:SECTIONS) } + & powershell.exe @harness + exit $LASTEXITCODE + + # PowerShell 7. Keep in step with test. + test-pwsh: + name: Module (PowerShell 7) + runs-on: windows-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.tag || github.event.release.tag_name || '' }} + + - name: Run the harness on the module under pwsh + shell: pwsh + run: | + $harness = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', '.\ParallelKit.Harness.ps1', '-AsModule') + if ($env:SECTIONS) { $harness += @('-Section', $env:SECTIONS) } + & pwsh @harness + exit $LASTEXITCODE + + package: + name: Module package + needs: [test, test-pwsh] + runs-on: windows-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.tag || github.event.release.tag_name || '' }} + + - name: Check the tag against the kit's version + if: inputs.tag || github.event_name == 'release' + shell: pwsh + env: + TAG: ${{ inputs.tag || github.event.release.tag_name }} + run: | + $version = [regex]::Match((Get-Content -Raw .\ParallelKit.ps1), '(?m)^# Version (\d+\.\d+(?:\.\d+)?)\b').Groups[1].Value + if ($env:TAG -ne "v$version") { throw "The tag is '$($env:TAG)', but ParallelKit.ps1 says version '$version'. Tag the commit whose version it is." } + + - name: Build under Windows PowerShell + shell: powershell + run: | + & powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\Build\New-ParallelKitModule.ps1 -OutputDirectory .\out-5.1\ParallelKit + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + Test-ModuleManifest -Path .\out-5.1\ParallelKit\ParallelKit.psd1 -ErrorAction Stop | Format-List Name, Version, Guid, CompatiblePSEditions + + - name: Build under PowerShell 7, and compare + shell: pwsh + run: | + & pwsh -NoProfile -ExecutionPolicy Bypass -File .\Build\New-ParallelKitModule.ps1 -OutputDirectory .\out\ParallelKit + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + Test-ModuleManifest -Path .\out\ParallelKit\ParallelKit.psd1 -ErrorAction Stop | Format-List Name, Version, Guid, CompatiblePSEditions + foreach ($file in 'ParallelKit.psm1', 'ParallelKit.psd1') { + $hashes = @('out', 'out-5.1' | ForEach-Object { (Get-FileHash ".\$_\ParallelKit\$file").Hash }) + if ($hashes[0] -ne $hashes[1]) { throw "$file differs between the build under 5.1 ($($hashes[1])) and under 7 ($($hashes[0])). The same ParallelKit.ps1 must always give the same module." } + } + + - name: Check it with PSScriptAnalyzer + shell: pwsh + run: | + if (-not (Get-Module -ListAvailable PSScriptAnalyzer)) { Install-PSResource PSScriptAnalyzer -Version 1.25.0 -Repository PSGallery -TrustRepository -Scope CurrentUser -ErrorAction Stop } + # It sometimes fails inside with "Object reference not set", losing + # results, or hangs. So: a process of its own, a time limit, and only + # a run with no such error counts. + $analyze = Join-Path $env:RUNNER_TEMP 'analyze.ps1' + $found = Join-Path $env:RUNNER_TEMP 'analyzer.json' + @' + param([string]$Path, [string]$Out) + Import-Module PSScriptAnalyzer -ErrorAction Stop + $problems = $null + $results = @(Invoke-ScriptAnalyzer -Path $Path -Recurse -ErrorAction SilentlyContinue -ErrorVariable problems) + if ($problems) { Write-Output ("PSScriptAnalyzer itself failed: " + $problems[0]); exit 2 } + Write-Output ("PSScriptAnalyzer " + (Get-Module PSScriptAnalyzer).Version + ": " + $results.Count + " results") + ConvertTo-Json -InputObject @($results | ForEach-Object { @{ Severity = [string]$_.Severity; RuleName = $_.RuleName; Line = $_.Line; Message = $_.Message } }) | Set-Content -LiteralPath $Out + exit 0 + '@ | Set-Content -LiteralPath $analyze + $done = $false + foreach ($try in 1..5) { + $run = Start-Process -FilePath (Get-Process -Id $PID).Path -ArgumentList @('-NoProfile', '-File', "`"$analyze`"", '-Path', '.\out\ParallelKit', '-Out', "`"$found`"") -NoNewWindow -PassThru + if (-not $run.WaitForExit(120000)) { Write-Output "Try ${try}: still running after 2 minutes, stopped."; $run.Kill($true); continue } + if ($run.ExitCode -eq 0) { $done = $true; break } + } + if (-not $done) { throw "PSScriptAnalyzer didn't complete in 5 tries. Run this job again." } + $results = @(Get-Content -Raw -LiteralPath $found | ConvertFrom-Json) + $results | Group-Object Severity, RuleName | Format-Table Count, Name -AutoSize + $errors = @($results | Where-Object { $_.Severity -in 'Error', 'ParseError' }) + if ($errors.Count -gt 0) { + $errors | Format-Table RuleName, Line, Message -Wrap + throw "PSScriptAnalyzer reports $($errors.Count) error(s), listed above. The Gallery wants those fixed before a module is published." + } + + - name: Pack it + shell: pwsh + run: | + # Compress-PSResource came with PSResourceGet 1.1.0. + $have = Get-Module -ListAvailable Microsoft.PowerShell.PSResourceGet | Sort-Object Version -Descending | Select-Object -First 1 + if ($null -eq $have -or $have.Version -lt [version]'1.1.0') { + Install-PSResource Microsoft.PowerShell.PSResourceGet -Version 1.2.0 -Repository PSGallery -TrustRepository -Scope CurrentUser -ErrorAction Stop + # A fresh process, so the version just installed is the one loaded. + $have = Get-Module -ListAvailable Microsoft.PowerShell.PSResourceGet | Sort-Object Version -Descending | Select-Object -First 1 + } + $null = New-Item -ItemType Directory -Force -Path .\package + & pwsh -NoProfile -Command "Import-Module Microsoft.PowerShell.PSResourceGet -RequiredVersion $($have.Version) -ErrorAction Stop; Compress-PSResource -Path .\out\ParallelKit -DestinationPath .\package -ErrorAction Stop" + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + $packages = @(Get-ChildItem .\package\*.nupkg) + if ($packages.Count -ne 1) { throw "Expected one package in .\package, found $($packages.Count)." } + # The module's files are the same for every build; the package around + # them isn't, so both are listed. + $lines = @(Get-FileHash $packages[0].FullName, .\out\ParallelKit\ParallelKit.psm1, .\out\ParallelKit\ParallelKit.psd1 | ForEach-Object { $_.Hash + ' ' + (Split-Path -Leaf $_.Path) }) + $lines | Set-Content .\package\SHA256SUMS.txt + (@('### Module package', '', '```') + $lines + @('```')) | Add-Content $env:GITHUB_STEP_SUMMARY + $lines + + - name: Keep the package + uses: actions/upload-artifact@v4 + with: + name: ParallelKit-module + path: package/ + if-no-files-found: error diff --git a/README.md b/README.md index cedab1a..57502c8 100644 --- a/README.md +++ b/README.md @@ -687,7 +687,7 @@ powershell -NoProfile -File .\ParallelKit.Harness.ps1 It takes about five and a half minutes. It prints a `[PASS]`/`[FAIL]` line per check and exits 0 only if everything passed. A few checks send real Ctrl+C, Ctrl+Break and window-close events to child PowerShell processes in hidden consoles of their own, never to yours. Where a machine can't deliver those, they print `[SKIP]` with the reason instead; set `PK_HARNESS_NO_SKIP=1` to count skips as failures. A few `message handler failed` lines in the output are expected: some checks break the message handler on purpose to test the fallback. `-Section AO,BB` runs only those sections (and A, the setup), for working on one area; it isn't a full pass, and its last line says so. `-AsModule` builds [the module](#as-a-module) and tests that instead of dot-sourcing the file; `-AsModule -Section MODULE` runs the sections where that can make a difference, in about a minute and a half. -GitHub runs the same harness on Windows PowerShell 5.1 and on PowerShell 7 for every pull request and every change to `main`, and then those sections on the module. It also runs the soak test below on both before each release ([runs](https://github.com/HalRiveria/PowerShell-ParallelKit/actions/workflows/soak.yml)). The other scripts in `Testing/` are run by hand. +GitHub runs the same harness on Windows PowerShell 5.1 and on PowerShell 7 for every pull request and every change to `main`, and then those sections on the module. It also runs the soak test below on both before each release ([runs](https://github.com/HalRiveria/PowerShell-ParallelKit/actions/workflows/soak.yml)), and the whole harness on the module, which it then checks with PSScriptAnalyzer and packs ([runs](https://github.com/HalRiveria/PowerShell-ParallelKit/actions/workflows/module.yml)). The other scripts in `Testing/` are run by hand. `Testing/` holds the longer stress tests and checks behind some of what this README says. They aren't part of the test suite and aren't needed to use the kit: