diff --git a/.github/workflows/release-gate.yml b/.github/workflows/release-gate.yml index 47876a1c..ca31680a 100644 --- a/.github/workflows/release-gate.yml +++ b/.github/workflows/release-gate.yml @@ -101,7 +101,9 @@ jobs: env: MUTATION_SHARD: ${{ matrix.shard }} with: - pass-env: MUTATION_SHARD + pass-env: | + GITHUB_ACTIONS + MUTATION_SHARD command: ./node_modules/.bin/stryker run --plan .cache/stryker-plan.json --shard "$MUTATION_SHARD" - if: always() uses: actions/upload-artifact@v6 diff --git a/sandbox-proofs/release-gate-planner.test.ts b/sandbox-proofs/release-gate.test.ts similarity index 52% rename from sandbox-proofs/release-gate-planner.test.ts rename to sandbox-proofs/release-gate.test.ts index 61a68657..ca6902d1 100644 --- a/sandbox-proofs/release-gate-planner.test.ts +++ b/sandbox-proofs/release-gate.test.ts @@ -8,38 +8,61 @@ const SANDBOX_ACTION = './.github/actions/sandbox' interface SandboxStep { readonly name: string readonly hosts: readonly string[] + readonly passEnv: readonly string[] readonly command: string } const repoRoot = new URL('..', import.meta.url).pathname -const Workflow = S.Struct({ - jobs: S.Struct({ - plan: S.Struct({ - steps: S.Array(S.Struct({ - name: S.optional(S.String), - uses: S.optional(S.String), - with: S.optional(S.Struct({ hosts: S.optional(S.String), command: S.optional(S.String) })), - })), - }), - }), +const Job = S.Struct({ + steps: S.Array(S.Struct({ + name: S.optional(S.String), + uses: S.optional(S.String), + with: S.optional(S.Struct({ + hosts: S.optional(S.String), + 'pass-env': S.optional(S.String), + command: S.optional(S.String), + })), + })), }) -const plannerSteps = async (): Promise => { +const Workflow = S.Struct({ jobs: S.Struct({ plan: Job, mutation: Job }) }) + +const lines = (value: string | undefined): readonly string[] => + (value ?? '').split('\n').map((line) => line.trim()).filter((line) => line !== '') + +const sandboxSteps = async ( + job: 'plan' | 'mutation', + runs: string, +): Promise => { const decoded = S.decodeUnknownResult(Workflow)( parseYaml(await Deno.readTextFile(`${repoRoot}.github/workflows/release-gate.yml`)), ) if (Result.isFailure(decoded)) throw new Error(`release-gate.yml: ${decoded.failure.message}`) - const workflow = decoded.success - return workflow.jobs.plan.steps - .filter((step) => step.uses === SANDBOX_ACTION && step.with?.command?.includes('scripts/stryker-plan-gate.ts')) + return decoded.success.jobs[job].steps + .filter((step) => step.uses === SANDBOX_ACTION && step.with?.command?.includes(runs)) .map((step) => ({ name: step.name ?? '', - hosts: (step.with?.hosts ?? '').split('\n').map((host) => host.trim()).filter((host) => host !== ''), + hosts: lines(step.with?.hosts), + passEnv: lines(step.with?.['pass-env']), command: step.with?.command ?? '', })) } +const inSandbox = (step: SandboxStep, root: string, env: Record = {}) => + run( + 'sandbox', + [ + ...step.hosts.flatMap((host) => ['--allow-host', host]), + ...step.passEnv.flatMap((name) => ['--pass-env', name]), + '--', + 'bash', + '-c', + step.command, + ], + { cwd: root, env: { ...env, SANDBOX_PROJECT: root } }, + ) + const PLAN = { version: 1, targetSeconds: 900, @@ -53,7 +76,7 @@ const PLAN = { } const writeFixture = async (): Promise => { - const root = await Deno.makeTempDir({ prefix: 'release-gate-planner-' }) + const root = await Deno.makeTempDir({ prefix: 'release-gate-' }) await Deno.mkdir(`${root}/scripts`) for (const file of ['deno.json', 'deno.lock', 'stryker-plan-gate.ts']) { await Deno.copyFile(`${repoRoot}scripts/${file}`, `${root}/scripts/${file}`) @@ -71,7 +94,7 @@ const writeFixture = async (): Promise => { } Deno.test('each release-gate planner step runs in the sandbox on only the hosts release-gate.yml declares for it', async () => { - const steps = await plannerSteps() + const steps = await sandboxSteps('plan', 'scripts/stryker-plan-gate.ts') if (steps.length !== 2) { throw new Error(`expected the discover and gate steps, found ${steps.map((step) => step.name)}`) } @@ -79,11 +102,7 @@ Deno.test('each release-gate planner step runs in the sandbox on only the hosts try { for (const step of steps) { await Deno.remove(`${root}/.cache/deno`, { recursive: true }).catch(() => undefined) - const outcome = await run( - 'sandbox', - [...step.hosts.flatMap((host) => ['--allow-host', host]), '--', 'bash', '-c', step.command], - { cwd: root, env: { SANDBOX_PROJECT: root } }, - ) + const outcome = await inSandbox(step, root) if (outcome.code !== 0) throw new Error(`"${step.name}" exited ${outcome.code}:\n${outcome.out}`) } const projects = await Deno.readTextFile(`${root}/.cache/mutation-projects.txt`) @@ -94,3 +113,23 @@ Deno.test('each release-gate planner step runs in the sandbox on only the hosts await Deno.remove(root, { recursive: true }) } }) + +Deno.test('the release-gate mutation step hands stryker the GITHUB_ACTIONS flag its main-CI guard admits', async () => { + const steps = await sandboxSteps('mutation', 'stryker run') + if (steps.length !== 1) throw new Error(`expected one stryker run step, found ${steps.map((step) => step.name)}`) + const root = await writeFixture() + try { + await Deno.mkdir(`${root}/node_modules/.bin`, { recursive: true }) + await Deno.writeTextFile( + `${root}/node_modules/.bin/stryker`, + '#!/usr/bin/env bash\nprintf \'GITHUB_ACTIONS=%s\\nMUTATION_SHARD=%s\\n\' "${GITHUB_ACTIONS-}" "${MUTATION_SHARD-}" > .cache/stryker-env.txt\n', + { mode: 0o755 }, + ) + const outcome = await inSandbox(steps[0]!, root, { GITHUB_ACTIONS: 'true', MUTATION_SHARD: '1/1' }) + if (outcome.code !== 0) throw new Error(`"${steps[0]!.name}" exited ${outcome.code}:\n${outcome.out}`) + const seen = await Deno.readTextFile(`${root}/.cache/stryker-env.txt`) + if (seen !== 'GITHUB_ACTIONS=true\nMUTATION_SHARD=1/1\n') throw new Error(`stryker saw ${JSON.stringify(seen)}`) + } finally { + await Deno.remove(root, { recursive: true }) + } +})