@@ -8,166 +8,6 @@ import * as NodePath from "node:path"; |
8 | 8 | import { expect, it, vi } from "vite-plus/test"; |
9 | 9 | import startRunHook from "./runHook.ts"; |
10 | 10 |
|
11 | | -it("uses one machine hook across fresh worktrees without modifying Vite+", async () => { |
12 | | - const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-hook-")); |
13 | | - const unrelated = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "unrelated-project-")); |
14 | | - const reports: { authorization: string | undefined; body: string }[] = []; |
15 | | - const server = NodeHttp.createServer(async (request, response) => { |
16 | | - if (!request.url?.endsWith("/telemetry")) { |
17 | | - response.writeHead(404).end(); |
18 | | - return; |
19 | | - } |
20 | | - let body = ""; |
21 | | - for await (const chunk of request) body += chunk; |
22 | | - reports.push({ authorization: request.headers.authorization, body }); |
23 | | - response.writeHead(204).end(); |
24 | | - }); |
25 | | - try { |
26 | | - await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
27 | | - const address = server.address(); |
28 | | - if (!address || typeof address === "string") throw new Error("Missing HTTP address"); |
29 | | - const runner = NodePath.join(root, "vite-plus"); |
30 | | - const distribution = await NodeFSP.realpath( |
31 | | - NodePath.resolve(import.meta.dirname, "../../../node_modules/vite-plus/dist"), |
32 | | - ); |
33 | | - await NodeFSP.cp(distribution, NodePath.join(runner, "dist"), { recursive: true }); |
34 | | - await NodeFSP.copyFile( |
35 | | - NodePath.join(distribution, "../package.json"), |
36 | | - NodePath.join(runner, "package.json"), |
37 | | - ); |
38 | | - await NodeFSP.symlink( |
39 | | - NodePath.resolve(distribution, "../binding"), |
40 | | - NodePath.join(runner, "binding"), |
41 | | - ); |
42 | | - await NodeFSP.symlink( |
43 | | - NodePath.resolve(distribution, "../.."), |
44 | | - NodePath.join(runner, "node_modules"), |
45 | | - ); |
46 | | - const entrypoint = NodePath.join(runner, "dist/bin.js"); |
47 | | - await NodeFSP.writeFile( |
48 | | - NodePath.join(unrelated, "package.json"), |
49 | | - JSON.stringify({ name: "unrelated", scripts: { build: 'node -e "console.log(7)"' } }), |
50 | | - ); |
51 | | - const nested = NodePath.join(root, "nested"); |
52 | | - await NodeFSP.mkdir(nested); |
53 | | - await NodeFSP.writeFile( |
54 | | - NodePath.join(nested, "package.json"), |
55 | | - JSON.stringify({ name: "nested", scripts: { build: 'node -e "console.log(8)"' } }), |
56 | | - ); |
57 | | - await NodeFSP.writeFile(NodePath.join(nested, "pnpm-workspace.yaml"), "packages: []\n"); |
58 | | - const endpoint = `http://127.0.0.1:${address.port}/projects/test`; |
59 | | - await NodeFSP.writeFile(NodePath.join(root, ".task-cache.json"), JSON.stringify({ endpoint })); |
60 | | - await NodeFSP.writeFile( |
61 | | - NodePath.join(root, "package.json"), |
62 | | - JSON.stringify({ |
63 | | - name: "hook-fixture", |
64 | | - private: true, |
65 | | - scripts: { |
66 | | - build: `node build.cjs ${JSON.stringify(entrypoint)} ${JSON.stringify(unrelated)}`, |
67 | | - fail: 'node -e "process.exit(7)"', |
68 | | - nested: `node ${JSON.stringify(entrypoint)} -C ${JSON.stringify(nested)} run --no-cache build`, |
69 | | - }, |
70 | | - }), |
71 | | - ); |
72 | | - await NodeFSP.writeFile( |
73 | | - NodePath.join(root, "build.cjs"), |
74 | | - 'require("node:fs").writeFileSync("environment.json", JSON.stringify({url: process.env.VP_REMOTE_CACHE_URL ?? null, mode: process.env.VP_REMOTE_CACHE ?? null})); console.log(42); try {for (const cwd of process.argv.slice(3)) require("node:child_process").execFileSync(process.execPath, [process.argv[2], "run", "--no-cache", "build"], {cwd, encoding:"utf8"})} catch (error) { throw new Error(error.stdout + error.stderr) }', |
75 | | - ); |
76 | | - await NodeFSP.writeFile( |
77 | | - NodePath.join(root, "vite.config.ts"), |
78 | | - `import { remoteCacheUrl } from ${JSON.stringify(new URL("./remoteCache.ts", import.meta.url).href)}; export default {run:{cache:{scripts:true,remote:{url:remoteCacheUrl(import.meta.dirname)}}}};`, |
79 | | - ); |
80 | | - await NodeFSP.mkdir(NodePath.join(root, ".config/t3-task-cache"), { recursive: true }); |
81 | | - await NodeFSP.writeFile( |
82 | | - NodePath.join(root, ".config/t3-task-cache/credentials.json"), |
83 | | - JSON.stringify({ [endpoint]: { name: "maria", token: "secret" } }), |
84 | | - ); |
85 | | - const configDirectory = NodePath.join(root, ".config/vite-plus"); |
86 | | - await NodeFSP.mkdir(configDirectory, { recursive: true }); |
87 | | - await NodeFSP.writeFile( |
88 | | - NodePath.join(configDirectory, "run-hook.json"), |
89 | | - JSON.stringify({ |
90 | | - rootMarker: ".task-cache.json", |
91 | | - module: NodePath.join(import.meta.dirname, "runHook.ts"), |
92 | | - }), |
93 | | - ); |
94 | | - const originalEntrypoint = await NodeFSP.readFile(entrypoint, "utf8"); |
95 | | - const run = (task: string, cwd = root, cache = "--cache") => |
96 | | - new Promise<number | null>((resolve, reject) => { |
97 | | - const child = NodeChildProcess.spawn( |
98 | | - process.execPath, |
99 | | - [entrypoint, "run", ...(task === "--last-details" ? [] : [cache]), task], |
100 | | - { |
101 | | - cwd, |
102 | | - env: { |
103 | | - ...process.env, |
104 | | - HOME: root, |
105 | | - // Flush inline so no detached flusher can write under root while it is removed. |
106 | | - GITHUB_ACTIONS: "true", |
107 | | - T3_TASK_CACHE_TELEMETRY: undefined, |
108 | | - T3_TASK_CACHE_HOST: undefined, |
109 | | - VITE_CACHE_PATH: undefined, |
110 | | - ACTIONS_ID_TOKEN_REQUEST_URL: undefined, |
111 | | - ACTIONS_ID_TOKEN_REQUEST_TOKEN: undefined, |
112 | | - NODE_OPTIONS: `--import=${new URL("./preload.mjs", import.meta.url).href}`, |
113 | | - VP_REMOTE_CACHE: undefined, |
114 | | - VP_REMOTE_CACHE_URL: undefined, |
115 | | - }, |
116 | | - stdio: ["ignore", "ignore", "pipe"], |
117 | | - }, |
118 | | - ); |
119 | | - let stderr = ""; |
120 | | - child.stderr.on("data", (chunk) => { |
121 | | - stderr += chunk; |
122 | | - }); |
123 | | - child.once("error", reject); |
124 | | - child.once("close", (code) => { |
125 | | - if (code !== 0 && task !== "fail") reject(new Error(stderr)); |
126 | | - else resolve(code); |
127 | | - }); |
128 | | - }); |
129 | | - expect(await run("build")).toBe(0); |
130 | | - expect(reports).toHaveLength(1); |
131 | | - expect(await run("--last-details")).toBe(0); |
132 | | - expect( |
133 | | - JSON.parse(await NodeFSP.readFile(NodePath.join(root, "environment.json"), "utf8")), |
134 | | - ).toMatchObject({ url: null }); |
135 | | - expect(await run("build")).toBe(0); |
136 | | - expect(reports).toHaveLength(2); |
137 | | - expect(await run("fail")).toBe(7); |
138 | | - expect(reports).toHaveLength(3); |
139 | | - const secondWorktree = NodePath.join(root, "second-worktree"); |
140 | | - await NodeFSP.mkdir(secondWorktree); |
141 | | - for (const file of ["package.json", ".task-cache.json", "vite.config.ts", "build.cjs"]) |
142 | | - await NodeFSP.copyFile(NodePath.join(root, file), NodePath.join(secondWorktree, file)); |
143 | | - expect(await run("build", secondWorktree)).toBe(0); |
144 | | - expect(reports).toHaveLength(4); |
145 | | - expect(await run("build", secondWorktree)).toBe(0); |
146 | | - expect(reports).toHaveLength(5); |
147 | | - expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint); |
148 | | - expect(await run("nested", root, "--no-cache")).toBe(0); |
149 | | - expect(await run("build", nested)).toBe(0); |
150 | | - expect(reports).toHaveLength(5); |
151 | | - expect( |
152 | | - reports.every( |
153 | | - (report) => |
154 | | - report.authorization === `Basic ${Buffer.from("maria:secret").toString("base64")}`, |
155 | | - ), |
156 | | - ).toBe(true); |
157 | | - const events = reports.map((report) => JSON.parse(report.body)); |
158 | | - expect(events[0].tasks[0].source).toBe("executed"); |
159 | | - expect(events[1].tasks[0].source).toBe("local"); |
160 | | - expect(events[3].tasks[0].source).toBe("executed"); |
161 | | - expect(new Set(events.map((event) => event.id)).size).toBe(5); |
162 | | - } finally { |
163 | | - await new Promise<void>((resolve, reject) => |
164 | | - server.close((error) => (error ? reject(error) : resolve())), |
165 | | - ); |
166 | | - await NodeFSP.rm(root, { recursive: true, force: true }); |
167 | | - await NodeFSP.rm(unrelated, { recursive: true, force: true }); |
168 | | - } |
169 | | -}); |
170 | | - |
171 | 11 | it("sends telemetry from a detached flusher that the task runner does not wait for", async () => { |
172 | 12 | const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-flusher-")); |
173 | 13 | const reports: string[] = []; |
|