11// Windows command tests cover command quoting and shell resolution on Windows.
2- import { mkdir , writeFile } from "node:fs/promises" ;
2+ import { mkdir , readFile , writeFile } from "node:fs/promises" ;
33import path from "node:path" ;
4- import { describe , expect , it } from "vitest" ;
4+ import { describe , expect , it , vi } from "vitest" ;
55import { withTempDir } from "../test-utils/temp-dir.js" ;
66import { withMockedWindowsPlatform } from "../test-utils/vitest-spies.js" ;
77import { runCommandWithTimeout } from "./exec.js" ;
8+ import { spawnTerminalPty } from "./terminal-pty.js" ;
89import { resolveSafeChildProcessInvocation , resolveWindowsCommandShim } from "./windows-command.js" ;
910
1011describe ( "Windows command helpers" , ( ) => {
@@ -189,13 +190,24 @@ describe.runIf(process.platform === "win32")("Windows batch argv preservation",
189190 { name : "double quotes" , args : [ 'say "hello"' , "omega" ] } ,
190191 { name : "a caret" , args : [ "left^right" , "omega" ] } ,
191192 { name : "a quoted trailing backslash" , args : [ "C:\\two words\\" , "omega" ] } ,
193+ { name : "a caret beside a quote" , args : [ 'left^"right' , "omega" ] } ,
194+ { name : "a backslash before a quote" , args : [ 'left\\"right' , "omega" ] } ,
195+ { name : "two backslashes before a quote" , args : [ 'left\\\\"right' , "omega" ] } ,
196+ {
197+ name : "permitted cmd punctuation" ,
198+ args : [ "(round)" , "[square]" , "{curly}" , "semi;colon" , "eq=sign" , "comma,value" , "bang!" ] ,
199+ } ,
192200 ] ;
193201
194- it . each ( cases ) (
195- "preserves $name through a real .cmd wrapper" ,
196- async ( { args } ) => {
202+ it . each (
203+ cases . flatMap ( ( testCase ) =>
204+ [ "argv.cmd" , "argv with spaces.cmd" , "argv^caret.cmd" ] . map ( ( file ) => ( { ...testCase , file } ) ) ,
205+ ) ,
206+ ) (
207+ "preserves $name through $file" ,
208+ async ( { args, file } ) => {
197209 await withTempDir ( "openclaw-batch-argv-" , async ( cwd ) => {
198- const command = path . join ( cwd , "argv.cmd" ) ;
210+ const command = path . join ( cwd , file ) ;
199211 await writeFile (
200212 path . join ( cwd , "argv.cjs" ) ,
201213 "process.stdout.write(JSON.stringify(process.argv.slice(2)))" ,
@@ -210,6 +222,50 @@ describe.runIf(process.platform === "win32")("Windows batch argv preservation",
210222 15_000 ,
211223 ) ;
212224
225+ it . each ( [ "argv.cmd" , "argv with spaces.cmd" , "argv^caret.cmd" , "node.exe" ] ) (
226+ "preserves the argument matrix through a real PTY running %s" ,
227+ async ( file ) => {
228+ await withTempDir ( "openclaw-batch-argv-pty-" , async ( cwd ) => {
229+ const args = cases . flatMap ( ( testCase ) => testCase . args ) ;
230+ const script = path . join ( cwd , "argv.cjs" ) ;
231+ const output = path . join ( cwd , "received.json" ) ;
232+ await writeFile (
233+ script ,
234+ 'require("node:fs").writeFileSync(require("node:path").join(__dirname, "received.json"), JSON.stringify(process.argv.slice(2)))' ,
235+ ) ;
236+ const command = file === "node.exe" ? process . execPath : path . join ( cwd , file ) ;
237+ if ( file !== "node.exe" ) {
238+ await writeFile ( command , `@"${ process . execPath } " "%~dp0argv.cjs" %*\r\n` ) ;
239+ }
240+ const pty = await spawnTerminalPty ( {
241+ file : command ,
242+ args : file === "node.exe" ? [ script , ...args ] : args ,
243+ cwd,
244+ env : Object . fromEntries (
245+ Object . entries ( process . env ) . filter (
246+ ( entry ) : entry is [ string , string ] => typeof entry [ 1 ] === "string" ,
247+ ) ,
248+ ) ,
249+ cols : 120 ,
250+ rows : 24 ,
251+ } ) ;
252+ let exitCode : number | undefined ;
253+ pty . onExit ( ( event ) => {
254+ exitCode = event . exitCode ;
255+ } ) ;
256+ try {
257+ await vi . waitFor ( ( ) => expect ( exitCode ) . toBe ( 0 ) , { timeout : 5_000 } ) ;
258+ expect ( JSON . parse ( await readFile ( output , "utf8" ) ) ) . toEqual ( args ) ;
259+ } finally {
260+ if ( exitCode === undefined ) {
261+ pty . kill ( ) ;
262+ }
263+ }
264+ } ) ;
265+ } ,
266+ 15_000 ,
267+ ) ;
268+
213269 it . each ( [ "&" , "|" , "<" , ">" , "%" , "\r" , "\n" ] ) (
214270 "continues to reject unsafe batch argument character %j before launch" ,
215271 async ( character ) => {
0 commit comments