Skip to content

Commit 6f0bbe0

Browse files
committed
Fix more tests
1 parent 46ba350 commit 6f0bbe0

File tree

2 files changed

+93
-28
lines changed

2 files changed

+93
-28
lines changed

Tests/TSCBasicTests/ProcessTests.swift

Lines changed: 91 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ class ProcessTests: XCTestCase {
2525

2626
func testBasics() throws {
2727
do {
28+
#if os(Windows)
29+
let process = Process(args: "cmd", "/c", "echo", "hello")
30+
#else
2831
let process = Process(args: "echo", "hello")
32+
#endif
2933
try process.launch()
3034
let result = try process.waitUntilExit()
3135
XCTAssertEqual(try result.utf8Output(), "hello\n")
@@ -34,46 +38,54 @@ class ProcessTests: XCTestCase {
3438
}
3539

3640
do {
41+
#if os(Windows)
42+
let process = Process(args: "cmd.exe", "/c", "exit", "4")
43+
#else
3744
let process = Process(args: script("exit4"))
45+
#endif
3846
try process.launch()
3947
let result = try process.waitUntilExit()
4048
XCTAssertEqual(result.exitStatus, .terminated(code: 4))
4149
}
4250
}
4351

4452
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
5058
// 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")
5260

5361
// Test buffer larger than that allocated.
5462
try withTemporaryFile { file in
5563
let count = 10_000
5664
let stream = BufferedOutputByteStream()
5765
stream <<< Format.asRepeating(string: "a", count: count)
5866
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
6573
XCTAssert(outputCount == count)
6674
}
6775
}
6876

6977
func testPopenAsync() throws {
70-
#if os(Windows)
78+
#if os(Windows)
7179
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
7486
let args = ["whoami"]
7587
let answer = NSUserName()
76-
#endif
88+
#endif
7789
var popenResult: Result<ProcessResult, Error>?
7890
let group = DispatchGroup()
7991
group.enter()
@@ -95,12 +107,20 @@ class ProcessTests: XCTestCase {
95107

96108
func testCheckNonZeroExit() throws {
97109
do {
110+
#if os(Windows)
111+
let output = try Process.checkNonZeroExit(args: "cmd.exe", "/c", "echo", "hello")
112+
#else
98113
let output = try Process.checkNonZeroExit(args: "echo", "hello")
114+
#endif
99115
XCTAssertEqual(output, "hello\n")
100116
}
101117

102118
do {
119+
#if os(Windows)
120+
let output = try Process.checkNonZeroExit(args: "cmd.exe", "/c", "exit", "4")
121+
#else
103122
let output = try Process.checkNonZeroExit(args: script("exit4"))
123+
#endif
104124
XCTFail("Unexpected success \(output)")
105125
} catch ProcessResult.Error.nonZeroExit(let result) {
106126
XCTAssertEqual(result.exitStatus, .terminated(code: 4))
@@ -176,7 +196,7 @@ class ProcessTests: XCTestCase {
176196
let process = Process(args: "nonExecutableProgram")
177197
try process.launch()
178198
XCTFail("Should have failed to validate nonExecutableProgram")
179-
} catch Process.Error.missingExecutableProgram (let program){
199+
} catch Process.Error.missingExecutableProgram(let program) {
180200
XCTAssert(program == "nonExecutableProgram")
181201
}
182202
}
@@ -262,7 +282,11 @@ class ProcessTests: XCTestCase {
262282
#endif
263283

264284
func testThreadSafetyOnWaitUntilExit() throws {
285+
#if os(Windows)
286+
let process = Process(args: "cmd", "/c", "echo", "hello")
287+
#else
265288
let process = Process(args: "echo", "hello")
289+
#endif
266290
try process.launch()
267291

268292
var result1: String = ""
@@ -287,7 +311,12 @@ class ProcessTests: XCTestCase {
287311

288312
func testStdin() throws {
289313
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
291320
stdout += stdoutBytes
292321
}, stderr: { _ in }))
293322
let stdinStream = try process.launch()
@@ -305,14 +334,24 @@ class ProcessTests: XCTestCase {
305334
func testStdoutStdErr() throws {
306335
// A simple script to check that stdout and stderr are captured separatly.
307336
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)
309343
XCTAssertEqual(try result.utf8Output(), "simple output\n")
310344
XCTAssertEqual(try result.utf8stderrOutput(), "simple error\n")
311345
}
312346

313347
// A long stdout and stderr output.
314348
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)
316355
let count = 16 * 1024
317356
XCTAssertEqual(try result.utf8Output(), String(repeating: "1", count: count))
318357
XCTAssertEqual(try result.utf8stderrOutput(), String(repeating: "2", count: count))
@@ -330,7 +369,12 @@ class ProcessTests: XCTestCase {
330369
func testStdoutStdErrRedirected() throws {
331370
// A simple script to check that stdout and stderr are captured in the same location.
332371
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))
334378
try process.launch()
335379
let result = try process.waitUntilExit()
336380
XCTAssertEqual(try result.utf8Output(), "simple error\nsimple output\n")
@@ -339,7 +383,12 @@ class ProcessTests: XCTestCase {
339383

340384
// A long stdout and stderr output.
341385
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))
343392
try process.launch()
344393
let result = try process.waitUntilExit()
345394

@@ -352,7 +401,12 @@ class ProcessTests: XCTestCase {
352401
func testStdoutStdErrStreaming() throws {
353402
var stdout = [UInt8]()
354403
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
356410
stdout += stdoutBytes
357411
}, stderr: { stderrBytes in
358412
stderr += stderrBytes
@@ -368,7 +422,12 @@ class ProcessTests: XCTestCase {
368422
func testStdoutStdErrStreamingRedirected() throws {
369423
var stdout = [UInt8]()
370424
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
372431
stdout += stdoutBytes
373432
}, stderr: { stderrBytes in
374433
stderr += stderrBytes
@@ -402,15 +461,21 @@ class ProcessTests: XCTestCase {
402461
try localFileSystem.createDirectory(childPath.parentDirectory, recursive: true)
403462
try localFileSystem.writeFileContents(childPath, bytes: ByteString("child"))
404463

464+
#if os(Windows)
465+
let args = ["cmd.exe", "/c", "type", "file"]
466+
#else
467+
let args = ["cat", "file"]
468+
#endif
469+
405470
do {
406-
let process = Process(arguments: ["cat", "file"], workingDirectory: tempDirPath)
471+
let process = Process(arguments: args, workingDirectory: tempDirPath)
407472
try process.launch()
408473
let result = try process.waitUntilExit()
409474
XCTAssertEqual(try result.utf8Output(), "parent")
410475
}
411476

412477
do {
413-
let process = Process(arguments: ["cat", "file"], workingDirectory: childPath.parentDirectory)
478+
let process = Process(arguments: args, workingDirectory: childPath.parentDirectory)
414479
try process.launch()
415480
let result = try process.waitUntilExit()
416481
XCTAssertEqual(try result.utf8Output(), "child")

Tests/TSCBasicTests/TemporaryFileTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ class TemporaryFileTests: XCTestCase {
143143
// sequence of creating and destroying TemporaryFile objects. I don't
144144
// believe that this is guaranteed by POSIX, but it is true on all
145145
// platforms I know of.
146-
#if !os(Windows)
146+
#if !os(Windows) // `fileDescriptor` is currently unavailable in Windows
147147
let initialFD = try Int(withTemporaryFile { return $0.fileHandle.fileDescriptor })
148148
for _ in 0..<10 {
149149
_ = try withTemporaryFile { return $0.fileHandle }
150150
}
151151
let endFD = try Int(withTemporaryFile { return $0.fileHandle.fileDescriptor })
152152
XCTAssertEqual(initialFD, endFD)
153-
#endif
153+
#endif
154154
}
155155
}

0 commit comments

Comments
 (0)