@@ -17,6 +17,12 @@ vi.mock("child_process", () => ({ spawn: vi.fn() }))
1717
1818const mockSpawn = vi . mocked ( spawn )
1919
20+ function decodePowerShellCommand ( args : readonly string [ ] ) : string {
21+ const encodedCommandIndex = args . indexOf ( "-EncodedCommand" )
22+ if ( encodedCommandIndex === - 1 ) throw new Error ( "Expected an encoded PowerShell command" )
23+ return Buffer . from ( args [ encodedCommandIndex + 1 ] , "base64" ) . toString ( "utf16le" )
24+ }
25+
2026function createChild ( ) {
2127 return Object . assign ( new EventEmitter ( ) , {
2228 stdout : new PassThrough ( ) ,
@@ -97,7 +103,7 @@ describe("managed binary archive utilities", () => {
97103 if ( process . platform === "win32" ) {
98104 expect ( mockSpawn ) . toHaveBeenCalledWith (
99105 "powershell" ,
100- [ "-NoProfile" , "-NonInteractive" , "-Command " , expect . any ( String ) , "/tmp/archive.zip" , "/tmp/output" ] ,
106+ [ "-NoProfile" , "-NonInteractive" , "-EncodedCommand " , expect . any ( String ) ] ,
101107 expect . objectContaining ( { shell : false } ) ,
102108 )
103109 } else {
@@ -109,6 +115,28 @@ describe("managed binary archive utilities", () => {
109115 }
110116 } )
111117
118+ it ( "passes Windows ZIP paths through an encoded PowerShell command" , async ( ) => {
119+ const child = createChild ( )
120+ mockSpawn . mockReturnValue ( child as unknown as ReturnType < typeof spawn > )
121+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" )
122+ Object . defineProperty ( process , "platform" , { value : "win32" , configurable : true } )
123+ try {
124+ const extraction = extractZipArchive ( "C:\\Roo's files\\archive.zip" , "C:\\Roo's files\\output" )
125+ child . emit ( "close" , 0 )
126+ await extraction
127+
128+ const args = mockSpawn . mock . calls [ 0 ] [ 1 ]
129+ expect ( args ) . toEqual ( [ "-NoProfile" , "-NonInteractive" , "-EncodedCommand" , expect . any ( String ) ] )
130+ const script = decodePowerShellCommand ( args )
131+ expect ( script ) . toContain ( "$archivePath = 'C:\\Roo''s files\\archive.zip'" )
132+ expect ( script ) . toContain ( "$destination = 'C:\\Roo''s files\\output'" )
133+ expect ( script ) . toContain ( "Expand-Archive -LiteralPath $archivePath" )
134+ expect ( script ) . not . toContain ( "$args" )
135+ } finally {
136+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform )
137+ }
138+ } )
139+
112140 it ( "validates a single-file tar.xz layout before extraction" , async ( ) => {
113141 const listing = createChild ( )
114142 const extraction = createChild ( )
@@ -165,15 +193,11 @@ describe("managed binary archive utilities", () => {
165193 await extraction
166194
167195 const args = mockSpawn . mock . calls [ 0 ] [ 1 ]
168- const script = args [ 3 ]
196+ const script = decodePowerShellCommand ( args )
169197 expect ( script ) . toContain ( "$entries.Count -ne 1" )
170- expect ( script ) . not . toContain ( "C:\\archive.zip" )
171- expect ( args . slice ( 4 ) ) . toEqual ( [
172- "C:\\archive.zip" ,
173- path . join ( "C:\\output" , "binary.exe" ) ,
174- "binary.exe" ,
175- "Tool" ,
176- ] )
198+ expect ( script ) . toContain ( "$archivePath = 'C:\\archive.zip'" )
199+ expect ( script ) . toContain ( `$outputPath = '${ path . join ( "C:\\output" , "binary.exe" ) } '` )
200+ expect ( args ) . toEqual ( [ "-NoProfile" , "-NonInteractive" , "-EncodedCommand" , expect . any ( String ) ] )
177201 } finally {
178202 if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform )
179203 }
0 commit comments