Repository navigation
Expand file tree
/
Copy pathbuild-prerequisites.test.mjs
More file actions
235 lines (208 loc) · 8.83 KB
/
Copy pathbuild-prerequisites.test.mjs
File metadata and controls
235 lines (208 loc) · 8.83 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
archiveExtractionInvocation,
bazelActionEnvironmentArgument,
bazelInvocationEnvironment,
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("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("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"}',
);
});
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";
}