1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

🏗 WIP: Shell rework

This commit is contained in:
2022-09-26 20:37:24 +02:00
parent a682d0cfb0
commit 5399bddfeb
4 changed files with 61 additions and 7 deletions

View File

@ -32,12 +32,21 @@ class ShellTest: XCTestCase {
with Xdebug v3.1.4, Copyright (c) 2002-2022, by Derick Rethans
"""
let slowVersionOutput = FakeTerminalOutput(
output: expectedPhpOutput,
duration: 1000,
isError: false
)
NxtShell.useTestable([
"php -v": expectedPhpOutput
"php -v": expectedPhpOutput,
"php --version": slowVersionOutput
])
XCTAssertTrue(NxtShell.shared is TestableShell)
XCTAssertEqual(expectedPhpOutput, NxtShell.shared.syncPipe("php -v"))
XCTAssertEqual(expectedPhpOutput, NxtShell.shared.syncPipe("php --version"))
}
}

View File

@ -12,7 +12,8 @@ class NxtShell {
static var shared: Shellable = SystemShell()
/// Uses a testable shell with predefined responses. You specify the terminal's output.
public static func useTestable(_ expectations: [String: String]) {
/// they also work with simple String objects.
public static func useTestable(_ expectations: [String: OutputsToShell]) {
Self.shared = TestableShell(expectations: expectations)
}

View File

@ -9,6 +9,7 @@
import Foundation
protocol Shellable {
// TODO: Rework this so it supports listening for updates (when piping) and
func syncPipe(_ command: String) -> String
func pipe(_ command: String) async -> String
}

View File

@ -8,18 +8,61 @@
import Foundation
class TestableShell: Shellable {
init(expectations: [String: String]) {
public class TestableShell: Shellable {
public typealias Input = String
init(expectations: [Input: OutputsToShell]) {
self.expectations = expectations
}
var expectations: [String: String] = [:]
var expectations: [Input: OutputsToShell] = [:]
func pipe(_ command: String) async -> String {
return expectations[command] ?? ""
// TODO: Deal with the duration and output to error
return expectations[command]?.getOutputAsString() ?? ""
}
func syncPipe(_ command: String) -> String {
return expectations[command] ?? ""
// TODO: Deal with the duration and output to error
return expectations[command]?.getOutputAsString() ?? ""
}
}
protocol OutputsToShell {
func getOutputAsString() -> String
func getDuration() -> Int
func outputsToError() -> Bool
}
struct FakeTerminalOutput: OutputsToShell {
var output: String
var duration: Int
var isError: Bool
func getOutputAsString() -> String {
return output
}
func getDuration() -> Int {
return duration
}
func outputsToError() -> Bool {
return isError
}
}
extension String: OutputsToShell {
func getOutputAsString() -> String {
return self
}
func getDuration() -> Int {
return 100
}
func outputsToError() -> Bool {
return false
}
}