Repository navigation
Expand file tree
/
Copy pathparity.test.ts
More file actions
320 lines (290 loc) · 13.5 KB
/
Copy pathparity.test.ts
File metadata and controls
320 lines (290 loc) · 13.5 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
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import { configureAuth } from "../ci/auth.js";
import { installVitePlus } from "../ci/install-viteplus.js";
import { setupSfw, SFW_VERSION } from "../ci/install-sfw.js";
import { parseRunInstall } from "../ci/run-install.js";
import { runPrepare, runFinalize } from "./index.js";
import type { AzurePorts } from "./index.js";
const directories: string[] = [];
afterEach(() => {
for (const dir of directories.splice(0)) rmSync(dir, { recursive: true, force: true });
});
function fixture() {
const root = mkdtempSync(path.join(tmpdir(), "setup-vp-azure-parity-"));
directories.push(root);
const project = path.join(root, "app");
mkdirSync(project);
const env = { SYSTEM_DEFAULTWORKINGDIRECTORY: root, SETUP_VP_WORKING_DIRECTORY: "app" };
const ports = {
installVitePlus: vi.fn<typeof installVitePlus>(async () => {}),
prepareCacheMetadata: vi.fn(() => ({ ready: false })),
configureAuth: vi.fn(configureAuth),
setupSfw: vi.fn<typeof setupSfw>().mockResolvedValue({ executable: "vp", sfw: false }),
parseRunInstall,
runInstall: vi.fn(),
getCommandOutput: vi.fn((_command: string, args: string[]): string | undefined =>
args[0] === "env" ? "{}" : "vp v0.3.1",
),
run: vi.fn(),
parseInstalledVpVersion: vi.fn(() => "0.3.1"),
prependPath: vi.fn(),
setVariable: vi.fn(),
logWarning: vi.fn(),
logInfo: vi.fn(),
} satisfies AzurePorts;
return { root, project, env, ports };
}
describe("Azure parity", () => {
it("exports the installer PATH and passes project Node discovery without a session override", async () => {
const { root, project, env, ports } = fixture();
writeFileSync(path.join(project, ".node-version"), "22.18.0");
const bin = path.join(root, "bin");
const data = path.join(root, "data");
const fallbackBin = path.join(data, "fallback-bin");
mkdirSync(fallbackBin, { recursive: true });
ports.installVitePlus.mockImplementation((version, options) =>
installVitePlus(version, {
...options,
runInstall: (_url, installEnv) => {
expect(installEnv.VP_SETUP_NODE_VERSION).toBe("22.18.0");
writeFileSync(installEnv.VP_SETUP_NODE_VERSION_FILE!, "22.18.0");
writeFileSync(
installEnv.SETUP_VP_DIRS_FILE!,
`data\t${data}\nbin\t${bin}\ncache\t/cache\nconfig\t/config\nstate\t/state\n`,
);
return 0;
},
}),
);
await runPrepare({ ...env, PATH: "/system/bin", SETUP_VP_VERSION: "latest" }, ports);
expect(ports.prependPath).toHaveBeenCalledWith(bin);
expect(ports.run).not.toHaveBeenCalledWith("vp", ["env", "use", "22.18.0"], expect.anything());
expect(ports.setVariable).toHaveBeenCalledWith(
"PATH",
[bin, "/system/bin", fallbackBin].join(path.delimiter),
);
});
it("resolves a monorepo catalog pin and selects Node from a file in workingDirectory", async () => {
const { root, project, env, ports } = fixture();
writeFileSync(
path.join(project, "package.json"),
JSON.stringify({ devDependencies: { "vite-plus": "catalog:" } }),
);
writeFileSync(path.join(root, "pnpm-workspace.yaml"), "catalog:\n vite-plus: 0.3.1\n");
writeFileSync(path.join(project, ".tool-versions"), "nodejs 22.15.0\n");
await runPrepare({ ...env, SETUP_VP_NODE_VERSION_FILE: ".tool-versions" }, ports);
expect(ports.installVitePlus).toHaveBeenCalledWith("0.3.1", expect.any(Object));
expect(ports.run).toHaveBeenCalledWith("vp", ["env", "use", "22.15.0"], { cwd: project });
});
it("supports explicit version files and rejects conflicting Node manager settings", async () => {
const { project, env, ports } = fixture();
writeFileSync(
path.join(project, "package.json"),
JSON.stringify({ devDependencies: { "vite-plus": "0.3.1" } }),
);
writeFileSync(path.join(project, "pnpm-workspace.yaml"), "catalog:\n vite-plus: 0.3.2\n");
await runPrepare({ ...env, SETUP_VP_VERSION_FILE: "pnpm-workspace.yaml" }, ports);
expect(ports.installVitePlus).toHaveBeenCalledWith("0.3.2", expect.any(Object));
ports.installVitePlus.mockClear();
await expect(
runPrepare({ ...env, SETUP_VP_NODE_VERSION: "22", SETUP_VP_NODE_MANAGER: "false" }, ports),
).rejects.toThrow("cannot be used with node-manager");
expect(ports.installVitePlus).not.toHaveBeenCalled();
});
it("emits named outputs and preserves secret status for project auth references", async () => {
const { project, env, ports } = fixture();
writeFileSync(path.join(project, ".npmrc"), "//registry.example/:_authToken=${CUSTOM_TOKEN}");
await runFinalize({ ...env, CUSTOM_TOKEN: "secret", SETUP_VP_CACHE_HIT: "inexact" }, ports);
expect(ports.setVariable).toHaveBeenCalledWith("CUSTOM_TOKEN", "secret", { isSecret: true });
expect(ports.setVariable).toHaveBeenCalledWith("version", "0.3.1", { isOutput: true });
expect(ports.setVariable).toHaveBeenCalledWith("cacheHit", "false", { isOutput: true });
expect(ports.getCommandOutput).toHaveBeenCalledWith("vp", ["--version"], { cwd: project });
});
it("emits Node.js and package-manager versions from vp env current", async () => {
const { project, env, ports } = fixture();
ports.getCommandOutput.mockImplementation((_command, args) =>
args[0] === "env"
? JSON.stringify({
node: { version: "22.15.0", source: "session" },
package_manager: { name: "yarn", version: "4.9.1", source: "packageManager" },
})
: "vp v0.3.1",
);
await runFinalize(env, ports);
const envCurrent = ports.getCommandOutput.mock.calls.findIndex(([, args]) => args[0] === "env");
expect(ports.getCommandOutput.mock.calls[envCurrent]).toEqual([
"vp",
["env", "current", "--json"],
{ cwd: project },
]);
expect(ports.runInstall.mock.invocationCallOrder[0]).toBeLessThan(
ports.getCommandOutput.mock.invocationCallOrder[envCurrent]!,
);
expect(ports.setVariable).toHaveBeenCalledWith("SETUP_VP_INSTALLED_NODE_VERSION", "22.15.0");
expect(ports.setVariable).toHaveBeenCalledWith("SETUP_VP_PACKAGE_MANAGER_NAME", "yarn");
expect(ports.setVariable).toHaveBeenCalledWith("SETUP_VP_PACKAGE_MANAGER_VERSION", "4.9.1");
expect(ports.setVariable).toHaveBeenCalledWith("nodeVersion", "22.15.0", { isOutput: true });
expect(ports.setVariable).toHaveBeenCalledWith("packageManager", "yarn", { isOutput: true });
expect(ports.setVariable).toHaveBeenCalledWith("packageManagerVersion", "4.9.1", {
isOutput: true,
});
});
it("warns and reports unknown versions when vp env current fails", async () => {
const { env, ports } = fixture();
ports.getCommandOutput.mockImplementation((_command, args) =>
args[0] === "env" ? undefined : "vp v0.3.1",
);
await runFinalize(env, ports);
expect(ports.logWarning).toHaveBeenCalledExactlyOnceWith(
expect.stringContaining("Could not read the Node.js and package manager versions"),
);
expect(ports.setVariable).toHaveBeenCalledWith("nodeVersion", "unknown", { isOutput: true });
});
it("reports unknown Node.js and package-manager versions on Vite+ before 0.3.1", async () => {
const { env, ports } = fixture();
ports.getCommandOutput.mockReturnValue("vp v0.3.0");
await runFinalize(env, ports);
expect(ports.getCommandOutput).not.toHaveBeenCalledWith(
"vp",
["env", "current", "--json"],
expect.anything(),
);
expect(ports.setVariable).toHaveBeenCalledWith("SETUP_VP_INSTALLED_NODE_VERSION", "unknown");
expect(ports.setVariable).toHaveBeenCalledWith("nodeVersion", "unknown", { isOutput: true });
expect(ports.setVariable).toHaveBeenCalledWith("packageManager", "unknown", {
isOutput: true,
});
expect(ports.setVariable).toHaveBeenCalledWith("packageManagerVersion", "unknown", {
isOutput: true,
});
});
it("does not treat an undefined Azure secret macro as an auth token", async () => {
const { project, env, ports } = fixture();
writeFileSync(path.join(project, ".npmrc"), "registry=https://registry.example/");
const target = { ...env, NODE_AUTH_TOKEN: "$(NODE_AUTH_TOKEN)" };
await runFinalize(target, ports);
expect(target.NODE_AUTH_TOKEN).toBeUndefined();
expect(ports.setVariable).not.toHaveBeenCalledWith(
"NPM_CONFIG_USERCONFIG",
expect.anything(),
expect.anything(),
);
});
it("does not attach NODE_AUTH_TOKEN to repository-controlled registries", async () => {
const { project, env, ports } = fixture();
writeFileSync(
path.join(project, ".npmrc"),
"registry=https://attacker.invalid/\n@scope:registry=https://other.invalid/npm/\n",
);
const target: NodeJS.ProcessEnv = { ...env, NODE_AUTH_TOKEN: "secret" };
await runFinalize(target, ports);
if (target.NPM_CONFIG_USERCONFIG) directories.push(path.dirname(target.NPM_CONFIG_USERCONFIG));
expect(target.NPM_CONFIG_USERCONFIG).toBeUndefined();
expect(target.PNPM_CONFIG_USERCONFIG).toBeUndefined();
expect(ports.setVariable.mock.calls.some(([name]) => name.endsWith("CONFIG_USERCONFIG"))).toBe(
false,
);
expect(ports.runInstall).toHaveBeenCalledWith(
expect.anything(),
project,
{ executable: "vp", sfw: false },
target,
);
});
it("passes the selected sfw executable to the install runner", async () => {
const { project, env, ports } = fixture();
const command = { executable: path.join(project, "trusted", "sfw"), sfw: true };
ports.setupSfw.mockResolvedValue(command);
const target = { ...env, SETUP_VP_SFW: "true" };
await runFinalize(target, ports);
expect(ports.runInstall).toHaveBeenCalledWith([{}], project, command, target);
});
it("prepares a version/platform-specific sfw cache only when an install will run", async () => {
const { env, ports } = fixture();
await runPrepare({ ...env, SETUP_VP_SFW: "true", SETUP_VP_RUN_INSTALL: "false" }, ports);
expect(ports.setVariable).not.toHaveBeenCalledWith("SETUP_VP_SFW_READY", "true");
await runPrepare({ ...env, SETUP_VP_SFW: "true" }, ports);
expect(ports.setVariable).toHaveBeenCalledWith("SETUP_VP_SFW_READY", "true");
expect(ports.setVariable).toHaveBeenCalledWith(
"SETUP_VP_SFW_CACHE_KEY",
expect.stringMatching(new RegExp(`^${SFW_VERSION.replaceAll(".", "\\.")}-sfw-free-`)),
);
});
it.each(["explicit", "package.json", "version-file"])(
"disables sfw and its cache for a preview resolved from %s",
async (source) => {
const { project, env, ports } = fixture();
const version = "0.0.0-commit.7d848b3da1987fa60b4cf18487fcc36a2a697e94";
const target: NodeJS.ProcessEnv = { ...env, SETUP_VP_SFW: "true" };
if (source === "explicit") {
target.SETUP_VP_VERSION = version;
} else if (source === "version-file") {
writeFileSync(
path.join(project, "pnpm-workspace.yaml"),
`catalog:\n vite-plus: ${version}\n`,
);
target.SETUP_VP_VERSION_FILE = "pnpm-workspace.yaml";
} else {
writeFileSync(
path.join(project, "package.json"),
JSON.stringify({ devDependencies: { "vite-plus": version } }),
);
}
// Azure carries task.setvariable values into the following task's environment.
ports.setVariable.mockImplementation((name, value) => {
target[name] = value;
});
ports.setupSfw.mockImplementation(setupSfw);
await runPrepare(target, ports);
expect(ports.installVitePlus).toHaveBeenCalledWith(version, expect.any(Object));
expect(target.SETUP_VP_SFW_READY).toBe("false");
expect(target.SETUP_VP_SFW_CACHE_DIR).toBeUndefined();
expect(target.SETUP_VP_RESOLVED_VERSION).toBe(version);
// Finalize has the original sfw input but does not receive version-file from the template.
delete target.SETUP_VP_VERSION_FILE;
await runFinalize(target, ports);
expect(ports.runInstall).toHaveBeenCalledWith(
[{}],
project,
{ executable: "vp", sfw: false },
target,
);
expect(ports.logWarning).toHaveBeenCalledExactlyOnceWith(
expect.stringContaining(`automatically disabled for Vite+ preview build ${version}`),
);
},
);
it.each(["$(CUSTOM_TOKEN)", "$(MISSING_SECRET)"])(
"removes an unresolved custom auth macro %s before install",
async (value) => {
const { project, env, ports } = fixture();
writeFileSync(
path.join(project, ".npmrc"),
"//registry.example/:_authToken=${CUSTOM_TOKEN}\n",
);
const target = { ...env, CUSTOM_TOKEN: value, UNRELATED_VALUE: "$(keep-me)" };
await runFinalize(target, ports);
expect(target.CUSTOM_TOKEN).toBeUndefined();
expect(target.UNRELATED_VALUE).toBe("$(keep-me)");
expect(ports.setVariable.mock.calls.some(([name]) => name === "CUSTOM_TOKEN")).toBe(false);
expect(ports.runInstall).toHaveBeenCalledWith(
expect.anything(),
project,
{ executable: "vp", sfw: false },
target,
);
},
);
it.each([undefined, ""])(
"fails the final version check without publishing success outputs (%s)",
async (output) => {
const { env, ports } = fixture();
ports.getCommandOutput.mockReturnValue(output);
await expect(runFinalize(env, ports)).rejects.toThrow("Failed to verify Vite+ installation");
expect(ports.setVariable).not.toHaveBeenCalled();
},
);
});