From c6975aefd13bae52f612c9befeec4de9c674377f Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Thu, 3 Sep 2015 18:25:28 +0200 Subject: [PATCH] Make AsyncShellTask.finish return self. For method chaining. --- SwiftShell/Command.swift | 6 ++++-- SwiftShellTests/Command_Tests.swift | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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") + } + } }