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

shell.windows.integration.test.ts

67 lines2.1 KB

1import * as NodeChildProcess from "node:child_process";
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as Effect from "effect/Effect";
6import { expect, it } from "vite-plus/test";
7​
8import { resolveSpawnCommand } from "./shell.ts";
9​
10it.runIf(process.platform === "win32")(
11 "round-trips hostile argv through a real cmd.exe .cmd shim under spaces and Unicode",
12 async () => {
13 const outer = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 cmd "));
14 const root = NodePath.join(outer, "T3 Code José & QA");
15 NodeFS.mkdirSync(root, { recursive: true });
16 const recorderPath = NodePath.join(root, "record-argv.cjs");
17 const outputPath = NodePath.join(root, "argv.json");
18 const shimPath = NodePath.join(root, "argv-recorder.cmd");
19 const expected = [
20 "plain",
21 "space value",
22 "amp&ersand",
23 "pipe|value",
24 "%PATH%",
25 "bang!value",
26 "caret^value",
27 "(paren)",
28 'quote"value',
29 "trailing\\",
30 "雪 José",
31 ];
32​
33 NodeFS.writeFileSync(
34 recorderPath,
35 `const fs = require("node:fs"); fs.writeFileSync(process.argv[2], JSON.stringify(process.argv.slice(3)));\n`,
36 );
37 NodeFS.writeFileSync(
38 shimPath,
39 `@echo off\r\n"${process.execPath}" "${recorderPath}" "${outputPath}" %*\r\n`,
40 );
41​
42 try {
43 const resolved = await Effect.runPromise(
44 resolveSpawnCommand(shimPath, expected, {
45 env: {
46 ...process.env,
47 PATHEXT: process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD",
48 },
49 }),
50 );
51 const child = NodeChildProcess.spawnSync(resolved.command, resolved.args, {
52 cwd: root,
53 env: process.env,
54 shell: resolved.shell,
55 encoding: "utf8",
56 windowsHide: true,
57 });
58​
59 expect(child.error).toBeUndefined();
60 expect(child.status).toBe(0);
61 expect(NodeFS.existsSync(outputPath)).toBe(true);
62 expect(JSON.parse(NodeFS.readFileSync(outputPath, "utf8"))).toEqual(expected);
63 } finally {
64 NodeFS.rmSync(outer, { recursive: true, force: true, maxRetries: 8, retryDelay: 125 });
65 }
66 },
67);