-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathbuild-prerequisites.test.mjs
More file actions
373 lines (338 loc) · 14.6 KB
/
Copy pathbuild-prerequisites.test.mjs
File metadata and controls
373 lines (338 loc) · 14.6 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import assert from "node:assert/strict";
import childProcess from "node:child_process";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import { syncBuiltinESMExports } from "node:module";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
archiveExtractionInvocation,
bazelActionEnvironmentArgument,
bazelInvocationEnvironment,
installCodegenDependencies,
npmInvocation,
parseBazelInfoPath,
syncGeneratedArchive,
syncSchemas,
} from "./build-prerequisites.mjs";
test("extracts archives by basename so Windows drive letters are not parsed as remote hosts", () => {
assert.deepEqual(
archiveExtractionInvocation("C:\\build\\nodejs.tar", "C:\\temp\\generated", "win32"),
{
args: ["-xf", "nodejs.tar", "-C", "C:\\temp\\generated"],
cwd: "C:\\build",
},
);
assert.deepEqual(archiveExtractionInvocation("/build/nodejs.tar", "/tmp/generated", "linux"), {
args: ["-xf", "nodejs.tar", "-C", "/tmp/generated"],
cwd: "/build",
});
});
test("passes Bazel's normalized tool PATH through the invocation environment", () => {
const systemBin = path.join(path.sep, "tools", "bin");
const environment = {
PATH: [
path.join(path.sep, "workspace", "src", "sdk", "node_modules", ".bin"),
systemBin,
path.join(path.sep, "workspace", "node_modules", ".bin"),
systemBin,
path.join(path.sep, "npm", "node-gyp-bin"),
].join(path.delimiter),
};
assert.equal(bazelInvocationEnvironment(environment).PATH, systemBin);
assert.equal(bazelActionEnvironmentArgument(), "--action_env=PATH");
});
test("uses only the final Bazel info path when preceding output is noisy", () => {
const bazelBin = path.resolve("bazel-bin");
assert.equal(
parseBazelInfoPath(`Verifying lockfile...\nDependencies ready\n${bazelBin}\n`),
bazelBin,
);
});
test("rejects Bazel info output without an absolute path", () => {
assert.throws(
() => parseBazelInfoPath("Verifying lockfile...\nrelative/bazel-bin\n"),
/Unable to resolve Bazel output path/,
);
});
test("runs npm through the Windows command interpreter", () => {
assert.deepEqual(npmInvocation("win32", { ComSpec: "C:\\Windows\\System32\\cmd.exe" }), {
command: "C:\\Windows\\System32\\cmd.exe",
args: ["/d", "/s", "/c", "npm.cmd"],
});
});
test("installs shared codegen dependencies once and Java dependencies only when selected", (t) => {
const sdkRoot = fs.mkdtempSync(path.join(import.meta.dirname, ".codegen-dependencies-"));
t.after(() => fs.rmSync(sdkRoot, { recursive: true, force: true }));
for (const directory of ["scripts/codegen", "java/scripts/codegen"]) {
writeFile(path.join(sdkRoot, directory, "package.json"), "{}");
writeFile(path.join(sdkRoot, directory, "package-lock.json"), "{}");
}
const calls = [];
const spawn = t.mock.method(childProcess, "spawnSync", (command, args, { cwd }) => {
calls.push({ command, args, cwd });
return { status: 0 };
});
syncBuiltinESMExports();
t.after(() => {
spawn.mock.restore();
syncBuiltinESMExports();
});
const invocation = npmInvocation();
const shared = path.join(sdkRoot, "scripts/codegen");
const java = path.join(sdkRoot, "java/scripts/codegen");
installCodegenDependencies(["nodejs", "rust"], sdkRoot);
installCodegenDependencies(["nodejs"], sdkRoot);
assert.deepEqual(calls, [
{
command: invocation.command,
args: [...invocation.args, "ci", "--ignore-scripts", "--include=dev"],
cwd: shared,
},
]);
assert.equal(fs.existsSync(path.join(java, "node_modules")), false);
installCodegenDependencies(["java"], sdkRoot);
installCodegenDependencies(["java"], sdkRoot);
assert.equal(calls.length, 2);
assert.deepEqual(calls[1], {
command: invocation.command,
args: [...invocation.args, "ci", "--ignore-scripts", "--include=dev"],
cwd: java,
});
});
test("syncs generated projections once without rewriting unchanged files", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
const stagedRoot = path.join(root, "staged");
const generatedPath = "nodejs/src/generated/rpc.ts";
writeFile(path.join(stagedRoot, generatedPath), "generated\n");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", stagedRoot, "."], root);
assert.equal(
syncGeneratedArchive({
archivePath,
generatedRoots: [generatedPath],
language: "nodejs",
runtimeRoot: root,
sdkRoot,
}),
true,
);
const destination = path.join(sdkRoot, generatedPath);
const firstModifiedTime = fs.statSync(destination).mtimeMs;
assert.equal(
syncGeneratedArchive({
archivePath,
generatedRoots: [generatedPath],
language: "nodejs",
runtimeRoot: root,
sdkRoot,
}),
false,
);
assert.equal(fs.statSync(destination).mtimeMs, firstModifiedTime);
});
test("refuses to overwrite modified generated projections", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
const generatedPath = "nodejs/src/generated/rpc.ts";
writeFile(path.join(sdkRoot, generatedPath), "tracked\n");
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
fs.writeFileSync(path.join(sdkRoot, generatedPath), "local edit\n");
const stagedRoot = path.join(root, "staged");
writeFile(path.join(stagedRoot, generatedPath), "new generated output\n");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", stagedRoot, "."], root);
assert.throws(
() =>
syncGeneratedArchive({
archivePath,
generatedRoots: [generatedPath],
language: "nodejs",
runtimeRoot: root,
sdkRoot,
}),
/Refusing to overwrite modified generated SDK outputs/,
);
assert.equal(fs.readFileSync(path.join(sdkRoot, generatedPath), "utf8"), "local edit\n");
});
test("rejects empty generator archives without deleting existing projections", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
const generatedPath = "nodejs/src/generated/rpc.ts";
writeFile(path.join(sdkRoot, generatedPath), "tracked\n");
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
const stagedRoot = path.join(root, "staged");
fs.mkdirSync(stagedRoot);
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", stagedRoot, "."], root);
assert.throws(
() =>
syncGeneratedArchive({
archivePath,
generatedRoots: ["nodejs/src/generated"],
language: "nodejs",
runtimeRoot: root,
sdkRoot,
}),
/No nodejs generated outputs/,
);
assert.equal(fs.readFileSync(path.join(sdkRoot, generatedPath), "utf8"), "tracked\n");
});
test("replaces prior generated projections while preserving manual edits", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
const generatedPath = "nodejs/src/generated/rpc.ts";
const destination = path.join(sdkRoot, generatedPath);
writeFile(destination, "committed\n");
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
const previousRoot = path.join(root, "previous");
writeFile(path.join(previousRoot, generatedPath), "previous generated output\n");
const previousArchivePath = path.join(root, "previous.tar");
run(tarCommand(), ["-cf", previousArchivePath, "-C", previousRoot, "."], root);
fs.copyFileSync(path.join(previousRoot, generatedPath), destination);
const stagedRoot = path.join(root, "staged");
writeFile(path.join(stagedRoot, generatedPath), "new generated output\n");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", stagedRoot, "."], root);
assert.equal(
syncGeneratedArchive({
archivePath,
generatedRoots: [generatedPath],
language: "nodejs",
previousArchivePath,
runtimeRoot: root,
sdkRoot,
}),
true,
);
assert.equal(fs.readFileSync(destination, "utf8"), "new generated output\n");
});
test("syncs Bazel schemas without rewriting equal tracked files", (t) => {
const root = createGitFixture(t);
const schemaOutputDirectory = path.join(root, "bazel-schemas");
for (const fileName of ["api.schema.json", "session-events.schema.json"]) {
writeFile(path.join(schemaOutputDirectory, fileName), `{"title":"${fileName}"}`);
}
assert.equal(syncSchemas({ runtimeRoot: root, schemaOutputDirectory }), true);
const apiPath = path.join(root, "generated/api.schema.json");
const firstModifiedTime = fs.statSync(apiPath).mtimeMs;
assert.equal(syncSchemas({ runtimeRoot: root, schemaOutputDirectory }), false);
assert.equal(fs.statSync(apiPath).mtimeMs, firstModifiedTime);
});
test("replaces schemas that match the prior Bazel output", (t) => {
const root = createGitFixture(t);
for (const fileName of ["api.schema.json", "session-events.schema.json"]) {
writeFile(path.join(root, "generated", fileName), `{"title":"committed ${fileName}"}`);
}
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
const previousSchemaDirectory = path.join(root, "previous-schemas");
const schemaOutputDirectory = path.join(root, "bazel-schemas");
for (const fileName of ["api.schema.json", "session-events.schema.json"]) {
writeFile(path.join(previousSchemaDirectory, fileName), `{"title":"previous ${fileName}"}`);
fs.copyFileSync(
path.join(previousSchemaDirectory, fileName),
path.join(root, "generated", fileName),
);
writeFile(path.join(schemaOutputDirectory, fileName), `{"title":"new ${fileName}"}`);
}
assert.equal(
syncSchemas({ previousSchemaDirectory, runtimeRoot: root, schemaOutputDirectory }),
true,
);
assert.equal(
fs.readFileSync(path.join(root, "generated/api.schema.json"), "utf8"),
'{"title":"new api.schema.json"}',
);
});
test("syncs new and obsolete Go outputs without changing handwritten neighbors", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
writeFile(path.join(sdkRoot, "go/rpc/zobsolete.go"), "obsolete");
writeFile(path.join(sdkRoot, "go/rpc/handwritten.go"), "handwritten");
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
const staged = path.join(root, "staged");
writeFile(path.join(staged, "go/rpc/znew.go"), "new");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", staged, "."], root);
syncGeneratedArchive({
archivePath,
generatedRoots: ["go/z*.go", "go/rpc/z*.go"],
language: "go",
runtimeRoot: root,
sdkRoot,
});
assert.equal(fs.existsSync(path.join(sdkRoot, "go/rpc/zobsolete.go")), false);
assert.equal(fs.readFileSync(path.join(sdkRoot, "go/rpc/znew.go"), "utf8"), "new");
assert.equal(fs.readFileSync(path.join(sdkRoot, "go/rpc/handwritten.go"), "utf8"), "handwritten");
});
test("preserves an edited Python initializer on a first generation without a cached archive", (t) => {
const root = createGitFixture(t);
const sdkRoot = path.join(root, "src/sdk");
const initializer = "python/copilot/generated/__init__.py";
writeFile(path.join(sdkRoot, initializer), "handwritten");
writeFile(path.join(sdkRoot, "python/copilot/generated/obsolete.py"), "obsolete");
run("git", ["add", "."], root);
run("git", ["commit", "-m", "fixture"], root);
writeFile(path.join(sdkRoot, initializer), "local handwritten edit");
const staged = path.join(root, "staged");
writeFile(path.join(staged, "python/copilot/generated/rpc.py"), "generated");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", staged, "."], root);
syncGeneratedArchive({
archivePath,
generatedRoots: ["python/copilot/generated"],
preservedFiles: [initializer],
language: "python",
runtimeRoot: root,
sdkRoot,
});
assert.equal(fs.readFileSync(path.join(sdkRoot, initializer), "utf8"), "local handwritten edit");
assert.equal(fs.existsSync(path.join(sdkRoot, "python/copilot/generated/obsolete.py")), false);
});
test("rejects archive outputs outside the declared generation roots", (t) => {
const root = createGitFixture(t);
const staged = path.join(root, "staged");
writeFile(path.join(staged, "nodejs/src/new-output.ts"), "generated");
const archivePath = path.join(root, "projection.tar");
run(tarCommand(), ["-cf", archivePath, "-C", staged, "."], root);
assert.throws(
() =>
syncGeneratedArchive({
archivePath,
generatedRoots: ["nodejs/src/generated"],
language: "nodejs",
runtimeRoot: root,
sdkRoot: path.join(root, "src/sdk"),
}),
/Undeclared nodejs generated output: nodejs\/src\/new-output.ts/,
);
});
function createGitFixture(t) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-sdk-build-prerequisites-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
run("git", ["init", "--quiet"], root);
run("git", ["config", "user.name", "SDK Test"], root);
run("git", ["config", "user.email", "sdk-test@example.com"], root);
return root;
}
function writeFile(filePath, contents) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, contents);
}
function run(command, args, cwd) {
const result = spawnSync(command, args, { cwd, encoding: "utf8" });
assert.equal(result.status, 0, result.stderr);
}
function tarCommand() {
return process.platform === "win32" ? "tar.exe" : "tar";
}