|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import test from "node:test"; |
| 7 | + |
| 8 | +const workflow = readFileSync(new URL("../../../.github/workflows/release.yml", import.meta.url), "utf8"); |
| 9 | +const publishStep = workflow.match(/^ {6}- name: Publish GitHub Release\n(?: {8}[^\n]*\n)*? {8}run: \|\n((?:(?: {10}[^\n]*)?\n)+)/m); |
| 10 | +assert.ok(publishStep, "Could not find the release workflow's publish step"); |
| 11 | +const publishScript = publishStep[1].replace(/^ {10}/gm, ""); |
| 12 | +const recordReleaseArgs = 'gh() { printf "%s\\n" "$@" > "$RELEASE_ARGS"; }\n'; |
| 13 | + |
| 14 | +const gitEnv = { |
| 15 | + PATH: process.env.PATH, |
| 16 | + GIT_CONFIG_GLOBAL: "/dev/null", |
| 17 | + GIT_CONFIG_NOSYSTEM: "1", |
| 18 | + GIT_TERMINAL_PROMPT: "0", |
| 19 | + GIT_AUTHOR_NAME: "Release Test", |
| 20 | + GIT_AUTHOR_EMAIL: "release@example.invalid", |
| 21 | + GIT_COMMITTER_NAME: "Release Test", |
| 22 | + GIT_COMMITTER_EMAIL: "release@example.invalid", |
| 23 | +}; |
| 24 | + |
| 25 | +function releaseFixture(t, tag = "v1.0.0", tagOptions = []) { |
| 26 | + const directory = mkdtempSync(join(tmpdir(), "python-release-")); |
| 27 | + t.after(() => rmSync(directory, { recursive: true, force: true })); |
| 28 | + const checkout = join(directory, "checkout"); |
| 29 | + const remote = join(directory, "remote.git"); |
| 30 | + const ref = `refs/tags/${tag}`; |
| 31 | + const cliLog = join(directory, "release-args"); |
| 32 | + |
| 33 | + function git(cwd, ...args) { |
| 34 | + return execFileSync("git", args, { cwd, env: gitEnv, encoding: "utf8", timeout: 5000 }).trim(); |
| 35 | + } |
| 36 | + |
| 37 | + git(directory, "init", "-q", checkout); |
| 38 | + git(directory, "init", "-q", "--bare", remote); |
| 39 | + git(checkout, "remote", "add", "origin", remote); |
| 40 | + git(checkout, "commit", "-qm", "packaged commit", "--allow-empty"); |
| 41 | + const packagedCommit = git(checkout, "rev-parse", "HEAD"); |
| 42 | + git(checkout, "tag", ...tagOptions, tag); |
| 43 | + git(checkout, "push", "-q", "origin", ref); |
| 44 | + |
| 45 | + mkdirSync(join(checkout, "docs", "releases"), { recursive: true }); |
| 46 | + mkdirSync(join(checkout, "dist")); |
| 47 | + writeFileSync(join(checkout, "docs", "releases", `${tag}.md`), "Release notes\n"); |
| 48 | + writeFileSync(join(checkout, "dist", "python-support.zip"), "Test archive contents\n"); |
| 49 | + |
| 50 | + return { |
| 51 | + cliLog, |
| 52 | + retag() { |
| 53 | + git(checkout, "commit", "-qm", "replacement commit", "--allow-empty"); |
| 54 | + git(checkout, "push", "-q", "origin", `+HEAD:${ref}`); |
| 55 | + git(checkout, "checkout", "-q", "--detach", packagedCommit); |
| 56 | + }, |
| 57 | + removeTag() { |
| 58 | + git(checkout, "push", "-q", "origin", `:${ref}`); |
| 59 | + }, |
| 60 | + publish() { |
| 61 | + const result = spawnSync("bash", ["--noprofile", "--norc", "-e", "-c", recordReleaseArgs + publishScript], { |
| 62 | + cwd: checkout, |
| 63 | + encoding: "utf8", |
| 64 | + timeout: 5000, |
| 65 | + env: { |
| 66 | + ...gitEnv, |
| 67 | + GITHUB_REF: ref, |
| 68 | + GITHUB_REF_NAME: tag, |
| 69 | + RUNNER_TEMP: directory, |
| 70 | + PRERELEASE_TAGS: "v0.1.0 v0.1.1", |
| 71 | + RELEASE_ARGS: cliLog, |
| 72 | + }, |
| 73 | + }); |
| 74 | + assert.ifError(result.error); |
| 75 | + return result; |
| 76 | + }, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +test("publish uses the unchanged lightweight release tag", (t) => { |
| 81 | + const fixture = releaseFixture(t); |
| 82 | + const result = fixture.publish(); |
| 83 | + assert.equal(result.status, 0, result.stdout + result.stderr); |
| 84 | + const args = readFileSync(fixture.cliLog, "utf8").trimEnd().split("\n"); |
| 85 | + assert.deepEqual(args.slice(0, 3), ["release", "create", "v1.0.0"]); |
| 86 | + assert.ok(args.includes("--verify-tag")); |
| 87 | + assert.ok(!args.includes("--prerelease")); |
| 88 | +}); |
| 89 | + |
| 90 | +test("publish resolves an annotated prerelease tag to its commit", (t) => { |
| 91 | + const fixture = releaseFixture(t, "v0.1.1", ["-a", "-m", "Prerelease"]); |
| 92 | + const result = fixture.publish(); |
| 93 | + assert.equal(result.status, 0, result.stdout + result.stderr); |
| 94 | + assert.match(readFileSync(fixture.cliLog, "utf8"), /^--prerelease$/m); |
| 95 | +}); |
| 96 | + |
| 97 | +test("publish stops when the remote tag is moved after packaging", (t) => { |
| 98 | + const fixture = releaseFixture(t); |
| 99 | + fixture.retag(); |
| 100 | + const result = fixture.publish(); |
| 101 | + assert.equal(result.status, 1, result.stdout + result.stderr); |
| 102 | + assert.match(result.stdout, /no longer points to the packaged commit/); |
| 103 | + assert.ok(!existsSync(fixture.cliLog), "Should stop before invoking gh release create"); |
| 104 | +}); |
| 105 | + |
| 106 | +test("publish stops when the remote tag is deleted after packaging", (t) => { |
| 107 | + const fixture = releaseFixture(t); |
| 108 | + fixture.removeTag(); |
| 109 | + const result = fixture.publish(); |
| 110 | + assert.equal(result.status, 1, result.stdout + result.stderr); |
| 111 | + assert.match(result.stdout, /Could not resolve release tag/); |
| 112 | + assert.ok(!existsSync(fixture.cliLog), "Should stop before invoking gh release create"); |
| 113 | +}); |
0 commit comments