Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/release-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<readonly SandboxStep[]> => {
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<readonly SandboxStep[]> => {
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<string, string> = {}) =>
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,
Expand All @@ -53,7 +76,7 @@ const PLAN = {
}

const writeFixture = async (): Promise<string> => {
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}`)
Expand All @@ -71,19 +94,15 @@ const writeFixture = async (): Promise<string> => {
}

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)}`)
}
const root = await writeFixture()
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`)
Expand All @@ -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 })
}
})
Loading