|
| 1 | +# Runs the shared tccbin conformance tests (and, if -Platform is given, |
| 2 | +# that platform's additional regression tests) against a built tcc. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# run.ps1 -Tcc <path to tcc.exe> [-Platform windows] [-- <extra tcc args>] |
| 6 | +# |
| 7 | +# Extra args (everything after --) are passed to every test compile - |
| 8 | +# e.g. GC defines, -bt25, -municode, -ldbghelp -luser32, path to |
| 9 | +# libgc.a. These vary per platform, so the caller supplies them; this |
| 10 | +# script doesn't hardcode anything platform-specific itself. |
| 11 | +# |
| 12 | +# Each test is a pair: <name>.c and <name>.expected. The .expected |
| 13 | +# file has 1 or 2 lines: |
| 14 | +# line 1: expected exit code, or the literal word "nonzero" |
| 15 | +# line 2 (optional): a substring that must appear in the program's |
| 16 | +# combined stdout+stderr. Omit for no check. |
| 17 | + |
| 18 | +param( |
| 19 | + [Parameter(Mandatory = $true)][string]$Tcc, |
| 20 | + [string]$Platform = "", |
| 21 | + [Parameter(ValueFromRemainingArguments = $true)][string[]]$ExtraArgs |
| 22 | +) |
| 23 | + |
| 24 | +$ErrorActionPreference = "Stop" |
| 25 | +$Here = $PSScriptRoot |
| 26 | + |
| 27 | +function Get-TestDirs { |
| 28 | + $dirs = @(Join-Path $Here "shared") |
| 29 | + if ($Platform -ne "") { |
| 30 | + $p = Join-Path $Here "platform\$Platform" |
| 31 | + if (Test-Path $p) { $dirs += $p } |
| 32 | + } |
| 33 | + return $dirs |
| 34 | +} |
| 35 | + |
| 36 | +$passed = 0 |
| 37 | +$failed = 0 |
| 38 | +$work = Join-Path ([System.IO.Path]::GetTempPath()) ("tccbin-tests-" + [guid]::NewGuid()) |
| 39 | +New-Item -ItemType Directory -Path $work | Out-Null |
| 40 | + |
| 41 | +foreach ($dir in (Get-TestDirs)) { |
| 42 | + foreach ($src in (Get-ChildItem (Join-Path $dir "*.c"))) { |
| 43 | + $name = $src.BaseName |
| 44 | + $expectedFile = Join-Path $dir "$name.expected" |
| 45 | + if (-not (Test-Path $expectedFile)) { |
| 46 | + Write-Host "FAIL $name (no .expected file)" |
| 47 | + $failed++ |
| 48 | + continue |
| 49 | + } |
| 50 | + $lines = @(Get-Content $expectedFile) |
| 51 | + $expectExit = $lines[0].Trim() |
| 52 | + $expectSubstr = if ($lines.Count -gt 1) { $lines[1] } else { "" } |
| 53 | + |
| 54 | + $exe = Join-Path $work "$name.exe" |
| 55 | + & $Tcc $src.FullName @ExtraArgs -o $exe 2>&1 | Out-Null |
| 56 | + if ($LASTEXITCODE -ne 0) { |
| 57 | + Write-Host "FAIL $name (compile error)" |
| 58 | + $failed++ |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + # Read via the .NET Process API directly rather than `&`/$LASTEXITCODE |
| 63 | + # (raced/read stale exit codes on processes that terminate via an |
| 64 | + # unhandled SEH exception) or Start-Process -RedirectStandardOutput |
| 65 | + # to a file (the file wasn't reliably flushed by the time -Wait |
| 66 | + # returned, for short-lived processes - both reproduced locally). |
| 67 | + # ReadToEnd() blocks until the stream closes (i.e. the process has |
| 68 | + # actually finished producing output), so there's no race. |
| 69 | + # |
| 70 | + # stdout and stderr are read one async + one sync (not both |
| 71 | + # ReadToEnd()'d sequentially) to avoid deadlocking: a process that |
| 72 | + # writes enough to the stream read second to fill its OS pipe |
| 73 | + # buffer before the stream read first closes would block forever, |
| 74 | + # since nothing drains the second stream while ReadToEnd() blocks |
| 75 | + # on the first - reproduced locally with a test program that only |
| 76 | + # writes to stderr. |
| 77 | + $psi = [System.Diagnostics.ProcessStartInfo]::new($exe) |
| 78 | + $psi.RedirectStandardOutput = $true |
| 79 | + $psi.RedirectStandardError = $true |
| 80 | + $psi.UseShellExecute = $false |
| 81 | + $proc = [System.Diagnostics.Process]::Start($psi) |
| 82 | + $stdoutTask = $proc.StandardOutput.ReadToEndAsync() |
| 83 | + $stderrText = $proc.StandardError.ReadToEnd() |
| 84 | + $stdoutText = $stdoutTask.GetAwaiter().GetResult() |
| 85 | + $proc.WaitForExit() |
| 86 | + $code = $proc.ExitCode |
| 87 | + $out = $stdoutText + $stderrText |
| 88 | + |
| 89 | + $ok = $true |
| 90 | + if ($expectExit -eq "nonzero") { |
| 91 | + if ($code -eq 0) { $ok = $false } |
| 92 | + } |
| 93 | + elseif ($code -ne [int]$expectExit) { |
| 94 | + $ok = $false |
| 95 | + } |
| 96 | + # -notmatch is case-INSENSITIVE by default in PowerShell, which |
| 97 | + # would make this laxer than run.sh's case-sensitive `case` |
| 98 | + # pattern match and could miss a diagnostic-wording regression |
| 99 | + # that only changes case. -cnotmatch forces case-sensitivity. |
| 100 | + if ($expectSubstr -ne "" -and $out -cnotmatch [regex]::Escape($expectSubstr)) { |
| 101 | + $ok = $false |
| 102 | + } |
| 103 | + |
| 104 | + if ($ok) { |
| 105 | + Write-Host "PASS $name" |
| 106 | + $passed++ |
| 107 | + } |
| 108 | + else { |
| 109 | + Write-Host "FAIL $name (exit=$code, output=$out)" |
| 110 | + $failed++ |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +Remove-Item -Recurse -Force $work -ErrorAction SilentlyContinue |
| 116 | +Write-Host "---" |
| 117 | +Write-Host "$passed passed, $failed failed" |
| 118 | +if ($failed -gt 0) { exit 1 } |
0 commit comments