perf(tests): run each child-process invocation once

Every Invoke-ZScript call starts a fresh Windows PowerShell, which costs about
1.7 seconds - and it is the dominant cost of this file. Thirteen of its
forty-three calls re-run a command an earlier check has already run. The usage
tests are the clearest case: a script is run bare three separate times to
assert that it exits non-zero, that its usage lists the project keys, and that
the usage never mentions the underscore comment key. Those are three questions
about one run.

Memoised on the exact command, so each distinct invocation happens once and
every check that asks for it gets the same captured result. The commands
reached here either refuse their input or inspect an unused fixture port, so
none has a side effect a second run would reveal.

ArgumentParsing.Tests.ps1, over three runs each: 83/108/88s before,
61/68/79s after. All 42 tests still pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
KellyMichels 2026-09-10 14:47:17 -05:00
parent f27f9dc5bc
commit 351cf9f574

View File

@ -73,14 +73,30 @@ BeforeAll {
$fixture | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath (Join-Path $script:Install "zconfig.json") -Encoding UTF8 $fixture | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath (Join-Path $script:Install "zconfig.json") -Encoding UTF8
# Run a script in a child process; capture merged output and exit code. # Run a script in a child process; capture merged output and exit code.
#
# Memoised on the exact command. Several checks deliberately assert
# different things about the SAME invocation - that running bare exits
# non-zero, that its usage lists the project keys, and that the usage
# never mentions the underscore comment key are three checks of one run.
# Starting a Windows PowerShell costs about 1.7 s, so re-running the same
# command to ask it a second question is the single most expensive thing
# this file does. The scripts reached here either refuse their input or
# inspect an unused fixture port, so none of them has a side effect a
# second run would reveal.
$script:zRuns = @{}
function Invoke-ZScript { function Invoke-ZScript {
param([string]$Script, [string[]]$ScriptArgs = @()) param([string]$Script, [string[]]$ScriptArgs = @())
$key = @($Script) + $ScriptArgs -join "`n"
if ($script:zRuns.ContainsKey($key)) { return $script:zRuns[$key] }
$path = Join-Path $script:Install $Script $path = Join-Path $script:Install $Script
$out = & powershell -NoProfile -ExecutionPolicy Bypass -File $path @ScriptArgs 2>&1 | ForEach-Object { "$_" } $out = & powershell -NoProfile -ExecutionPolicy Bypass -File $path @ScriptArgs 2>&1 | ForEach-Object { "$_" }
return [pscustomobject]@{ $result = [pscustomobject]@{
ExitCode = $LASTEXITCODE ExitCode = $LASTEXITCODE
Output = ($out -join "`n") Output = ($out -join "`n")
} }
$script:zRuns[$key] = $result
return $result
} }
} }