Repository navigation
Expand file tree
/
Copy pathrelease-workflow.test.ts
More file actions
216 lines (202 loc) · 8.14 KB
/
Copy pathrelease-workflow.test.ts
File metadata and controls
216 lines (202 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { expect, test } from "bun:test";
import { chmod, mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
const workflow = Bun.YAML.parse(await Bun.file(".github/workflows/publish.yml").text()) as {
permissions: Record<string, string>;
concurrency: { group: string; "cancel-in-progress": boolean };
jobs: { publish: { steps: { name: string; run?: string; if?: string; env?: Record<string, string> }[] } };
};
const steps = workflow.jobs.publish.steps;
const releaseStep = steps.find((step) => step.name === "Publish GitHub release")!;
const publishedCommit = "1953ff2f29156e6b4bc45efd678c81b7dc2c31e8";
// Execute the workflow's actual shell script with stub CLIs. No npm publication
// or GitHub request is made by these tests.
async function runRelease(options: Record<string, string> = {}) {
const root = join(tmpdir(), "opencode");
await mkdir(root, { recursive: true });
const directory = await mkdtemp(join(root, "wakelock-release-test-"));
const argsFile = join(directory, "release-args");
const tagArgsFile = join(directory, "tag-args");
const attemptsFile = join(directory, "npm-attempts");
const sleepsFile = join(directory, "sleeps");
const scripts = {
node: `#!/bin/bash
if [[ "$*" == *".name"* ]]; then
printf '%s\\n' opencode-wakelock
else
printf '%s\\n' "$TEST_VERSION"
fi
`,
npm: `#!/bin/bash
if [[ "$*" == *"gitHead"* ]]; then
attempt=0
if [[ -f "$TEST_ATTEMPTS_FILE" ]]; then
read -r attempt < "$TEST_ATTEMPTS_FILE"
fi
attempt=$((attempt + 1))
printf '%s\\n' "$attempt" > "$TEST_ATTEMPTS_FILE"
if [[ "$TEST_NPM_FAILURE" == true || "$attempt" -le "$TEST_NPM_FAILURES" ]]; then
exit 1
fi
printf '%s\\n' "$TEST_COMMIT"
else
printf '%s\\n' "$TEST_LATEST"
fi
`,
sleep: `#!/bin/bash
printf '%s\\n' "$1" >> "$TEST_SLEEPS_FILE"
`,
gh: `#!/bin/bash
if [[ "$1" == api ]]; then
if [[ "$*" == *"/git/ref/tags/"* ]]; then
[[ "$TEST_TAG_EXISTS" == true ]]
elif [[ "$*" == *"/commits/"* ]]; then
printf '%s\\n' "$TEST_TAG_COMMIT"
else
printf '%s\\n' "$@" > "$TEST_TAG_ARGS_FILE"
if [[ "$TEST_TAG_FAILURE" == true ]]; then
exit 1
fi
fi
elif [[ "$2" == view ]]; then
[[ "$TEST_EXISTS" == true ]]
else
printf '%s\\n' "$@" > "$TEST_ARGS_FILE"
if [[ "$TEST_GH_FAILURE" == true ]]; then
exit 1
fi
fi
`,
};
try {
for (const [name, script] of Object.entries(scripts)) {
await Bun.write(join(directory, name), script);
await chmod(join(directory, name), 0o755);
}
const process = Bun.spawn(["bash", "-e", "-o", "pipefail", "-c", releaseStep.run!], {
env: {
PATH: `${directory}:/usr/bin:/bin`,
GITHUB_REPOSITORY: "IgnisDa/opencode-wakelock",
TEST_VERSION: "0.3.0",
TEST_LATEST: "0.3.0",
TEST_COMMIT: publishedCommit,
TEST_NPM_FAILURES: "0",
TEST_ARGS_FILE: argsFile,
TEST_TAG_ARGS_FILE: tagArgsFile,
TEST_TAG_COMMIT: publishedCommit,
TEST_ATTEMPTS_FILE: attemptsFile,
TEST_SLEEPS_FILE: sleepsFile,
...options,
},
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([
new Response(process.stdout).text(),
new Response(process.stderr).text(),
process.exited,
]);
const args = await Bun.file(argsFile).exists()
? (await Bun.file(argsFile).text()).trimEnd().split("\n")
: [];
const attempts = await Bun.file(attemptsFile).exists()
? Number(await Bun.file(attemptsFile).text())
: 0;
const sleeps = await Bun.file(sleepsFile).exists()
? (await Bun.file(sleepsFile).text()).trimEnd().split("\n")
: [];
const tagArgs = await Bun.file(tagArgsFile).exists()
? (await Bun.file(tagArgsFile).text()).trimEnd().split("\n")
: [];
return { args, tagArgs, stdout, stderr, exitCode, attempts, sleeps };
} finally {
await rm(directory, { recursive: true, force: true });
}
}
test("GitHub release follows npm publication and can recover on rerun", () => {
const publishIndex = steps.findIndex((step) => step.name === "Publish to npm");
expect(steps.indexOf(releaseStep)).toBeGreaterThan(publishIndex);
expect(releaseStep.if).toBeUndefined();
expect(releaseStep.env?.GH_TOKEN).toBe("${{ github.token }}");
expect(workflow.permissions["contents"]).toBe("write");
expect(workflow.permissions["id-token"]).toBe("write");
expect(workflow.concurrency["cancel-in-progress"]).toBe(false);
});
test("creates the latest release at npm's published commit with generated notes", async () => {
const result = await runRelease();
expect(result.exitCode).toBe(0);
expect(result.args.slice(0, 3)).toEqual(["release", "create", "v0.3.0"]);
expect(result.tagArgs).toContain(`sha=${publishedCommit}`);
expect(result.tagArgs).toContain("ref=refs/tags/v0.3.0");
expect(result.args).toContain("--verify-tag");
expect(result.args).not.toContain("--target");
expect(result.args[result.args.indexOf("--repo") + 1]).toBe("IgnisDa/opencode-wakelock");
expect(result.args).toContain("--generate-notes");
expect(result.args).toContain("--latest");
expect(result.args).toContain("Published to npm: https://www.npmjs.com/package/opencode-wakelock/v/0.3.0");
});
test("an existing GitHub release is skipped", async () => {
const result = await runRelease({ TEST_EXISTS: "true" });
expect(result.exitCode).toBe(0);
expect(result.args).toEqual([]);
expect(result.stdout).toContain("already exists");
});
test("recovering an older version does not replace the latest release", async () => {
const result = await runRelease({ TEST_LATEST: "0.4.0" });
expect(result.exitCode).toBe(0);
expect(result.args).toContain("--latest=false");
expect(result.args).not.toContain("--latest");
});
test("prereleases are marked and never replace the latest stable release", async () => {
const result = await runRelease({ TEST_VERSION: "0.4.0-beta.1", TEST_LATEST: "0.4.0-beta.1" });
expect(result.exitCode).toBe(0);
expect(result.args).toContain("v0.4.0-beta.1");
expect(result.args).toContain("--prerelease");
expect(result.args).toContain("--latest=false");
});
test("missing published commit fails instead of tagging unpublished code", async () => {
const result = await runRelease({ TEST_COMMIT: "" });
expect(result.exitCode).not.toBe(0);
expect(result.args).toEqual([]);
expect(result.stderr).toContain("Cannot determine the published commit");
});
test("npm lookup failure prevents release creation", async () => {
const result = await runRelease({ TEST_NPM_FAILURE: "true" });
expect(result.exitCode).not.toBe(0);
expect(result.args).toEqual([]);
expect(result.attempts).toBe(30);
expect(result.sleeps).toHaveLength(29);
});
test("waits for a newly published npm version to become visible", async () => {
const result = await runRelease({ TEST_NPM_FAILURES: "2" });
expect(result.exitCode).toBe(0);
expect(result.attempts).toBe(3);
expect(result.sleeps).toEqual(["10", "10"]);
expect(result.args).toContain("create");
expect(result.tagArgs).toContain(`sha=${publishedCommit}`);
});
test("uses an existing tag when it matches the published npm commit", async () => {
const result = await runRelease({ TEST_TAG_EXISTS: "true" });
expect(result.exitCode).toBe(0);
expect(result.tagArgs).toEqual([]);
expect(result.args).toContain("--verify-tag");
});
test("refuses an existing tag at the wrong commit without overwriting it", async () => {
const result = await runRelease({ TEST_TAG_EXISTS: "true", TEST_TAG_COMMIT: "a".repeat(40) });
expect(result.exitCode).not.toBe(0);
expect(result.tagArgs).toEqual([]);
expect(result.args).toEqual([]);
expect(result.stderr).toContain("does not match the published npm commit");
});
test("tag creation failure prevents release publication", async () => {
const result = await runRelease({ TEST_TAG_FAILURE: "true" });
expect(result.exitCode).not.toBe(0);
expect(result.tagArgs).toContain(`sha=${publishedCommit}`);
expect(result.args).toEqual([]);
});
test("GitHub publication failure fails the workflow", async () => {
const result = await runRelease({ TEST_GH_FAILURE: "true" });
expect(result.exitCode).not.toBe(0);
expect(result.args).toContain("create");
});