|
| 1 | +// Regression tests for how `release.yml`'s `publish-npm` job gets its npm (#5104). |
| 2 | +// |
| 3 | +// That job holds the OIDC credential, so it installs nothing: it runs an |
| 4 | +// exactly pinned Node whose bundled npm meets the floor for OIDC trusted |
| 5 | +// publishing (11.5.1), and a step fails the job before anything is published |
| 6 | +// if that npm is ever below the floor. The step's comparator is inline shell |
| 7 | +// and JavaScript in the workflow file, which runs only after a Release is |
| 8 | +// published, so a later edit that let 11.5.0 through would otherwise surface |
| 9 | +// at release time. |
| 10 | +// |
| 11 | +// As in release-dist-tag.test.mjs, these tests read the step out of the |
| 12 | +// workflow file and run THAT, with `npm` on PATH replaced by a stub that |
| 13 | +// reports a chosen version. They also pin the job's shape: an exact Node |
| 14 | +// pin, the check ahead of the download and the publish, no step that |
| 15 | +// installs a package, and the `./` that makes `npm publish` read the tarball |
| 16 | +// as a local file. Without it npm reads `release-artifact/x.tgz` as GitHub |
| 17 | +// `owner/repo` shorthand and tries to clone it over SSH: the MCP Inspector's |
| 18 | +// first release through the same split publish job failed exactly that way |
| 19 | +// (modelcontextprotocol/inspector#2551). |
| 20 | +// |
| 21 | +// The step's shell is bash, so those tests are skipped where there is none. |
| 22 | +// Run via `npm run test:scripts`. |
| 23 | + |
| 24 | +import { test } from "node:test"; |
| 25 | +import assert from "node:assert/strict"; |
| 26 | +import { spawnSync } from "node:child_process"; |
| 27 | +import { |
| 28 | + chmodSync, |
| 29 | + mkdtempSync, |
| 30 | + readFileSync, |
| 31 | + rmSync, |
| 32 | + writeFileSync, |
| 33 | +} from "node:fs"; |
| 34 | +import { tmpdir } from "node:os"; |
| 35 | +import path from "node:path"; |
| 36 | +import { fileURLToPath } from "node:url"; |
| 37 | +import { parse } from "yaml"; |
| 38 | + |
| 39 | +const repoRoot = path.resolve( |
| 40 | + path.dirname(fileURLToPath(import.meta.url)), |
| 41 | + "..", |
| 42 | +); |
| 43 | + |
| 44 | +const CHECK_STEP = "Check the bundled npm supports OIDC trusted publishing"; |
| 45 | + |
| 46 | +/** The `publish-npm` job's steps, as the workflow file declares them. */ |
| 47 | +function publishSteps() { |
| 48 | + const workflow = parse( |
| 49 | + readFileSync( |
| 50 | + path.join(repoRoot, ".github", "workflows", "release.yml"), |
| 51 | + "utf8", |
| 52 | + ), |
| 53 | + ); |
| 54 | + const job = workflow.jobs["publish-npm"]; |
| 55 | + assert.ok(job, "release.yml has no publish-npm job"); |
| 56 | + return job.steps; |
| 57 | +} |
| 58 | + |
| 59 | +const hasBash = |
| 60 | + process.platform !== "win32" && |
| 61 | + spawnSync("bash", ["-c", "exit 0"]).status === 0; |
| 62 | + |
| 63 | +/** |
| 64 | + * Run the workflow's npm floor check with `npm --version` reporting `version`. |
| 65 | + * |
| 66 | + * @param {string} version what the stub npm reports |
| 67 | + * @returns {{ status: number | null, stderr: string }} |
| 68 | + */ |
| 69 | +function runCheck(version) { |
| 70 | + const step = publishSteps().find((s) => s.name === CHECK_STEP); |
| 71 | + assert.ok(step, `publish-npm has no \`${CHECK_STEP}\` step`); |
| 72 | + const dir = mkdtempSync(path.join(tmpdir(), "npm-floor-")); |
| 73 | + try { |
| 74 | + const stub = path.join(dir, "npm"); |
| 75 | + writeFileSync(stub, `#!/bin/sh\necho "${version}"\n`); |
| 76 | + chmodSync(stub, 0o755); |
| 77 | + const result = spawnSync("bash", ["-e", "-c", step.run], { |
| 78 | + encoding: "utf8", |
| 79 | + env: { |
| 80 | + ...process.env, |
| 81 | + PATH: `${dir}${path.delimiter}${process.env.PATH}`, |
| 82 | + }, |
| 83 | + }); |
| 84 | + return { status: result.status, stderr: result.stderr }; |
| 85 | + } finally { |
| 86 | + rmSync(dir, { recursive: true, force: true }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +test( |
| 91 | + "the npm floor check accepts 11.5.1 and later", |
| 92 | + { skip: !hasBash && "needs bash" }, |
| 93 | + () => { |
| 94 | + for (const version of ["11.5.1", "11.6.0", "11.19.0", "12.0.0"]) { |
| 95 | + const { status, stderr } = runCheck(version); |
| 96 | + assert.equal(status, 0, `npm ${version} was refused: ${stderr}`); |
| 97 | + } |
| 98 | + }, |
| 99 | +); |
| 100 | + |
| 101 | +test( |
| 102 | + "the npm floor check refuses anything below 11.5.1", |
| 103 | + { skip: !hasBash && "needs bash" }, |
| 104 | + () => { |
| 105 | + for (const version of ["11.5.0", "11.4.9", "10.9.9", "9.0.0"]) { |
| 106 | + const { status, stderr } = runCheck(version); |
| 107 | + assert.notEqual(status, 0, `npm ${version} was accepted`); |
| 108 | + assert.match(stderr, /below 11\.5\.1/); |
| 109 | + } |
| 110 | + }, |
| 111 | +); |
| 112 | + |
| 113 | +test("publish-npm pins Node exactly, to a release whose npm meets the floor", () => { |
| 114 | + const setup = publishSteps().find((s) => |
| 115 | + String(s.uses ?? "").startsWith("actions/setup-node@"), |
| 116 | + ); |
| 117 | + assert.ok(setup, "publish-npm has no actions/setup-node step"); |
| 118 | + const pin = String(setup.with?.["node-version"] ?? ""); |
| 119 | + assert.match( |
| 120 | + pin, |
| 121 | + /^\d+\.\d+\.\d+$/, |
| 122 | + `node-version ${pin} is not an exact x.y.z pin`, |
| 123 | + ); |
| 124 | + // Node 24.5.0 is the first release whose bundled npm is 11.5.1. |
| 125 | + const [major, minor] = pin.split(".").map(Number); |
| 126 | + assert.ok( |
| 127 | + major > 24 || (major === 24 && minor >= 5), |
| 128 | + `Node ${pin} bundles an npm below the OIDC floor`, |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +test("publish-npm checks npm before it downloads or publishes anything", () => { |
| 133 | + const names = publishSteps().map((s) => s.name ?? s.uses); |
| 134 | + const check = names.indexOf(CHECK_STEP); |
| 135 | + assert.notEqual(check, -1, `publish-npm has no \`${CHECK_STEP}\` step`); |
| 136 | + const download = names.findIndex((n) => |
| 137 | + String(n).startsWith("Download the artifact"), |
| 138 | + ); |
| 139 | + const publish = names.indexOf("Publish package"); |
| 140 | + assert.ok( |
| 141 | + download !== -1 && publish !== -1, |
| 142 | + "publish-npm lost its download or publish step", |
| 143 | + ); |
| 144 | + assert.ok( |
| 145 | + check < download && check < publish, |
| 146 | + `the npm check runs after: ${names.join(" > ")}`, |
| 147 | + ); |
| 148 | +}); |
| 149 | + |
| 150 | +test("publish-npm installs no package next to the OIDC credential", () => { |
| 151 | + const install = |
| 152 | + /\b(?:npm|pnpm|yarn)\s+(?:install|i|ci|add|exec|dlx)\b|\bnpx\b|\bcorepack\b|\bpip3?\s+install\b|\buvx?\b/; |
| 153 | + for (const step of publishSteps()) { |
| 154 | + if (!step.run) continue; |
| 155 | + // Shell comments may name these tools in prose; only commands count. |
| 156 | + const commands = step.run |
| 157 | + .split("\n") |
| 158 | + .filter((line) => !line.trim().startsWith("#")) |
| 159 | + .join("\n"); |
| 160 | + assert.doesNotMatch( |
| 161 | + commands, |
| 162 | + install, |
| 163 | + `publish-npm step \`${step.name}\` installs or runs a fetched package`, |
| 164 | + ); |
| 165 | + } |
| 166 | +}); |
| 167 | + |
| 168 | +test("publish-npm hands npm the tarball as a ./ path, not a bare dir/file", () => { |
| 169 | + const step = publishSteps().find((s) => s.name === "Publish package"); |
| 170 | + assert.ok(step, "publish-npm has no `Publish package` step"); |
| 171 | + const commands = step.run |
| 172 | + .split("\n") |
| 173 | + .filter((line) => !line.trim().startsWith("#")) |
| 174 | + .join("\n"); |
| 175 | + assert.match( |
| 176 | + commands, |
| 177 | + /^\s*set -- \.\/release-artifact\/\*\.tgz\s*$/m, |
| 178 | + "the publish step must resolve the tarball as ./release-artifact/*.tgz", |
| 179 | + ); |
| 180 | + assert.doesNotMatch( |
| 181 | + commands, |
| 182 | + /set -- release-artifact\//, |
| 183 | + "a bare release-artifact/ path makes npm read the tarball as a GitHub repo", |
| 184 | + ); |
| 185 | + assert.match( |
| 186 | + commands, |
| 187 | + /npm publish "\$1"/, |
| 188 | + "npm publish must be given the ./ path resolved above", |
| 189 | + ); |
| 190 | +}); |
0 commit comments