返回 CodeWhale
assemble-release-assets.test.js
根目录 / scripts / release / assemble-release-assets.test.js
1 #!/usr/bin/env node
2
3 const assert = require("node:assert/strict");
4 const { execFileSync, spawnSync } = require("node:child_process");
5 const crypto = require("node:crypto");
6 const fs = require("node:fs");
7 const os = require("node:os");
8 const path = require("node:path");
9 const test = require("node:test");
10
11 const {
12 allReleaseAssetNames,
13 BUNDLE_ASSET_NAMES,
14 BUNDLE_CHECKSUM_MANIFEST,
15 CHECKSUM_MANIFEST,
16 checksummedReleaseAssetNames,
17 } = require("../../npm/codewhale/scripts/artifacts");
18 const {
19 assemble,
20 parseChecksumManifest,
21 verifyAssetDirectory,
22 windowsLauncherContents,
23 } = require("./assemble-release-assets");
24
25 const repoRoot = path.resolve(__dirname, "..", "..");
26
27 function toCrlf(text) {
28 return text.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n");
29 }
30
31 function sha256(filePath) {
32 return crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
33 }
34
35 function createBundleInputs(input) {
36 fs.mkdirSync(input, { recursive: true });
37 for (const name of allReleaseAssetNames().filter((asset) =>
38 /^(codewhale|codew)-(linux|android|ohos|macos|windows)-/.test(asset) &&
39 !asset.endsWith(".tar.gz") &&
40 !asset.endsWith(".zip"),
41 )) {
42 const artifactDirectory = path.join(input, name);
43 fs.mkdirSync(artifactDirectory, { recursive: true });
44 // GitHub's artifact transport normalizes regular files to 0644. The
45 // bundler must restore executable modes for non-Windows archives.
46 fs.writeFileSync(path.join(artifactDirectory, name), `fixture:${name}\n`, { mode: 0o644 });
47 }
48 }
49
50 function runBundle(input, output, sourceDateEpoch) {
51 const env = { ...process.env };
52 if (sourceDateEpoch === undefined) {
53 delete env.SOURCE_DATE_EPOCH;
54 } else {
55 env.SOURCE_DATE_EPOCH = sourceDateEpoch;
56 }
57 return spawnSync(
58 "bash",
59 [path.join(repoRoot, "scripts/release/create-release-bundles.sh"), input, output],
60 { cwd: repoRoot, encoding: "utf8", env },
61 );
62 }
63
64 function makeIntermediateArtifacts(root) {
65 const generated = new Set(["codewhale.bat", CHECKSUM_MANIFEST]);
66 const copied = allReleaseAssetNames().filter((name) => !generated.has(name));
67 for (const name of copied) {
68 if (name === BUNDLE_CHECKSUM_MANIFEST) {
69 continue;
70 }
71 const artifactDirectory = BUNDLE_ASSET_NAMES.includes(name)
72 ? path.join(root, "codewhale-bundles")
73 : path.join(root, name);
74 fs.mkdirSync(artifactDirectory, { recursive: true });
75 fs.writeFileSync(path.join(artifactDirectory, name), `fixture:${name}\n`);
76 }
77
78 const bundleManifestDirectory = path.join(root, "codewhale-bundles");
79 fs.mkdirSync(bundleManifestDirectory, { recursive: true });
80 const rows = BUNDLE_ASSET_NAMES.map((name) => {
81 const matches = fs
82 .readdirSync(root, { recursive: true })
83 .map((entry) => path.join(root, entry))
84 .filter((entry) => path.basename(entry) === name && fs.statSync(entry).isFile());
85 assert.equal(matches.length, 1, `fixture should contain one ${name}`);
86 return `${sha256(matches[0])} ${name}`;
87 }).sort();
88 fs.writeFileSync(
89 path.join(bundleManifestDirectory, BUNDLE_CHECKSUM_MANIFEST),
90 `${rows.join("\n")}\n`,
91 );
92 }
93
94 test("authoritative release inventory contains eight targets and 38 bridge assets", () => {
95 const assets = allReleaseAssetNames();
96 assert.equal(assets.length, 38);
97 assert.equal(checksummedReleaseAssetNames().length, 37);
98 for (const required of [
99 "codewhale-android-arm64",
100 "codew-android-arm64",
101 "codewhale-ohos-arm64",
102 "codew-ohos-arm64",
103 "codewhale-ohos-arm64.tar.gz",
104 "codewhale-tui-ohos-arm64",
105 "codewhale-windows-arm64.exe",
106 "codew-windows-arm64.exe",
107 "codewhale-windows-arm64.zip",
108 "codewhale-tui-android-arm64",
109 "codewhale-tui-windows-arm64.exe",
110 "CodeWhaleSetup.exe",
111 ]) {
112 assert.ok(assets.includes(required), `missing ${required}`);
113 }
114 });
115
116 test("NSIS installer ships the Windows Terminal launcher and Start Menu shortcut", () => {
117 const nsi = fs.readFileSync(path.join(repoRoot, "scripts/installer/codewhale.nsi"), "utf8");
118 const bat = fs.readFileSync(path.join(repoRoot, "scripts/installer/codewhale.bat"), "utf8");
119
120 assert.match(nsi, /^\s*File "codewhale\.bat"\s*$/m);
121 assert.match(
122 nsi,
123 /CreateShortCut "\$SMPROGRAMS\\\$\{PRODUCT_NAME\}\\\$\{PRODUCT_NAME\}\.lnk" "\$INSTDIR\\bin\\codewhale\.bat"/,
124 );
125 assert.match(
126 nsi,
127 /^\s*!insertmacro RemoveOwnedFile "\$INSTDIR\\bin\\codewhale\.bat"\s*$/m,
128 );
129 assert.match(
130 nsi,
131 /^\s*!insertmacro RemoveOwnedFile "\$SMPROGRAMS\\\$\{PRODUCT_NAME\}\\\$\{PRODUCT_NAME\}\.lnk"\s*$/m,
132 );
133 assert.match(
134 nsi,
135 /^\s*!insertmacro RemoveOwnedDir "\$SMPROGRAMS\\\$\{PRODUCT_NAME\}"\s*$/m,
136 );
137
138 const batNormalized = bat.replace(/\r\n/g, "\n");
139 assert.match(batNormalized, /where wt >nul 2>nul/);
140 assert.match(batNormalized, /wt --title Codewhale cmd \/k "%~dp0codewhale\.exe"/);
141 assert.match(batNormalized, /"%~dp0codewhale\.exe"/);
142 assert.doesNotMatch(batNormalized, /codewhale-windows-x64\.exe/);
143 assert.ok(
144 batNormalized.startsWith("@echo off\n"),
145 "installer launcher must start with @echo off",
146 );
147 assert.match(
148 windowsLauncherContents(),
149 /\r\n/,
150 "the GitHub/npm codewhale.bat asset must be generated with CRLF",
151 );
152 });
153
154 test("assembly creates and verifies the exact release asset directory", async () => {
155 const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-asset-assembly-"));
156 const input = path.join(tempRoot, "input");
157 const output = path.join(tempRoot, "output");
158 try {
159 fs.mkdirSync(input, { recursive: true });
160 makeIntermediateArtifacts(input);
161 await assemble(input, output);
162 await assert.doesNotReject(() => verifyAssetDirectory(output));
163
164 assert.deepEqual(
165 fs.readdirSync(output).sort(),
166 [...allReleaseAssetNames()].sort(),
167 );
168 assert.equal(
169 fs.readFileSync(path.join(output, "codewhale.bat"), "utf8"),
170 windowsLauncherContents(),
171 );
172
173 const checksums = parseChecksumManifest(
174 fs.readFileSync(path.join(output, CHECKSUM_MANIFEST), "utf8"),
175 CHECKSUM_MANIFEST,
176 );
177 assert.deepEqual([...checksums.keys()].sort(), [...checksummedReleaseAssetNames()].sort());
178 } finally {
179 fs.rmSync(tempRoot, { force: true, recursive: true });
180 }
181 });
182
183 test("bundle helper emits reproducible timestamped tar and zip archives from paths with spaces", () => {
184 const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-bundle-assembly-"));
185 const input = path.join(tempRoot, "input artifacts with spaces");
186 const output = path.join(tempRoot, "output bundles with spaces");
187 const repeatedOutput = path.join(tempRoot, "output bundles repeated with spaces");
188 const sourceDateEpoch = "1700000000";
189 try {
190 createBundleInputs(input);
191 assert.equal(runBundle(input, output, sourceDateEpoch).status, 0);
192 assert.equal(runBundle(input, repeatedOutput, sourceDateEpoch).status, 0);
193 assert.deepEqual(
194 fs.readdirSync(output).sort(),
195 [...BUNDLE_ASSET_NAMES, BUNDLE_CHECKSUM_MANIFEST].sort(),
196 );
197 const checksums = parseChecksumManifest(
198 fs.readFileSync(path.join(output, BUNDLE_CHECKSUM_MANIFEST), "utf8"),
199 BUNDLE_CHECKSUM_MANIFEST,
200 );
201 for (const name of BUNDLE_ASSET_NAMES) {
202 assert.equal(checksums.get(name), sha256(path.join(output, name)));
203 assert.deepEqual(
204 fs.readFileSync(path.join(output, name)),
205 fs.readFileSync(path.join(repeatedOutput, name)),
206 `${name} should be byte-reproducible for identical inputs`,
207 );
208 }
209 assert.deepEqual(
210 fs.readFileSync(path.join(output, BUNDLE_CHECKSUM_MANIFEST)),
211 fs.readFileSync(path.join(repeatedOutput, BUNDLE_CHECKSUM_MANIFEST)),
212 "bundle checksum manifest should be reproducible",
213 );
214
215 const noticeFiles = ["LICENSE", "THIRD_PARTY_NOTICES.md", "docs/THIRD_PARTY_NOTICES.md"];
216 const noticeEntries = (stem) => ["", "docs/", ...noticeFiles].map((entry) => stem + "/codewhale-notices/" + entry);
217 const linuxEntries = execFileSync(
218 "tar",
219 ["-tzf", path.join(output, "codewhale-linux-x64.tar.gz")],
220 { encoding: "utf8" },
221 ).trim().split("\n").sort();
222 assert.deepEqual(linuxEntries, [
223 "codewhale-linux-x64/",
224 ...noticeEntries("codewhale-linux-x64"),
225 "codewhale-linux-x64/codew",
226 "codewhale-linux-x64/codewhale",
227 "codewhale-linux-x64/install.sh",
228 ].sort());
229 const extracted = path.join(tempRoot, "tar extracted");
230 fs.mkdirSync(extracted);
231 execFileSync(
232 "tar",
233 ["-xzf", path.join(output, "codewhale-linux-x64.tar.gz"), "-C", extracted],
234 { stdio: "pipe" },
235 );
236 for (const entry of ["codewhale", "codew", "install.sh"]) {
237 const mode = fs.statSync(path.join(extracted, "codewhale-linux-x64", entry)).mode & 0o777;
238 assert.equal(mode, 0o755, `${entry} should remain executable after artifact transport`);
239 assert.equal(
240 Math.trunc(fs.statSync(path.join(extracted, "codewhale-linux-x64", entry)).mtimeMs / 1000),
241 Number(sourceDateEpoch),
242 `${entry} should retain the source commit timestamp`,
243 );
244 }
245 for (const file of noticeFiles) {
246 assert.deepEqual(fs.readFileSync(path.join(extracted, "codewhale-linux-x64/codewhale-notices", file)), fs.readFileSync(path.join(repoRoot, file)));
247 }
248 const expectedLauncher = toCrlf(
249 fs.readFileSync(path.join(repoRoot, "scripts/installer/codewhale.bat"), "utf8"),
250 );
251 const expectedInstallBat = toCrlf(
252 fs.readFileSync(path.join(repoRoot, "scripts/release/install.bat"), "utf8"),
253 );
254 const zipListing = (name) =>
255 execFileSync("unzip", ["-Z1", path.join(output, name)], { encoding: "utf8" })
256 .trim()
257 .split("\n")
258 .sort();
259 const zipFile = (archive, inner) =>
260 execFileSync("unzip", ["-p", path.join(output, archive), inner]);
261
262 assert.deepEqual(zipListing("codewhale-windows-x64.zip"), [
263 "codewhale-windows-x64/",
264 ...noticeEntries("codewhale-windows-x64"),
265 "codewhale-windows-x64/codew.exe",
266 "codewhale-windows-x64/codewhale.bat",
267 "codewhale-windows-x64/codewhale.exe",
268 "codewhale-windows-x64/install.bat",
269 "codewhale-windows-x64/install.ps1",
270 ].sort());
271 assert.deepEqual(zipListing("codewhale-windows-arm64.zip"), [
272 "codewhale-windows-arm64/",
273 ...noticeEntries("codewhale-windows-arm64"),
274 "codewhale-windows-arm64/codew.exe",
275 "codewhale-windows-arm64/codewhale.bat",
276 "codewhale-windows-arm64/codewhale.exe",
277 "codewhale-windows-arm64/install.bat",
278 "codewhale-windows-arm64/install.ps1",
279 ].sort());
280 const portableEntries = zipListing("codewhale-windows-arm64-portable.zip");
281 assert.deepEqual(portableEntries, [
282 "codewhale-windows-arm64-portable/",
283 ...noticeEntries("codewhale-windows-arm64-portable"),
284 "codewhale-windows-arm64-portable/codew.exe",
285 "codewhale-windows-arm64-portable/codewhale.bat",
286 "codewhale-windows-arm64-portable/codewhale.exe",
287 ].sort());
288 assert.deepEqual(zipListing("codewhale-windows-x64-portable.zip"), [
289 "codewhale-windows-x64-portable/",
290 ...noticeEntries("codewhale-windows-x64-portable"),
291 "codewhale-windows-x64-portable/codew.exe",
292 "codewhale-windows-x64-portable/codewhale.bat",
293 "codewhale-windows-x64-portable/codewhale.exe",
294 ].sort());
295
296 for (const [archive, prefix, includeInstall] of [
297 ["codewhale-windows-x64.zip", "codewhale-windows-x64", true],
298 ["codewhale-windows-arm64.zip", "codewhale-windows-arm64", true],
299 ["codewhale-windows-x64-portable.zip", "codewhale-windows-x64-portable", false],
300 ["codewhale-windows-arm64-portable.zip", "codewhale-windows-arm64-portable", false],
301 ]) {
302 for (const file of noticeFiles) {
303 assert.deepEqual(zipFile(archive, prefix + "/codewhale-notices/" + file), fs.readFileSync(path.join(repoRoot, file)));
304 }
305 const launcher = zipFile(archive, `${prefix}/codewhale.bat`);
306 assert.deepEqual(
307 launcher,
308 Buffer.from(expectedLauncher, "utf8"),
309 `${archive} must ship the NSIS launcher with CRLF`,
310 );
311 assert.match(launcher.toString("utf8"), /where wt >nul 2>nul/);
312 assert.match(launcher.toString("utf8"), /codewhale\.exe/);
313 assert.doesNotMatch(launcher.toString("utf8"), /codewhale-windows-x64\.exe/);
314 if (includeInstall) {
315 const installBat = zipFile(archive, `${prefix}/install.bat`);
316 assert.deepEqual(
317 installBat,
318 Buffer.from(expectedInstallBat, "utf8"),
319 `${archive} install.bat must be staged with CRLF`,
320 );
321 assert.match(installBat.toString("utf8"), /powershell\.exe -NoProfile.*install\.ps1/);
322 const installPs = zipFile(archive, `${prefix}/install.ps1`);
323 assert.deepEqual(installPs, Buffer.from(toCrlf(fs.readFileSync(path.join(repoRoot, "scripts/release/install.ps1"), "utf8"))));
324 assert.match(installPs.toString("utf8"), /codewhale\.bat/);
325 assert.match(installPs.toString("utf8"), /Rollback incomplete; recovery files retained/);
326 }
327 }
328
329 const zipExtracted = path.join(tempRoot, "zip extracted");
330 fs.mkdirSync(zipExtracted);
331 execFileSync(
332 "unzip",
333 ["-qq", path.join(output, "codewhale-windows-arm64-portable.zip"), "-d", zipExtracted],
334 { env: { ...process.env, TZ: "UTC" }, stdio: "pipe" },
335 );
336 for (const entry of ["codewhale.exe", "codew.exe", "codewhale.bat"]) {
337 assert.equal(
338 Math.trunc(
339 fs.statSync(path.join(zipExtracted, "codewhale-windows-arm64-portable", entry)).mtimeMs / 1000,
340 ),
341 Number(sourceDateEpoch),
342 `${entry} should retain the source commit timestamp`,
343 );
344 }
345 } finally {
346 fs.rmSync(tempRoot, { force: true, recursive: true });
347 }
348 });
349
350 test("bundle helper rejects missing, malformed, and ZIP-unrepresentable release epochs", () => {
351 const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-bundle-input-validation-"));
352 const input = path.join(tempRoot, "input");
353 try {
354 createBundleInputs(input);
355
356 const missingEpoch = runBundle(input, path.join(tempRoot, "missing-epoch"));
357 assert.notEqual(missingEpoch.status, 0);
358 assert.match(missingEpoch.stderr, /SOURCE_DATE_EPOCH is required/);
359
360 const malformedEpoch = runBundle(input, path.join(tempRoot, "malformed-epoch"), "not-an-epoch");
361 assert.notEqual(malformedEpoch.status, 0);
362 assert.match(malformedEpoch.stderr, /SOURCE_DATE_EPOCH must be an integer Unix timestamp/);
363
364 const preZipEpoch = runBundle(input, path.join(tempRoot, "pre-zip-epoch"), "0");
365 assert.notEqual(preZipEpoch.status, 0);
366 assert.match(preZipEpoch.stderr, /SOURCE_DATE_EPOCH must be between 315532800/);
367 } finally {
368 fs.rmSync(tempRoot, { force: true, recursive: true });
369 }
370 });
371
372 test("bundle helper names a missing required release artifact", () => {
373 const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-bundle-missing-artifact-"));
374 const input = path.join(tempRoot, "input");
375 try {
376 createBundleInputs(input);
377 fs.rmSync(path.join(input, "codew-linux-x64", "codew-linux-x64"));
378 const result = runBundle(input, path.join(tempRoot, "output"), "1700000000");
379 assert.notEqual(result.status, 0);
380 assert.match(result.stderr, /missing required release artifact for linux-x64: .*codew-linux-x64/);
381 } finally {
382 fs.rmSync(tempRoot, { force: true, recursive: true });
383 }
384 });
385
386 test("verification rejects modified and unexpected assets", async () => {
387 const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-asset-tamper-"));
388 const input = path.join(tempRoot, "input");
389 const output = path.join(tempRoot, "output");
390 try {
391 fs.mkdirSync(input, { recursive: true });
392 makeIntermediateArtifacts(input);
393 await assemble(input, output);
394 fs.appendFileSync(path.join(output, "codewhale-linux-x64"), "tampered\n");
395 await assert.rejects(
396 () => verifyAssetDirectory(output),
397 /checksum mismatch for codewhale-linux-x64/,
398 );
399
400 fs.writeFileSync(path.join(output, "unexpected.txt"), "unexpected\n");
401 await assert.rejects(
402 () => verifyAssetDirectory(output),
403 /unexpected: unexpected\.txt/,
404 );
405 } finally {
406 fs.rmSync(tempRoot, { force: true, recursive: true });
407 }
408 });
409
410 test("both npm packages ship the canonical release notices", () => {
411 const destination = fs.mkdtempSync(path.join(os.tmpdir(), "codewhale-npm-notices-"));
412 try {
413 for (const packageName of ["codewhale", "runtime-sdk"]) {
414 const result = execFileSync("npm", ["pack", "--offline", "--ignore-scripts", "--json", "--pack-destination", destination], {
415 cwd: path.join(repoRoot, "npm", packageName), encoding: "utf8",
416 });
417 const archive = path.join(destination, JSON.parse(result)[0].filename);
418 for (const document of ["LICENSE", "THIRD_PARTY_NOTICES.md", "docs/THIRD_PARTY_NOTICES.md"]) {
419 const actual = execFileSync("tar", ["-xOzf", archive, "package/codewhale-notices/" + document]);
420 assert.deepEqual(actual, fs.readFileSync(path.join(repoRoot, document)));
421 }
422 }
423 } finally {
424 fs.rmSync(destination, { recursive: true, force: true });
425 }
426 });
427
427 lines JAVASCRIPT