diff --git a/SwiftShell/Command.swift b/SwiftShell/Command.swift index 8e505e1..a2b7a90 100644 --- a/SwiftShell/Command.swift +++ b/SwiftShell/Command.swift @@ -121,13 +121,15 @@ public struct AsyncShellTask { /** Wait for this shell task to finish. + - returns: itself - throws: a ShellError if the return code is anything but 0. */ - public func finish() throws { + public func finish() throws -> AsyncShellTask { task.waitUntilExit() - if task.terminationStatus != 0 { + guard task.terminationStatus == 0 else { throw ShellError.ReturnedErrorCode(errorcode: task.terminationStatus) } + return self } } diff --git a/SwiftShellTests/Command_Tests.swift b/SwiftShellTests/Command_Tests.swift index 6115c32..57d00f8 100644 --- a/SwiftShellTests/Command_Tests.swift +++ b/SwiftShellTests/Command_Tests.swift @@ -50,4 +50,11 @@ class RunAsync_Tests: XCTestCase { AssertThrows(ShellError.ReturnedErrorCode(errorcode: 1)) { try asynctask.finish() } XCTAssertEqual( asynctask.stderror.read(), "errormessage\n" ) } + + func testFinishReturnsSelf () { + AssertNoThrow { + let output = try main.runAsync(bash: "echo hi").finish().stdout.read() + XCTAssertEqual(output, "hi\n") + } + } }