|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Composition tests for vale-detect.cjs. |
| 6 | + * |
| 7 | + * vale-release.test.cjs covers the pure functions individually. This file |
| 8 | + * covers the one thing that cannot: how main() sequences them, with both |
| 9 | + * network calls stubbed. That gap is not hypothetical — the detect job once |
| 10 | + * routed its cheap "is upstream ahead?" check through planManifestUpdate with |
| 11 | + * an empty checksums payload, which throws on precisely the ahead path, so the |
| 12 | + * job failed on every run that had a release to propose while the no-op path |
| 13 | + * kept passing. Every function involved was green in isolation. |
| 14 | + * |
| 15 | + * Nothing here writes: main() is called without `--write`, so the committed |
| 16 | + * manifest is only read. |
| 17 | + */ |
| 18 | + |
| 19 | +const test = require("node:test"); |
| 20 | +const assert = require("node:assert/strict"); |
| 21 | +const { mkdtempSync, readFileSync, rmSync } = require("node:fs"); |
| 22 | +const { tmpdir } = require("node:os"); |
| 23 | +const { join } = require("node:path"); |
| 24 | + |
| 25 | +const { main } = require("./vale-detect.cjs"); |
| 26 | +const { applyTemplate } = require("./vale-release.cjs"); |
| 27 | + |
| 28 | +const MANIFEST = JSON.parse( |
| 29 | + readFileSync(join(__dirname, "vale-manifest.json"), "utf8") |
| 30 | +); |
| 31 | + |
| 32 | +/** A digest that is syntactically valid and obviously synthetic. */ |
| 33 | +const digestFor = (index) => |
| 34 | + String(index + 1) |
| 35 | + .repeat(64) |
| 36 | + .slice(0, 64); |
| 37 | + |
| 38 | +/** Upstream's sha256sum-format checksums file for a given Vale version. */ |
| 39 | +function checksumsFor(version) { |
| 40 | + return `${MANIFEST.platforms |
| 41 | + .map( |
| 42 | + (platform, index) => |
| 43 | + `${digestFor(index)} ${applyTemplate(platform.asset, { version })}` |
| 44 | + ) |
| 45 | + .join("\n")}\n`; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Run main() with both fetches stubbed and $GITHUB_OUTPUT pointed at a temp |
| 50 | + * file, then return the parsed step outputs plus which URLs were fetched. |
| 51 | + */ |
| 52 | +async function runDetect({ upstreamTag, checksums }) { |
| 53 | + const directory = mkdtempSync(join(tmpdir(), "vale-detect-test-")); |
| 54 | + const outputPath = join(directory, "github-output"); |
| 55 | + const previous = process.env.GITHUB_OUTPUT; |
| 56 | + const fetched = []; |
| 57 | + process.env.GITHUB_OUTPUT = outputPath; |
| 58 | + try { |
| 59 | + await main({ |
| 60 | + argv: [], |
| 61 | + latestTag: async () => upstreamTag, |
| 62 | + text: async (url) => { |
| 63 | + fetched.push(url); |
| 64 | + return checksums; |
| 65 | + }, |
| 66 | + }); |
| 67 | + const outputs = Object.fromEntries( |
| 68 | + readFileSync(outputPath, "utf8") |
| 69 | + .split("\n") |
| 70 | + .filter(Boolean) |
| 71 | + .map((line) => { |
| 72 | + const at = line.indexOf("="); |
| 73 | + return [line.slice(0, at), line.slice(at + 1)]; |
| 74 | + }) |
| 75 | + ); |
| 76 | + return { outputs, fetched }; |
| 77 | + } finally { |
| 78 | + if (previous === undefined) { |
| 79 | + delete process.env.GITHUB_OUTPUT; |
| 80 | + } else { |
| 81 | + process.env.GITHUB_OUTPUT = previous; |
| 82 | + } |
| 83 | + rmSync(directory, { recursive: true, force: true }); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +test("detect: an upstream release ahead of the pin plans an update", async () => { |
| 88 | + // The regression case. Before the fix this rejected with "upstream checksums |
| 89 | + // file for 3.99.0 parsed to no entries" — the detect job's failure mode on |
| 90 | + // every real upstream bump. |
| 91 | + const { outputs, fetched } = await runDetect({ |
| 92 | + upstreamTag: "v3.99.0", |
| 93 | + checksums: checksumsFor("3.99.0"), |
| 94 | + }); |
| 95 | + |
| 96 | + assert.equal(outputs.update, "true"); |
| 97 | + assert.equal(outputs.vale_version, "3.99.0"); |
| 98 | + assert.equal(outputs.pinned_version, MANIFEST.valeVersion); |
| 99 | + assert.deepEqual(fetched, [ |
| 100 | + `https://github.com/${MANIFEST.upstream.repository}/releases/download/v3.99.0/vale_3.99.0_checksums.txt`, |
| 101 | + ]); |
| 102 | +}); |
| 103 | + |
| 104 | +test("detect: the pinned version being current is a no-op", async () => { |
| 105 | + const { outputs, fetched } = await runDetect({ |
| 106 | + upstreamTag: `v${MANIFEST.valeVersion}`, |
| 107 | + checksums: "", |
| 108 | + }); |
| 109 | + |
| 110 | + assert.equal(outputs.update, "false"); |
| 111 | + assert.equal(outputs.vale_version, MANIFEST.valeVersion); |
| 112 | + assert.equal(outputs.pinned_version, MANIFEST.valeVersion); |
| 113 | + // The whole reason the check is cheap: no checksums file is downloaded for a |
| 114 | + // release we are not going to propose. |
| 115 | + assert.deepEqual(fetched, []); |
| 116 | +}); |
| 117 | + |
| 118 | +test("detect: an upstream tag behind the pin is also a no-op", async () => { |
| 119 | + const { outputs, fetched } = await runDetect({ |
| 120 | + upstreamTag: "v0.1.0", |
| 121 | + checksums: "", |
| 122 | + }); |
| 123 | + |
| 124 | + assert.equal(outputs.update, "false"); |
| 125 | + assert.deepEqual(fetched, []); |
| 126 | +}); |
| 127 | + |
| 128 | +test("detect: a checksums file missing a platform aborts", async () => { |
| 129 | + await assert.rejects( |
| 130 | + runDetect({ |
| 131 | + upstreamTag: "v3.99.0", |
| 132 | + checksums: checksumsFor("3.99.0").split("\n").slice(1).join("\n"), |
| 133 | + }), |
| 134 | + /publishes no asset named/ |
| 135 | + ); |
| 136 | +}); |
0 commit comments