Repository navigation
Expand file tree
/
Copy pathlinux-packaging.test.ts
More file actions
406 lines (374 loc) · 16.9 KB
/
Copy pathlinux-packaging.test.ts
File metadata and controls
406 lines (374 loc) · 16.9 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { BUNDLE_TARGETS } from "./bundle-targets.js";
import {
archiveAsset,
archPkgver,
cacheControl,
contentType,
desktopPackageFileName,
DECLARED_LIBRARIES,
glibcFloor,
neededLibraries,
undeclaredLibraries,
INSTALL_DIR,
isPackageFile,
maxGlibc,
PACKAGE_TARGETS,
packageAssets,
parseProductId,
parseSha256File,
parseSha256Sums,
productOfFile,
productOfTag,
PRODUCTS,
readmeInstallBlock,
renderPkgbuild,
renderRepoFile,
REPOSITORY,
repoPath,
selectReleases,
syncPlan,
type GithubRelease,
} from "./linux-packaging.js";
describe("package assets", () => {
it("names one deb and one rpm per architecture, none matching the bundles' atomic-agent-* glob", () => {
const names = packageAssets("0.7.0");
expect(names).toEqual([
"atomic-agent_0.7.0_amd64.deb",
"atomic-agent_0.7.0_arm64.deb",
"atomic-agent_0.7.0_x86_64.rpm",
"atomic-agent_0.7.0_aarch64.rpm",
]);
for (const name of names) {
expect(name.startsWith("atomic-agent-")).toBe(false);
expect(isPackageFile(name)).toBe(true);
}
expect(isPackageFile("atomic-agent_0.7.0_amd64.deb.sha256")).toBe(false);
expect(isPackageFile("atomic-agent-linux-x64.tar.gz")).toBe(false);
});
it("knows the desktop's packages by electron-builder's names, and nothing else of that release", () => {
const [x64, arm64] = PACKAGE_TARGETS as [(typeof PACKAGE_TARGETS)[number], (typeof PACKAGE_TARGETS)[number]];
expect(desktopPackageFileName("0.0.6", "deb", x64)).toBe("Atomic-Agent-0.0.6-amd64.deb");
expect(desktopPackageFileName("0.0.6", "deb", arm64)).toBe("Atomic-Agent-0.0.6-arm64.deb");
expect(desktopPackageFileName("0.0.6", "rpm", x64)).toBe("Atomic-Agent-0.0.6-x86_64.rpm");
for (const name of [
"Atomic-Agent-0.0.6-amd64.deb",
"Atomic-Agent-0.0.6-arm64.deb",
"Atomic-Agent-0.0.6-x86_64.rpm",
"Atomic-Agent-0.0.6-aarch64.rpm",
]) {
expect(productOfFile(name)?.id).toBe("desktop");
}
for (const name of [
"Atomic-Agent-0.0.6-x86_64.AppImage",
"Atomic-Agent-0.0.6-arm64.dmg",
"Atomic-Agent-Setup-0.0.6-x64.exe",
"SHA256SUMS.txt",
"stable-linux.yml",
]) {
expect(productOfFile(name)).toBeUndefined();
}
expect(productOfFile("atomic-agent_0.7.0_amd64.deb")?.id).toBe("cli");
});
it("maps release tags to products and plain versions", () => {
expect(productOfTag("v0.7.0")).toEqual({ product: PRODUCTS.cli, version: "0.7.0" });
expect(productOfTag("desktop-v0.0.6")).toEqual({ product: PRODUCTS.desktop, version: "0.0.6" });
expect(productOfTag("desktop-latest")).toBeUndefined();
expect(productOfTag("v0.7.0-rc.1")).toBeUndefined();
expect(productOfTag("0.7.0")).toBeUndefined();
expect(parseProductId("desktop")).toBe("desktop");
expect(() => parseProductId("snap")).toThrow("unknown product snap");
});
it("packages the Linux bundle targets and nothing else", () => {
const linux = BUNDLE_TARGETS.filter((t) => t.platform === "linux").map((t) => t.slug);
expect(PACKAGE_TARGETS.map((t) => t.slug)).toEqual(linux);
expect(archiveAsset(PACKAGE_TARGETS[0]!)).toBe("atomic-agent-linux-x64.tar.gz");
expect(archiveAsset(PACKAGE_TARGETS[1]!)).toBe("atomic-agent-linux-arm64.tar.gz");
});
it("puts the tree where src/update/install-source.ts expects it", () => {
const source = readFileSync("src/update/install-source.ts", "utf8");
expect(source).toContain(`PACKAGE_DIR = "${INSTALL_DIR}"`);
const nfpm = readFileSync("packaging/nfpm.yaml", "utf8");
expect(nfpm).toContain(`dst: ${INSTALL_DIR}\n`);
expect(nfpm).toContain(`dst: ${INSTALL_DIR}/install-source`);
const pkgbuild = readFileSync("packaging/PKGBUILD.in", "utf8");
expect(pkgbuild).toContain(`"\${pkgdir}${INSTALL_DIR}"`);
});
});
describe("neededLibraries", () => {
// readelf -d of the Node 25 Linux x64 binary (abridged).
const readelf = [
"Dynamic section at offset 0x5a3c8f0 contains 32 entries:",
" Tag Type Name/Value",
" 0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]",
" 0x0000000000000001 (NEEDED) Shared library: [libatomic.so.1]",
" 0x000000000000000e (SONAME) Library soname: [libnode.so.1]",
" 0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN]",
].join("\n");
it("lists the NEEDED entries in order, nothing else", () => {
expect(neededLibraries(readelf)).toEqual(["libdl.so.2", "libatomic.so.1"]);
expect(neededLibraries("")).toEqual([]);
});
it("names the libraries no package dependency covers, the loader aside", () => {
expect(
undeclaredLibraries(["libc.so.6", "libstdc++.so.6", "libatomic.so.1", "ld-linux-x86-64.so.2"]),
).toEqual([]);
expect(undeclaredLibraries(["libz.so.1", "libc.so.6", "libssl.so.3"])).toEqual([
"libz.so.1",
"libssl.so.3",
]);
});
it("declares every library of the shipped binaries in nfpm.yaml and the PKGBUILD", () => {
// What the Node 25 SEA binary, better_sqlite3.node and ripgrep load (readelf -d of v0.6.11).
const shipped = [
"libatomic.so.1",
"libc.so.6",
"libdl.so.2",
"libgcc_s.so.1",
"libm.so.6",
"libpthread.so.0",
"libstdc++.so.6",
];
expect(undeclaredLibraries(shipped)).toEqual([]);
const nfpm = readFileSync("packaging/nfpm.yaml", "utf8");
expect(nfpm).toContain("- libatomic1\n");
expect(nfpm).toContain("- libatomic\n");
expect(nfpm).toContain("- libstdc++6\n");
expect(nfpm).toContain("- libstdc++\n");
expect(readFileSync("packaging/PKGBUILD.in", "utf8")).toContain("depends=('glibc' 'gcc-libs')");
expect(DECLARED_LIBRARIES["libatomic.so.1"]).toBe("libatomic");
});
});
describe("glibcFloor", () => {
// readelf -V of a Node 25 Linux build (abridged).
const readelf = [
"Version needs section '.gnu.version_r' contains 2 entries:",
" 0x0000: Version: 1 File: libc.so.6 Cnt: 12",
" 0x0010: Name: GLIBC_2.2.5 Flags: none Version: 2",
" 0x0130: Name: GLIBC_2.18 Flags: none Version: 23",
" 0x0140: Name: GLIBC_2.28 Flags: none Version: 8",
" 0x0150: Name: GLIBC_2.25 Flags: none Version: 22",
" 0x0160: Name: GLIBC_2.34 Flags: WEAK Version: 24",
].join("\n");
it("is the newest required version, compared as numbers, ignoring WEAK ones", () => {
expect(glibcFloor(readelf)).toBe("2.28");
});
it("is undefined for a static binary", () => {
expect(glibcFloor("There are no version dependencies in this file.")).toBeUndefined();
});
it("the package depends on the newest floor of any ELF in the tree", () => {
expect(maxGlibc(["2.28", undefined, "2.35", "2.17"])).toBe("2.35");
expect(maxGlibc([undefined])).toBeUndefined();
});
});
describe("archPkgver", () => {
it.each([
["0.7.0", "0.7.0"],
["v1.2.3", "1.2.3"],
["0.7.0-rc.1", "0.7.0rc.1"],
])("%s → %s", (version, pkgver) => {
expect(archPkgver(version)).toBe(pkgver);
});
});
describe("parseSha256File", () => {
const hash = "a".repeat(64);
it("reads the digest package-bundle.ts writes", () => {
expect(parseSha256File(`${hash} atomic-agent-linux-x64.tar.gz\n`, "atomic-agent-linux-x64.tar.gz")).toBe(hash);
});
it("refuses a digest of another file", () => {
expect(() => parseSha256File(`${hash} other.tar.gz\n`, "atomic-agent-linux-x64.tar.gz")).toThrow(
"not a sha256 digest",
);
});
});
describe("parseSha256Sums", () => {
it("reads sha256sum's list, text or binary mode, CRLF or not, and skips what is not a digest", () => {
const text = [
`${"A".repeat(64)} Atomic-Agent-0.0.6-amd64.deb`,
`${"b".repeat(64)} *Atomic-Agent-0.0.6-arm64.deb`,
"not a digest line",
"",
].join("\r\n");
expect(parseSha256Sums(text)).toEqual({
"Atomic-Agent-0.0.6-amd64.deb": "a".repeat(64),
"Atomic-Agent-0.0.6-arm64.deb": "b".repeat(64),
});
});
});
describe("renderPkgbuild", () => {
const template = readFileSync("packaging/PKGBUILD.in", "utf8");
const sums = { "linux-x64": "4".repeat(64), "linux-arm64": "5".repeat(64) };
it("fills the version, the download base, the archives and every checksum", () => {
const text = renderPkgbuild(template, { version: "0.7.0", sums, sourceBase: "https://dl/v0.7.0" });
expect(text).not.toMatch(/@[A-Z0-9_]+@/);
expect(text).toContain("pkgver=0.7.0\n");
expect(text).toContain("_version=0.7.0\n");
expect(text).toContain("_base='https://dl/v0.7.0'");
expect(text).toContain(
'source_x86_64=("atomic-agent-${_version}-linux-x64.tar.gz::${_base}/atomic-agent-linux-x64.tar.gz")',
);
expect(text).toContain(
'source_aarch64=("atomic-agent-${_version}-linux-arm64.tar.gz::${_base}/atomic-agent-linux-arm64.tar.gz")',
);
expect(text).toContain(`sha256sums_x86_64=('${"4".repeat(64)}')`);
expect(text).toContain(`sha256sums_aarch64=('${"5".repeat(64)}')`);
expect(text).toContain("x86_64) slug='linux-x64'");
expect(text).toContain("aarch64) slug='linux-arm64'");
});
it("writes LF line ends from a CRLF template", () => {
const crlf = template.replace(/\r?\n/g, "\r\n");
expect(renderPkgbuild(crlf, { version: "0.7.0", sums, sourceBase: "x" })).not.toContain("\r");
});
it("refuses a release missing an archive's digest", () => {
expect(() =>
renderPkgbuild(template, { version: "0.7.0", sums: { "linux-x64": sums["linux-x64"] }, sourceBase: "x" }),
).toThrow("no sha256 for linux-arm64");
});
it("fills the desktop template from the release's debs and their SHA256SUMS digests", () => {
const desktop = readFileSync("packaging/PKGBUILD-desktop.in", "utf8");
const debSums = {
"Atomic-Agent-0.0.6-amd64.deb": "6".repeat(64),
"Atomic-Agent-0.0.6-arm64.deb": "7".repeat(64),
};
const text = renderPkgbuild(desktop, { version: "0.0.6", sums: debSums, sourceBase: "https://dl/desktop-v0.0.6" });
expect(text).not.toMatch(/@[A-Z0-9_]+@/);
expect(text).toContain("pkgname=atomic-agent-desktop-bin\n");
expect(text).toContain("pkgver=0.0.6\n");
expect(text).toContain("_base='https://dl/desktop-v0.0.6'");
expect(text).toContain('source_x86_64=("${_base}/Atomic-Agent-0.0.6-amd64.deb")');
expect(text).toContain('source_aarch64=("${_base}/Atomic-Agent-0.0.6-arm64.deb")');
expect(text).toContain(`sha256sums_x86_64=('${"6".repeat(64)}')`);
expect(text).toContain(`sha256sums_aarch64=('${"7".repeat(64)}')`);
expect(text).toContain("noextract=('Atomic-Agent-0.0.6-amd64.deb' 'Atomic-Agent-0.0.6-arm64.deb')");
// The CLI's digests are not demanded by a template that does not use them.
expect(() => renderPkgbuild(desktop, { version: "0.0.6", sums: { "Atomic-Agent-0.0.6-amd64.deb": "6".repeat(64) }, sourceBase: "x" }))
.toThrow("no sha256 for Atomic-Agent-0.0.6-arm64.deb");
});
});
describe("selectReleases", () => {
const release = (tag: string, over: Partial<GithubRelease> = {}): GithubRelease => ({
tag_name: tag,
draft: false,
prerelease: false,
assets: [{ name: `atomic-agent_${tag.slice(1)}_amd64.deb` }],
...over,
});
it("keeps the newest published stable releases that carry packages, newest first", () => {
const releases = [
release("v0.9.0"),
release("v0.10.0"),
release("v0.11.0", { draft: true }),
release("v0.11.0-rc.1", { prerelease: true }),
release("v0.2.0", { assets: [{ name: "atomic-agent-linux-x64.tar.gz" }] }),
release("v0.8.0"),
];
expect(selectReleases(releases, 2)).toEqual([
{ tag: "v0.10.0", version: "0.10.0" },
{ tag: "v0.9.0", version: "0.9.0" },
]);
expect(selectReleases(releases).map((r) => r.tag)).toEqual(["v0.10.0", "v0.9.0", "v0.8.0"]);
});
it("keeps each product's own window: desktop releases never count for the CLI, nor the reverse", () => {
const desktop = (tag: string, over: Partial<GithubRelease> = {}): GithubRelease => ({
tag_name: tag,
draft: false,
prerelease: false,
assets: [{ name: `Atomic-Agent-${tag.slice("desktop-v".length)}-amd64.deb` }],
...over,
});
const releases = [
desktop("desktop-v0.0.5"),
release("v0.9.0"),
desktop("desktop-v0.0.6"),
desktop("desktop-latest", { prerelease: true }),
desktop("desktop-v0.0.1", { assets: [{ name: "Atomic-Agent-0.0.1-x86_64.AppImage" }] }),
release("v0.10.0"),
];
expect(selectReleases(releases, 10, PRODUCTS.desktop)).toEqual([
{ tag: "desktop-v0.0.6", version: "0.0.6" },
{ tag: "desktop-v0.0.5", version: "0.0.5" },
]);
expect(selectReleases(releases, 1, PRODUCTS.cli)).toEqual([{ tag: "v0.10.0", version: "0.10.0" }]);
});
});
describe("repository layout", () => {
it("puts debs in the pool by package name and rpms beside the dnf metadata", () => {
expect(repoPath("atomic-agent_0.7.0_amd64.deb")).toBe(
"deb/pool/main/a/atomic-agent/atomic-agent_0.7.0_amd64.deb",
);
expect(repoPath("atomic-agent_0.7.0_x86_64.rpm")).toBe("rpm/packages/atomic-agent_0.7.0_x86_64.rpm");
expect(repoPath("Atomic-Agent-0.0.6-amd64.deb")).toBe(
"deb/pool/main/a/atomic-agent-desktop/Atomic-Agent-0.0.6-amd64.deb",
);
expect(repoPath("Atomic-Agent-0.0.6-x86_64.rpm")).toBe("rpm/packages/Atomic-Agent-0.0.6-x86_64.rpm");
expect(() => repoPath("SHA256SUMS.txt")).toThrow("not a package");
});
it("writes a .repo that checks the metadata and, when signed, the packages", () => {
const repo = renderRepoFile({ baseUrl: "https://repo.example" });
expect(repo).toContain("[atomic-agent]\n");
expect(repo).toContain("baseurl=https://repo.example/rpm\n");
expect(repo).toContain("gpgcheck=1\nrepo_gpgcheck=1\n");
expect(repo).toContain("gpgkey=https://repo.example/atomic-chat.asc\n");
expect(renderRepoFile({ baseUrl: "http://x", packageSignatures: false })).toContain("gpgcheck=0\n");
});
it.each([
["deb/pool/main/a/atomic-agent/atomic-agent_0.7.0_amd64.deb", "immutable", "application/vnd.debian.binary-package"],
["rpm/packages/atomic-agent_0.7.0_x86_64.rpm", "immutable", "application/x-rpm"],
["deb/pool/main/a/atomic-agent-desktop/Atomic-Agent-0.0.6-arm64.deb", "immutable", "application/vnd.debian.binary-package"],
["rpm/packages/Atomic-Agent-0.0.6-aarch64.rpm", "immutable", "application/x-rpm"],
["deb/dists/stable/InRelease", "no-cache", "text/plain; charset=utf-8"],
["deb/dists/stable/Release.gpg", "no-cache", "application/pgp-signature"],
["deb/dists/stable/main/binary-amd64/Packages.gz", "no-cache", "application/gzip"],
["rpm/repodata/repomd.xml", "no-cache", "application/xml"],
["rpm/repodata/repomd.xml.asc", "no-cache", "application/pgp-signature"],
["atomic-chat.asc", "no-cache", "application/pgp-keys"],
["atomic-chat.gpg", "no-cache", "application/pgp-keys"],
])("%s: %s, %s", (path, cache, type) => {
expect(cacheControl(path)).toContain(cache);
expect(contentType(path)).toBe(type);
});
});
describe("syncPlan", () => {
it("uploads packages, then indexes, then what points at them, and deletes last", () => {
const local = [
"atomic-chat.asc",
"deb/dists/stable/InRelease",
"deb/dists/stable/main/binary-amd64/Packages",
"deb/pool/main/a/atomic-agent/atomic-agent_0.8.0_amd64.deb",
"deb/pool/main/a/atomic-agent/atomic-agent_0.7.0_amd64.deb",
"rpm/repodata/repomd.xml",
"rpm/repodata/bbb-primary.xml.gz",
];
const remote = [
"deb/pool/main/a/atomic-agent/atomic-agent_0.7.0_amd64.deb",
"deb/pool/main/a/atomic-agent/atomic-agent_0.1.0_amd64.deb",
"rpm/repodata/aaa-primary.xml.gz",
"rpm/repodata/repomd.xml",
];
expect(syncPlan(local, remote).map((s) => `${s.op} ${s.path}`)).toEqual([
"put deb/pool/main/a/atomic-agent/atomic-agent_0.8.0_amd64.deb",
"put deb/dists/stable/main/binary-amd64/Packages",
"put rpm/repodata/bbb-primary.xml.gz",
"put atomic-chat.asc",
"put deb/dists/stable/InRelease",
"put rpm/repodata/repomd.xml",
"delete deb/pool/main/a/atomic-agent/atomic-agent_0.1.0_amd64.deb",
"delete rpm/repodata/aaa-primary.xml.gz",
]);
});
});
describe("readmeInstallBlock", () => {
it("returns the shell block after the marker", () => {
const readme = "x\n<!-- install:apt -->\n\n```bash\nsudo apt install atomic-agent\n```\n";
expect(readmeInstallBlock(readme, "apt")).toBe("sudo apt install atomic-agent\n");
expect(() => readmeInstallBlock(readme, "dnf")).toThrow("no <!-- install:dnf -->");
});
it("reads a CRLF checkout (Windows) as plain lines", () => {
const readme = "<!-- install:apt -->\r\n\r\n```sh\r\nsudo apt install atomic-agent\r\n```\r\n";
expect(readmeInstallBlock(readme, "apt")).toBe("sudo apt install atomic-agent\n");
});
it.each(["apt", "dnf"])("finds the %s instructions in README.md, on the repository URL", (name) => {
expect(readmeInstallBlock(readFileSync("README.md", "utf8"), name)).toContain(REPOSITORY.baseUrl);
});
});