1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-06 21:00: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

@@ -31,14 +31,18 @@ class FakeShellTest: XCTestCase {
XCTAssertTrue(Shell is TestableShell) 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() { func test_unrecognized_commands_output_stderr() {
ActiveShell.useTestable([:]) 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 import XCTest
class SystemShellTest: XCTestCase { class SystemShellTest: XCTestCase {
override class func setUp() {
// Reset to the default shell
ActiveShell.useSystem()
}
func test_system_shell_is_default() { func test_system_shell_is_default() {
XCTAssertTrue(Shell is SystemShell) 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() { func test_system_shell_has_path() {
@@ -28,14 +34,14 @@ class SystemShellTest: XCTestCase {
let shellOutput = try! await Shell.attach( let shellOutput = try! await Shell.attach(
"php -r \"echo 'Hello world' . PHP_EOL; usleep(200); echo 'Goodbye world';\"", "php -r \"echo 'Hello world' . PHP_EOL; usleep(200); echo 'Goodbye world';\"",
didReceiveOutput: { incoming in didReceiveOutput: { incoming in
bits.append(incoming.output) bits.append(incoming.out)
}, },
withTimeout: 2.0 withTimeout: 2.0
) )
XCTAssertTrue(bits.contains("Hello world\n")) XCTAssertTrue(bits.contains("Hello world\n"))
XCTAssertTrue(bits.contains("Goodbye world")) 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 { func test_system_shell_can_timeout_and_throw_error() async {
@@ -52,6 +58,6 @@ class SystemShellTest: XCTestCase {
expectation.fulfill() expectation.fulfill()
} }
wait(for: [expectation], timeout: 3.0) wait(for: [expectation], timeout: 5.0)
} }
} }

View File

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

View File

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

View File

@@ -35,10 +35,11 @@ public class TestableShell: Shellable {
func sync(_ command: String) -> ShellOutput { func sync(_ command: String) -> ShellOutput {
guard let expectation = expectations[command] else { 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)
} }
} }