-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.test.ts
More file actions
325 lines (272 loc) · 11.1 KB
/
build.test.ts
File metadata and controls
325 lines (272 loc) · 11.1 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { describe, it, expect } from "bun:test";
import {
parseTarget,
parseTargetArg,
isWindowsTarget,
getOutfile,
getBuildCompileOptions,
buildLabel,
buildCopyrightLine,
type WindowsMeta,
} from "./build";
// ─── parseTargetArg ─────────────────────────────────────────────────────────────
describe("parseTargetArg", () => {
it("extracts the target value from argv", () => {
expect(parseTargetArg(["bun", "build.ts", "--target=bun-linux-x64"])).toBe("bun-linux-x64");
});
it("returns null when no --target= flag is present", () => {
expect(parseTargetArg(["bun", "build.ts"])).toBeNull();
});
it("returns null for empty argv", () => {
expect(parseTargetArg([])).toBeNull();
});
it("ignores unrelated flags", () => {
expect(parseTargetArg(["bun", "--verbose", "--target=bun-windows-x64"])).toBe(
"bun-windows-x64",
);
});
it("returns empty string when --target= has no value", () => {
expect(parseTargetArg(["bun", "--target="])).toBe("");
});
});
// ─── parseTarget ─────────────────────────────────────────────────────────────
describe("parseTarget", () => {
it("parses bun-linux-x64", () => {
expect(parseTarget("bun-linux-x64")).toEqual({ os: "linux", arch: "x64" });
});
it("parses bun-linux-x64-baseline", () => {
expect(parseTarget("bun-linux-x64-baseline")).toEqual({
os: "linux",
arch: "x64-baseline",
});
});
it("parses bun-linux-arm64", () => {
expect(parseTarget("bun-linux-arm64")).toEqual({
os: "linux",
arch: "arm64",
});
});
it("parses bun-linux-arm64-musl", () => {
expect(parseTarget("bun-linux-arm64-musl")).toEqual({
os: "linux",
arch: "arm64-musl",
});
});
it("parses bun-darwin-x64", () => {
expect(parseTarget("bun-darwin-x64")).toEqual({
os: "darwin",
arch: "x64",
});
});
it("parses bun-darwin-arm64", () => {
expect(parseTarget("bun-darwin-arm64")).toEqual({
os: "darwin",
arch: "arm64",
});
});
it("parses bun-windows-x64", () => {
expect(parseTarget("bun-windows-x64")).toEqual({
os: "windows",
arch: "x64",
});
});
it("parses bun-windows-x64-baseline", () => {
expect(parseTarget("bun-windows-x64-baseline")).toEqual({
os: "windows",
arch: "x64-baseline",
});
});
it("parses bun-windows-x64-modern", () => {
expect(parseTarget("bun-windows-x64-modern")).toEqual({
os: "windows",
arch: "x64-modern",
});
});
it("parses bun-windows-arm64", () => {
expect(parseTarget("bun-windows-arm64")).toEqual({
os: "windows",
arch: "arm64",
});
});
it("returns native platform when target is null", () => {
const result = parseTarget(null);
// We can only assert the shape, not the exact values (depends on the runner)
expect(typeof result.os).toBe("string");
expect(typeof result.arch).toBe("string");
expect(result.os.length).toBeGreaterThan(0);
});
it("returns native platform when target is undefined", () => {
const result = parseTarget(undefined);
expect(typeof result.os).toBe("string");
expect(typeof result.arch).toBe("string");
});
// baseline must be parsed before plain x64 (order matters in the if-chain)
it("does not confuse windows-x64-baseline with windows-x64", () => {
expect(parseTarget("bun-windows-x64-baseline").arch).toBe("x64-baseline");
expect(parseTarget("bun-windows-x64").arch).toBe("x64");
});
it("does not confuse linux-x64-baseline with linux-x64", () => {
expect(parseTarget("bun-linux-x64-baseline").arch).toBe("x64-baseline");
expect(parseTarget("bun-linux-x64").arch).toBe("x64");
});
// Regression: on Windows, process.platform is "win32" which isWindowsTarget()
// does not recognise. parseTarget(null) must normalise it to "windows".
// We can't execute the null path on a non-Windows runner, but we can assert the
// downstream contract: "win32" alone must NOT satisfy isWindowsTarget (proving
// normalisation is necessary) and getOutfile("windows", null) must add .exe.
it("isWindowsTarget rejects bare win32 — normalisation in parseTarget is required", () => {
expect(isWindowsTarget("win32")).toBe(false);
expect(isWindowsTarget("windows")).toBe(true);
expect(getOutfile("windows", null)).toBe("./dist/github-code-search.exe");
});
// Regression: the end-of-function fallback (unrecognised target string) must
// also normalise win32 → windows, not leak the raw Node.js platform alias.
it("never returns win32 as os for unknown target strings", () => {
const result = parseTarget("bun-completely-unknown-future-target");
expect(result.os).not.toBe("win32");
});
});
// ─── isWindowsTarget ─────────────────────────────────────────────────────────
describe("isWindowsTarget", () => {
it("returns true for windows", () => {
expect(isWindowsTarget("windows")).toBe(true);
});
it("returns false for linux", () => {
expect(isWindowsTarget("linux")).toBe(false);
});
it("returns false for darwin", () => {
expect(isWindowsTarget("darwin")).toBe(false);
});
});
// ─── getOutfile ───────────────────────────────────────────────────────────────
describe("getOutfile", () => {
it("adds .exe suffix for windows targets", () => {
expect(getOutfile("windows", "bun-windows-x64")).toBe(
"./dist/github-code-search-windows-x64.exe",
);
});
it("adds .exe suffix for windows-x64-modern", () => {
expect(getOutfile("windows", "bun-windows-x64-modern")).toBe(
"./dist/github-code-search-windows-x64-modern.exe",
);
});
it("adds .exe suffix for windows-x64-baseline", () => {
expect(getOutfile("windows", "bun-windows-x64-baseline")).toBe(
"./dist/github-code-search-windows-x64-baseline.exe",
);
});
it("adds .exe suffix for windows-arm64", () => {
expect(getOutfile("windows", "bun-windows-arm64")).toBe(
"./dist/github-code-search-windows-arm64.exe",
);
});
it("does not add .exe for linux targets", () => {
expect(getOutfile("linux", "bun-linux-x64")).toBe("./dist/github-code-search-linux-x64");
});
it("does not add .exe for darwin targets", () => {
expect(getOutfile("darwin", "bun-darwin-arm64")).toBe("./dist/github-code-search-darwin-arm64");
});
it("omits suffix for native (no target)", () => {
const outfile = getOutfile("linux", null);
expect(outfile).toBe("./dist/github-code-search");
});
it("strips the bun- prefix from the suffix", () => {
expect(getOutfile("linux", "bun-linux-arm64")).toBe("./dist/github-code-search-linux-arm64");
});
});
// ─── getBuildCompileOptions ───────────────────────────────────────────────────
const FULL_META: WindowsMeta = {
iconPath: "/abs/path/favicon.ico",
title: "github-code-search",
publisher: "fulll",
appVersion: "1.2.3",
description: "Interactive GitHub code search",
copyright: "Copyright © 2026 fulll — MIT",
};
describe("getBuildCompileOptions", () => {
it("returns only outfile for non-windows targets", () => {
const opts = getBuildCompileOptions("linux", "./dist/foo", FULL_META);
expect(opts).toEqual({ outfile: "./dist/foo" });
});
it("returns only outfile for darwin", () => {
const opts = getBuildCompileOptions("darwin", "./dist/foo", FULL_META);
expect(opts).toEqual({ outfile: "./dist/foo" });
});
it("includes hideConsole for windows target", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META);
expect(opts).toMatchObject({
outfile: "./dist/foo.exe",
windows: { hideConsole: true },
});
});
it("sets icon from iconPath", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: WindowsMeta & { icon: string; hideConsole: boolean };
};
expect(opts.windows.icon).toBe("/abs/path/favicon.ico");
});
it("sets title", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: { title: string };
};
expect(opts.windows.title).toBe("github-code-search");
});
it("sets publisher", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: { publisher: string };
};
expect(opts.windows.publisher).toBe("fulll");
});
it("sets version from appVersion", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: { version: string };
};
expect(opts.windows.version).toBe("1.2.3");
});
it("sets description", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: { description: string };
};
expect(opts.windows.description).toBe("Interactive GitHub code search");
});
it("sets copyright", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe", FULL_META) as {
windows: { copyright: string };
};
expect(opts.windows.copyright).toBe("Copyright © 2026 fulll — MIT");
});
it("works with empty meta (all windows fields undefined)", () => {
const opts = getBuildCompileOptions("windows", "./dist/foo.exe") as {
outfile: string;
windows: Record<string, unknown>;
};
expect(opts.outfile).toBe("./dist/foo.exe");
expect(opts.windows.icon).toBeUndefined();
expect(opts.windows.title).toBeUndefined();
});
});
// ─── buildLabel ──────────────────────────────────────────────────────────────
describe("buildLabel", () => {
it("formats the label string correctly", () => {
expect(buildLabel("1.9.0", "abc1234", "linux", "x64")).toBe("1.9.0 (abc1234 · linux/x64)");
});
it("works with windows target", () => {
expect(buildLabel("1.9.0", "abc1234", "windows", "x64-modern")).toBe(
"1.9.0 (abc1234 · windows/x64-modern)",
);
});
it("works with dev commit", () => {
expect(buildLabel("1.9.0", "dev", "darwin", "arm64")).toBe("1.9.0 (dev · darwin/arm64)");
});
});
// ─── buildCopyrightLine ──────────────────────────────────────────────────────
describe("buildCopyrightLine", () => {
it("formats the copyright string correctly", () => {
expect(buildCopyrightLine(2026, "fulll", "MIT")).toBe("Copyright © 2026 fulll — MIT");
});
it("uses the provided year", () => {
expect(buildCopyrightLine(2030, "Acme Corp", "Apache-2.0")).toBe(
"Copyright © 2030 Acme Corp — Apache-2.0",
);
});
});