$ErrorActionPreference = "Stop" $root = $PSScriptRoot $accepted = Join-Path $root "accepted" $rejected = Join-Path $root "rejected" $errors = New-Object System.Collections.Generic.List[string] function Test-SourceProfile([System.IO.FileInfo]$file) { if ($file.BaseName.Length -gt 12) { $errors.Add("$($file.FullName): base name exceeds 12 characters") } $bytes = [IO.File]::ReadAllBytes($file.FullName) if ($bytes.Length -gt 18999) { $errors.Add("$($file.FullName): $($bytes.Length) bytes exceeds 18999") } $text = [Text.Encoding]::ASCII.GetString($bytes) $lines = $text -split "`r`n|`n|`r" if ($lines.Count -gt 599) { $errors.Add("$($file.FullName): $($lines.Count) lines exceeds 599") } for ($i = 0; $i -lt $lines.Count; $i++) { $count = [Text.Encoding]::ASCII.GetByteCount($lines[$i]) if ($count -gt 80) { $errors.Add("$($file.FullName):$($i + 1): $count bytes exceeds 80") } } } $acceptedSources = @(Get-ChildItem -LiteralPath $accepted -Filter "*.n8" -File) $rejectedSources = @(Get-ChildItem -LiteralPath $rejected -Filter "*.n8" -File) if ($acceptedSources.Count -eq 0) { $errors.Add("no accepted fixtures found") } if ($rejectedSources.Count -eq 0) { $errors.Add("no rejected fixtures found") } foreach ($file in $acceptedSources) { Test-SourceProfile $file $sidecar = [IO.Path]::ChangeExtension($file.FullName, ".expect") if (Test-Path -LiteralPath $sidecar) { $errors.Add("${sidecar}: accepted fixtures must not have diagnostic sidecars") } } foreach ($file in $rejectedSources) { Test-SourceProfile $file $sidecar = [IO.Path]::ChangeExtension($file.FullName, ".expect") if (-not (Test-Path -LiteralPath $sidecar)) { $errors.Add("$($file.FullName): missing .expect sidecar") continue } $expect = Get-Content -Raw -LiteralPath $sidecar $matches = [regex]::Matches($expect, '(?m)^diagnostic: ([A-Z][A-Z0-9_]+)$') if ($matches.Count -ne 1) { $errors.Add("${sidecar}: expected exactly one symbolic diagnostic") } } $orphanSidecars = @(Get-ChildItem -LiteralPath $rejected -Filter "*.expect" -File | Where-Object { -not (Test-Path -LiteralPath ([IO.Path]::ChangeExtension($_.FullName, ".n8"))) }) foreach ($orphan in $orphanSidecars) { $errors.Add("$($orphan.FullName): orphan diagnostic sidecar") } if ($errors.Count -gt 0) { $errors | ForEach-Object { Write-Error $_ } exit 1 } Write-Host ("grammar fixtures ok: {0} accepted, {1} rejected" -f $acceptedSources.Count, $rejectedSources.Count)