| 1 | import { spawnSync } from "node:child_process"; |
| 2 | import { resolve } from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | |
| 5 | export const groups = ["A-B", "C", "D", "E-H", "I-P", "Q-S", "T-Z"]; |
| 6 | export const conptyProbe = "TestWindowsTerminalProcessConPTYSmoke"; |
| 7 | export const filters = { |
| 8 | "A-B": { skip: "^Test[C-Z]" }, |
| 9 | "C": { run: "^TestC" }, |
| 10 | "D": { run: "^TestD" }, |
| 11 | "E-H": { run: "^Test[E-H]" }, |
| 12 | "I-P": { run: "^Test[I-P]" }, |
| 13 | "Q-S": { run: "^Test[Q-S]" }, |
| 14 | "T-Z": { run: "^Test[T-Z]", skip: `^${conptyProbe}$` }, |
| 15 | }; |
| 16 | |
| 17 | export function owners(name) { |
| 18 | const assigned = groups.filter(group => { |
| 19 | const { run, skip } = filters[group]; |
| 20 | return (!run || new RegExp(run).test(name)) && (!skip || !new RegExp(skip).test(name)); |
| 21 | }); |
| 22 | if (name === conptyProbe) assigned.push("conpty-probe"); |
| 23 | return assigned; |
| 24 | } |
| 25 | |
| 26 | export function inventoryFromJSON(output) { |
| 27 | const names = new Map(); |
| 28 | for (const line of output.split(/\r?\n/).filter(Boolean)) { |
| 29 | const event = JSON.parse(line); |
| 30 | const name = event.Output?.trim(); |
| 31 | if (event.Action === "output" && /^(Test|Example|Fuzz)\S*$/u.test(name ?? "")) { |
| 32 | names.set(`${event.Package}/${name}`, name); |
| 33 | } |
| 34 | } |
| 35 | if (!names.size) throw new Error("Go returned no desktop test inventory"); |
| 36 | return names; |
| 37 | } |
| 38 | |
| 39 | export function verifyInventory(inventory, requireProbe = process.platform === "win32") { |
| 40 | const counts = Object.fromEntries([...groups, "conpty-probe"].map(group => [group, 0])); |
| 41 | for (const [key, name] of inventory) { |
| 42 | const assigned = owners(name); |
| 43 | if (assigned.length !== 1) throw new Error(`${key} belongs to ${assigned.length} test groups`); |
| 44 | counts[assigned[0]]++; |
| 45 | } |
| 46 | for (const group of groups) { |
| 47 | if (!counts[group]) throw new Error(`Empty desktop Windows test group: ${group}`); |
| 48 | } |
| 49 | if (requireProbe && counts["conpty-probe"] !== 1) throw new Error("Missing or duplicated native ConPTY probe"); |
| 50 | return counts; |
| 51 | } |
| 52 | |
| 53 | export function testArgs(group, race = false) { |
| 54 | if (!groups.includes(group)) throw new Error(`Unknown desktop Windows test group: ${group}`); |
| 55 | const { run, skip } = filters[group]; |
| 56 | return ["test", ...(race ? ["-race"] : []), ...(run ? ["-run", run] : []), ...(skip ? ["-skip", skip] : []), "./..."]; |
| 57 | } |
| 58 | |
| 59 | function main(group, mode) { |
| 60 | if (mode && mode !== "--race") throw new Error(`Unknown test mode: ${mode}`); |
| 61 | const race = mode === "--race"; |
| 62 | const args = group === "--verify" ? null : testArgs(group, race); |
| 63 | // Ask Go for the current platform's inventory, including build-tagged tests, |
| 64 | // examples and fuzz seeds. JSON is used only for listing, not test execution. |
| 65 | const listed = spawnSync("go", ["test", ...(race ? ["-race"] : []), "-list", ".", "-json", "./..."], { encoding: "utf8", maxBuffer: 16 << 20 }); |
| 66 | if (listed.error) throw listed.error; |
| 67 | if (listed.status !== 0) { |
| 68 | process.stderr.write(listed.stdout + listed.stderr); |
| 69 | return listed.status ?? 1; |
| 70 | } |
| 71 | console.log("Desktop test partition:", verifyInventory(inventoryFromJSON(listed.stdout))); |
| 72 | if (!args) return 0; |
| 73 | console.log(`go ${args.join(" ")}`); |
| 74 | const result = spawnSync("go", args, { stdio: "inherit" }); |
| 75 | if (result.error) throw result.error; |
| 76 | return result.status ?? 1; |
| 77 | } |
| 78 | |
| 79 | if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 80 | process.exitCode = main(process.argv[2], process.argv[3]); |
| 81 | } |
| 82 |