|
12 | 12 |
|
13 | 13 | import { test } from "node:test"; |
14 | 14 | import assert from "node:assert/strict"; |
15 | | -import { readFileSync } from "node:fs"; |
| 15 | +import { spawnSync } from "node:child_process"; |
| 16 | +import { |
| 17 | + chmodSync, |
| 18 | + mkdtempSync, |
| 19 | + readFileSync, |
| 20 | + rmSync, |
| 21 | + writeFileSync, |
| 22 | +} from "node:fs"; |
| 23 | +import { tmpdir } from "node:os"; |
16 | 24 | import path from "node:path"; |
17 | 25 | import { fileURLToPath } from "node:url"; |
18 | 26 | import { parse } from "yaml"; |
@@ -67,6 +75,99 @@ for (const { file, context } of CI) { |
67 | 75 | }); |
68 | 76 | } |
69 | 77 |
|
| 78 | +// Run a `dispatch-status` script with a stub `gh` on PATH. The stub answers |
| 79 | +// `gh run list` with the number of newer dispatched runs and records every |
| 80 | +// other call, so a test sees exactly which status, if any, the run posts. |
| 81 | +function runResultScript(script, { needs, newer }) { |
| 82 | + const dir = mkdtempSync(path.join(tmpdir(), "dispatch-status-")); |
| 83 | + try { |
| 84 | + const calls = path.join(dir, "calls"); |
| 85 | + writeFileSync(calls, ""); |
| 86 | + writeFileSync( |
| 87 | + path.join(dir, "gh"), |
| 88 | + [ |
| 89 | + "#!/usr/bin/env bash", |
| 90 | + `if [ "$1 $2" = "run list" ]; then echo ${newer}; exit 0; fi`, |
| 91 | + `printf '%s\\n' "$*" >> "${calls}"`, |
| 92 | + ].join("\n"), |
| 93 | + ); |
| 94 | + chmodSync(path.join(dir, "gh"), 0o755); |
| 95 | + const result = spawnSync("bash", ["-e", "-c", script], { |
| 96 | + encoding: "utf8", |
| 97 | + env: { |
| 98 | + ...process.env, |
| 99 | + PATH: `${dir}${path.delimiter}${process.env.PATH}`, |
| 100 | + NEEDS: JSON.stringify(needs), |
| 101 | + GITHUB_SHA: "0123abcd", |
| 102 | + GITHUB_RUN_ID: "100", |
| 103 | + GITHUB_REPOSITORY: "owner/repo", |
| 104 | + GITHUB_SERVER_URL: "https://github.com", |
| 105 | + }, |
| 106 | + }); |
| 107 | + assert.equal(result.status, 0, result.stderr); |
| 108 | + const posted = readFileSync(calls, "utf8").trim(); |
| 109 | + return posted ? posted.match(/state=(\w+)/)?.[1] : null; |
| 110 | + } finally { |
| 111 | + rmSync(dir, { recursive: true, force: true }); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// The job's script needs bash and jq, as on the ubuntu runner. Without |
| 116 | +// either (Windows, or a machine with no jq), skip rather than fail the gate |
| 117 | +// over a missing test prerequisite. |
| 118 | +const hasTool = (cmd) => spawnSync(cmd, ["--version"]).status === 0; |
| 119 | +const SCRIPT_SKIP = |
| 120 | + process.platform === "win32" |
| 121 | + ? "runs the job's bash script" |
| 122 | + : !hasTool("bash") |
| 123 | + ? "needs bash" |
| 124 | + : !hasTool("jq") |
| 125 | + ? "needs jq, which the job's script calls" |
| 126 | + : false; |
| 127 | + |
| 128 | +const FAILED = { a: { result: "failure" }, b: { result: "cancelled" } }; |
| 129 | +const PASSED = { a: { result: "success" }, b: { result: "skipped" } }; |
| 130 | +const CANCELLED = { a: { result: "success" }, b: { result: "cancelled" } }; |
| 131 | + |
| 132 | +for (const { file } of CI) { |
| 133 | + const script = workflow(file).jobs["dispatch-status"].steps[0].run; |
| 134 | + |
| 135 | + // #5178: the newer-run check guarded only the cancellation branch, so a |
| 136 | + // superseded run whose job had already failed posted `failure` over the |
| 137 | + // newer run's status. A superseded run now posts nothing at all. |
| 138 | + test( |
| 139 | + `${file}: a superseded run posts no status, whatever its jobs did`, |
| 140 | + { |
| 141 | + skip: SCRIPT_SKIP, |
| 142 | + }, |
| 143 | + () => { |
| 144 | + for (const needs of [FAILED, PASSED, CANCELLED]) |
| 145 | + assert.equal(runResultScript(script, { needs, newer: 1 }), null); |
| 146 | + }, |
| 147 | + ); |
| 148 | + |
| 149 | + test( |
| 150 | + `${file}: the latest run posts success, failure or error`, |
| 151 | + { |
| 152 | + skip: SCRIPT_SKIP, |
| 153 | + }, |
| 154 | + () => { |
| 155 | + assert.equal( |
| 156 | + runResultScript(script, { needs: PASSED, newer: 0 }), |
| 157 | + "success", |
| 158 | + ); |
| 159 | + assert.equal( |
| 160 | + runResultScript(script, { needs: FAILED, newer: 0 }), |
| 161 | + "failure", |
| 162 | + ); |
| 163 | + assert.equal( |
| 164 | + runResultScript(script, { needs: CANCELLED, newer: 0 }), |
| 165 | + "error", |
| 166 | + ); |
| 167 | + }, |
| 168 | + ); |
| 169 | +} |
| 170 | + |
70 | 171 | for (const { file, job } of VERSION_WORKFLOWS) { |
71 | 172 | const steps = workflow(file).jobs[job]; |
72 | 173 |
|
|
0 commit comments