-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrelease.test.mts
More file actions
287 lines (232 loc) Β· 8.38 KB
/
Copy pathrelease.test.mts
File metadata and controls
287 lines (232 loc) Β· 8.38 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import assert from "node:assert/strict";
import { execFileSync, spawnSync } from "node:child_process";
import {
appendFileSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
import * as yaml from "js-yaml";
type ActionStep = {
uses?: string;
};
type ActionMetadata = {
runs?: {
steps?: ActionStep[];
};
};
const REPOSITORY_ROOT = fileURLToPath(new URL(".", import.meta.url));
const PREPARE_RELEASE = fileURLToPath(
new URL("./.github/scripts/prepare-release.sh", import.meta.url),
);
function git(repository: string, ...arguments_: string[]): string {
return execFileSync("git", ["-C", repository, ...arguments_], {
encoding: "utf8",
}).trim();
}
function repository(): string {
const path = mkdtempSync(join(tmpdir(), "homebrew-actions-release-"));
git(path, "init", "--initial-branch=main");
git(path, "config", "user.email", "test@example.com");
git(path, "config", "user.name", "Release Test");
git(path, "config", "commit.gpgsign", "false");
git(path, "config", "tag.gpgsign", "false");
return path;
}
function commit(repository: string, message: string, date: string): string {
appendFileSync(join(repository, "state.txt"), `${message}\n`);
git(repository, "add", "state.txt");
execFileSync("git", ["-C", repository, "commit", "-m", message], {
env: {
...process.env,
GIT_AUTHOR_DATE: date,
GIT_COMMITTER_DATE: date,
},
});
return git(repository, "rev-parse", "HEAD");
}
function tag(repository: string, name: string): void {
git(repository, "tag", "--annotate", name, "--message", name);
}
function prepare(
repository: string,
eventName: "schedule" | "workflow_dispatch",
now: string,
releaseDate = "2026.07.06",
): Record<string, string> {
const output = join(repository, "github-output");
writeFileSync(output, "");
const result = spawnSync("bash", [PREPARE_RELEASE], {
cwd: repository,
encoding: "utf8",
env: {
...process.env,
GITHUB_EVENT_NAME: eventName,
GITHUB_OUTPUT: output,
RELEASE_DATE: releaseDate,
RELEASE_MINIMUM_AGE_SECONDS: "86400",
RELEASE_NOW: now,
},
});
assert.equal(result.status, 0, result.stderr);
return Object.fromEntries(
readFileSync(output, "utf8")
.trim()
.split("\n")
.filter(Boolean)
.map((line) => {
const separator = line.indexOf("=");
return [line.slice(0, separator), line.slice(separator + 1)];
}),
);
}
test("prepares the first scheduled release from an old commit", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
const candidate = commit(path, "initial", "2026-07-05T12:00:00Z");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "true");
assert.equal(output.version, "2026.07.06.1");
assert.equal(output.candidate_sha, candidate);
});
test("increments the sequence for the UTC release date", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "released", "2026-07-04T12:00:00Z");
tag(path, "2026.07.06.1");
tag(path, "v2026.07.06.9");
commit(path, "next", "2026-07-05T12:00:00Z");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "true");
assert.equal(output.version, "2026.07.06.2");
assert.equal(output.latest_tag, "2026.07.06.1");
});
test("increments past single digits with lightweight tags", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "released", "2026-07-04T12:00:00Z");
git(path, "tag", "2026.07.06.9");
commit(path, "next", "2026-07-05T12:00:00Z");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "true");
assert.equal(output.version, "2026.07.06.10");
assert.equal(output.latest_tag, "2026.07.06.9");
});
test("avoids a sequence held by an unreachable same-date tag", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "base", "2026-07-01T12:00:00Z");
git(path, "checkout", "-b", "side");
commit(path, "side", "2026-07-02T12:00:00Z");
git(path, "tag", "2026.07.06.1");
git(path, "checkout", "main");
commit(path, "main", "2026-07-03T12:00:00Z");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "true");
assert.equal(output.latest_tag, "");
assert.equal(output.version, "2026.07.06.2");
});
test("skips when the latest release already contains the candidate", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "released", "2026-07-04T12:00:00Z");
tag(path, "2026.07.05.1");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "false");
assert.equal(output.reason, "No commits since 2026.07.05.1.");
});
test("manual releases also skip when there are no new commits", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "released", "2026-07-04T12:00:00Z");
tag(path, "2026.07.05.1");
const output = prepare(path, "workflow_dispatch", "1783339200");
assert.equal(output.release, "false");
assert.equal(output.reason, "No commits since 2026.07.05.1.");
});
test("scheduled releases require 24 hours of bake time", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "recent", "2026-07-06T11:00:00Z");
const output = prepare(path, "schedule", "1783339200");
assert.equal(output.release, "false");
assert.equal(
output.reason,
"Candidate commit is 3600 seconds old; scheduled releases require 86400 seconds.",
);
});
test("manual releases bypass the bake-time requirement", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
commit(path, "urgent fix", "2026-07-06T11:00:00Z");
const output = prepare(path, "workflow_dispatch", "1783339200");
assert.equal(output.release, "true");
assert.equal(output.version, "2026.07.06.1");
});
test("clamps a future-dated candidate age to zero", (t) => {
const path = repository();
t.after(() => rmSync(path, { force: true, recursive: true }));
// Committer date one hour ahead of RELEASE_NOW (clock skew).
commit(path, "future", "2026-07-06T13:00:00Z");
const output = prepare(path, "workflow_dispatch", "1783339200");
assert.equal(output.candidate_age_seconds, "0");
assert.equal(output.release, "true");
});
test("internal Homebrew/actions dependencies use full commit SHAs", () => {
const actionFiles = git(REPOSITORY_ROOT, "ls-files")
.split("\n")
.filter((file) => file.endsWith("/action.yml"));
const invalidReferences: string[] = [];
for (const actionFile of actionFiles) {
const action = yaml.load(
readFileSync(join(REPOSITORY_ROOT, actionFile), "utf8"),
) as ActionMetadata;
for (const step of action.runs?.steps ?? []) {
if (
step.uses?.startsWith("Homebrew/actions/") &&
!/^Homebrew\/actions\/[^@]+@[0-9a-f]{40}$/.test(step.uses)
) {
invalidReferences.push(`${actionFile}: ${step.uses}`);
}
}
}
assert.deepEqual(invalidReferences, []);
});
test("internal Homebrew/actions pins reference the current sub-action tree", () => {
const actionFiles = git(REPOSITORY_ROOT, "ls-files")
.split("\n")
.filter((file) => file.endsWith("/action.yml"));
const staleReferences: string[] = [];
for (const actionFile of actionFiles) {
const action = yaml.load(
readFileSync(join(REPOSITORY_ROOT, actionFile), "utf8"),
) as ActionMetadata;
for (const step of action.runs?.steps ?? []) {
const match = step.uses?.match(
/^Homebrew\/actions\/([^@]+)@([0-9a-f]{40})$/,
);
if (!match) continue;
const [, subAction, sha] = match;
const changedFiles = git(
REPOSITORY_ROOT,
"diff",
"--name-only",
`${sha}..HEAD`,
"--",
`${subAction}/`,
`:(exclude)${subAction}/README.md`,
);
if (changedFiles) {
staleReferences.push(
`${actionFile}: ${step.uses} is behind ${subAction}/`,
);
}
}
}
assert.deepEqual(staleReferences, []);
});