Skip to content

Commit aee3ba4

Browse files
authored
Merge pull request #1245 from spevans/pr_process_tests
Process: Uncomment disabled tests and make compatible with Darwin
2 parents 561c75b + 9f15f19 commit aee3ba4

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

TestFoundation/TestProcess.swift

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ class TestProcess : XCTestCase {
2828
("test_pipe_stdout", test_pipe_stdout),
2929
("test_pipe_stderr", test_pipe_stderr),
3030
("test_current_working_directory", test_current_working_directory),
31-
// disabled for now
32-
// ("test_pipe_stdout_and_stderr_same_pipe", test_pipe_stdout_and_stderr_same_pipe),
31+
("test_pipe_stdout_and_stderr_same_pipe", test_pipe_stdout_and_stderr_same_pipe),
3332
("test_file_stdout", test_file_stdout),
34-
// disabled for now
35-
// ("test_passthrough_environment", test_passthrough_environment),
36-
// ("test_no_environment", test_no_environment),
37-
// ("test_custom_environment", test_custom_environment),
33+
("test_passthrough_environment", test_passthrough_environment),
34+
("test_no_environment", test_no_environment),
35+
("test_custom_environment", test_custom_environment),
3836
]
3937
}
4038

@@ -183,7 +181,7 @@ class TestProcess : XCTestCase {
183181
XCTFail("Could not read stdout")
184182
return
185183
}
186-
// testing the return value of an external process is does not port well, and may change.
184+
// testing the return value of an external process does not port well, and may change.
187185
// XCTAssertEqual(string, "/bin/cat: invalid_file_name: No such file or directory\n")
188186
}
189187

@@ -197,6 +195,9 @@ class TestProcess : XCTestCase {
197195
process.standardOutput = pipe
198196
process.standardError = pipe
199197

198+
// Clear the environment to stop the malloc debug flags used in Xcode debug being
199+
// set in the subprocess.
200+
process.environment = [:]
200201
process.launch()
201202
process.waitUntilExit()
202203
XCTAssertEqual(process.terminationStatus, 1)
@@ -206,7 +207,12 @@ class TestProcess : XCTestCase {
206207
XCTFail("Could not read stdout")
207208
return
208209
}
209-
XCTAssertEqual(string, "/bin/cat: invalid_file_name: No such file or directory\n")
210+
211+
// Remove the leading '/bin/' since on macOS '/bin/cat' just outputs 'cat:'
212+
let searchStr = "/bin/"
213+
let errMsg = string.replacingOccurrences(of: searchStr, with: "", options: [.literal, .anchored],
214+
range: searchStr.startIndex..<searchStr.endIndex)
215+
XCTAssertEqual(errMsg, "cat: invalid_file_name: No such file or directory\n")
210216
}
211217

212218
func test_file_stdout() {
@@ -234,7 +240,7 @@ class TestProcess : XCTestCase {
234240

235241
func test_passthrough_environment() {
236242
do {
237-
let output = try runTask(["/usr/bin/env"], environment: nil)
243+
let (output, _) = try runTask(["/usr/bin/env"], environment: nil)
238244
let env = try parseEnv(output)
239245
XCTAssertGreaterThan(env.count, 0)
240246
} catch let error {
@@ -244,7 +250,7 @@ class TestProcess : XCTestCase {
244250

245251
func test_no_environment() {
246252
do {
247-
let output = try runTask(["/usr/bin/env"], environment: [:])
253+
let (output, _) = try runTask(["/usr/bin/env"], environment: [:])
248254
let env = try parseEnv(output)
249255
XCTAssertEqual(env.count, 0)
250256
} catch let error {
@@ -255,7 +261,7 @@ class TestProcess : XCTestCase {
255261
func test_custom_environment() {
256262
do {
257263
let input = ["HELLO": "WORLD", "HOME": "CUPERTINO"]
258-
let output = try runTask(["/usr/bin/env"], environment: input)
264+
let (output, _) = try runTask(["/usr/bin/env"], environment: input)
259265
let env = try parseEnv(output)
260266
XCTAssertEqual(env, input)
261267
} catch let error {
@@ -267,9 +273,9 @@ class TestProcess : XCTestCase {
267273
do {
268274
let previousWorkingDirectory = FileManager.default.currentDirectoryPath
269275

270-
// `bash` will not be found if the current working directory is not set correctly.
271-
let _ = try runTask(["bash", "-c", "exit 0"], currentDirectoryPath: "/bin")
272-
276+
// Darwin Foundation requires the full path to the executable (.launchPath)
277+
let (output, _) = try runTask(["/bin/bash", "-c", "pwd"], currentDirectoryPath: "/bin")
278+
XCTAssertEqual(output.trimmingCharacters(in: .newlines), "/bin")
273279
XCTAssertEqual(previousWorkingDirectory, FileManager.default.currentDirectoryPath)
274280
} catch let error {
275281
XCTFail("Test failed: \(error)")
@@ -297,34 +303,44 @@ private enum Error: Swift.Error {
297303
case InvalidEnvironmentVariable(String)
298304
}
299305

300-
private func runTask(_ arguments: [String], environment: [String: String]? = nil, currentDirectoryPath: String? = nil) throws -> String {
306+
private func runTask(_ arguments: [String], environment: [String: String]? = nil, currentDirectoryPath: String? = nil) throws -> (String, String) {
301307
let process = Process()
302308

303309
var arguments = arguments
304310
process.launchPath = arguments.removeFirst()
305311
process.arguments = arguments
306-
process.environment = environment
312+
// Darwin Foundation doesnt allow .environment to be set to nil although the documentation
313+
// says it is an optional. https://developer.apple.com/documentation/foundation/process/1409412-environment
314+
if let e = environment {
315+
process.environment = e
316+
}
307317

308318
if let directoryPath = currentDirectoryPath {
309319
process.currentDirectoryPath = directoryPath
310320
}
311321

312-
let pipe = Pipe()
313-
process.standardOutput = pipe
314-
process.standardError = pipe
322+
let stdoutPipe = Pipe()
323+
let stderrPipe = Pipe()
324+
process.standardOutput = stdoutPipe
325+
process.standardError = stderrPipe
315326
process.launch()
316327
process.waitUntilExit()
317328

318329
guard process.terminationStatus == 0 else {
319330
throw Error.TerminationStatus(process.terminationStatus)
320331
}
321332

322-
let data = pipe.fileHandleForReading.availableData
323-
guard let output = String(data: data, encoding: .utf8) else {
324-
throw Error.UnicodeDecodingError(data)
333+
let stdoutData = stdoutPipe.fileHandleForReading.availableData
334+
guard let stdout = String(data: stdoutData, encoding: .utf8) else {
335+
throw Error.UnicodeDecodingError(stdoutData)
336+
}
337+
338+
let stderrData = stderrPipe.fileHandleForReading.availableData
339+
guard let stderr = String(data: stderrData, encoding: .utf8) else {
340+
throw Error.UnicodeDecodingError(stderrData)
325341
}
326342

327-
return output
343+
return (stdout, stderr)
328344
}
329345

330346
private func parseEnv(_ env: String) throws -> [String: String] {

0 commit comments

Comments
 (0)