Skip to content

Commit

Permalink
Add process description to error output
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoconti83 committed Aug 10, 2016
1 parent bfb8e62 commit 5620771
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
34 changes: 18 additions & 16 deletions Sources/Subprocess+CompactAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ extension Subprocess {
_ arguments: String...,
workingDirectory: String = ".") -> String {

guard let result = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory).execute(true) else {
Error.die("Can't execute \(executablePath) \(arguments.joinWithSeparator(" "))")
let process = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory)
guard let result = process.execute(true) else {
Error.die("Can't execute \"\(process)\"")
}
if result.status != 0 {
let errorLines = result.errors == "" ? "" : "\n" + result.errors
Error.die("Process \(executablePath) returned non-zero status", errorLines, "\n",
"with arguments: \(arguments.joinWithSeparator(" "))")
Error.die("Process \"\(process)\" returned status \(result.status)", errorLines)
}
return result.output
}
Expand All @@ -63,8 +63,9 @@ extension Subprocess {
_ arguments: String...,
workingDirectory: String = ".") -> ExecutionResult {

guard let result = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory).execute(true) else {
Error.die("Can't execute \(executablePath) \(arguments.joinWithSeparator(" "))")
let process = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory)
guard let result = process.execute(true) else {
Error.die("Can't execute \"\(process)\"")
}
return result
}
Expand All @@ -81,13 +82,13 @@ extension Subprocess {
_ arguments: String...,
workingDirectory: String = ".") -> [String] {

guard let result = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory).execute(true) else {
Error.die("Can't execute \(executablePath) \(arguments.joinWithSeparator(" "))")
let process = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory)
guard let result = process.execute(true) else {
Error.die("Can't execute \"\(process)\"")
}
if result.status != 0 {
let errorLines = result.errors == "" ? "" : "\n" + result.errors
Error.die("Process \(executablePath) returned non-zero status", errorLines, "\n",
"with arguments: \(arguments.joinWithSeparator(" "))")
Error.die("Process \"\(process)\" returned status \(result.status)", errorLines)
}
return result.outputLines
}
Expand All @@ -104,8 +105,9 @@ extension Subprocess {
_ arguments: String...,
workingDirectory: String = ".") -> Int32 {

guard let result = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory).run() else {
Error.die("Can't execute \(executablePath) \(arguments.joinWithSeparator(" "))")
let process = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory)
guard let result = process.run() else {
Error.die("Can't execute \"\(process)\"")
}
return result
}
Expand All @@ -121,12 +123,12 @@ extension Subprocess {
_ arguments: String...,
workingDirectory: String = ".") {

guard let result = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory).run() else {
Error.die("Can't execute \(executablePath) \(arguments.joinWithSeparator(" "))")
let process = Subprocess.init(executablePath: executablePath, arguments: arguments, workingDirectory: workingDirectory)
guard let result = process.run() else {
Error.die("Can't execute \"\(process)\"")
}
if result != 0 {
Error.die("Process \(executablePath) returned non-zero status\n",
"with arguments: \(arguments.joinWithSeparator(" "))")
Error.die("Process \"\(process)\" returned status \(result)")
}
}
}
15 changes: 15 additions & 0 deletions Sources/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,18 @@ extension Subprocess {
return TaskPipeline(task: task, captureOutput: captureOutput)
}
}

// MARK: - Description
extension Subprocess : CustomStringConvertible {

public var description : String {
return self.executablePath
+ (self.arguments.count > 0
? " " + self.arguments
.map { $0.stringByReplacingOccurrencesOfString(" ", withString: "\\ ") }
.joinWithSeparator(" ")
: ""
)
+ (self.pipeDestination != nil ? " | " + self.pipeDestination!.description : "" )
}
}
48 changes: 48 additions & 0 deletions Tests/SubprocessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,51 @@ extension SubprocessTests {
// then: not crashing
}
}

// MARK: - Description
extension SubprocessTests {

func testThatItDescribesExecutable() {

// given
let path = "/bin/ls"
let sut = Subprocess(path)

// then
XCTAssertEqual(sut.description, path)
}

func testThatItDescribesExecutableAndArguments() {

// given
let path = "/bin/ls"
let sut = Subprocess(path, "-l", "foo")

// then
XCTAssertEqual(sut.description, "\(path) -l foo")
}

func testThatItDescribesExecutableAndArgumentsWithSpace() {

// given
let path = "/bin/ls"
let sut = Subprocess(path, "-l", "My Documents")

// then
XCTAssertEqual(sut.description, "\(path) -l My\\ Documents")
}

func testThatItDescribesPipe() {

// given
let sut1 = Subprocess("/bin/echo", "That's it")
let sut2 = Subprocess("/usr/bin/grep", "it")
let sut3 = Subprocess("/usr/bin/sed", "s/it/not it/")

// when
let sut = sut1 | sut2 | sut3

// then
XCTAssertEqual(sut.description, "\(sut1.description) | \(sut2.description) | \(sut3.description)")
}
}

0 comments on commit 5620771

Please sign in to comment.