Files
orca/config/scripts/release/workflow-contracts/adhoc-build-pinned-commit.test.mjs

73 lines
2.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Pin every job to dispatch’s resolved commit so mid-run pushes cannot mix artifacts.
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { parse } from 'yaml'
import { runProcess } from '@orca/process-host'
const workflow = parse(readFileSync('.github/workflows/adhoc-mac-build.yml', 'utf8'))
const resolveStep = workflow.jobs['resolve-ref'].steps.find((step) => step.id === 'resolve')
const directory = mkdtempSync(join(tmpdir(), 'adhoc-pinned-commit-'))
const repository = join(directory, 'remote.git')
const identity = {
...process.env,
GIT_AUTHOR_NAME: 'Ref test',
GIT_AUTHOR_EMAIL: 'ref-test@example.com',
GIT_COMMITTER_NAME: 'Ref test',
GIT_COMMITTER_EMAIL: 'ref-test@example.com'
}
let branchTip, tagged
async function git(args) {
const result = await runProcess({ program: 'git', args, env: identity })
expect(result.code, result.stderr).toBe(0)
return result.stdout.trim()
}
async function resolve(ref) {
const scratch = mkdtempSync(join(directory, 'attempt-'))
const script = join(scratch, 'resolve.sh')
writeFileSync(script, resolveStep.run)
const output = join(scratch, 'output')
writeFileSync(output, '')
const result = await runProcess({
program: 'bash',
args: [script],
env: {
...identity,
REPO_URL: pathToFileURL(repository).href,
GITHUB_OUTPUT: output,
RUNNER_TEMP: scratch,
REQUESTED_REF: ref
}
})
return { code: result.code, output: readFileSync(output, 'utf8').trim() }
}
beforeAll(async () => {
await git(['init', '--bare', repository])
const tree = await git(['-C', repository, 'mktree'])
tagged = await git(['-C', repository, 'commit-tree', tree, '-m', 'tagged'])
branchTip = await git(['-C', repository, 'commit-tree', tree, '-p', tagged, '-m', 'tip'])
await git(['-C', repository, 'update-ref', 'refs/heads/feature/x', branchTip])
await git(['-C', repository, 'tag', '-a', 'v1', tagged, '-m', 'annotated'])
})
afterAll(() => rmSync(directory, { recursive: true, force: true }))
describe('adhoc build pins one commit for the whole run', () => {
it('resolves a branch, an annotated tag to its commit, and passes a full SHA through', async () => {
expect(await resolve('feature/x')).toEqual({ code: 0, output: `sha=${branchTip}` })
expect(await resolve('v1')).toEqual({ code: 0, output: `sha=${tagged}` })
expect(await resolve(tagged)).toEqual({ code: 0, output: `sha=${tagged}` })
})
it('refuses PR refs and names it cannot resolve', async () => {
for (const ref of ['refs/pull/1/head', 'pull/1/head', 'missing', tagged.slice(0, 12)]) {
expect(await resolve(ref), ref).toEqual({ code: 1, output: '' })
}
})
})