|
| 1 | +$ErrorActionPreference = "Stop" |
| 2 | +$CLI = $args[0] |
| 3 | +$TMP = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) |
| 4 | +New-Item -ItemType Directory -Path $TMP | Out-Null |
| 5 | +try { |
| 6 | + $schema = Join-Path $TMP "schema.json" |
| 7 | + $instance = Join-Path $TMP "instance.json" |
| 8 | + |
| 9 | + [System.IO.File]::WriteAllText($schema, @' |
| 10 | +{ |
| 11 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 12 | + "title": "Test", |
| 13 | + "description": "Test schema", |
| 14 | + "allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ] |
| 15 | +} |
| 16 | +'@) |
| 17 | + |
| 18 | + [System.IO.File]::WriteAllText($instance, '{ "type": "string" }') |
| 19 | + |
| 20 | + $pinfo = [System.Diagnostics.ProcessStartInfo]@{ |
| 21 | + FileName = $CLI |
| 22 | + Arguments = "validate `"$schema`" `"$instance`" --http" |
| 23 | + RedirectStandardOutput = $true |
| 24 | + RedirectStandardError = $true |
| 25 | + UseShellExecute = $false |
| 26 | + } |
| 27 | + $process = [System.Diagnostics.Process]::Start($pinfo) |
| 28 | + $stdout = $process.StandardOutput.ReadToEnd() |
| 29 | + $stderr = $process.StandardError.ReadToEnd() |
| 30 | + $process.WaitForExit() |
| 31 | + |
| 32 | + if ($process.ExitCode -ne 4) { |
| 33 | + throw "Expected exit code 4, got $($process.ExitCode)" |
| 34 | + } |
| 35 | + |
| 36 | + $resolvedTmp = (Resolve-Path $TMP).Path.TrimEnd('\') |
| 37 | + $expected = @" |
| 38 | +error: The JSON document is not a valid JSON Schema |
| 39 | + at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema |
| 40 | + at file path $resolvedTmp\schema.json |
| 41 | + at location "/allOf/0/`$ref" |
| 42 | +"@ |
| 43 | + |
| 44 | + [System.IO.File]::WriteAllText((Join-Path $TMP "stderr.txt"), $stderr) |
| 45 | + [System.IO.File]::WriteAllText((Join-Path $TMP "expected.txt"), $expected) |
| 46 | + |
| 47 | + $actualNorm = ($stderr -replace "`r`n", "`n").TrimEnd() |
| 48 | + $expectedNorm = ($expected -replace "`r`n", "`n").TrimEnd() |
| 49 | + if ($actualNorm -cne $expectedNorm) { |
| 50 | + Write-Host "EXPECTED:" |
| 51 | + Write-Host $expected |
| 52 | + Write-Host "---" |
| 53 | + Write-Host "ACTUAL:" |
| 54 | + Write-Host $stderr |
| 55 | + throw "Stderr output mismatch" |
| 56 | + } |
| 57 | +} finally { |
| 58 | + Remove-Item -Recurse -Force $TMP -ErrorAction SilentlyContinue |
| 59 | +} |
0 commit comments