Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeHook.test.ts

96 lines4.1 KB

1import { expect, it } from "vite-plus/test";
2import { patchNativeCli } from "./nativeHook.js";
3​
4it("wraps the native runner in a finally hook and patches only once", () => {
5 const original =
6 "\tconst exitCode = await run({\n\t\targs: rustCliArgs\n\t});\n\tprocess.exit(exitCode);";
7 const patched = patchNativeCli(original);
8 expect(patched).toContain("await startRunHook(command,");
9 expect(patched).toContain("finally {\n\t\tawait finishRunHook?.();");
10 expect(patched).toContain("process.exit(exitCode)");
11 expect(patchNativeCli(patched)).toBe(patched);
12});
13​
14it("wraps the Vite+ 1.8 runner that also returns a dev command", () => {
15 const patched = patchNativeCli(
16 "\tconst { exitCode, devCommand } = await run({\n\t\targs: rustCliArgs\n\t});\n\tif (devCommand) await runInProcess(devCommand);",
17 );
18 expect(patched).toContain(
19 "({ exitCode, devCommand } = await run({\n\t\targs: rustCliArgs\n\t}));",
20 );
21 expect(patched).toContain("finally {\n\t\tawait finishRunHook?.();");
22});
23​
24it("rejects an unknown native entrypoint before changing it", () => {
25 expect(() => patchNativeCli("changed upstream entrypoint")).toThrow(
26 "Unsupported Vite+ entrypoint",
27 );
28});
29​
30it("leaves an unsupported CLI runnable when the machine preload is enabled", async () => {
31 const fs = await import("node:fs/promises");
32 const os = await import("node:os");
33 const path = await import("node:path");
34 const { promisify } = await import("node:util");
35 const { execFile } = await import("node:child_process");
36 const root = await fs.mkdtemp(path.join(os.tmpdir(), "cache-preload-"));
37 try {
38 const directory = path.join(root, "vite-plus/dist");
39 await fs.mkdir(directory, { recursive: true });
40 await fs.writeFile(path.join(root, "package.json"), '{"type":"module"}');
41 await fs.writeFile(path.join(directory, "bin.js"), 'console.log("unmodified CLI");');
42 const result = await promisify(execFile)(
43 process.execPath,
44 ["--import", new URL("./preload.mjs", import.meta.url).href, path.join(directory, "bin.js")],
45 { env: { ...process.env, NODE_OPTIONS: "" } },
46 );
47 expect(result.stdout.trim()).toBe("unmodified CLI");
48 expect(result.stderr).toBe("");
49 await fs.mkdir(path.join(root, "vite-plus/binding"));
50 await fs.writeFile(path.join(root, "vite-plus/binding/index.js"), "export {};");
51 await fs.writeFile(
52 path.join(directory, "bin.js"),
53 'const command="--version"; const rustCliArgs=[]; const run=async()=>0;\n\tconst exitCode = await run({\n\t\targs: rustCliArgs\n\t});\nconsole.log("compatible CLI");',
54 );
55 const compatible = await promisify(execFile)(
56 process.execPath,
57 ["--import", new URL("./preload.mjs", import.meta.url).href, path.join(directory, "bin.js")],
58 { env: { ...process.env, NODE_OPTIONS: "" } },
59 );
60 expect(compatible.stdout.trim()).toBe("compatible CLI");
61 } finally {
62 await fs.rm(root, { recursive: true, force: true });
63 }
64});
65​
66it("does not execute hook modules supplied by ancestor package manifests", async () => {
67 const fs = await import("node:fs/promises");
68 const os = await import("node:os");
69 const path = await import("node:path");
70 const { promisify } = await import("node:util");
71 const { execFile } = await import("node:child_process");
72 const root = await fs.mkdtemp(path.join(os.tmpdir(), "cache-ancestor-"));
73 try {
74 await fs.mkdir(path.join(root, "project"));
75 await fs.writeFile(
76 path.join(root, "package.json"),
77 JSON.stringify({ vitePlus: { runHook: "./malicious.mjs" } }),
78 );
79 await fs.writeFile(
80 path.join(root, "malicious.mjs"),
81 'throw new Error("ancestor module executed")',
82 );
83 const result = await promisify(execFile)(
84 process.execPath,
85 [
86 "--input-type=module",
87 "-e",
88 `import { startRunHook } from ${JSON.stringify(new URL("./nativeRunHook.js", import.meta.url).href)}; await startRunHook("run", ${JSON.stringify(path.join(root, "project"))});`,
89 ],
90 { env: { ...process.env, HOME: root, NODE_OPTIONS: "" } },
91 );
92 expect(result.stderr).toBe("");
93 } finally {
94 await fs.rm(root, { recursive: true, force: true });
95 }
96});