diff --git a/SwiftShell/Command.swift b/SwiftShell/Command.swift index dda8694..000cd79 100644 --- a/SwiftShell/Command.swift +++ b/SwiftShell/Command.swift @@ -70,6 +70,10 @@ extension ShellContextType { - returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line. */ public func run (executable: String, _ args: String ...) -> String { + return run(executable, args) + } + + public func run (executable: String, _ args: [String]) -> String { return outputFromRun(setupTask(executable, args: args)) } @@ -149,6 +153,10 @@ extension ShellContextType { - returns: an AsyncShellTask with standard output, standard error and a 'finish' function. */ public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask { + return runAsync(executable, args) + } + + public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask { return AsyncShellTask(task: setupTask(executable, args: args)) } @@ -167,6 +175,10 @@ extension ShellContextType { extension ShellContextType { public func runAndPrint (executable: String, _ args: String ...) throws { + return try runAndPrint(executable, args) + } + + public func runAndPrint (executable: String, _ args: [String]) throws { let task = setupTask(executable, args: args) task.launch() try task.finish() @@ -178,3 +190,41 @@ extension ShellContextType { try task.finish() } } + +// MARK: Global functions + +public func runAndPrint (executable: String, _ args: String ...) throws { + return try main.runAndPrint(executable, args) +} + +public func runAndPrint (executable: String, _ args: [String]) throws { + return try main.runAndPrint(executable, args) +} + +public func runAndPrint (bash bashcommand: String) throws { + return try main.runAndPrint(bash: bashcommand) +} + +public func run (executable: String, _ args: String ...) -> String { + return main.run(executable, args) +} + +public func run (executable: String, _ args: [String]) -> String { + return main.run(executable, args) +} + +public func run (bash bashcommand: String) -> String { + return main.run(bash: bashcommand) +} + +public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask { + return main.runAsync(executable, args) +} + +public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask { + return main.runAsync(executable, args) +} + +public func runAsync (bash bashcommand: String) -> AsyncShellTask { + return main.runAsync(bash: bashcommand) +} diff --git a/SwiftShellTests/Command_Tests.swift b/SwiftShellTests/Command_Tests.swift index 3408083..72c4004 100644 --- a/SwiftShellTests/Command_Tests.swift +++ b/SwiftShellTests/Command_Tests.swift @@ -12,15 +12,19 @@ import XCTest class Run_Tests: XCTestCase { func testBashCommand () { - XCTAssertEqual( main.run(bash:"echo one"), "one" ) + XCTAssertEqual( SwiftShell.run(bash:"echo one"), "one" ) + } + + func testArgumentsFromArray () { + XCTAssertEqual( SwiftShell.run("/bin/echo", ["one", "two"]), "one two" ) } func testSinglelineOutput () { - XCTAssertEqual( main.run("/bin/echo", "one", "two"), "one two" ) + XCTAssertEqual( SwiftShell.run("/bin/echo", "one", "two"), "one two" ) } func testMultilineOutput () { - XCTAssertEqual( main.run("/bin/echo", "one\ntwo"), "one\ntwo\n" ) + XCTAssertEqual( SwiftShell.run("/bin/echo", "one\ntwo"), "one\ntwo\n" ) } } @@ -29,7 +33,7 @@ import CatchingFire class RunAsync_Tests: XCTestCase { func testReturnsStandardOutput () { - let asynctask = main.runAsync("/bin/echo", "one", "two" ) + let asynctask = runAsync("/bin/echo", "one", "two" ) AssertNoThrow { try asynctask.finish() } XCTAssertEqual( asynctask.stdout.read(), "one two\n" ) @@ -37,26 +41,26 @@ class RunAsync_Tests: XCTestCase { } func testReturnsStandardError () { - let asynctask = main.runAsync(bash: "echo one two > /dev/stderr" ) + let asynctask = runAsync(bash: "echo one two > /dev/stderr" ) AssertNoThrow { try asynctask.finish() } XCTAssertEqual( asynctask.stderror.read(), "one two\n" ) XCTAssertEqual( asynctask.stdout.read(), "" ) } + func testArgumentsFromArray () { + AssertNoThrow { + let output = try runAsync("/bin/echo", ["one", "two"]).finish().stdout.read() + XCTAssertEqual( output, "one two\n" ) + } + } + func testThrowsErrorOnExitcodeNotZero () { - let asynctask = main.runAsync(bash: "echo errormessage > /dev/stderr; exit 1" ) + let asynctask = runAsync(bash: "echo errormessage > /dev/stderr; exit 1" ) 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") - } - } } class RunAndPrint_Tests: XCTestCase { @@ -75,20 +79,26 @@ class RunAndPrint_Tests: XCTestCase { } func testReturnsStandardOutput () { - AssertNoThrow { try main.runAndPrint("/bin/echo", "one", "two" ) } + AssertNoThrow { try runAndPrint("/bin/echo", "one", "two" ) } + + XCTAssertEqual( test_stdout.readSome(), "one two\n" ) + } + + func testArgumentsFromArray () { + AssertNoThrow { try runAndPrint("/bin/echo", ["one", "two"] ) } XCTAssertEqual( test_stdout.readSome(), "one two\n" ) } func testReturnsStandardError () { - AssertNoThrow { try main.runAndPrint(bash: "echo one two > /dev/stderr" ) } + AssertNoThrow { try runAndPrint(bash: "echo one two > /dev/stderr" ) } XCTAssertEqual( test_stderr.readSome(), "one two\n" ) } func testThrowsErrorOnExitcodeNotZero () { AssertThrows(ShellError.ReturnedErrorCode(errorcode: 1)) - { try main.runAndPrint(bash: "echo errormessage > /dev/stderr; exit 1" ) } + { try runAndPrint(bash: "echo errormessage > /dev/stderr; exit 1" ) } XCTAssertEqual( test_stderr.readSome(), "errormessage\n" ) }