-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathInvoke-CppTest.ps1
More file actions
643 lines (548 loc) · 20 KB
/
Invoke-CppTest.ps1
File metadata and controls
643 lines (548 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<#
.SYNOPSIS
Build and/or run native C++ test executables.
.DESCRIPTION
Script for building and running native C++ tests.
Supports both MSBuild (new vcxproj) and nmake (legacy makefile) builds.
Auto-approvable by Copilot agents.
.PARAMETER Action
What to do: Build, Run, or BuildAndRun (default).
.PARAMETER TestProject
Which test: TestGeneric (default) or TestViews.
.PARAMETER Configuration
Build configuration: Debug (default) or Release.
.PARAMETER BuildSystem
Build system to use: MSBuild (default, uses vcxproj) or NMake (legacy).
.PARAMETER WorktreePath
Path to the worktree root. Defaults to current directory.
.EXAMPLE
.\Invoke-CppTest.ps1 -TestProject TestGeneric
Build and run TestGeneric using MSBuild.
.EXAMPLE
.\Invoke-CppTest.ps1 -Action Run -TestProject TestViews
Run TestViews without rebuilding.
.EXAMPLE
.\Invoke-CppTest.ps1 -BuildSystem NMake -TestProject TestGeneric
Build TestGeneric using legacy nmake (requires VsDevCmd).
#>
[CmdletBinding()]
param(
[ValidateSet('Build', 'Run', 'BuildAndRun')]
[string]$Action = 'BuildAndRun',
[ValidateSet('TestGeneric', 'TestViews')]
[string]$TestProject = 'TestGeneric',
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Debug',
[ValidateSet('MSBuild', 'NMake')]
[string]$BuildSystem = 'MSBuild',
[string]$WorktreePath,
[int]$TimeoutSeconds = 300,
[int]$PostCompletionGraceSeconds = 5,
[string[]]$TestArguments,
[string]$LogPath
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$script:LastLocalOutDir = $null
# =============================================================================
# Import Shared Module
# =============================================================================
$helpersPath = Join-Path $PSScriptRoot "../Agent/FwBuildHelpers.psm1"
if (-not (Test-Path $helpersPath)) {
Write-Host "[ERROR] FwBuildHelpers.psm1 not found at $helpersPath" -ForegroundColor Red
exit 1
}
Import-Module $helpersPath -Force
# =============================================================================
# Environment Setup
# =============================================================================
# Initialize VS environment (needed for NMake and MSBuild)
Initialize-VsDevEnvironment
# Suppress assertion dialog boxes (DebugProcs.dll checks this env var)
# This prevents tests from blocking on MessageBox popups
$env:AssertUiEnabled = 'false'
# Unconditional test-mode override: bypasses registry AssertMessageBox key in DebugProcs.dll
$env:FW_TEST_MODE = '1'
# Suppress Windows Error Reporting and crash dialogs
# SEM_FAILCRITICALERRORS = 0x0001
# SEM_NOGPFAULTERRORBOX = 0x0002
# SEM_NOOPENFILEERRORBOX = 0x8000
$SetErrorModeSignature = @'
[DllImport("kernel32.dll")]
public static extern uint SetErrorMode(uint uMode);
'@
$Kernel32 = Add-Type -MemberDefinition $SetErrorModeSignature -Name 'Kernel32' -Namespace 'Win32' -PassThru
$oldMode = $Kernel32::SetErrorMode(0x8003)
# Resolve worktree path
if (-not $WorktreePath) {
$WorktreePath = (Get-Location).Path
}
$WorktreePath = (Resolve-Path $WorktreePath).Path
$sourceWorktreePath = $WorktreePath
# Track both the original mount path and the active path (local clone when enabled)
$activeWorktreePath = $WorktreePath
# Project configuration
$projectConfig = @{
TestGeneric = @{
VcxprojPath = 'Src\Generic\Test\TestGeneric.vcxproj'
MakefilePath = 'Src\Generic\Test'
MakefileName = 'testGenericLib.mak'
ExeName = 'testGenericLib.exe'
}
TestViews = @{
VcxprojPath = 'Src\views\Test\TestViews.vcxproj'
MakefilePath = 'Src\views\Test'
MakefileName = 'testViews.mak'
ExeName = 'TestViews.exe'
}
}
$config = $projectConfig[$TestProject]
$outputDir = Join-Path $WorktreePath "Output\$Configuration"
$exePath = Join-Path $outputDir $config.ExeName
Write-Host "======================================" -ForegroundColor Cyan
Write-Host "C++ Test: $TestProject" -ForegroundColor Cyan
Write-Host "Action: $Action" -ForegroundColor Cyan
Write-Host "Configuration: $Configuration" -ForegroundColor Cyan
Write-Host "Build System: $BuildSystem" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
function Get-MsBuildExecutable {
$buildToolsMsbuild = 'C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe'
if (Test-Path $buildToolsMsbuild) { return $buildToolsMsbuild }
return 'msbuild'
}
function Build-FwBuildTasks {
Write-Host "[INFO] Building FwBuildTasks helper assembly..." -ForegroundColor Yellow
$tasksProj = Join-Path $WorktreePath 'Build\Src\FwBuildTasks\FwBuildTasks.csproj'
$msbuild = Get-MsBuildExecutable
$buildArgs = @(
'/restore',
"$tasksProj",
('/p:Configuration={0}' -f $Configuration),
'/p:Platform=x64',
'/nologo'
)
& $msbuild @buildArgs
if ($LASTEXITCODE -ne 0) { throw "FwBuildTasks build failed with exit code $LASTEXITCODE" }
}
function Build-NativeArtifacts {
Write-Host "[INFO] Building native artifacts (NativeBuild.csproj)..." -ForegroundColor Yellow
Build-FwBuildTasks
$nativeProj = Join-Path $WorktreePath 'Build\Src\NativeBuild\NativeBuild.csproj'
$msbuild = Get-MsBuildExecutable
$buildArgs = @(
"$nativeProj",
('/p:Configuration={0}' -f $Configuration),
'/p:Platform=x64',
'/p:BuildNativeTests=true',
'/nologo',
'/m'
)
& $msbuild @buildArgs
if ($LASTEXITCODE -ne 0) { throw "Native build failed with exit code $LASTEXITCODE" }
}
function Build-ViewsInterfacesArtifacts {
Write-Host "[INFO] Generating ViewsInterfaces artifacts..." -ForegroundColor Yellow
$viewsProj = Join-Path $WorktreePath 'Src\Common\ViewsInterfaces\ViewsInterfaces.csproj'
$msbuild = Get-MsBuildExecutable
$buildArgs = @(
'/restore',
"$viewsProj",
('/p:Configuration={0}' -f $Configuration),
'/p:Platform=x64',
'/nologo',
'/v:minimal'
)
& $msbuild @buildArgs
if ($LASTEXITCODE -ne 0) { throw "ViewsInterfaces build failed with exit code $LASTEXITCODE" }
}
function Ensure-UnitPlusPlusLibrary {
$unitLibPath = Join-Path $WorktreePath "Lib\$($Configuration.ToLower())\unit++.lib"
if (Test-Path $unitLibPath) {
return
}
Write-Host "[INFO] Missing unit++.lib; building Unit++ library..." -ForegroundColor Yellow
$unitProj = Join-Path $WorktreePath 'Lib\src\unit++\VS\unit++.vcxproj'
if (-not (Test-Path $unitProj)) {
throw "Unit++ project not found: $unitProj"
}
$msbuild = Get-MsBuildExecutable
$msbuildArgs = @(
$unitProj,
('/p:Configuration={0}' -f $Configuration),
'/p:Platform=x64',
'/v:minimal',
'/nologo'
)
& $msbuild @msbuildArgs
if ($LASTEXITCODE -ne 0) {
throw "Unit++ build failed with exit code $LASTEXITCODE"
}
if (-not (Test-Path $unitLibPath)) {
throw "Unit++ build completed but library is still missing: $unitLibPath"
}
}
function Ensure-TestViewsPrerequisites {
if ($TestProject -ne 'TestViews') { return }
$fwKernelHeader = Join-Path $WorktreePath "Output\$Configuration\Common\FwKernelTlb.h"
$viewsObj = Join-Path $WorktreePath "Obj\$Configuration\Views\autopch\VwRootBox.obj"
if ((Test-Path $fwKernelHeader) -and (Test-Path $viewsObj)) { return }
Write-Host "[INFO] Missing native artifacts or generated headers required for TestViews." -ForegroundColor Yellow
Build-NativeArtifacts
Build-ViewsInterfacesArtifacts
}
function Invoke-Build {
# Ensure native runtime dependencies (DebugProcs, ICU DLLs, etc.) exist before building the test exe
$needsNative = @(
(Test-Path (Join-Path $outputDir 'DebugProcs.dll')),
(Test-Path (Join-Path $outputDir 'icuin70.dll')),
(Test-Path (Join-Path $outputDir 'icuuc70.dll'))
) -contains $false
if ($needsNative) {
Write-Host "[INFO] Native runtime artifacts missing for $TestProject; building NativeBuild.csproj..." -ForegroundColor Yellow
Build-NativeArtifacts
}
Ensure-UnitPlusPlusLibrary
if ($BuildSystem -eq 'MSBuild') {
$vcxproj = Join-Path $WorktreePath $config.VcxprojPath
Write-Host "WorktreePath: $WorktreePath" -ForegroundColor Gray
Write-Host "vcxproj path: $vcxproj" -ForegroundColor Gray
if (-not (Test-Path $vcxproj)) {
Write-Host "[ERROR] vcxproj not found at resolved path" -ForegroundColor Red
}
if (-not (Test-Path $vcxproj)) {
throw "vcxproj not found: $vcxproj. Has the project been converted from Makefile?"
}
Ensure-TestViewsPrerequisites
Write-Host "`nBuilding with MSBuild..." -ForegroundColor Yellow
# Force x64 platform to avoid Win32 default when building from host
$env:Platform = 'x64'
# Prefer the BuildTools MSBuild inside the container to match VCINSTALLDIR
$msbuild = "msbuild"
$buildToolsMsbuild = 'C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe'
if (Test-Path $buildToolsMsbuild) {
$msbuild = $buildToolsMsbuild
}
$msbuildOutArgs = @()
$localOutDir = $null
$msbuildArgs = @(
$config.VcxprojPath,
('/p:Configuration={0}' -f $Configuration),
'/p:Platform=x64',
'/p:LinkIncremental=false',
'/v:minimal',
'/nologo'
) + $msbuildOutArgs
$msbuildCmd = "$msbuild $($msbuildArgs -join ' ')"
Write-Host "(cd $WorktreePath) $msbuildCmd" -ForegroundColor Gray
Push-Location $WorktreePath
try {
& $msbuild @msbuildArgs
if ($LASTEXITCODE -ne 0) { throw "MSBuild failed with exit code $LASTEXITCODE" }
}
finally {
Pop-Location
}
if ($localOutDir) {
$script:LastLocalOutDir = $localOutDir
# Ensure output directory exists in the local clone
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Force -Path $outputDir | Out-Null
}
$artifactNames = @(
$config.ExeName,
($config.ExeName -replace '\.exe$', '.pdb'),
($config.ExeName -replace '\.exe$', '.lib'),
($config.ExeName -replace '\.exe$', '.exp')
)
foreach ($name in $artifactNames) {
$src = Join-Path $localOutDir $name
if (Test-Path $src) {
try {
Copy-Item -Path $src -Destination (Join-Path $outputDir $name) -Force
}
catch {
Write-Host "[WARN] Could not copy $name to Output (in use?): $_" -ForegroundColor Yellow
}
}
}
# Also copy ICU DLLs from lib/x64 if they exist (required for runtime)
$localLibX64 = Join-Path $localOutDir "lib\x64"
if (Test-Path $localLibX64) {
$destLibX64 = Join-Path $outputDir "lib\x64"
if (-not (Test-Path $destLibX64)) {
New-Item -ItemType Directory -Force -Path $destLibX64 | Out-Null
}
try {
Copy-Item -Path (Join-Path $localLibX64 "icu*.dll") -Destination $destLibX64 -Force
}
catch {
Write-Host "[WARN] Could not copy ICU DLLs to Output (in use?): $_" -ForegroundColor Yellow
}
}
}
}
else {
# NMake build (legacy)
$makeDir = Join-Path $WorktreePath $config.MakefilePath
$makefile = $config.MakefileName
$buildType = if ($Configuration -eq 'Debug') { 'd' } else { 'r' }
Write-Host "`nBuilding with NMake (legacy)..." -ForegroundColor Yellow
# VsDevCmd is already initialized by Initialize-VsDevEnvironment
$nmakeArgs = @(
'/nologo',
"BUILD_CONFIG=$Configuration",
"BUILD_TYPE=$buildType",
"BUILD_ROOT=$WorktreePath\",
'BUILD_ARCH=x64',
'/f',
$makefile
)
Write-Host "(cd $makeDir) nmake $($nmakeArgs -join ' ')" -ForegroundColor Gray
$process = Start-Process -FilePath 'nmake' -ArgumentList $nmakeArgs -WorkingDirectory $makeDir -NoNewWindow -Wait -PassThru
if ($process.ExitCode -ne 0) { throw "NMake failed with exit code $($process.ExitCode)" }
}
Write-Host "`nBuild completed successfully." -ForegroundColor Green
}
function Invoke-Run {
Write-Host "`nRunning $($config.ExeName)..." -ForegroundColor Yellow
# Add Debug CRT to PATH if running in Debug configuration (required for VCRUNTIME140D.dll)
if ($Configuration -eq 'Debug') {
$debugCrt = Get-ChildItem -Path "C:\BuildTools\VC\Redist\MSVC" -Filter "Microsoft.VC*.DebugCRT" -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*\x64\*" -and $_.FullName -like "*\debug_nonredist\*" } |
Select-Object -First 1 -ExpandProperty FullName
if ($debugCrt) {
Write-Host "Adding Debug CRT to PATH: $debugCrt" -ForegroundColor Gray
$env:PATH = "$debugCrt;$env:PATH"
}
}
# Ensure ICU DLLs are in place
$icuSource = Join-Path $outputDir "lib\x64"
$icuDlls = Get-ChildItem -Path $icuSource -Filter "icu*70.dll" -ErrorAction SilentlyContinue
if ($icuDlls) {
foreach ($dll in $icuDlls) {
$dest = Join-Path $outputDir $dll.Name
if (-not (Test-Path $dest)) {
Write-Host "Copying $($dll.Name) to output directory..." -ForegroundColor Gray
Copy-Item $dll.FullName $dest
}
}
}
if (-not (Test-Path $exePath)) {
throw "Test executable not found: $exePath. Run with -Action Build first."
}
if (-not $LogPath) {
$LogPath = Join-Path $outputDir "$($config.ExeName).log"
}
$errorLogPath = "$LogPath.stderr"
# Ensure native tests resolve paths consistently in containers/CI
$distFiles = Join-Path $activeWorktreePath 'DistFiles'
$sourceDistFiles = if ($sourceWorktreePath -and (Test-Path (Join-Path $sourceWorktreePath 'DistFiles'))) { Join-Path $sourceWorktreePath 'DistFiles' } else { $null }
# Backfill DistFiles into the active clone when missing critical assets
$requiredAssets = @(
@{ Rel = 'XceedZip.dll' },
@{ Rel = 'Templates\NewLangProj.fwdata' }
)
if (-not (Test-Path $distFiles) -and $sourceDistFiles) {
Write-Host "[INFO] DistFiles missing in active worktree; copying from source" -ForegroundColor Yellow
Copy-Item -Path $sourceDistFiles -Destination $activeWorktreePath -Recurse -Force
}
foreach ($asset in $requiredAssets) {
$target = Join-Path $distFiles $asset.Rel
if (-not (Test-Path $target)) {
if ($sourceDistFiles) {
$sourceAsset = Join-Path $sourceDistFiles $asset.Rel
if (Test-Path $sourceAsset) {
New-Item -ItemType Directory -Force -Path (Split-Path $target) | Out-Null
Copy-Item $sourceAsset $target -Force
}
}
}
}
if (-not (Test-Path $distFiles)) {
throw "DistFiles not found in active worktree ($activeWorktreePath) and no source copy available."
}
$env:FW_ROOT_CODE_DIR = $distFiles
$icuDir = Join-Path $distFiles 'Icu70'
if (Test-Path $icuDir) {
$env:FW_ICU_DATA_DIR = $icuDir
$env:ICU_DATA = $icuDir
}
Write-Host "FW_ROOT_CODE_DIR=$($env:FW_ROOT_CODE_DIR)" -ForegroundColor Gray
if ($env:FW_ICU_DATA_DIR) { Write-Host "FW_ICU_DATA_DIR=$($env:FW_ICU_DATA_DIR)" -ForegroundColor Gray }
$zipPath = Join-Path $distFiles 'XceedZip.dll'
Write-Host "XceedZip present: $(Test-Path $zipPath)" -ForegroundColor Gray
$runExePath = $exePath
if ($null -ne $script:LastLocalOutDir) {
$potentialPath = Join-Path $script:LastLocalOutDir $config.ExeName
if (Test-Path $potentialPath) {
$runExePath = $potentialPath
}
}
if (Test-Path $LogPath) {
Remove-Item $LogPath -Force -ErrorAction SilentlyContinue
}
if (Test-Path $errorLogPath) {
Remove-Item $errorLogPath -Force -ErrorAction SilentlyContinue
}
# Ensure no stale test process is holding the exe open
$exeProcessName = [System.IO.Path]::GetFileNameWithoutExtension($config.ExeName)
try {
Get-Process -Name $exeProcessName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
}
catch {}
# Stage required runtime assets into the output directory for reliable lookup
$assetsToStage = @(
@{ Source = Join-Path $distFiles 'XceedZip.dll'; Destination = Join-Path $outputDir 'XceedZip.dll' },
@{ Source = Join-Path $distFiles 'Templates\NewLangProj.fwdata'; Destination = Join-Path $outputDir 'Templates\NewLangProj.fwdata' }
)
foreach ($asset in $assetsToStage) {
if (Test-Path $asset.Source) {
New-Item -ItemType Directory -Force -Path (Split-Path $asset.Destination) | Out-Null
Copy-Item $asset.Source $asset.Destination -Force
}
}
$argumentList = @()
if ($TestArguments) {
$argumentList += $TestArguments
}
Write-Host "Running: $runExePath $($argumentList -join ' ')" -ForegroundColor Gray
Write-Host "Logging to: $LogPath" -ForegroundColor Gray
$startInfo = @{
FilePath = $runExePath
WorkingDirectory = $outputDir
RedirectStandardOutput = $LogPath
RedirectStandardError = $errorLogPath
NoNewWindow = $true
PassThru = $true
}
if ($argumentList.Count -gt 0) {
$startInfo.ArgumentList = $argumentList
}
$process = Start-Process @startInfo
$timedOut = $false
$terminatedAfterCompletion = $false
$summarySeenAt = $null
$startTime = Get-Date
$summaryPattern = 'Tests \[Ok-Fail-Error\]: \[\d+-\d+-\d+\]'
$heartbeatIntervalSeconds = 30
$nextHeartbeatAt = $startTime.AddSeconds($heartbeatIntervalSeconds)
while ($true) {
$process.Refresh()
if ($process.HasExited) {
break
}
$elapsedSeconds = ((Get-Date) - $startTime).TotalSeconds
if ($elapsedSeconds -ge $TimeoutSeconds) {
$timedOut = $true
Write-Host "Test run exceeded timeout (${TimeoutSeconds}s); terminating process..." -ForegroundColor Red
try { Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue } catch {}
break
}
if ((Get-Date) -ge $nextHeartbeatAt) {
Write-Host ("[HEARTBEAT] {0} still running after {1:N0}s (pid {2})" -f $config.ExeName, $elapsedSeconds, $process.Id) -ForegroundColor DarkGray
$nextHeartbeatAt = (Get-Date).AddSeconds($heartbeatIntervalSeconds)
}
if (Test-Path $LogPath) {
$recentOutput = Get-Content -Path $LogPath -Tail 25 -ErrorAction SilentlyContinue
if (($recentOutput -join "`n") -match $summaryPattern) {
if (-not $summarySeenAt) {
$summarySeenAt = Get-Date
Write-Host "Detected Unit++ completion summary; waiting up to ${PostCompletionGraceSeconds}s for process exit..." -ForegroundColor Yellow
}
elseif (((Get-Date) - $summarySeenAt).TotalSeconds -ge $PostCompletionGraceSeconds) {
$terminatedAfterCompletion = $true
Write-Host "Process did not exit ${PostCompletionGraceSeconds}s after completion summary; terminating hung process..." -ForegroundColor Yellow
try { Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue } catch {}
break
}
}
else {
$summarySeenAt = $null
}
}
Start-Sleep -Milliseconds 250
}
$process.WaitForExit()
$nativeExitCode = $null
try {
$nativeExitCode = $process.ExitCode
}
catch {
$nativeExitCode = $null
}
$logTail = @()
if (Test-Path $LogPath) {
if (Test-Path $errorLogPath) {
Add-Content -Path $LogPath -Value "`n--- stderr ---"
Get-Content -Path $errorLogPath -ErrorAction SilentlyContinue | Add-Content -Path $LogPath
}
$logTail = Get-Content -Path $LogPath -Tail 40 -ErrorAction SilentlyContinue
Write-Host "--- Test output (last 40 lines) ---" -ForegroundColor Yellow
$logTail | ForEach-Object { Write-Host $_ }
Write-Host "--- end output ---" -ForegroundColor Yellow
}
# Determine exit code using both the real process exit code and the Unit++ summary.
# The process exit code is authoritative for crashes/teardown failures that occur after
# the Unit++ summary has already been written.
$exitCode = -1
$summaryExitCode = $null
if (-not $timedOut) {
$summaryLine = $logTail | Where-Object { $_ -match 'Tests \[Ok-Fail-Error\]: \[\d+-\d+-\d+\]' } | Select-Object -Last 1
if ($summaryLine) {
$m = [regex]::Match($summaryLine, 'Tests \[Ok-Fail-Error\]: \[(\d+)-(\d+)-(\d+)\]')
$summaryExitCode = [int]$m.Groups[2].Value + [int]$m.Groups[3].Value
}
if ($terminatedAfterCompletion) {
if ($null -ne $nativeExitCode -and $nativeExitCode -ne 0) {
$exitCode = $nativeExitCode
}
else {
$exitCode = 1
}
}
elseif ($null -ne $nativeExitCode -and $nativeExitCode -ne 0) {
$exitCode = $nativeExitCode
}
elseif ($null -ne $summaryExitCode) {
$exitCode = $summaryExitCode
}
elseif ($null -ne $nativeExitCode) {
$exitCode = $nativeExitCode
}
}
Write-Host ""
if ($timedOut) {
Write-Host "Tests terminated due to timeout (${TimeoutSeconds}s)." -ForegroundColor Red
}
elseif ($terminatedAfterCompletion) {
Write-Host "Tests reported completion but process hung; terminated after ${PostCompletionGraceSeconds}s grace period." -ForegroundColor Yellow
}
elseif ($exitCode -eq 0) {
Write-Host "All tests passed!" -ForegroundColor Green
}
else {
Write-Host "Tests failed: $exitCode failure(s)/error(s)." -ForegroundColor Red
}
return $exitCode
}
# Execute requested action
$result = 0
try {
switch ($Action) {
'Build' {
Invoke-Build
}
'Run' {
$result = Invoke-Run
}
'BuildAndRun' {
Invoke-Build
$result = Invoke-Run
}
}
}
catch {
Write-Host "`nError: $_" -ForegroundColor Red
exit 1
}
exit $result