Add 'ShellError', which may be thrown from completed shell commands.

This commit is contained in:
Kare Morstol
2015-09-03 17:45:28 +02:00
parent 10cf827d8b
commit de708331a8
2 changed files with 25 additions and 3 deletions
+21 -1
View File
@@ -42,6 +42,7 @@ extension ShellContextType {
}
}
extension ShellContextType {
private func outputFromRun (task: NSTask) -> String {
@@ -82,6 +83,22 @@ extension ShellContextType {
}
}
/** Error type for completed shell commands. */
public enum ShellError: ErrorType, Equatable {
/** Exit code was not zero. */
case ReturnedErrorCode (errorcode: Int32)
}
public func == (e1: ShellError, e2: ShellError) -> Bool {
switch (e1, e2) {
case (.ReturnedErrorCode(let c1), .ReturnedErrorCode(let c2)):
return c1 == c2
}
}
/** Output from the 'runAsync' methods. */
public struct AsyncShellTask {
public let stdout: NSFileHandle
public let stderror: NSFileHandle
@@ -104,10 +121,13 @@ public struct AsyncShellTask {
/**
Wait for this shell task to finish.
- throws: a TaskError if the return code is anything but 0.
- throws: a ShellError if the return code is anything but 0.
*/
public func finish() throws {
task.waitUntilExit()
if task.terminationStatus != 0 {
throw ShellError.ReturnedErrorCode(errorcode: task.terminationStatus)
}
}
}
+4 -2
View File
@@ -44,8 +44,10 @@ class RunAsync_Tests: XCTestCase {
XCTAssertEqual( asynctask.stdout.read(), "" )
}
func testThrowsErrorOnExitcodeNotZero () {
let asynctask = main.runAsync(bash: "echo errormessage > /dev/stderr; exit 1" )
XCTAssertEqual( asynctask.stderror.read(), "one two\n" )
XCTAssertEqual( asynctask.stdout.read(), "" )
AssertThrows(ShellError.ReturnedErrorCode(errorcode: 1)) { try asynctask.finish() }
XCTAssertEqual( asynctask.stderror.read(), "errormessage\n" )
}
}