-
-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathXCTestCase+Shell.swift
More file actions
29 lines (24 loc) · 901 Bytes
/
XCTestCase+Shell.swift
File metadata and controls
29 lines (24 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Foundation
/// Returns the output of running `executable` with `args`. Throws an error if the process exits indicating failure.
@discardableResult
func checkedOutput(_ executable: String, _ args: [String]) throws -> String? {
#if os(macOS)
let process = Process()
let output = Pipe()
if executable.contains("/") {
process.launchPath = executable
} else {
process.launchPath = try checkedOutput("/usr/bin/which", [executable])?.trimmingCharacters(in: .newlines)
}
process.arguments = args
process.standardOutput = output
process.launch()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw NSError(domain: NSPOSIXErrorDomain, code: Int(process.terminationStatus))
}
return String(data: output.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
#else
return nil
#endif
}