1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-07 21:20:07 +01:00

🏗 WIP: Even better Shell functionality

This commit is contained in:
2022-09-28 21:43:18 +02:00
parent bbac2632a2
commit 99da328921
5 changed files with 31 additions and 23 deletions

View File

@@ -8,19 +8,20 @@
import Foundation
struct ShellOutput: CustomStringConvertible {
var output: String
var isError: Bool
var description: String {
return output
struct ShellOutput {
var out: String
var err: String
var hasError: Bool {
return err.lengthOfBytes(using: .utf8) > 0
}
static func out(_ output: String) -> ShellOutput {
return ShellOutput(output: output, isError: false)
static func out(_ out: String?, _ err: String? = nil) -> ShellOutput {
return ShellOutput(out: out ?? "", err: err ?? "")
}
static func err(_ output: String) -> ShellOutput {
return ShellOutput(output: output, isError: true)
static func err(_ err: String?) -> ShellOutput {
return ShellOutput(out: "", err: err ?? "")
}
}

View File

@@ -103,11 +103,7 @@ class SystemShell: Shellable {
encoding: .utf8
)!
if stdErr.lengthOfBytes(using: .utf8) > 0 {
return ShellOutput(output: stdErr, isError: true)
}
return ShellOutput(output: stdOut, isError: false)
return .out(stdOut, stdErr)
}
func pipe(_ command: String) async -> ShellOutput {

View File

@@ -35,10 +35,11 @@ public class TestableShell: Shellable {
func sync(_ command: String) -> ShellOutput {
guard let expectation = expectations[command] else {
return ShellOutput(output: "Unexpected Command", isError: true)
return .err("Unexpected Command")
}
return ShellOutput(output: expectation.getOutputAsString(), isError: expectation.outputsToError())
let output = expectation.getOutputAsString()
return expectation.outputsToError() ? .err(output) : .out(output)
}
}