-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathsnapshot-version.test.mjs
More file actions
184 lines (174 loc) · 8.35 KB
/
Copy pathsnapshot-version.test.mjs
File metadata and controls
184 lines (174 loc) · 8.35 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
// Copyright (c) Microsoft Corporation. All rights reserved.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
const scriptsDir = dirname(fileURLToPath(import.meta.url));
const targets = [
"darwin-arm64",
"darwin-x64",
"linux-arm64",
"linux-x64",
"linuxmusl-arm64",
"linuxmusl-x64",
"win32-arm64",
"win32-x64",
];
const scripts = [
["snapshot-bundled-cli-version.sh", "cli-version.txt", false],
["snapshot-bundled-in-process-version.sh", "cli-version-in-process.txt", true],
];
function fixture(t, version) {
const root = mkdtempSync(join(scriptsDir, ".snapshot-test-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
const rust = join(root, "rust");
mkdirSync(join(rust, "scripts"), { recursive: true });
mkdirSync(join(root, "nodejs"));
mkdirSync(join(root, "bin"));
writeFileSync(join(root, "nodejs", "package.json"), JSON.stringify({ copilotCliVersion: version }));
for (const script of [...scripts.map(([script]) => script), "snapshot-release.sh"]) {
copyFileSync(join(scriptsDir, script), join(rust, "scripts", script));
}
const hashes = new Map();
for (const target of targets) {
for (const asset of [
`copilot-${target}.${target.startsWith("win32") ? "zip" : "tar.gz"}`,
`github-copilot-${version}-${target}.tgz`,
]) {
hashes.set(asset, createHash("sha256").update(asset).digest("hex"));
}
}
const sums = [...hashes].map(([asset, hash]) => `${hash.toUpperCase()} *${asset}`).join("\r\n");
writeFileSync(join(rust, "SHA256SUMS.txt"), `${sums}\r\n`);
// Capture the download boundary without contacting GitHub.
writeFileSync(join(root, "bin", "curl"), `#!/usr/bin/env bash
printf '%s\\n' "$@" > curl-args.txt
cat SHA256SUMS.txt
`, { mode: 0o755 });
return {
rust,
hashes,
run(script, args = []) {
const result = spawnSync("bash", [
"-c",
'export PATH="$PWD/../bin:$PATH"; exec bash "$@"',
"snapshot-test",
join("scripts", script),
...args,
], {
cwd: rust,
encoding: "utf8",
windowsHide: true,
timeout: 30_000,
});
assert.ifError(result.error);
return result;
},
};
}
function assertSnapshot(fixture, filename, version, runtime, releaseUrl) {
const contents = readFileSync(join(fixture.rust, filename), "utf8");
const entries = new Map(contents.split(/\r?\n/).filter((line) => line && !line.startsWith("#"))
.map((line) => line.split("=")));
assert.equal(entries.get("version"), version);
assert.equal(entries.get("release-url"), releaseUrl);
assert.equal(entries.size, targets.length + 1 + Number(Boolean(releaseUrl)));
for (const target of targets) {
const key = runtime ? `copilot-${target}` : `copilot-${target}.${target.startsWith("win32") ? "zip" : "tar.gz"}`;
const asset = runtime ? `github-copilot-${version}-${target}.tgz` : key;
assert.equal(entries.get(key), fixture.hashes.get(asset));
}
}
test("no-option snapshots keep the legacy exact version, URL, and hash format", (t) => {
const version = "1.2.3-4";
const f = fixture(t, version);
for (const [script, output, runtime] of scripts) {
const result = f.run(script);
assert.equal(result.status, 0, result.stderr);
assertSnapshot(f, output, version, runtime, undefined);
assert.match(
readFileSync(join(f.rust, "curl-args.txt"), "utf8"),
/https:\/\/github\.com\/github\/copilot-cli\/releases\/download\/v1\.2\.3-4\/SHA256SUMS\.txt/,
);
}
});
test("local checksum snapshots pin the final unstable URL without publication or sibling metadata", (t) => {
const version = "1.2.3-unstable.20260923";
const url = `https://github.com/github/copilot-sdk/releases/download/runtime-${version}`;
const f = fixture(t, version);
rmSync(join(f.rust, "..", "nodejs"), { recursive: true });
for (const [script, output, runtime] of scripts) {
const result = f.run(script, ["--version", version, "--release-url", url, "--checksums", "SHA256SUMS.txt"]);
assert.equal(result.status, 0, result.stderr);
assertSnapshot(f, output, version, runtime, url);
}
assert.throws(() => readFileSync(join(f.rust, "curl-args.txt")), { code: "ENOENT" });
});
test("URL-only override fetches checksums from the exact selected release", (t) => {
const version = "1.2.3-unstable.20260923";
const url = `https://github.com/github/copilot-sdk/releases/download/runtime-${version}`;
const f = fixture(t, version);
for (const [script, output, runtime] of scripts) {
const result = f.run(script, ["--release-url", url]);
assert.equal(result.status, 0, result.stderr);
assertSnapshot(f, output, version, runtime, url);
assert.equal(readFileSync(join(f.rust, "curl-args.txt"), "utf8").trim().split("\n").at(-1), `${url}/SHA256SUMS.txt`);
}
});
test("reviewed producers can stamp normal promotions without changing legacy product code", (t) => {
for (const version of ["1.2.3", "1.2.3-4"]) {
const f = fixture(t, version);
const product = join(f.rust, "..", "old-product", "rust");
mkdirSync(product, { recursive: true });
writeFileSync(join(product, "build.rs"), "// unchanged legacy build script\n");
rmSync(join(f.rust, "..", "nodejs"), { recursive: true });
for (const [script, output, runtime] of scripts) {
writeFileSync(join(f.rust, output), "reviewed snapshot must remain unchanged");
const result = f.run(script, [
"--version", version,
"--release-url", `https://github.com/github/copilot-cli/releases/download/v${version}`,
"--checksums", "SHA256SUMS.txt",
"--output", join(product, output),
]);
assert.equal(result.status, 0, result.stderr);
assertSnapshot({ ...f, rust: product }, output, version, runtime, undefined);
assert.equal(readFileSync(join(f.rust, output), "utf8"), "reviewed snapshot must remain unchanged");
}
assert.equal(readFileSync(join(product, "build.rs"), "utf8"), "// unchanged legacy build script\n");
assert.throws(() => readFileSync(join(product, "scripts", "snapshot-release.sh")), { code: "ENOENT" });
assert.throws(() => readFileSync(join(f.rust, "curl-args.txt")), { code: "ENOENT" });
}
});
test("invalid or mismatched release locations fail before acquisition", (t) => {
const f = fixture(t, "1.2.3");
for (const [script] of scripts) {
for (const url of [
"https://github.com/github/copilot-sdk/releases/download/runtime-9.9.9",
"https://github.com/github/copilot-cli/releases/latest",
"https://user:token@github.com/github/copilot-sdk/releases/download/runtime-1.2.3",
]) {
const result = f.run(script, ["--release-url", url]);
assert.notEqual(result.status, 0);
assert.match(result.stderr, /exact public/);
}
}
assert.throws(() => readFileSync(join(f.rust, "curl-args.txt")), { code: "ENOENT" });
});
test("missing, duplicate, and invalid hashes preserve an existing snapshot", (t) => {
const f = fixture(t, "1.2.3");
for (const [script, output, runtime] of scripts) {
const asset = runtime ? "github-copilot-1.2.3-darwin-arm64.tgz" : "copilot-darwin-arm64.tar.gz";
const hash = f.hashes.get(asset);
for (const contents of ["", `bad ${asset}\n`, `${hash} ${asset}\n${hash} ${asset}\n`]) {
writeFileSync(join(f.rust, output), "original snapshot");
writeFileSync(join(f.rust, "invalid-sums.txt"), contents);
const result = f.run(script, ["--checksums", "invalid-sums.txt"]);
assert.notEqual(result.status, 0);
assert.match(result.stderr, /one valid SHA-256/);
assert.equal(readFileSync(join(f.rust, output), "utf8"), "original snapshot");
}
}
});