From 5399bddfebd98329beda1abf7baa6ea3af1ff601 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 26 Sep 2022 20:37:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20WIP:=20Shell=20rework?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon-tests/Next/ShellTest.swift | 11 ++++++- phpmon/Next/NxtShell.swift | 3 +- phpmon/Next/Shellable.swift | 1 + phpmon/Next/TestableShell.swift | 53 ++++++++++++++++++++++++++++--- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/phpmon-tests/Next/ShellTest.swift b/phpmon-tests/Next/ShellTest.swift index 98d24c0..e7b937a 100644 --- a/phpmon-tests/Next/ShellTest.swift +++ b/phpmon-tests/Next/ShellTest.swift @@ -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")) } } diff --git a/phpmon/Next/NxtShell.swift b/phpmon/Next/NxtShell.swift index f924b7c..540d289 100644 --- a/phpmon/Next/NxtShell.swift +++ b/phpmon/Next/NxtShell.swift @@ -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) } diff --git a/phpmon/Next/Shellable.swift b/phpmon/Next/Shellable.swift index b238a36..441c846 100644 --- a/phpmon/Next/Shellable.swift +++ b/phpmon/Next/Shellable.swift @@ -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 } diff --git a/phpmon/Next/TestableShell.swift b/phpmon/Next/TestableShell.swift index ccdd2af..a210b7e 100644 --- a/phpmon/Next/TestableShell.swift +++ b/phpmon/Next/TestableShell.swift @@ -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 + } +}