test(codemode): keep another platform's cases in the current format, and fail when they need a run there

This commit is contained in:
Sebastian Herrlinger 2026-11-19 02:24:27 +01:00
parent 0a8fd7250b
commit 7df0f9074e

View file

@ -15,7 +15,8 @@
// both "error")
// // @platform "linux"
// A case with @platform depends on the system libm or compiler, so it is recorded and checked only on that platform
// (`process.platform`); elsewhere this leaves its expectation as it is.
// (`process.platform`). Elsewhere this keeps its recorded outcome but rewrites it in the current format, and fails
// when the case needs a run on its platform (its inputs changed): see `refreshSkipped`.
//
// Each <name>.json: `{ bindings, limits?, translate?, ok: true, value } | { bindings, limits?, translate?, ok: false,
// kind }`, then `logs` (the result's logs, `[]` when none) and `metadata` (the result's `run.metadata`: always `null`
@ -35,6 +36,47 @@ const header = (code: string, tag: string) => {
}
const dir = join(import.meta.dir, "cases")
// A case recorded only on another platform keeps its recorded outcome here, but follows the format the cases are
// written in: its fields are rewritten in the order below, and a field this generator can fill without running the
// case is filled (`metadata` is always null, as `tools` keeps no run state). When its inputs changed, or its outcome
// is missing, it needs a run on its platform: it is listed in `stale`, and the generator fails.
const stale: Array<string> = []
const refreshSkipped = (name: string, inputs: { bindings: unknown; limits: unknown; translate: unknown }): boolean => {
const path = join(dir, `${name}.json`)
const before = readFileSync(path, "utf8")
const recorded = JSON.parse(before)
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
const outcome =
recorded.ok === true && "value" in recorded
? { ok: true, value: recorded.value }
: recorded.ok === false && typeof recorded.kind === "string"
? { ok: false, kind: recorded.kind }
: undefined
if (
outcome === undefined ||
!Array.isArray(recorded.logs) ||
("metadata" in recorded && recorded.metadata !== null) ||
!same(recorded.bindings, inputs.bindings) ||
!same(recorded.limits, inputs.limits) ||
!same(recorded.translate, inputs.translate)
) {
stale.push(` ${name}`)
return false
}
const expected = {
bindings: inputs.bindings,
...(inputs.limits === undefined ? {} : { limits: inputs.limits }),
...(inputs.translate === undefined ? {} : { translate: inputs.translate }),
...outcome,
logs: recorded.logs,
metadata: null,
}
const after = JSON.stringify(expected, null, 2) + "\n"
if (after === before) return false
writeFileSync(path, after)
return true
}
const names = readdirSync(dir)
.filter((f) => f.endsWith(".js"))
.map((f) => f.slice(0, -3))
@ -43,13 +85,15 @@ const names = readdirSync(dir)
for (const name of names) {
const code = readFileSync(join(dir, `${name}.js`), "utf8")
const platform = header(code, "platform")
if (platform !== undefined && platform !== process.platform) {
console.log(`${name.padEnd(36)} skipped (${platform} only)`)
continue
}
const bindings = header(code, "bindings") ?? {}
const limits = header(code, "limits")
const translate = header(code, "translate")
if (platform !== undefined && platform !== process.platform) {
console.log(
`${name.padEnd(36)} ${refreshSkipped(name, { bindings, limits, translate }) ? "reformatted" : "skipped"} (${platform} only)`,
)
continue
}
const runtime = ref.Language.Runtime.make({
globals: () => ({ tools }),
...(limits === undefined
@ -69,3 +113,10 @@ for (const name of names) {
writeFileSync(join(dir, `${name}.json`), JSON.stringify(expected, null, 2) + "\n")
console.log(`${name.padEnd(36)} ${result.ok ? "ok" : result.error.kind}`)
}
if (stale.length > 0) {
console.error(
`These cases are recorded only on another platform, and their expectations need a run there (regenerate on it):\n${stale.join("\n")}`,
)
process.exit(1)
}