From 8a6139d5e7d73c405cd09a4c1b30eabb90af9ded Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 4 Oct 2022 18:40:41 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Remove=20synchronous=20terminal?= =?UTF-8?q?=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon-tests/Next/SystemShellTest.swift | 6 ++++-- .../Domain/SwiftUI/Common/SwiftUIHelper.swift | 6 ++++++ phpmon/Next/Shellable.swift | 6 ------ phpmon/Next/SystemShell.swift | 6 +----- phpmon/Next/TestableShell.swift | 18 ++++++++++-------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/phpmon-tests/Next/SystemShellTest.swift b/phpmon-tests/Next/SystemShellTest.swift index 3d04221..207586b 100644 --- a/phpmon-tests/Next/SystemShellTest.swift +++ b/phpmon-tests/Next/SystemShellTest.swift @@ -15,10 +15,12 @@ class SystemShellTest: XCTestCase { ActiveShell.useSystem() } - func test_system_shell_is_default() { + func test_system_shell_is_default() async { XCTAssertTrue(Shell is SystemShell) - XCTAssertTrue(Shell.sync("php -v").out.contains("Copyright (c) The PHP Group")) + let output = await Shell.pipe("php -v") + + XCTAssertTrue(output.out.contains("Copyright (c) The PHP Group")) } func test_system_shell_has_path() { diff --git a/phpmon/Domain/SwiftUI/Common/SwiftUIHelper.swift b/phpmon/Domain/SwiftUI/Common/SwiftUIHelper.swift index 3598836..e553b7e 100644 --- a/phpmon/Domain/SwiftUI/Common/SwiftUIHelper.swift +++ b/phpmon/Domain/SwiftUI/Common/SwiftUIHelper.swift @@ -9,6 +9,12 @@ import Foundation import SwiftUI +var isRunningTests: Bool { + let environment = ProcessInfo.processInfo.environment + return environment["TEST_MODE"] != nil + || environment["XCTestConfigurationFilePath"] != nil +} + var isRunningSwiftUIPreview: Bool { #if DEBUG // If running SwiftUI *and* when debugging diff --git a/phpmon/Next/Shellable.swift b/phpmon/Next/Shellable.swift index 816fba1..0b4f78b 100644 --- a/phpmon/Next/Shellable.swift +++ b/phpmon/Next/Shellable.swift @@ -30,12 +30,6 @@ struct ShellOutput { } protocol Shellable { - /** - Run a command synchronously. Waits until the command is done. - Returns the most relevant output (prefers error output if it exists). - */ - func sync(_ command: String) -> ShellOutput - /** Run a command asynchronously. Returns the most relevant output (prefers error output if it exists). diff --git a/phpmon/Next/SystemShell.swift b/phpmon/Next/SystemShell.swift index 28f3637..ec8c3b8 100644 --- a/phpmon/Next/SystemShell.swift +++ b/phpmon/Next/SystemShell.swift @@ -82,7 +82,7 @@ class SystemShell: Shellable { // MARK: - Shellable Protocol - func sync(_ command: String) -> ShellOutput { + func pipe(_ command: String) async -> ShellOutput { let task = getShellProcess(for: command) let outputPipe = Pipe() @@ -106,10 +106,6 @@ class SystemShell: Shellable { return .out(stdOut, stdErr) } - func pipe(_ command: String) async -> ShellOutput { - return sync(command) - } - func quiet(_ command: String) async { _ = await self.pipe(command) } diff --git a/phpmon/Next/TestableShell.swift b/phpmon/Next/TestableShell.swift index 3605779..c2b5311 100644 --- a/phpmon/Next/TestableShell.swift +++ b/phpmon/Next/TestableShell.swift @@ -18,11 +18,12 @@ public class TestableShell: Shellable { var expectations: [Input: BatchFakeShellOutput] = [:] func quiet(_ command: String) async { - return + _ = try! await self.attach(command, didReceiveOutput: { _, _ in }, withTimeout: 60) } func pipe(_ command: String) async -> ShellOutput { - self.sync(command) + let (_, output) = try! await self.attach(command, didReceiveOutput: { _, _ in }, withTimeout: 60) + return output } func attach( @@ -30,14 +31,15 @@ public class TestableShell: Shellable { didReceiveOutput: @escaping (String, ShellStream) -> Void, withTimeout timeout: TimeInterval ) async throws -> (Process, ShellOutput) { - return (Process(), self.sync(command)) - } - - func sync(_ command: String) -> ShellOutput { guard let expectation = expectations[command] else { - return .err("Unexpected Command") + return (Process(), .err("No Expected Output")) } - return ShellOutput(out: "", err: "") + + let output = await expectation.output(didReceiveOutput: { output, type in + didReceiveOutput(output, type) + }, ignoreDelay: isRunningTests) + + return (Process(), output) } }