diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..8c50098 --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +3.1 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..06f52f3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +os: linux +language: generic +sudo: required +dist: trusty +install: + - eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)" +script: + - swift test diff --git a/Sources/ShellOut.swift b/Sources/ShellOut.swift index c8af0f6..6cee545 100644 --- a/Sources/ShellOut.swift +++ b/Sources/ShellOut.swift @@ -15,7 +15,9 @@ import Foundation * - parameter arguments: The arguments to pass to the command * - parameter path: The path to execute the commands at (defaults to current folder) * - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to + * (at the moment this is only supported on macOS) * - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to + * (at the moment this is only supported on macOS) * * - returns: The output of running the command * - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error @@ -39,7 +41,9 @@ import Foundation * - parameter commands: The commands to run * - parameter path: The path to execute the commands at (defaults to current folder) * - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to + * (at the moment this is only supported on macOS) * - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to + * (at the moment this is only supported on macOS) * * - returns: The output of running the command * - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error @@ -315,34 +319,42 @@ private extension Process { var outputData = Data() var errorData = Data() - let stdoutHandler: (FileHandle) -> Void = { handler in + let outputPipe = Pipe() + standardOutput = outputPipe + + let errorPipe = Pipe() + standardError = errorPipe + + #if !os(Linux) + outputPipe.fileHandleForReading.readabilityHandler = { handler in let data = handler.availableData outputData.append(data) outputHandle?.write(data) } - let stderrHandler: (FileHandle) -> Void = { handler in + errorPipe.fileHandleForReading.readabilityHandler = { handler in let data = handler.availableData errorData.append(data) errorHandle?.write(data) } + #endif - let outputPipe = Pipe() - standardOutput = outputPipe - outputPipe.fileHandleForReading.readabilityHandler = stdoutHandler + launch() - let errorPipe = Pipe() - standardError = errorPipe - errorPipe.fileHandleForReading.readabilityHandler = stderrHandler + #if os(Linux) + outputData = outputPipe.fileHandleForReading.readDataToEndOfFile() + errorData = errorPipe.fileHandleForReading.readDataToEndOfFile() + #endif - launch() waitUntilExit() outputHandle?.closeFile() errorHandle?.closeFile() + #if !os(Linux) outputPipe.fileHandleForReading.readabilityHandler = nil errorPipe.fileHandleForReading.readabilityHandler = nil + #endif if terminationStatus != 0 { throw ShellOutError( diff --git a/Tests/ShellOutTests/ShellOutTests+Linux.swift b/Tests/ShellOutTests/ShellOutTests+Linux.swift index fef0b31..508ad34 100644 --- a/Tests/ShellOutTests/ShellOutTests+Linux.swift +++ b/Tests/ShellOutTests/ShellOutTests+Linux.swift @@ -13,8 +13,12 @@ extension ShellOutTests { ("testWithoutArguments", testWithoutArguments), ("testWithArguments", testWithArguments), ("testWithInlineArguments", testWithInlineArguments), + ("testSingleCommandAtPath", testSingleCommandAtPath), + ("testSeriesOfCommands", testSeriesOfCommands), + ("testSeriesOfCommandsAtPath", testSeriesOfCommandsAtPath), ("testThrowingError", testThrowingError), - ("testRedirection", testRedirection) + ("testGitCommands", testGitCommands), + ("testSwiftPackageManagerCommands", testSwiftPackageManagerCommands) ] } #endif