|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { |
| 4 | + existsSync, |
| 5 | + mkdtempSync, |
| 6 | + mkdirSync, |
| 7 | + readFileSync, |
| 8 | + rmSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
| 11 | +import { tmpdir } from "node:os"; |
| 12 | +import { dirname, join } from "node:path"; |
| 13 | +import { after, test } from "node:test"; |
| 14 | + |
| 15 | +const workflow = readFileSync( |
| 16 | + new URL("../.github/workflows/ci.yml", import.meta.url), |
| 17 | + "utf8", |
| 18 | +); |
| 19 | +const scratch = mkdtempSync(join(tmpdir(), "buzz-ci-selection-")); |
| 20 | +after(() => rmSync(scratch, { recursive: true, force: true })); |
| 21 | +const actionSha = workflow.match(/uses: dorny\/paths-filter@([a-f0-9]{40})/)[1]; |
| 22 | +const filters = workflow |
| 23 | + .match(/ {10}filters: \|\n([\s\S]*?)(?= {6}- name:)/)[1] |
| 24 | + .split("\n") |
| 25 | + .map((line) => line.slice(12)) |
| 26 | + .join("\n"); |
| 27 | +// GitHub-hosted runners prepare pinned actions beside RUNNER_TEMP before any |
| 28 | +// steps run. Reuse that bundle, without another download in the selection gate. |
| 29 | +// Locally, point PATHS_FILTER_ACTION at dist/index.js from the pinned action. |
| 30 | +const actionPath = |
| 31 | + process.env.PATHS_FILTER_ACTION || |
| 32 | + (process.env.RUNNER_TEMP && |
| 33 | + join( |
| 34 | + process.env.RUNNER_TEMP, |
| 35 | + "..", |
| 36 | + "_actions", |
| 37 | + "dorny", |
| 38 | + "paths-filter", |
| 39 | + actionSha, |
| 40 | + "dist", |
| 41 | + "index.js", |
| 42 | + )); |
| 43 | +assert.ok( |
| 44 | + actionPath && existsSync(actionPath), |
| 45 | + `Set PATHS_FILTER_ACTION to the local dist/index.js from dorny/paths-filter@${actionSha}`, |
| 46 | +); |
| 47 | +function select(paths) { |
| 48 | + const repo = mkdtempSync(join(scratch, "repo-")); |
| 49 | + const git = (...args) => |
| 50 | + execFileSync("git", args, { cwd: repo, stdio: "pipe", timeout: 10000 }); |
| 51 | + git("init", "-q"); |
| 52 | + // Fixture repositories must not inherit developer machine hooks. |
| 53 | + mkdirSync(join(repo, "empty-hooks")); |
| 54 | + git("config", "core.hooksPath", join(repo, "empty-hooks")); |
| 55 | + git( |
| 56 | + "-c", |
| 57 | + "user.name=CI fixture", |
| 58 | + "-c", |
| 59 | + |
| 60 | + "-c", |
| 61 | + "commit.gpgsign=false", |
| 62 | + "commit", |
| 63 | + "-q", |
| 64 | + "--allow-empty", |
| 65 | + "-m", |
| 66 | + "fixture", |
| 67 | + "-s", |
| 68 | + ); |
| 69 | + for (const path of paths) { |
| 70 | + mkdirSync(dirname(join(repo, path)), { recursive: true }); |
| 71 | + writeFileSync(join(repo, path), "fixture\n"); |
| 72 | + } |
| 73 | + git("add", "."); |
| 74 | + const output = join(repo, "action-output"); |
| 75 | + writeFileSync(output, ""); |
| 76 | + const child = spawnSync(process.execPath, [actionPath], { |
| 77 | + cwd: repo, |
| 78 | + encoding: "utf8", |
| 79 | + timeout: 30000, |
| 80 | + env: { |
| 81 | + ...process.env, |
| 82 | + INPUT_BASE: "HEAD", |
| 83 | + INPUT_FILTERS: filters, |
| 84 | + INPUT_TOKEN: "", |
| 85 | + INPUT_REF: "", |
| 86 | + GITHUB_OUTPUT: output, |
| 87 | + "INPUT_PREDICATE-QUANTIFIER": |
| 88 | + workflow.match(/predicate-quantifier: ['"]?([\w-]+)/)?.[1] ?? "some", |
| 89 | + }, |
| 90 | + }); |
| 91 | + assert.equal(child.status, 0, child.stdout + child.stderr); |
| 92 | + return Object.fromEntries( |
| 93 | + [ |
| 94 | + ...readFileSync(output, "utf8").matchAll( |
| 95 | + /^(rust|desktop|desktop-rust|web|mobile)<<([^\n]+)\n(true|false)\n\2/gm, |
| 96 | + ), |
| 97 | + ].map(([, key, , value]) => [key, value]), |
| 98 | + ); |
| 99 | +} |
| 100 | +const scenarios = [ |
| 101 | + [ |
| 102 | + "mobile source and tests", |
| 103 | + [ |
| 104 | + "mobile/lib/features/age_gate/age_signal_provider.dart", |
| 105 | + "mobile/test/features/age_gate/age_signal_provider_test.dart", |
| 106 | + ], |
| 107 | + ["mobile"], |
| 108 | + ], |
| 109 | + ["mobile lockfile", ["mobile/pubspec.lock"], ["mobile"]], |
| 110 | + ["mobile release script", ["scripts/mobile-release.sh"], ["mobile"]], |
| 111 | + ["desktop", ["desktop/src/main.tsx"], ["desktop"]], |
| 112 | + ["Tauri", ["desktop/src-tauri/src/main.rs"], ["desktop", "desktop-rust"]], |
| 113 | + ["relay", ["crates/buzz-relay/src/main.rs"], ["rust"]], |
| 114 | + ["migration", ["migrations/123.sql"], ["rust"]], |
| 115 | + ["shared workflow", [".github/workflows/ci.yml"], ["rust", "mobile"]], |
| 116 | + [ |
| 117 | + "mixed mobile and relay", |
| 118 | + ["mobile/lib/main.dart", "crates/buzz-core/src/lib.rs"], |
| 119 | + ["rust", "mobile"], |
| 120 | + ], |
| 121 | + [ |
| 122 | + "mixed mobile and desktop", |
| 123 | + ["mobile/lib/main.dart", "desktop/src/main.tsx"], |
| 124 | + ["desktop", "mobile"], |
| 125 | + ], |
| 126 | + ["web", ["web/src/main.tsx"], ["web"]], |
| 127 | + ["documentation", ["README.md"], []], |
| 128 | +]; |
| 129 | +for (const [name, paths, expected] of scenarios) { |
| 130 | + test(`real paths-filter: ${name}`, () => { |
| 131 | + const outputs = select(paths); |
| 132 | + assert.equal(Object.keys(outputs).length, 5); |
| 133 | + assert.deepEqual( |
| 134 | + Object.keys(outputs) |
| 135 | + .filter((key) => outputs[key] === "true") |
| 136 | + .sort(), |
| 137 | + expected.sort(), |
| 138 | + ); |
| 139 | + }); |
| 140 | +} |
0 commit comments