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 { parse } from "yaml"; const workflow = parse( readFileSync( new URL("../.github/workflows/promote.yml", import.meta.url), "utf8", ), ); const moveStep = workflow.jobs.promote.steps.find((step) => step.id === "move"); // 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 = [] } = {}) { 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', ...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, }, }); const read = (file) => { try { return readFileSync(file, "utf8"); } catch { return ""; } }; 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 }); } } // The npm config line refers to the token through the shell's variable syntax. const TOKEN_REFERENCE = "$" + "{NODE_AUTH_TOKEN}"; const DRY_RUN_GROUP = /^\$\{\{ \(inputs\.dry-run \|\| github\.ref != 'refs\/heads\/main'\) && format\('npm-promote-dry-run-\{0\}', github\.run_id\) \|\| 'npm-promote' \}\}$/; const ON_MAIN = /refs\/heads\/main/; const NOT_DRY_RUN = /!inputs\.dry-run/; const NOT_ON_REGISTRY = /prisma@8\.1\.0 is not on the registry/; const RERUN_FINISHES = /Moved in this run: @prisma\/cli\. Run the workflow again to move the rest/; const plan = { version: "8.1.0", packages: ["@prisma/cli", "prisma"].map((name) => ({ name, 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, ON_MAIN); assert.match(promote.if, NOT_DRY_RUN); }); }); describe("concurrency", () => { it("gives every run but a real one from main its own group, so it cannot replace a waiting real promotion", () => { assert.match(workflow.concurrency.group, DRY_RUN_GROUP); assert.equal(workflow.concurrency["cancel-in-progress"], false); }); }); 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/cli": "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(TOKEN_REFERENCE), }, { moves: ["dist-tag add prisma@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 package of ours that is not promoted", [{ name: "@prisma/cli-engine", version: "8.1.0" }], ], [ "a name that only starts like ours", [{ name: "prismax", version: "8.1.0" }], ], [ "a version that is not a version", [{ name: "prisma", 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@8.1.0"] }); assert.notEqual(result.status, 0); assert.deepEqual(result.moves, []); assert.match(result.log, NOT_ON_REGISTRY); }); it("says which packages moved, and that a rerun finishes, when a move fails part-way", () => { const result = runMove(plan, { failing: ["prisma@8.1.0"] }); assert.notEqual(result.status, 0); assert.match(result.log, RERUN_FINISHES); assert.deepEqual(result.moves, [ "dist-tag add @prisma/cli@8.1.0 latest", "dist-tag add prisma@8.1.0 latest", "dist-tag add prisma@8.1.0 latest", ]); }); });