|
| 1 | +import * as NodeChildProcess from "node:child_process"; |
| 2 | +import * as NodeEvents from "node:events"; |
1 | 3 | import * as NodeFS from "node:fs"; |
2 | 4 | import * as NodeOS from "node:os"; |
3 | 5 | import * as NodePath from "node:path"; |
| 6 | +import * as NodeVM from "node:vm"; |
4 | 7 |
|
5 | 8 | import { assert, describe, it } from "vite-plus/test"; |
6 | 9 |
|
@@ -151,3 +154,93 @@ describe("electron development launcher", () => { |
151 | 154 | assert.equal(production.generatedIconPath, "/runtime/icon-prod.icns"); |
152 | 155 | }); |
153 | 156 | }); |
| 157 | + |
| 158 | +async function startupCleanupPattern(desktopDir) { |
| 159 | + const source = NodeFS.readFileSync(new URL("./dev-electron.mjs", import.meta.url), "utf8"); |
| 160 | + let pattern; |
| 161 | + // Supply inert imports so the real startup runs without launching Electron or signalling processes. |
| 162 | + await NodeVM.runInNewContext( |
| 163 | + `(async () => { ${source.replace(/^import\s[\s\S]*?;\n/gm, "")} })()`, |
| 164 | + { |
| 165 | + NodeChildProcess: { |
| 166 | + execFileSync: () => undefined, |
| 167 | + spawnSync: (command, args) => { |
| 168 | + if (command === "pkill" && args.includes("-f")) pattern = args.at(-1); |
| 169 | + }, |
| 170 | + spawn: () => new NodeEvents.EventEmitter(), |
| 171 | + }, |
| 172 | + NodeFS: { watch: () => ({ close: () => undefined }) }, |
| 173 | + NodeOS: { platform: () => "linux" }, |
| 174 | + NodePath, |
| 175 | + desktopDir, |
| 176 | + resolveDevProtocolClient: () => null, |
| 177 | + resolveElectronLaunchCommand: (args) => ({ electronPath: "electron", args }), |
| 178 | + waitForResources: async () => undefined, |
| 179 | + URL, |
| 180 | + process: { |
| 181 | + env: { VITE_DEV_SERVER_URL: "http://localhost:1234" }, |
| 182 | + execPath: "node", |
| 183 | + once: () => undefined, |
| 184 | + }, |
| 185 | + }, |
| 186 | + ); |
| 187 | + assert.isString(pattern); |
| 188 | + return pattern; |
| 189 | +} |
| 190 | + |
| 191 | +it |
| 192 | + // oxlint-disable-next-line t3code/no-global-process-runtime -- Native pgrep is unavailable on Windows. |
| 193 | + .skipIf(NodeOS.platform() === "win32") |
| 194 | + .each([ |
| 195 | + "/tmp/t3-feature.one/apps/desktop", |
| 196 | + "/tmp/t3-feature.one [two](three)+?^${four}|back\\slash*/apps/desktop", |
| 197 | + ])("dev cleanup matches only the literal root argument: %s", async (desktopDir) => { |
| 198 | + const pattern = await startupCleanupPattern(desktopDir); |
| 199 | + const flag = `--t3code-dev-root=${desktopDir}`; |
| 200 | + const argvCases = [ |
| 201 | + [flag], |
| 202 | + [flag, "main.cjs"], |
| 203 | + [flag.replace(".one", "Xone")], |
| 204 | + [`${flag}-two`], |
| 205 | + [`prefix${flag}`], |
| 206 | + [`--t3code-dev-root=/other${desktopDir}`], |
| 207 | + ]; |
| 208 | + const children = []; |
| 209 | + try { |
| 210 | + for (const args of argvCases) { |
| 211 | + const child = NodeChildProcess.spawn( |
| 212 | + process.execPath, |
| 213 | + ["-e", "process.send('ready'); setInterval(() => {}, 1000)", "--", ...args], |
| 214 | + { stdio: ["ignore", "ignore", "inherit", "ipc"] }, |
| 215 | + ); |
| 216 | + children.push(child); |
| 217 | + await Promise.race([ |
| 218 | + NodeEvents.once(child, "message"), |
| 219 | + NodeEvents.once(child, "exit").then(() => { |
| 220 | + throw new Error("Fixture exited before readiness"); |
| 221 | + }), |
| 222 | + ]); |
| 223 | + } |
| 224 | + // Use the native ERE engine, but never execute the launcher's signal-producing command. |
| 225 | + const result = NodeChildProcess.spawnSync( |
| 226 | + "pgrep", |
| 227 | + ["-P", String(process.pid), "-f", "--", pattern], |
| 228 | + { encoding: "utf8" }, |
| 229 | + ); |
| 230 | + assert.equal(result.status, 0, result.stderr); |
| 231 | + const matches = result.stdout.trim().split(/\s+/).map(Number); |
| 232 | + assert.sameMembers( |
| 233 | + matches, |
| 234 | + children.slice(0, 2).map((child) => child.pid), |
| 235 | + ); |
| 236 | + } finally { |
| 237 | + await Promise.all( |
| 238 | + children.map(async (child) => { |
| 239 | + if (child.exitCode !== null || child.signalCode !== null) return; |
| 240 | + const exited = NodeEvents.once(child, "exit"); |
| 241 | + child.kill("SIGTERM"); |
| 242 | + await exited; |
| 243 | + }), |
| 244 | + ); |
| 245 | + } |
| 246 | +}); |
0 commit comments