mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-12 22:10:07 +02:00
🏗 Remove synchronous terminal commands
This commit is contained in:
@@ -15,10 +15,12 @@ class SystemShellTest: XCTestCase {
|
|||||||
ActiveShell.useSystem()
|
ActiveShell.useSystem()
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_system_shell_is_default() {
|
func test_system_shell_is_default() async {
|
||||||
XCTAssertTrue(Shell is SystemShell)
|
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() {
|
func test_system_shell_has_path() {
|
||||||
|
@@ -9,6 +9,12 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
var isRunningTests: Bool {
|
||||||
|
let environment = ProcessInfo.processInfo.environment
|
||||||
|
return environment["TEST_MODE"] != nil
|
||||||
|
|| environment["XCTestConfigurationFilePath"] != nil
|
||||||
|
}
|
||||||
|
|
||||||
var isRunningSwiftUIPreview: Bool {
|
var isRunningSwiftUIPreview: Bool {
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// If running SwiftUI *and* when debugging
|
// If running SwiftUI *and* when debugging
|
||||||
|
@@ -30,12 +30,6 @@ struct ShellOutput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protocol Shellable {
|
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.
|
Run a command asynchronously.
|
||||||
Returns the most relevant output (prefers error output if it exists).
|
Returns the most relevant output (prefers error output if it exists).
|
||||||
|
@@ -82,7 +82,7 @@ class SystemShell: Shellable {
|
|||||||
|
|
||||||
// MARK: - Shellable Protocol
|
// MARK: - Shellable Protocol
|
||||||
|
|
||||||
func sync(_ command: String) -> ShellOutput {
|
func pipe(_ command: String) async -> ShellOutput {
|
||||||
let task = getShellProcess(for: command)
|
let task = getShellProcess(for: command)
|
||||||
|
|
||||||
let outputPipe = Pipe()
|
let outputPipe = Pipe()
|
||||||
@@ -106,10 +106,6 @@ class SystemShell: Shellable {
|
|||||||
return .out(stdOut, stdErr)
|
return .out(stdOut, stdErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pipe(_ command: String) async -> ShellOutput {
|
|
||||||
return sync(command)
|
|
||||||
}
|
|
||||||
|
|
||||||
func quiet(_ command: String) async {
|
func quiet(_ command: String) async {
|
||||||
_ = await self.pipe(command)
|
_ = await self.pipe(command)
|
||||||
}
|
}
|
||||||
|
@@ -18,11 +18,12 @@ public class TestableShell: Shellable {
|
|||||||
var expectations: [Input: BatchFakeShellOutput] = [:]
|
var expectations: [Input: BatchFakeShellOutput] = [:]
|
||||||
|
|
||||||
func quiet(_ command: String) async {
|
func quiet(_ command: String) async {
|
||||||
return
|
_ = try! await self.attach(command, didReceiveOutput: { _, _ in }, withTimeout: 60)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pipe(_ command: String) async -> ShellOutput {
|
func pipe(_ command: String) async -> ShellOutput {
|
||||||
self.sync(command)
|
let (_, output) = try! await self.attach(command, didReceiveOutput: { _, _ in }, withTimeout: 60)
|
||||||
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func attach(
|
func attach(
|
||||||
@@ -30,14 +31,15 @@ public class TestableShell: Shellable {
|
|||||||
didReceiveOutput: @escaping (String, ShellStream) -> Void,
|
didReceiveOutput: @escaping (String, ShellStream) -> Void,
|
||||||
withTimeout timeout: TimeInterval
|
withTimeout timeout: TimeInterval
|
||||||
) async throws -> (Process, ShellOutput) {
|
) async throws -> (Process, ShellOutput) {
|
||||||
return (Process(), self.sync(command))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sync(_ command: String) -> ShellOutput {
|
|
||||||
guard let expectation = expectations[command] else {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user