|
| 1 | +// Regression tests for the npm dist-tag `release.yml` publishes with (#4873). |
| 2 | +// |
| 3 | +// The choice between `latest` and `backfill` is made by a few lines of |
| 4 | +// JavaScript INSIDE the `publish-npm` job's publish step. It cannot be a |
| 5 | +// script in this directory: that job holds the OIDC credential and has no |
| 6 | +// checkout, on purpose, so the only code it can run is what the workflow file |
| 7 | +// itself carries. A helper tested here and re-typed there would be two copies |
| 8 | +// that can drift, with the untested one being the one that publishes. |
| 9 | +// |
| 10 | +// So these tests read the step out of the workflow file and run THAT: the |
| 11 | +// same text GitHub Actions hands to bash. A change to the workflow that lets |
| 12 | +// `latest` move backward fails here. |
| 13 | +// |
| 14 | +// The step's shell is bash, so the tests are skipped where there is none. |
| 15 | +// Run via `npm run test:scripts`. |
| 16 | + |
| 17 | +import { test } from "node:test"; |
| 18 | +import assert from "node:assert/strict"; |
| 19 | +import { spawnSync } from "node:child_process"; |
| 20 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 21 | +import { tmpdir } from "node:os"; |
| 22 | +import path from "node:path"; |
| 23 | +import { fileURLToPath } from "node:url"; |
| 24 | +import { parse } from "yaml"; |
| 25 | + |
| 26 | +const repoRoot = path.resolve( |
| 27 | + path.dirname(fileURLToPath(import.meta.url)), |
| 28 | + "..", |
| 29 | +); |
| 30 | + |
| 31 | +/** The `TAG="$(node -e "…")"` assignment, exactly as the publish step has it. */ |
| 32 | +function tagAssignment() { |
| 33 | + const workflow = parse( |
| 34 | + readFileSync( |
| 35 | + path.join(repoRoot, ".github", "workflows", "release.yml"), |
| 36 | + "utf8", |
| 37 | + ), |
| 38 | + ); |
| 39 | + const step = workflow.jobs["publish-npm"].steps.find( |
| 40 | + (s) => s.name === "Publish package", |
| 41 | + ); |
| 42 | + assert.ok(step, "publish-npm has no `Publish package` step"); |
| 43 | + const start = step.run.indexOf('TAG="$(node -e "'); |
| 44 | + const end = step.run.indexOf('")" || {', start); |
| 45 | + assert.ok( |
| 46 | + start !== -1 && end !== -1, |
| 47 | + 'the publish step no longer chooses its dist-tag with `TAG="$(node -e …)"`; update this test with it', |
| 48 | + ); |
| 49 | + return step.run.slice(start, end + 3); |
| 50 | +} |
| 51 | + |
| 52 | +const hasBash = spawnSync("bash", ["-c", "exit 0"]).status === 0; |
| 53 | + |
| 54 | +/** |
| 55 | + * Run the step's tag choice for one version against one registry document. |
| 56 | + * |
| 57 | + * @param {string} version the version about to be published |
| 58 | + * @param {string} registry the text of registry.json, as the step saved it |
| 59 | + * @returns {{ status: number | null, tag: string, stderr: string }} |
| 60 | + */ |
| 61 | +function chooseTag(version, registry) { |
| 62 | + const dir = mkdtempSync(path.join(tmpdir(), "dist-tag-")); |
| 63 | + try { |
| 64 | + writeFileSync(path.join(dir, "registry.json"), registry); |
| 65 | + const res = spawnSync( |
| 66 | + "bash", |
| 67 | + ["-e", "-c", `${tagAssignment()}\nprintf '%s' "$TAG"`], |
| 68 | + { |
| 69 | + cwd: dir, |
| 70 | + encoding: "utf8", |
| 71 | + env: { ...process.env, EXPECTED_VERSION: version }, |
| 72 | + }, |
| 73 | + ); |
| 74 | + return { status: res.status, tag: res.stdout, stderr: res.stderr }; |
| 75 | + } finally { |
| 76 | + rmSync(dir, { recursive: true, force: true }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const latest = (version) => |
| 81 | + JSON.stringify({ "dist-tags": { latest: version } }); |
| 82 | + |
| 83 | +const cases = [ |
| 84 | + { |
| 85 | + name: "the move from the date-stamped line to 1.x takes latest", |
| 86 | + version: "1.0.0", |
| 87 | + registry: latest("2026.8.31"), |
| 88 | + tag: "latest", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "a version higher than the registry's latest takes latest", |
| 92 | + version: "1.1.0", |
| 93 | + registry: latest("1.0.3"), |
| 94 | + tag: "latest", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "a higher major takes latest", |
| 98 | + version: "2.0.0", |
| 99 | + registry: latest("1.9.9"), |
| 100 | + tag: "latest", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "components compare as numbers, not text: 1.10.0 is above 1.9.0", |
| 104 | + version: "1.10.0", |
| 105 | + registry: latest("1.9.0"), |
| 106 | + tag: "latest", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "an older release re-run after a newer one published is a backfill", |
| 110 | + version: "1.0.0", |
| 111 | + registry: latest("1.1.0"), |
| 112 | + tag: "backfill", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "1.2.0 is below 1.10.0, so it is a backfill", |
| 116 | + version: "1.2.0", |
| 117 | + registry: latest("1.10.0"), |
| 118 | + tag: "backfill", |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "the version already at latest does not re-take it", |
| 122 | + version: "1.0.0", |
| 123 | + registry: latest("1.0.0"), |
| 124 | + tag: "backfill", |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "a never-published package (the step saves {} for a 404) takes latest", |
| 128 | + version: "1.0.0", |
| 129 | + registry: "{}", |
| 130 | + tag: "latest", |
| 131 | + }, |
| 132 | +]; |
| 133 | + |
| 134 | +for (const c of cases) |
| 135 | + test(c.name, { skip: !hasBash }, () => { |
| 136 | + const res = chooseTag(c.version, c.registry); |
| 137 | + assert.equal(res.status, 0, res.stderr); |
| 138 | + assert.equal(res.tag, c.tag); |
| 139 | + }); |
| 140 | + |
| 141 | +const refusals = [ |
| 142 | + { |
| 143 | + name: "a prerelease to publish is refused: its tag is a person's choice", |
| 144 | + version: "1.1.0-rc.1", |
| 145 | + registry: latest("1.0.0"), |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "a registry latest that is not plain x.y.z is refused", |
| 149 | + version: "1.0.0", |
| 150 | + registry: latest("1.1.0-rc.1"), |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "a registry document that does not parse is refused", |
| 154 | + version: "1.0.0", |
| 155 | + registry: "<html>bad gateway</html>", |
| 156 | + }, |
| 157 | +]; |
| 158 | + |
| 159 | +for (const c of refusals) |
| 160 | + test(c.name, { skip: !hasBash }, () => { |
| 161 | + const res = chooseTag(c.version, c.registry); |
| 162 | + assert.notEqual(res.status, 0); |
| 163 | + assert.equal(res.tag, ""); |
| 164 | + }); |
| 165 | + |
| 166 | +test("the step passes the chosen tag to npm publish, and nothing else", () => { |
| 167 | + const workflow = readFileSync( |
| 168 | + path.join(repoRoot, ".github", "workflows", "release.yml"), |
| 169 | + "utf8", |
| 170 | + ); |
| 171 | + const publishes = workflow |
| 172 | + .split("\n") |
| 173 | + .filter((l) => /^\s*npm publish\b/.test(l)); |
| 174 | + assert.equal(publishes.length, 1); |
| 175 | + assert.match(publishes[0], /--tag "\$TAG"/); |
| 176 | +}); |
0 commit comments