import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { describe, it } from 'node:test'; import matter from 'gray-matter'; const workflow = matter( `---\n${readFileSync(new URL('../.github/workflows/promote.yml', import.meta.url), 'utf8')}\n---`, ).data; const moveStep = workflow.jobs.promote.steps.find((step) => step.id === 'move'); const markStep = workflow.jobs['mark-latest'].steps.at(-1); const semverLt = workflow.env.SEMVER_LT; function read(file) { try { return readFileSync(file, 'utf8'); } catch { return ''; } } // Runs the move step against a stand-in registry client that logs its calls. `latest` gives a // package's current latest; `missing` lists name@version the registry lacks; `failing` lists // name@version whose tag move fails. function runMove( plan, { latest = {}, missing = [], failing = [], allowDowngrade = false, lookupFailsOnce = [] } = {}, ) { const dir = mkdtempSync(join(tmpdir(), 'promote-move-')); try { const calls = join(dir, 'calls'); const list = (values) => values.map((value) => `"${value}"`).join(' '); writeFileSync( join(dir, 'npm'), [ '#!/bin/bash', `echo "$*" >> "${calls}"`, 'if [ "$1" = view ] && [ "$3" = version ]; then', ` for m in ${list(missing)}; do [ "$m" = "$2" ] && exit 0; done`, ' echo "${2##*@}"; exit 0', 'fi', 'if [ "$1" = view ]; then', ` for f in ${list(lookupFailsOnce)}; do [ "$f" = "$2" ] && [ ! -e "${dir}/lookup-failed" ] && touch "${dir}/lookup-failed" && exit 1; done`, ...Object.entries(latest).map( ([name, version]) => ` [ "$2" = "${name}" ] && echo "${version}" && exit 0`, ), ' echo 0.0.0; exit 0', 'fi', `for f in ${list(failing)}; do [ "$f" = "$3" ] && exit 1; done`, 'exit 0', '', ].join('\n'), ); chmodSync(join(dir, 'npm'), 0o755); const output = join(dir, 'output'); writeFileSync(output, ''); const result = spawnSync('bash', ['-e', '-c', moveStep.run], { encoding: 'utf8', env: { PATH: `${dir}:${process.env.PATH}`, PLAN: JSON.stringify(plan), NODE_AUTH_TOKEN: 'the-token', RUNNER_TEMP: dir, GITHUB_OUTPUT: output, SEMVER_LT: semverLt, ALLOW_DOWNGRADE: String(allowDowngrade), }, }); return { status: result.status, log: `${result.stdout}${result.stderr}`, moves: read(calls) .split('\n') .filter((line) => line.startsWith('dist-tag')), output: read(output).trim(), config: read(join(dir, 'promote.npmrc')), }; } finally { rmSync(dir, { recursive: true, force: true }); } } const plan = { version: '8.1.0', packages: ['a', 'b', 'c'].map((letter) => ({ name: `@prisma/orm-${letter}`, version: '8.1.0' })), }; describe('promote.yml', () => { it('keeps the token away from repository code and repository write access', () => { const { check, promote } = workflow.jobs; assert.equal(promote.environment, 'npm-promote'); assert.deepEqual(promote.permissions, {}); assert.ok(promote.steps.every((step) => step.uses === undefined)); for (const job of [check, workflow.jobs['mark-latest']]) { assert.equal(JSON.stringify(job).includes('NPM_PROMOTE_TOKEN'), false); } assert.match(promote.if, /refs\/heads\/main/); assert.match(promote.if, /!inputs\.dry-run/); }); }); describe('the move step', () => { it('moves latest for each package not already there, and refers to the token without writing it', () => { const result = runMove(plan, { latest: { '@prisma/orm-a': '8.1.0' } }); assert.equal(result.status, 0, result.log); assert.deepEqual( { moves: result.moves, output: result.output, tokenInConfig: result.config.includes('the-token'), reference: result.config.includes('${NODE_AUTH_TOKEN}'), }, { moves: [ 'dist-tag add @prisma/orm-b@8.1.0 latest', 'dist-tag add @prisma/orm-c@8.1.0 latest', ], output: 'version=8.1.0', tokenInConfig: false, reference: true, }, ); }); for (const [what, packages] of [ ['a name that is not one of ours', [{ name: 'left-pad', version: '8.1.0' }]], ['a version that is not a version', [{ name: '@prisma/orm-a', version: '8.1.0 --tag x' }]], ['no package', []], ]) { it(`refuses a plan with ${what}, before any registry call`, () => { const result = runMove({ version: '8.1.0', packages }); assert.notEqual(result.status, 0); assert.deepEqual(result.moves, []); }); } it('moves nothing when a package is missing from the registry', () => { const result = runMove(plan, { missing: ['@prisma/orm-c@8.1.0'] }); assert.notEqual(result.status, 0); assert.deepEqual(result.moves, []); assert.match(result.log, /@prisma\/orm-c@8\.1\.0 is not on the registry/); }); it('says which packages moved, and that a rerun finishes, when a move fails part-way', () => { const result = runMove(plan, { failing: ['@prisma/orm-b@8.1.0'] }); assert.notEqual(result.status, 0); assert.match( result.log, /Moved in this run: @prisma\/orm-a\. Run the workflow again to move the rest/, ); assert.deepEqual(result.moves, [ 'dist-tag add @prisma/orm-a@8.1.0 latest', 'dist-tag add @prisma/orm-b@8.1.0 latest', 'dist-tag add @prisma/orm-b@8.1.0 latest', ]); }); }); describe('semver_lt', () => { it('orders versions by Semantic Versioning precedence, prerelease identifiers included', () => { const ordered = [ '8.0.0-rc.9', '8.0.0-rc.10', '8.0.1-dev.40', '8.0.1-ea.0', '8.0.1-preview.2', '8.0.1-rc.0', '8.0.1', '8.1.0', '8.10.0', '10.0.0', ]; const pairs = ordered.flatMap((lower, index) => ordered.slice(index + 1).map((higher) => [lower, higher]), ); const script = [ semverLt, ...pairs.map( ([lower, higher]) => `semver_lt ${lower} ${higher} && ! semver_lt ${higher} ${lower} || echo "wrong: ${lower} ${higher}"`, ), ...ordered.map((version) => `! semver_lt ${version} ${version} || echo "wrong: ${version}"`), ].join('\n'); const result = spawnSync('bash', ['-c', script], { encoding: 'utf8', env: { LC_ALL: 'C' } }); assert.equal(result.status, 0, result.stderr); assert.equal(result.stdout, ''); }); }); describe('the move step and a plan older than latest', () => { const stale = { version: '8.1.0', packages: [{ name: '@prisma/orm-a', version: '8.1.0' }] }; it('refuses to move latest back, and moves nothing, when the plan is from an earlier run', () => { const result = runMove(stale, { latest: { '@prisma/orm-a': '8.2.0' } }); assert.notEqual(result.status, 0); assert.deepEqual(result.moves, []); assert.match(result.log, /from 8\.2\.0 back to 8\.1\.0.*allow-downgrade/); }); it('moves latest back with allow-downgrade on', () => { const result = runMove(stale, { latest: { '@prisma/orm-a': '8.2.0' }, allowDowngrade: true }); assert.equal(result.status, 0, result.log); assert.deepEqual(result.moves, ['dist-tag add @prisma/orm-a@8.1.0 latest']); }); it('moves nothing when reading the current latest fails, since a move back cannot be ruled out', () => { const result = runMove(stale, { latest: { '@prisma/orm-a': '8.2.0' }, lookupFailsOnce: ['@prisma/orm-a'], }); assert.notEqual(result.status, 0); assert.deepEqual(result.moves, []); assert.match(result.log, /Reading the current latest of @prisma\/orm-a failed/); }); it('treats a latest on a development build as no release, so moving off it is not back', () => { const result = runMove(stale, { latest: { '@prisma/orm-a': '8.2.0-dev.3' } }); assert.equal(result.status, 0, result.log); assert.deepEqual(result.moves, ['dist-tag add @prisma/orm-a@8.1.0 latest']); }); }); // Runs the mark-latest step against a stand-in GitHub client. `prerelease` says whether the // release is a pre-release; `latest` is the tag of GitHub's current Latest release, if any. function runMark(version, { prerelease = false, latest, allowDowngrade = false } = {}) { const dir = mkdtempSync(join(tmpdir(), 'promote-mark-')); try { const calls = join(dir, 'calls'); writeFileSync( join(dir, 'gh'), [ '#!/bin/bash', `echo "$*" >> "${calls}"`, `if [ "$1 $2" = "release view" ]; then echo ${prerelease}; exit 0; fi`, latest === undefined ? 'if [ "$1" = api ]; then exit 1; fi' : `if [ "$1" = api ]; then echo ${latest}; exit 0; fi`, 'exit 0', '', ].join('\n'), ); chmodSync(join(dir, 'gh'), 0o755); const result = spawnSync('bash', ['-e', '-c', markStep.run], { encoding: 'utf8', env: { PATH: `${dir}:${process.env.PATH}`, VERSION: version, GITHUB_REPOSITORY: 'prisma/orm', SEMVER_LT: semverLt, ALLOW_DOWNGRADE: String(allowDowngrade), }, }); return { status: result.status, log: `${result.stdout}${result.stderr}`, edits: read(calls) .split('\n') .filter((line) => line.startsWith('release edit')), }; } finally { rmSync(dir, { recursive: true, force: true }); } } describe('the mark-latest step', () => { const edit = ['release edit v8.1.0 --repo prisma/orm --latest']; it('marks a stable release Latest when it is newer than the current one, or there is none', () => { assert.deepEqual(runMark('8.1.0', { latest: 'v8.0.1' }).edits, edit); assert.deepEqual(runMark('8.1.0').edits, edit); }); it('leaves a pre-release unmarked', () => { const result = runMark('8.0.0-rc.26', { prerelease: true, latest: 'v8.0.0-rc.25' }); assert.equal(result.status, 0, result.log); assert.deepEqual(result.edits, []); }); it('refuses to mark an older release Latest, as a rerun of an earlier run would', () => { const result = runMark('8.1.0', { latest: 'v8.2.0' }); assert.notEqual(result.status, 0); assert.deepEqual(result.edits, []); assert.match(result.log, /v8\.2\.0, newer than v8\.1\.0.*allow-downgrade/); }); it('marks an older release Latest with allow-downgrade on', () => { assert.deepEqual(runMark('8.1.0', { latest: 'v8.2.0', allowDowngrade: true }).edits, edit); }); });