1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02: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

@ -31,14 +31,18 @@ class FakeShellTest: XCTestCase {
XCTAssertTrue(Shell is TestableShell)
XCTAssertEqual(expectedPhpOutput, Shell.sync("php -v").output)
XCTAssertEqual(expectedPhpOutput, Shell.sync("php -v").out)
XCTAssertEqual(expectedPhpOutput, Shell.sync("php --version").output)
XCTAssertEqual(expectedPhpOutput, Shell.sync("php --version").out)
}
func test_unrecognized_commands_output_stderr() {
ActiveShell.useTestable([:])
XCTAssertEqual("Unexpected Command", Shell.sync("unrecognized command").output)
let output = Shell.sync("unrecognized command")
XCTAssertTrue(output.hasError)
XCTAssertEqual("Unexpected Command", output.err)
XCTAssertEqual("", output.out)
}
}

View File

@ -9,10 +9,16 @@
import XCTest
class SystemShellTest: XCTestCase {
override class func setUp() {
// Reset to the default shell
ActiveShell.useSystem()
}
func test_system_shell_is_default() {
XCTAssertTrue(Shell is SystemShell)
XCTAssertTrue(Shell.sync("php -v").output.contains("Copyright (c) The PHP Group"))
XCTAssertTrue(Shell.sync("php -v").out.contains("Copyright (c) The PHP Group"))
}
func test_system_shell_has_path() {
@ -28,14 +34,14 @@ class SystemShellTest: XCTestCase {
let shellOutput = try! await Shell.attach(
"php -r \"echo 'Hello world' . PHP_EOL; usleep(200); echo 'Goodbye world';\"",
didReceiveOutput: { incoming in
bits.append(incoming.output)
bits.append(incoming.out)
},
withTimeout: 2.0
)
XCTAssertTrue(bits.contains("Hello world\n"))
XCTAssertTrue(bits.contains("Goodbye world"))
XCTAssertEqual("Hello world\nGoodbye world", shellOutput.output)
XCTAssertEqual("Hello world\nGoodbye world", shellOutput.out)
}
func test_system_shell_can_timeout_and_throw_error() async {
@ -52,6 +58,6 @@ class SystemShellTest: XCTestCase {
expectation.fulfill()
}
wait(for: [expectation], timeout: 3.0)
wait(for: [expectation], timeout: 5.0)
}
}

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)
}
}