|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { |
| 4 | + mkdtempSync, |
| 5 | + mkdirSync, |
| 6 | + readFileSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from "node:fs"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { dirname, join } from "node:path"; |
| 12 | +import { after, test } from "node:test"; |
| 13 | + |
| 14 | +const workflow = readFileSync( |
| 15 | + new URL("../.github/workflows/ci.yml", import.meta.url), |
| 16 | + "utf8", |
| 17 | +); |
| 18 | +const jobs = Object.fromEntries( |
| 19 | + [ |
| 20 | + ...workflow.matchAll( |
| 21 | + /^ {2}([\w-]+):\n([\s\S]*?)(?=^ {2}[\w-]+:|$(?![\s\S]))/gm, |
| 22 | + ), |
| 23 | + ].map(([, id, body]) => [id, body]), |
| 24 | +); |
| 25 | +const field = (job, key) => { |
| 26 | + const match = jobs[job].match(new RegExp(`^ ${key}: (.+)$`, "m")); |
| 27 | + assert.ok(match, `${job}.${key} must exist`); |
| 28 | + return match[1]; |
| 29 | +}; |
| 30 | +// Evaluate the small expression subset used by these production gates. Values |
| 31 | +// come from fixtures, never from a pull request title, path, or other metadata. |
| 32 | +function evaluate(expression, event, outputs, result = "success", check = "") { |
| 33 | + const translated = expression |
| 34 | + .replace(/^\$\{\{\s*|\s*\}\}$/g, "") |
| 35 | + .replace(/needs\.changes\.outputs\.([\w-]+)/g, (_, key) => |
| 36 | + JSON.stringify(outputs[key] ?? ""), |
| 37 | + ) |
| 38 | + .replaceAll("needs.changes.result", JSON.stringify(result)) |
| 39 | + .replaceAll("github.event_name", JSON.stringify(event)) |
| 40 | + .replaceAll("matrix.check", JSON.stringify(check)) |
| 41 | + .replaceAll("always()", "true"); |
| 42 | + return Function( |
| 43 | + "format", |
| 44 | + `return (${translated});`, |
| 45 | + )((template, value) => template.replace("{0}", value)); |
| 46 | +} |
| 47 | +const scratch = mkdtempSync(join(tmpdir(), "buzz-ci-selection-")); |
| 48 | +after(() => rmSync(scratch, { recursive: true, force: true })); |
| 49 | +const actionSha = workflow.match(/uses: dorny\/paths-filter@([a-f0-9]{40})/)[1]; |
| 50 | +const filters = workflow |
| 51 | + .match(/ {10}filters: \|\n([\s\S]*?)(?= {6}- name:)/)[1] |
| 52 | + .split("\n") |
| 53 | + .map((line) => line.slice(12)) |
| 54 | + .join("\n"); |
| 55 | +let actionPath; |
| 56 | +async function select(paths) { |
| 57 | + if (!actionPath) { |
| 58 | + // Exercise the exact bundled matcher CI executes, including negation and |
| 59 | + // dotfile semantics, instead of maintaining an approximation of picomatch. |
| 60 | + actionPath = join(scratch, "paths-filter.cjs"); |
| 61 | + execFileSync("curl", [ |
| 62 | + "--fail", |
| 63 | + "--silent", |
| 64 | + "--show-error", |
| 65 | + "--max-time", |
| 66 | + "30", |
| 67 | + "-o", |
| 68 | + actionPath, |
| 69 | + `https://raw.githubusercontent.com/dorny/paths-filter/${actionSha}/dist/index.js`, |
| 70 | + ]); |
| 71 | + } |
| 72 | + const repo = mkdtempSync(join(scratch, "repo-")); |
| 73 | + const git = (...args) => |
| 74 | + execFileSync("git", args, { cwd: repo, stdio: "pipe" }); |
| 75 | + git("init", "-q"); |
| 76 | + // Fixture repositories must not inherit developer machine hooks. |
| 77 | + mkdirSync(join(repo, "empty-hooks")); |
| 78 | + git("config", "core.hooksPath", join(repo, "empty-hooks")); |
| 79 | + git( |
| 80 | + "-c", |
| 81 | + "user.name=CI fixture", |
| 82 | + "-c", |
| 83 | + |
| 84 | + "-c", |
| 85 | + "commit.gpgsign=false", |
| 86 | + "commit", |
| 87 | + "-q", |
| 88 | + "--allow-empty", |
| 89 | + "-m", |
| 90 | + "fixture", |
| 91 | + "-s", |
| 92 | + ); |
| 93 | + for (const path of paths) { |
| 94 | + mkdirSync(dirname(join(repo, path)), { recursive: true }); |
| 95 | + writeFileSync(join(repo, path), "fixture\n"); |
| 96 | + } |
| 97 | + git("add", "."); |
| 98 | + const output = join(repo, "action-output"); |
| 99 | + writeFileSync(output, ""); |
| 100 | + const child = spawnSync(process.execPath, [actionPath], { |
| 101 | + cwd: repo, |
| 102 | + encoding: "utf8", |
| 103 | + timeout: 30000, |
| 104 | + env: { |
| 105 | + ...process.env, |
| 106 | + INPUT_BASE: "HEAD", |
| 107 | + INPUT_FILTERS: filters, |
| 108 | + INPUT_TOKEN: "", |
| 109 | + INPUT_REF: "", |
| 110 | + GITHUB_OUTPUT: output, |
| 111 | + "INPUT_PREDICATE-QUANTIFIER": |
| 112 | + workflow.match(/predicate-quantifier: ['"]?([\w-]+)/)?.[1] ?? "some", |
| 113 | + }, |
| 114 | + }); |
| 115 | + assert.equal(child.status, 0, child.stdout + child.stderr); |
| 116 | + return Object.fromEntries( |
| 117 | + [ |
| 118 | + ...readFileSync(output, "utf8").matchAll( |
| 119 | + /^(rust|desktop|desktop-rust|web|mobile)<<([^\n]+)\n(true|false)\n\2/gm, |
| 120 | + ), |
| 121 | + ].map(([, key, , value]) => [key, value]), |
| 122 | + ); |
| 123 | +} |
| 124 | +const scenarios = [ |
| 125 | + [ |
| 126 | + "mobile source and tests", |
| 127 | + [ |
| 128 | + "mobile/lib/features/age_gate/age_signal_provider.dart", |
| 129 | + "mobile/test/features/age_gate/age_signal_provider_test.dart", |
| 130 | + ], |
| 131 | + ["mobile"], |
| 132 | + ], |
| 133 | + ["mobile lockfile", ["mobile/pubspec.lock"], ["mobile"]], |
| 134 | + ["mobile release script", ["scripts/mobile-release.sh"], ["mobile"]], |
| 135 | + ["desktop", ["desktop/src/main.tsx"], ["desktop"]], |
| 136 | + ["Tauri", ["desktop/src-tauri/src/main.rs"], ["desktop", "desktop-rust"]], |
| 137 | + ["relay", ["crates/buzz-relay/src/main.rs"], ["rust"]], |
| 138 | + ["migration", ["migrations/123.sql"], ["rust"]], |
| 139 | + ["shared workflow", [".github/workflows/ci.yml"], ["rust", "mobile"]], |
| 140 | + [ |
| 141 | + "mixed mobile and relay", |
| 142 | + ["mobile/lib/main.dart", "crates/buzz-core/src/lib.rs"], |
| 143 | + ["rust", "mobile"], |
| 144 | + ], |
| 145 | + [ |
| 146 | + "mixed mobile and desktop", |
| 147 | + ["mobile/lib/main.dart", "desktop/src/main.tsx"], |
| 148 | + ["desktop", "mobile"], |
| 149 | + ], |
| 150 | + ["web", ["web/src/main.tsx"], ["web"]], |
| 151 | + ["documentation", ["README.md"], []], |
| 152 | +]; |
| 153 | +for (const [name, paths, expected] of scenarios) { |
| 154 | + test(`real paths-filter: ${name}`, async () => { |
| 155 | + const outputs = await select(paths); |
| 156 | + assert.equal(Object.keys(outputs).length, 5); |
| 157 | + assert.deepEqual( |
| 158 | + Object.keys(outputs) |
| 159 | + .filter((key) => outputs[key] === "true") |
| 160 | + .sort(), |
| 161 | + expected.sort(), |
| 162 | + ); |
| 163 | + const desktop = |
| 164 | + expected.includes("desktop") || |
| 165 | + expected.includes("desktop-rust") || |
| 166 | + expected.includes("rust"); |
| 167 | + for (const job of [ |
| 168 | + "desktop-domain", |
| 169 | + "desktop-macos-domain", |
| 170 | + "relay-artifacts-domain", |
| 171 | + "relay-domain", |
| 172 | + ]) { |
| 173 | + assert.equal( |
| 174 | + evaluate(field(job, "if"), "pull_request", outputs), |
| 175 | + desktop, |
| 176 | + job, |
| 177 | + ); |
| 178 | + assert.equal( |
| 179 | + evaluate(field(job, "if"), "push", outputs), |
| 180 | + true, |
| 181 | + `${job} on push`, |
| 182 | + ); |
| 183 | + } |
| 184 | + assert.equal( |
| 185 | + evaluate(field("postgres-domain", "if"), "pull_request", outputs), |
| 186 | + expected.includes("rust"), |
| 187 | + ); |
| 188 | + }); |
| 189 | +} |
| 190 | +const checks = { |
| 191 | + "backend-integration": [ |
| 192 | + "Backend Integration (relay e2e)", |
| 193 | + "relay-domain", |
| 194 | + "backend_integration_result", |
| 195 | + ], |
| 196 | + "relay-e2e": ["Relay E2E", "relay-domain", "relay_e2e_result"], |
| 197 | + "postgres-tests": [ |
| 198 | + "PostgreSQL Tests", |
| 199 | + "postgres-domain", |
| 200 | + "postgres_tests_result", |
| 201 | + ], |
| 202 | +}; |
| 203 | +test("unneeded backend contexts have no dependency on desktop or relay work", () => { |
| 204 | + assert.equal(field("backend-not-required", "needs"), "[changes]"); |
| 205 | + for (const [name] of Object.values(checks)) |
| 206 | + assert.ok(jobs["backend-not-required"].includes(`'${name}'`)); |
| 207 | + const run = jobs["backend-not-required"].match(/ {8}run: \|\n([\s\S]*)/)[1]; |
| 208 | + for (const result of ["success", "failure", "cancelled", "skipped", ""]) { |
| 209 | + for (const rust of ["false", "true", ""]) { |
| 210 | + const child = spawnSync("bash", ["-e", "-c", run], { |
| 211 | + env: { ...process.env, CHANGES_RESULT: result, RUST: rust }, |
| 212 | + encoding: "utf8", |
| 213 | + }); |
| 214 | + assert.equal( |
| 215 | + child.status === 0, |
| 216 | + result === "success" && rust === "false", |
| 217 | + ); |
| 218 | + } |
| 219 | + } |
| 220 | +}); |
| 221 | +test("exactly one reporter owns each required name, including failed path detection", () => { |
| 222 | + for (const event of ["push", "pull_request"]) { |
| 223 | + for (const rust of ["true", "false", ""]) { |
| 224 | + for (const result of ["success", "failure", "cancelled", "skipped"]) { |
| 225 | + for (const [job, [name]] of Object.entries(checks)) { |
| 226 | + const outputs = { rust }; |
| 227 | + const early = evaluate( |
| 228 | + field("backend-not-required", "if"), |
| 229 | + event, |
| 230 | + outputs, |
| 231 | + result, |
| 232 | + ); |
| 233 | + const late = evaluate(field(job, "if"), event, outputs, result); |
| 234 | + assert.notEqual(early, late, `${event}/${rust}/${result}/${job}`); |
| 235 | + assert.equal( |
| 236 | + evaluate( |
| 237 | + field("backend-not-required", "name"), |
| 238 | + event, |
| 239 | + outputs, |
| 240 | + result, |
| 241 | + name, |
| 242 | + ) === name, |
| 243 | + early, |
| 244 | + ); |
| 245 | + assert.equal( |
| 246 | + evaluate(field(job, "name"), event, outputs, result) === name, |
| 247 | + late, |
| 248 | + ); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | +}); |
| 254 | +test("selected backend gates fail on missing, failed, cancelled, or skipped test results", () => { |
| 255 | + for (const [job, [, domain, output]] of Object.entries(checks)) { |
| 256 | + assert.ok(field(job, "needs").includes(domain)); |
| 257 | + assert.ok( |
| 258 | + jobs[job].includes(`RESULT: \${{ needs.${domain}.outputs.${output} }}`), |
| 259 | + ); |
| 260 | + const run = jobs[job].match(/ {8}run: (.+)/)[1]; |
| 261 | + for (const result of ["success", "failure", "cancelled", "skipped", ""]) { |
| 262 | + const child = spawnSync("bash", ["-e", "-c", run], { |
| 263 | + env: { ...process.env, RESULT: result }, |
| 264 | + }); |
| 265 | + assert.equal( |
| 266 | + child.status === 0, |
| 267 | + result === "success", |
| 268 | + `${job}/${result}`, |
| 269 | + ); |
| 270 | + } |
| 271 | + } |
| 272 | +}); |
0 commit comments