@@ -25,7 +25,11 @@ class ProcessTests: XCTestCase {
25
25
26
26
func testBasics( ) throws {
27
27
do {
28
+ #if os(Windows)
29
+ let process = Process ( args: " cmd " , " /c " , " echo " , " hello " )
30
+ #else
28
31
let process = Process ( args: " echo " , " hello " )
32
+ #endif
29
33
try process. launch ( )
30
34
let result = try process. waitUntilExit ( )
31
35
XCTAssertEqual ( try result. utf8Output ( ) , " hello \n " )
@@ -34,46 +38,54 @@ class ProcessTests: XCTestCase {
34
38
}
35
39
36
40
do {
41
+ #if os(Windows)
42
+ let process = Process ( args: " cmd.exe " , " /c " , " exit " , " 4 " )
43
+ #else
37
44
let process = Process ( args: script ( " exit4 " ) )
45
+ #endif
38
46
try process. launch ( )
39
47
let result = try process. waitUntilExit ( )
40
48
XCTAssertEqual ( result. exitStatus, . terminated( code: 4 ) )
41
49
}
42
50
}
43
51
44
52
func testPopen( ) throws {
45
- #if os(Windows)
46
- let echo = " echo .exe"
47
- #else
48
- let echo = " echo "
49
- #endif
53
+ #if os(Windows)
54
+ let echo = [ " cmd .exe" , " /c " , " echo " ]
55
+ #else
56
+ let echo = [ " echo " ]
57
+ #endif
50
58
// Test basic echo.
51
- XCTAssertEqual ( try Process . popen ( arguments: [ echo, " hello " ] ) . utf8Output ( ) , " hello \n " )
59
+ XCTAssertEqual ( try Process . popen ( arguments: echo + [ " hello " ] ) . utf8Output ( ) , " hello \n " )
52
60
53
61
// Test buffer larger than that allocated.
54
62
try withTemporaryFile { file in
55
63
let count = 10_000
56
64
let stream = BufferedOutputByteStream ( )
57
65
stream <<< Format . asRepeating ( string: " a " , count: count)
58
66
try localFileSystem. writeFileContents ( file. path, bytes: stream. bytes)
59
- #if os(Windows)
60
- let cat = " cat .exe"
61
- #else
62
- let cat = " cat "
63
- #endif
64
- let outputCount = try Process . popen ( args : cat , file . path . pathString ) . utf8Output ( ) . count
67
+ #if os(Windows)
68
+ let process = try Process . popen ( args : " cmd .exe" , " /c " , " type " , file . path . pathString )
69
+ #else
70
+ let process = try Process . popen ( args : " cat " , file . path . pathString )
71
+ #endif
72
+ let outputCount = try process . utf8Output ( ) . count
65
73
XCTAssert ( outputCount == count)
66
74
}
67
75
}
68
76
69
77
func testPopenAsync( ) throws {
70
- #if os(Windows)
78
+ #if os(Windows)
71
79
let args = [ " where.exe " , " where " ]
72
- let answer = " C: \\ Windows \\ System32 \\ where.exe "
73
- #else
80
+ var buffer = Array < WCHAR > ( repeating: 0 , count: Int ( MAX_PATH + 1 ) )
81
+ guard GetSystemDirectoryW ( & buffer, . init( buffer. count) ) > 0 else {
82
+ return XCTFail ( )
83
+ }
84
+ let answer = String ( decodingCString: buffer, as: UTF16 . self) + " \\ where.exe "
85
+ #else
74
86
let args = [ " whoami " ]
75
87
let answer = NSUserName ( )
76
- #endif
88
+ #endif
77
89
var popenResult : Result < ProcessResult , Error > ?
78
90
let group = DispatchGroup ( )
79
91
group. enter ( )
@@ -95,12 +107,20 @@ class ProcessTests: XCTestCase {
95
107
96
108
func testCheckNonZeroExit( ) throws {
97
109
do {
110
+ #if os(Windows)
111
+ let output = try Process . checkNonZeroExit ( args: " cmd.exe " , " /c " , " echo " , " hello " )
112
+ #else
98
113
let output = try Process . checkNonZeroExit ( args: " echo " , " hello " )
114
+ #endif
99
115
XCTAssertEqual ( output, " hello \n " )
100
116
}
101
117
102
118
do {
119
+ #if os(Windows)
120
+ let output = try Process . checkNonZeroExit ( args: " cmd.exe " , " /c " , " exit " , " 4 " )
121
+ #else
103
122
let output = try Process . checkNonZeroExit ( args: script ( " exit4 " ) )
123
+ #endif
104
124
XCTFail ( " Unexpected success \( output) " )
105
125
} catch ProcessResult . Error . nonZeroExit( let result) {
106
126
XCTAssertEqual ( result. exitStatus, . terminated( code: 4 ) )
@@ -176,7 +196,7 @@ class ProcessTests: XCTestCase {
176
196
let process = Process ( args: " nonExecutableProgram " )
177
197
try process. launch ( )
178
198
XCTFail ( " Should have failed to validate nonExecutableProgram " )
179
- } catch Process . Error . missingExecutableProgram ( let program) {
199
+ } catch Process . Error . missingExecutableProgram( let program) {
180
200
XCTAssert ( program == " nonExecutableProgram " )
181
201
}
182
202
}
@@ -262,7 +282,11 @@ class ProcessTests: XCTestCase {
262
282
#endif
263
283
264
284
func testThreadSafetyOnWaitUntilExit( ) throws {
285
+ #if os(Windows)
286
+ let process = Process ( args: " cmd " , " /c " , " echo " , " hello " )
287
+ #else
265
288
let process = Process ( args: " echo " , " hello " )
289
+ #endif
266
290
try process. launch ( )
267
291
268
292
var result1 : String = " "
@@ -287,7 +311,12 @@ class ProcessTests: XCTestCase {
287
311
288
312
func testStdin( ) throws {
289
313
var stdout = [ UInt8] ( )
290
- let process = Process ( args: script ( " in-to-out " ) , outputRedirection: . stream( stdout: { stdoutBytes in
314
+ #if os(Windows)
315
+ let inToOut = [ " python.exe " , script ( " in-to-out " ) ]
316
+ #else
317
+ let inToOut = [ script ( " in-to-out " ) ]
318
+ #endif
319
+ let process = Process ( arguments: inToOut, outputRedirection: . stream( stdout: { stdoutBytes in
291
320
stdout += stdoutBytes
292
321
} , stderr: { _ in } ) )
293
322
let stdinStream = try process. launch ( )
@@ -305,14 +334,24 @@ class ProcessTests: XCTestCase {
305
334
func testStdoutStdErr( ) throws {
306
335
// A simple script to check that stdout and stderr are captured separatly.
307
336
do {
308
- let result = try Process . popen ( args: script ( " simple-stdout-stderr " ) )
337
+ #if os(Windows)
338
+ let simpleStdoutStdErr = [ " python.exe " , script ( " simple-stdout-stderr " ) ]
339
+ #else
340
+ let simpleStdoutStdErr = [ script ( " simple-stdout-stderr " ) ]
341
+ #endif
342
+ let result = try Process . popen ( arguments: simpleStdoutStdErr)
309
343
XCTAssertEqual ( try result. utf8Output ( ) , " simple output \n " )
310
344
XCTAssertEqual ( try result. utf8stderrOutput ( ) , " simple error \n " )
311
345
}
312
346
313
347
// A long stdout and stderr output.
314
348
do {
315
- let result = try Process . popen ( args: script ( " long-stdout-stderr " ) )
349
+ #if os(Windows)
350
+ let longStdoutStdErr = [ " python.exe " , script ( " long-stdout-stderr " ) ]
351
+ #else
352
+ let longStdoutStdErr = [ script ( " long-stdout-stderr " ) ]
353
+ #endif
354
+ let result = try Process . popen ( arguments: longStdoutStdErr)
316
355
let count = 16 * 1024
317
356
XCTAssertEqual ( try result. utf8Output ( ) , String ( repeating: " 1 " , count: count) )
318
357
XCTAssertEqual ( try result. utf8stderrOutput ( ) , String ( repeating: " 2 " , count: count) )
@@ -330,7 +369,12 @@ class ProcessTests: XCTestCase {
330
369
func testStdoutStdErrRedirected( ) throws {
331
370
// A simple script to check that stdout and stderr are captured in the same location.
332
371
do {
333
- let process = Process ( args: script ( " simple-stdout-stderr " ) , outputRedirection: . collect( redirectStderr: true ) )
372
+ #if os(Windows)
373
+ let simpleStdoutStdErr = [ " python.exe " , script ( " simple-stdout-stderr " ) ]
374
+ #else
375
+ let simpleStdoutStdErr = [ script ( " simple-stdout-stderr " ) ]
376
+ #endif
377
+ let process = Process ( arguments: simpleStdoutStdErr, outputRedirection: . collect( redirectStderr: true ) )
334
378
try process. launch ( )
335
379
let result = try process. waitUntilExit ( )
336
380
XCTAssertEqual ( try result. utf8Output ( ) , " simple error \n simple output \n " )
@@ -339,7 +383,12 @@ class ProcessTests: XCTestCase {
339
383
340
384
// A long stdout and stderr output.
341
385
do {
342
- let process = Process ( args: script ( " long-stdout-stderr " ) , outputRedirection: . collect( redirectStderr: true ) )
386
+ #if os(Windows)
387
+ let longStdoutStdErr = [ " python.exe " , script ( " long-stdout-stderr " ) ]
388
+ #else
389
+ let longStdoutStdErr = [ script ( " long-stdout-stderr " ) ]
390
+ #endif
391
+ let process = Process ( arguments: longStdoutStdErr, outputRedirection: . collect( redirectStderr: true ) )
343
392
try process. launch ( )
344
393
let result = try process. waitUntilExit ( )
345
394
@@ -352,7 +401,12 @@ class ProcessTests: XCTestCase {
352
401
func testStdoutStdErrStreaming( ) throws {
353
402
var stdout = [ UInt8] ( )
354
403
var stderr = [ UInt8] ( )
355
- let process = Process ( args: script ( " long-stdout-stderr " ) , outputRedirection: . stream( stdout: { stdoutBytes in
404
+ #if os(Windows)
405
+ let longStdoutStdErr = [ " python.exe " , script ( " long-stdout-stderr " ) ]
406
+ #else
407
+ let longStdoutStdErr = [ script ( " long-stdout-stderr " ) ]
408
+ #endif
409
+ let process = Process ( arguments: longStdoutStdErr, outputRedirection: . stream( stdout: { stdoutBytes in
356
410
stdout += stdoutBytes
357
411
} , stderr: { stderrBytes in
358
412
stderr += stderrBytes
@@ -368,7 +422,12 @@ class ProcessTests: XCTestCase {
368
422
func testStdoutStdErrStreamingRedirected( ) throws {
369
423
var stdout = [ UInt8] ( )
370
424
var stderr = [ UInt8] ( )
371
- let process = Process ( args: script ( " long-stdout-stderr " ) , outputRedirection: . stream( stdout: { stdoutBytes in
425
+ #if os(Windows)
426
+ let longStdoutStdErr = [ " python.exe " , script ( " long-stdout-stderr " ) ]
427
+ #else
428
+ let longStdoutStdErr = [ script ( " long-stdout-stderr " ) ]
429
+ #endif
430
+ let process = Process ( arguments: longStdoutStdErr, outputRedirection: . stream( stdout: { stdoutBytes in
372
431
stdout += stdoutBytes
373
432
} , stderr: { stderrBytes in
374
433
stderr += stderrBytes
@@ -402,15 +461,21 @@ class ProcessTests: XCTestCase {
402
461
try localFileSystem. createDirectory ( childPath. parentDirectory, recursive: true )
403
462
try localFileSystem. writeFileContents ( childPath, bytes: ByteString ( " child " ) )
404
463
464
+ #if os(Windows)
465
+ let args = [ " cmd.exe " , " /c " , " type " , " file " ]
466
+ #else
467
+ let args = [ " cat " , " file " ]
468
+ #endif
469
+
405
470
do {
406
- let process = Process ( arguments: [ " cat " , " file " ] , workingDirectory: tempDirPath)
471
+ let process = Process ( arguments: args , workingDirectory: tempDirPath)
407
472
try process. launch ( )
408
473
let result = try process. waitUntilExit ( )
409
474
XCTAssertEqual ( try result. utf8Output ( ) , " parent " )
410
475
}
411
476
412
477
do {
413
- let process = Process ( arguments: [ " cat " , " file " ] , workingDirectory: childPath. parentDirectory)
478
+ let process = Process ( arguments: args , workingDirectory: childPath. parentDirectory)
414
479
try process. launch ( )
415
480
let result = try process. waitUntilExit ( )
416
481
XCTAssertEqual ( try result. utf8Output ( ) , " child " )
0 commit comments