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

🏗 Remove synchronous terminal commands

This commit is contained in:
2022-10-04 18:40:41 +02:00
parent 953ccb3792
commit 8a6139d5e7
5 changed files with 21 additions and 21 deletions

View File

@ -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() {

View File

@ -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

View File

@ -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).

View File

@ -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)
}

View File

@ -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)
}
}