mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 03:50:08 +02:00
🏗 WIP
This commit is contained in:
@ -9,6 +9,19 @@
|
||||
import XCTest
|
||||
|
||||
class FakeShellTest: XCTestCase {
|
||||
|
||||
func test_fake_shell_output_can_be_declared() {
|
||||
let greeting = BatchFakeShellOutput(items: [
|
||||
.instant("Hello world"),
|
||||
.delayed(0.3, "Goodbye world")
|
||||
])
|
||||
|
||||
let output = greeting.outputInstantaneously()
|
||||
|
||||
XCTAssertEqual("Hello world\nGoodbye world", output.out)
|
||||
}
|
||||
|
||||
/*
|
||||
func test_we_can_predefine_responses_for_dummy_shell() {
|
||||
let expectedPhpOutput = """
|
||||
PHP 8.1.10 (cli) (built: Sep 3 2022 12:09:27) (NTS)
|
||||
@ -45,4 +58,5 @@ class FakeShellTest: XCTestCase {
|
||||
XCTAssertEqual("Unexpected Command", output.err)
|
||||
XCTAssertEqual("", output.out)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -15,13 +15,10 @@ var Shell: Shellable {
|
||||
class ActiveShell {
|
||||
static var shared: Shellable = SystemShell()
|
||||
|
||||
/// Uses a testable shell with predefined responses. You specify the terminal's output.
|
||||
/// they also work with simple String objects.
|
||||
public static func useTestable(_ expectations: [String: OutputsToShell]) {
|
||||
public static func useTestable(_ expectations: [String: BatchFakeShellOutput]) {
|
||||
Self.shared = TestableShell(expectations: expectations)
|
||||
}
|
||||
|
||||
/// Reverts back to the system shell. You do not need to call this, only after using `useTestable()`.
|
||||
public static func useSystem() {
|
||||
Self.shared = SystemShell()
|
||||
}
|
||||
|
@ -11,11 +11,11 @@ import Foundation
|
||||
public class TestableShell: Shellable {
|
||||
public typealias Input = String
|
||||
|
||||
init(expectations: [Input: OutputsToShell]) {
|
||||
init(expectations: [Input: BatchFakeShellOutput]) {
|
||||
self.expectations = expectations
|
||||
}
|
||||
|
||||
var expectations: [Input: OutputsToShell] = [:]
|
||||
var expectations: [Input: BatchFakeShellOutput] = [:]
|
||||
|
||||
func quiet(_ command: String) async {
|
||||
return
|
||||
@ -37,9 +37,7 @@ public class TestableShell: Shellable {
|
||||
guard let expectation = expectations[command] else {
|
||||
return .err("Unexpected Command")
|
||||
}
|
||||
|
||||
let output = expectation.getOutputAsString()
|
||||
return expectation.outputsToError() ? .err(output) : .out(output)
|
||||
return ShellOutput(out: "", err: "")
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,40 +47,57 @@ public class TestableShell: Shellable {
|
||||
// 2. Delayed but then all at once: `.delay(300, "string")`
|
||||
// 3. A stream of data spread over multiple seconds: `.multiple([.delay(300, "hello"), .delay(300, "bye")])`
|
||||
|
||||
protocol OutputsToShell {
|
||||
func getOutputAsString() -> String
|
||||
func getDuration() -> Int
|
||||
func outputsToError() -> Bool
|
||||
}
|
||||
struct FakeShellOutput {
|
||||
let delay: TimeInterval
|
||||
let output: ShellOutput
|
||||
|
||||
struct FakeTerminalOutput: OutputsToShell {
|
||||
var output: String
|
||||
var duration: Int
|
||||
var isError: Bool
|
||||
|
||||
func getOutputAsString() -> String {
|
||||
return output
|
||||
static func instant(_ stdOut: String, _ stdErr: String? = nil) -> FakeShellOutput {
|
||||
return FakeShellOutput(delay: 0, output: ShellOutput(out: stdOut, err: stdErr ?? ""))
|
||||
}
|
||||
|
||||
func getDuration() -> Int {
|
||||
return duration
|
||||
}
|
||||
|
||||
func outputsToError() -> Bool {
|
||||
return isError
|
||||
static func delayed(_ delay: TimeInterval, _ stdOut: String, _ stdErr: String? = nil) -> FakeShellOutput {
|
||||
return FakeShellOutput(delay: delay, output: ShellOutput(out: stdOut, err: stdErr ?? ""))
|
||||
}
|
||||
}
|
||||
|
||||
extension String: OutputsToShell {
|
||||
func getOutputAsString() -> String {
|
||||
return self
|
||||
struct BatchFakeShellOutput {
|
||||
var items: [FakeShellOutput]
|
||||
|
||||
/**
|
||||
Outputs the fake shell output as expected.
|
||||
*/
|
||||
public func output(
|
||||
didReceiveOutput: @escaping (ShellOutput) -> Void,
|
||||
ignoreDelay: Bool = false
|
||||
) async -> ShellOutput {
|
||||
var allOut: String = ""
|
||||
var allErr: String = ""
|
||||
|
||||
Task {
|
||||
self.items.forEach { fakeShellOutput in
|
||||
let delay = UInt64(fakeShellOutput.delay * 1_000_000_000)
|
||||
try await Task.sleep(nanoseconds: delay)
|
||||
|
||||
allOut += fakeShellOutput.output.out
|
||||
|
||||
if fakeShellOutput.output.hasError {
|
||||
allErr += fakeShellOutput.output.err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ShellOutput(
|
||||
out: allOut,
|
||||
err: allErr
|
||||
)
|
||||
}
|
||||
|
||||
func getDuration() -> Int {
|
||||
return 100
|
||||
}
|
||||
|
||||
func outputsToError() -> Bool {
|
||||
return false
|
||||
/**
|
||||
For testing purposes (and speed) we may omit the delay, regardless of its timespan.
|
||||
*/
|
||||
public func outputInstantaneously(
|
||||
didReceiveOutput: @escaping (ShellOutput) -> Void
|
||||
) -> ShellOutput {
|
||||
self.output(didReceiveOutput: didReceiveOutput, ignoreDelay: true)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user