|
| 1 | +# RoboSy parallel test runner |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Copyright (c) 2026 Kiaro Sama |
| 4 | +# |
| 5 | +# Runs every tests/*.Tests.ps1 file concurrently as separate child processes, |
| 6 | +# with a bounded, resource-aware worker ceiling and outer wall timeouts. Each |
| 7 | +# test FILE stays internally sequential (they have intra-file order dependencies |
| 8 | +# and their own $script:Passed counters); only the files run in parallel. Every |
| 9 | +# file uses its own unique GUID sandbox (New-Sandbox), so they share no mutable |
| 10 | +# fixture, port, database, account, or clipboard and are safe to run at once. |
| 11 | +# |
| 12 | +# Fail-closed: zero discovered files, any non-zero child exit, a per-file or |
| 13 | +# whole-run timeout, or fewer results than discovered files all exit 1. |
| 14 | +# |
| 15 | +# powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\tests\Invoke-RoboSyTests.ps1 |
| 16 | +# pwsh -NoProfile -File .\tests\Invoke-RoboSyTests.ps1 -TestHost pwsh |
| 17 | +# |
| 18 | +# Worker ceiling (first that is set wins): -MaxWorkers, $env:HOOKMAKER_MAX_TEST_WORKERS, |
| 19 | +# $env:ROBOSY_MAX_TEST_WORKERS, else max(2, cores-2). Always clamped to |
| 20 | +# [1, min(fileCount, 8)] so it never oversubscribes the machine. |
| 21 | + |
| 22 | +[CmdletBinding()] |
| 23 | +param( |
| 24 | + [string]$TestHost = $(if ($PSVersionTable.PSEdition -eq 'Core') { 'pwsh' } else { 'powershell.exe' }), |
| 25 | + [int]$MaxWorkers = 0, |
| 26 | + [int]$TimeoutSeconds = 300, |
| 27 | + [int]$PerFileTimeoutSeconds = 180, |
| 28 | + [string]$TestsDirectory = '' |
| 29 | +) |
| 30 | + |
| 31 | +$ErrorActionPreference = 'Stop' |
| 32 | + |
| 33 | +if ([string]::IsNullOrWhiteSpace($TestsDirectory)) { |
| 34 | + $TestsDirectory = if (-not [string]::IsNullOrWhiteSpace($PSScriptRoot)) { $PSScriptRoot } else { (Get-Location).Path } |
| 35 | +} |
| 36 | + |
| 37 | +# --- Discover suites (fail closed on zero, matching the CI guard) --- |
| 38 | +$testFiles = @(Get-ChildItem -Path $TestsDirectory -Filter '*.Tests.ps1' -File | Sort-Object Name) |
| 39 | +if ($testFiles.Count -eq 0) { |
| 40 | + Write-Host ("No regression test files were found under: {0}" -f $TestsDirectory) -ForegroundColor Red |
| 41 | + exit 1 |
| 42 | +} |
| 43 | + |
| 44 | +# --- Resolve the child test host to an absolute path (safe arg passing) --- |
| 45 | +$resolvedHost = Get-Command $TestHost -ErrorAction SilentlyContinue |
| 46 | +$hostPath = if ($resolvedHost) { $resolvedHost.Source } else { $TestHost } |
| 47 | + |
| 48 | +# --- Worker ceiling: one documented shared cap, clamped and resource-aware --- |
| 49 | +$ceiling = $env:HOOKMAKER_MAX_TEST_WORKERS |
| 50 | +if ([string]::IsNullOrWhiteSpace($ceiling)) { $ceiling = $env:ROBOSY_MAX_TEST_WORKERS } |
| 51 | +if ($MaxWorkers -le 0) { |
| 52 | + $parsed = 0 |
| 53 | + if (-not [string]::IsNullOrWhiteSpace($ceiling) -and [int]::TryParse($ceiling, [ref]$parsed) -and $parsed -gt 0) { |
| 54 | + $MaxWorkers = $parsed |
| 55 | + } |
| 56 | + else { |
| 57 | + $MaxWorkers = [Math]::Max(2, [Environment]::ProcessorCount - 2) |
| 58 | + } |
| 59 | +} |
| 60 | +$MaxWorkers = [Math]::Max(1, [Math]::Min($MaxWorkers, [Math]::Min($testFiles.Count, 8))) |
| 61 | + |
| 62 | +Write-Host ("Discovered {0} regression test file(s); host={1}; workers={2}; wall<={3}s per-file<={4}s" -f ` |
| 63 | + $testFiles.Count, [System.IO.Path]::GetFileName($hostPath), $MaxWorkers, $TimeoutSeconds, $PerFileTimeoutSeconds) -ForegroundColor Cyan |
| 64 | +$testFiles | ForEach-Object { Write-Host (" - {0}" -f $_.Name) -ForegroundColor Cyan } |
| 65 | + |
| 66 | +# taskkill /T terminates the whole owned process tree (robocopy / cmd / spawned |
| 67 | +# interactive hosts), which killing the parent alone would leave behind on |
| 68 | +# Windows. Fully swallows output/errors and no-ops if the process already exited |
| 69 | +# (a normal race during cleanup), so it can never abort the run. |
| 70 | +function Stop-ProcessTree { |
| 71 | + param([int]$ProcessId) |
| 72 | + try { |
| 73 | + if ($null -ne (Get-Process -Id $ProcessId -ErrorAction SilentlyContinue)) { |
| 74 | + & taskkill.exe /PID $ProcessId /T /F *> $null |
| 75 | + } |
| 76 | + } |
| 77 | + catch { } |
| 78 | +} |
| 79 | + |
| 80 | +# Start one test file as an isolated child process. Uses [Diagnostics.Process] |
| 81 | +# (not Start-Process, whose -PassThru object never populates ExitCode) with the |
| 82 | +# .Arguments string (ProcessStartInfo.ArgumentList is null under Windows |
| 83 | +# PowerShell 5.1). Test paths contain spaces but no embedded quotes, so simple |
| 84 | +# double-quote wrapping is safe. stdout/stderr are drained asynchronously so a |
| 85 | +# chatty child (hundreds of lines) can never deadlock on a full pipe buffer. |
| 86 | +function Start-TestProcess { |
| 87 | + param([System.IO.FileInfo]$File, [string]$HostPath) |
| 88 | + |
| 89 | + $psi = New-Object System.Diagnostics.ProcessStartInfo |
| 90 | + $psi.FileName = $HostPath |
| 91 | + $psi.Arguments = '-NoProfile -ExecutionPolicy Bypass -File "{0}"' -f $File.FullName |
| 92 | + $psi.UseShellExecute = $false |
| 93 | + $psi.RedirectStandardOutput = $true |
| 94 | + $psi.RedirectStandardError = $true |
| 95 | + $psi.RedirectStandardInput = $true |
| 96 | + $psi.CreateNoWindow = $true |
| 97 | + |
| 98 | + $proc = [System.Diagnostics.Process]::Start($psi) |
| 99 | + $proc.StandardInput.Close() # detached, non-interactive stdin |
| 100 | + |
| 101 | + return @{ |
| 102 | + Proc = $proc |
| 103 | + File = $File |
| 104 | + OutTask = $proc.StandardOutput.ReadToEndAsync() |
| 105 | + ErrTask = $proc.StandardError.ReadToEndAsync() |
| 106 | + Sw = [System.Diagnostics.Stopwatch]::StartNew() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +$queue = [System.Collections.Generic.Queue[object]]::new() |
| 111 | +$testFiles | ForEach-Object { $queue.Enqueue($_) } |
| 112 | +$running = @{} |
| 113 | +$results = New-Object System.Collections.Generic.List[object] |
| 114 | +$overall = [System.Diagnostics.Stopwatch]::StartNew() |
| 115 | + |
| 116 | +try { |
| 117 | + while ($queue.Count -gt 0 -or $running.Count -gt 0) { |
| 118 | + while ($running.Count -lt $MaxWorkers -and $queue.Count -gt 0) { |
| 119 | + $state = Start-TestProcess -File $queue.Dequeue() -HostPath $hostPath |
| 120 | + $running[$state.Proc.Id] = $state |
| 121 | + } |
| 122 | + |
| 123 | + Start-Sleep -Milliseconds 200 |
| 124 | + |
| 125 | + foreach ($childId in @($running.Keys)) { |
| 126 | + $state = $running[$childId] |
| 127 | + $proc = $state.Proc |
| 128 | + |
| 129 | + if ($proc.HasExited) { |
| 130 | + $proc.WaitForExit() # ensure the async read tasks have reached EOF |
| 131 | + $state.Sw.Stop() |
| 132 | + $out = $state.OutTask.Result |
| 133 | + $err = $state.ErrTask.Result |
| 134 | + $summaryLine = (($out -split "`n") | Where-Object { $_ -match 'Passed:' } | Select-Object -Last 1) |
| 135 | + $results.Add([pscustomobject]@{ |
| 136 | + Name = $state.File.Name |
| 137 | + Exit = $proc.ExitCode |
| 138 | + Seconds = $state.Sw.Elapsed.TotalSeconds |
| 139 | + Summary = ([string]$summaryLine).Trim() |
| 140 | + Output = $out |
| 141 | + Error = $err |
| 142 | + }) |
| 143 | + $running.Remove($childId) |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + # Per-file wall ceiling. |
| 148 | + if ($state.Sw.Elapsed.TotalSeconds -gt $PerFileTimeoutSeconds) { |
| 149 | + Stop-ProcessTree -ProcessId $childId |
| 150 | + $state.Sw.Stop() |
| 151 | + $results.Add([pscustomobject]@{ |
| 152 | + Name = $state.File.Name |
| 153 | + Exit = 124 |
| 154 | + Seconds = $state.Sw.Elapsed.TotalSeconds |
| 155 | + Summary = ("PER-FILE TIMEOUT (> {0}s)" -f $PerFileTimeoutSeconds) |
| 156 | + Output = '' |
| 157 | + Error = '' |
| 158 | + }) |
| 159 | + $running.Remove($childId) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + # Whole-run wall ceiling. |
| 164 | + if ($overall.Elapsed.TotalSeconds -gt $TimeoutSeconds) { |
| 165 | + foreach ($childId in @($running.Keys)) { |
| 166 | + Stop-ProcessTree -ProcessId $childId |
| 167 | + $state = $running[$childId] |
| 168 | + $state.Sw.Stop() |
| 169 | + $results.Add([pscustomobject]@{ |
| 170 | + Name = $state.File.Name |
| 171 | + Exit = 124 |
| 172 | + Seconds = $state.Sw.Elapsed.TotalSeconds |
| 173 | + Summary = ("WALL TIMEOUT (run exceeded {0}s)" -f $TimeoutSeconds) |
| 174 | + Output = '' |
| 175 | + Error = '' |
| 176 | + }) |
| 177 | + $running.Remove($childId) |
| 178 | + } |
| 179 | + break |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | +finally { |
| 184 | + foreach ($childId in @($running.Keys)) { Stop-ProcessTree -ProcessId $childId } |
| 185 | +} |
| 186 | +$overall.Stop() |
| 187 | + |
| 188 | +# --- Report --- |
| 189 | +Write-Host '' |
| 190 | +$anyFail = $false |
| 191 | +foreach ($r in ($results | Sort-Object Name)) { |
| 192 | + $color = if ($r.Exit -eq 0) { 'Green' } else { 'Red' } |
| 193 | + Write-Host (" {0,-38} {1,7:N1}s exit={2} {3}" -f $r.Name, $r.Seconds, $r.Exit, $r.Summary) -ForegroundColor $color |
| 194 | + if ($r.Exit -ne 0) { |
| 195 | + $anyFail = $true |
| 196 | + if (-not [string]::IsNullOrWhiteSpace($r.Output)) { Write-Host $r.Output } |
| 197 | + if (-not [string]::IsNullOrWhiteSpace($r.Error)) { Write-Host $r.Error -ForegroundColor Red } |
| 198 | + } |
| 199 | +} |
| 200 | +Write-Host '' |
| 201 | +Write-Host ("Total: {0} file(s) in {1:N1}s (workers={2}, host={3})" -f ` |
| 202 | + $results.Count, $overall.Elapsed.TotalSeconds, $MaxWorkers, [System.IO.Path]::GetFileName($hostPath)) ` |
| 203 | + -ForegroundColor $(if ($anyFail) { 'Red' } else { 'Green' }) |
| 204 | + |
| 205 | +# Coverage guard: every discovered suite must have produced a result. |
| 206 | +if ($results.Count -ne $testFiles.Count) { |
| 207 | + Write-Host ("Coverage error: discovered {0} suite(s) but recorded {1} result(s)." -f $testFiles.Count, $results.Count) -ForegroundColor Red |
| 208 | + exit 1 |
| 209 | +} |
| 210 | +if ($anyFail) { exit 1 } |
| 211 | +exit 0 |
0 commit comments