|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { spawn } from "node:child_process"; |
| 3 | +import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { describe, it } from "node:test"; |
| 7 | + |
| 8 | +const ROOT_URL = new URL("../../", import.meta.url); |
| 9 | +const SCRIPT_URL = new URL(".github/scripts/publish-crate-if-needed.sh", ROOT_URL); |
| 10 | +const WORKFLOW_URL = new URL(".github/workflows/release.yml", ROOT_URL); |
| 11 | + |
| 12 | +const writeExecutable = async (path, contents) => { |
| 13 | + await writeFile(path, contents); |
| 14 | + await chmod(path, 0o755); |
| 15 | +}; |
| 16 | + |
| 17 | +const runPublishHelper = async ({ lookup = "existing", publishExit = 0 } = {}) => { |
| 18 | + const directory = await mkdtemp(join(tmpdir(), "srcmap-publish-test-")); |
| 19 | + const logPath = join(directory, "commands.log"); |
| 20 | + |
| 21 | + await writeFile(logPath, ""); |
| 22 | + |
| 23 | + await Promise.all([ |
| 24 | + writeExecutable( |
| 25 | + join(directory, "cargo"), |
| 26 | + `#!/usr/bin/env bash |
| 27 | +set -eu |
| 28 | +printf 'cargo %s\\n' "$*" >> "$COMMAND_LOG" |
| 29 | +if [[ "$1" == "metadata" ]]; then |
| 30 | + printf '{"packages":[{"name":"srcmap-codec","version":"1.2.3"}]}' |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | +exit "${publishExit}" |
| 34 | +`, |
| 35 | + ), |
| 36 | + writeExecutable( |
| 37 | + join(directory, "curl"), |
| 38 | + `#!/usr/bin/env bash |
| 39 | +set -eu |
| 40 | +printf 'curl %s\\n' "$*" >> "$COMMAND_LOG" |
| 41 | +if [[ "${lookup}" == "failure" ]]; then |
| 42 | + exit 7 |
| 43 | +fi |
| 44 | +output='' |
| 45 | +while [[ $# -gt 0 ]]; do |
| 46 | + if [[ "$1" == "--output" || "$1" == "-o" ]]; then |
| 47 | + output="$2" |
| 48 | + shift 2 |
| 49 | + continue |
| 50 | + fi |
| 51 | + shift |
| 52 | +done |
| 53 | +if [[ "${lookup}" == "existing" ]]; then |
| 54 | + printf '{"version":{"num":"1.2.3"}}' > "$output" |
| 55 | + printf '200' |
| 56 | +else |
| 57 | + printf '{"errors":[{"detail":"Not Found"}]}' > "$output" |
| 58 | + printf '404' |
| 59 | +fi |
| 60 | +`, |
| 61 | + ), |
| 62 | + writeExecutable( |
| 63 | + join(directory, "jq"), |
| 64 | + `#!/usr/bin/env bash |
| 65 | +set -eu |
| 66 | +printf 'jq %s\\n' "$*" >> "$COMMAND_LOG" |
| 67 | +if [[ "$*" == *'.packages[]'* ]]; then |
| 68 | + printf '1.2.3\\n' |
| 69 | + exit 0 |
| 70 | +fi |
| 71 | +if [[ "$*" == *'.version.num'* ]]; then |
| 72 | + printf '1.2.3\\n' |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | +exit 1 |
| 76 | +`, |
| 77 | + ), |
| 78 | + ]); |
| 79 | + |
| 80 | + const result = await new Promise((resolve, reject) => { |
| 81 | + const child = spawn("bash", [SCRIPT_URL.pathname, "srcmap-codec"], { |
| 82 | + cwd: new URL("../..", import.meta.url).pathname, |
| 83 | + env: { |
| 84 | + ...process.env, |
| 85 | + COMMAND_LOG: logPath, |
| 86 | + PATH: `${directory}:${process.env.PATH}`, |
| 87 | + }, |
| 88 | + }); |
| 89 | + let stderr = ""; |
| 90 | + let stdout = ""; |
| 91 | + |
| 92 | + child.stderr.setEncoding("utf8"); |
| 93 | + child.stderr.on("data", (chunk) => { |
| 94 | + stderr += chunk; |
| 95 | + }); |
| 96 | + child.stdout.setEncoding("utf8"); |
| 97 | + child.stdout.on("data", (chunk) => { |
| 98 | + stdout += chunk; |
| 99 | + }); |
| 100 | + child.on("error", reject); |
| 101 | + child.on("close", (exitCode) => resolve({ exitCode, stderr, stdout })); |
| 102 | + }); |
| 103 | + |
| 104 | + const commands = await readFile(logPath, "utf8"); |
| 105 | + await rm(directory, { force: true, recursive: true }); |
| 106 | + return { ...result, commands }; |
| 107 | +}; |
| 108 | + |
| 109 | +describe("publish-crate-if-needed", () => { |
| 110 | + it("skips publishing when crates.io confirms the workspace version exists", async () => { |
| 111 | + const result = await runPublishHelper(); |
| 112 | + |
| 113 | + assert.equal(result.exitCode, 0); |
| 114 | + assert.match(result.stdout, /already published/i); |
| 115 | + assert.doesNotMatch(result.commands, /cargo publish/); |
| 116 | + }); |
| 117 | + |
| 118 | + it("publishes when crates.io reports that the workspace version is missing", async () => { |
| 119 | + const result = await runPublishHelper({ lookup: "missing" }); |
| 120 | + |
| 121 | + assert.equal(result.exitCode, 0); |
| 122 | + assert.match(result.commands, /cargo publish -p srcmap-codec/); |
| 123 | + }); |
| 124 | + |
| 125 | + it("stops when the crates.io lookup fails", async () => { |
| 126 | + const result = await runPublishHelper({ lookup: "failure" }); |
| 127 | + |
| 128 | + assert.notEqual(result.exitCode, 0); |
| 129 | + assert.doesNotMatch(result.commands, /cargo publish/); |
| 130 | + }); |
| 131 | + |
| 132 | + it("propagates cargo publish failures", async () => { |
| 133 | + const result = await runPublishHelper({ lookup: "missing", publishExit: 42 }); |
| 134 | + |
| 135 | + assert.equal(result.exitCode, 42); |
| 136 | + }); |
| 137 | + |
| 138 | + it("does not mask cargo publish failures in the release workflow", async () => { |
| 139 | + const workflow = await readFile(WORKFLOW_URL, "utf8"); |
| 140 | + |
| 141 | + assert.doesNotMatch(workflow, /cargo publish[^\n]*\|\|/); |
| 142 | + }); |
| 143 | +}); |
0 commit comments