Skip to content

Commit c3c0b4b

Browse files
committed
test: prove drift's ancestry check
1 parent 2454c06 commit c3c0b4b

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

‎test/drift-workflow.test.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ test('no run script in the drift workflow interpolates an expression', () => {
411411
});
412412

413413
test('every checkout keeps no credentials and takes no ref, and is the first step of its job', () => {
414-
// Each checkout takes the event's commit, and the step right after it moves the job to the tip of main.
414+
// Each checkout takes the event's commit. On main, the step right after it moves the build job to the tip of main,
415+
// and the pr job to the commit the build job handed on, which main can have moved past since. A dispatch from
416+
// another branch skips the build job's move, and runs no pr job.
415417
for (const [job, list] of [['build', steps(buildJob())], ['pr', steps(prJob())]] as const) {
416418
const checkouts = list.filter((step) => /uses: *actions\/checkout@/.test(step));
417419
assert.equal(checkouts.length, 1, `expected one checkout in the ${job} job`);
@@ -515,15 +517,23 @@ const moveFixture = (): MoveRepo => {
515517

516518
/**
517519
* Runs one of the moves as the runner does, `bash -e` with GITHUB_SHA at the event's commit, in a checkout of that
518-
* commit alone, as actions/checkout leaves it. `origin` serves the repository in place of the fixture's. git is real.
520+
* commit alone, as actions/checkout leaves it. `fetch` is one more commit the checkout holds before the move, and
521+
* `origin` serves the repository in place of the fixture's. git is real.
519522
*/
520-
const runMove = (script: string, { sha, origin }: { sha?: string; origin?: string } = {}) => {
523+
const runMove = (script: string, { sha, fetch, origin }: { sha?: string; fetch?: string; origin?: string } = {}) => {
521524
const repo = moveFixture();
522525
const work = moveScratch('checkout');
523526
gitIn(work, ['init', '--quiet']);
524527
gitIn(work, ['remote', 'add', 'origin', `file://${repo.origin}`]);
525528
gitIn(work, ['fetch', '--quiet', '--no-tags', '--depth=1', 'origin', repo.event]);
526529
gitIn(work, ['switch', '--quiet', '--detach', repo.event]);
530+
if (fetch !== undefined) gitIn(work, ['fetch', '--quiet', '--no-tags', '--depth=1', 'origin', fetch]);
531+
const has = (commit: string): boolean =>
532+
spawnSync('git', ['cat-file', '-e', `${commit}^{commit}`], {
533+
cwd: work,
534+
env: { PATH: process.env['PATH'] ?? '', HOME: work, GIT_CONFIG_NOSYSTEM: '1', GIT_NO_LAZY_FETCH: '1' },
535+
}).status === 0;
536+
const held = fetch === undefined || has(fetch);
527537
if (origin !== undefined) gitIn(work, ['remote', 'set-url', 'origin', origin]);
528538
const output = join(work, '.github-output');
529539
writeFileSync(output, '');
@@ -544,6 +554,8 @@ const runMove = (script: string, { sha, origin }: { sha?: string; origin?: strin
544554
stderr: run.stderr,
545555
output: readFileSync(output, 'utf8'),
546556
head: gitIn(work, ['rev-parse', 'HEAD']),
557+
/** Whether the checkout held the commit `fetch` names before the move ran. */
558+
held,
547559
log: `${run.stdout}${run.stderr}`,
548560
};
549561
};
@@ -573,8 +585,18 @@ test("the pr move switches to a commit of main at or after the event's, and refu
573585
assert.equal(run.status, 0, `the move refused ${what}\n${run.log}`);
574586
assert.equal(run.head, sha, `the move did not switch to ${what}`);
575587
}
588+
// A commit outside main that the checkout holds, so the ancestry check itself refuses it, without an error from git.
589+
const side = runMove(script, { sha: repo.side, fetch: repo.side });
590+
assert.ok(side.held, 'the checkout does not hold the side commit before the move');
591+
assert.equal(side.status, 1, `the move did not end red on a commit outside main it holds\n${side.log}`);
592+
assert.equal(side.head, repo.event, "the move left the event's commit on a commit outside main it holds");
593+
assert.ok(side.stderr.split('\n').includes('not a commit of main'), `the move did not say not a commit of main\n${side.log}`);
594+
assert.doesNotMatch(side.stderr, /fatal|error/i, `git failed on the side commit, so the ancestry check decided nothing\n${side.log}`);
595+
// The move's filtered fetch makes origin a promisor, so git fetches a commit outside main it does not hold when the
596+
// ancestry check asks for it, and one origin does not serve is refused as well.
576597
const refused: Array<[string, string, string]> = [
577-
['a commit outside main', repo.side, 'not a commit of main'],
598+
['a commit outside main the checkout does not hold', repo.side, 'not a commit of main'],
599+
['a commit origin does not serve', 'f'.repeat(40), 'not a commit of main'],
578600
["a commit of main older than the event's", repo.root, "older than the event's commit"],
579601
['no commit SHA', 'nope', 'the build job handed on no full commit SHA'],
580602
// A dispatch from another branch hands on no commit, and its pr job does not run. Were it to, it moves nowhere.

0 commit comments

Comments
 (0)