1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-07 21:20:07 +01:00
This commit is contained in:
2022-10-04 17:57:05 +02:00
parent c26c491340
commit 953ccb3792
6 changed files with 52 additions and 93 deletions

View File

@@ -57,16 +57,9 @@ import Foundation
let (process, _) = try await Shell.attach(
command,
didReceiveOutput: { [weak self] output in
didReceiveOutput: { [weak self] (incoming, _) in
guard let window = self?.window else { return }
if output.hasError {
window.addToConsole(output.err)
}
if !output.out.isEmpty {
window.addToConsole(output.out)
}
window.addToConsole(incoming)
},
withTimeout: .minutes(5)
)

View File

@@ -8,6 +8,10 @@
import Foundation
enum ShellStream {
case stdOut, stdErr, stdIn
}
struct ShellOutput {
var out: String
var err: String
@@ -56,7 +60,7 @@ protocol Shellable {
*/
func attach(
_ command: String,
didReceiveOutput: @escaping (ShellOutput) -> Void,
didReceiveOutput: @escaping (String, ShellStream) -> Void,
withTimeout timeout: TimeInterval
) async throws -> (Process, ShellOutput)
}

View File

@@ -116,18 +116,16 @@ class SystemShell: Shellable {
func attach(
_ command: String,
didReceiveOutput: @escaping (ShellOutput) -> Void,
didReceiveOutput: @escaping (String, ShellStream) -> Void,
withTimeout timeout: TimeInterval = 5.0
) async throws -> (Process, ShellOutput) {
let task = getShellProcess(for: command)
var output = ShellOutput(out: "", err: "")
var allOut: String = ""
var allErr: String = ""
task.listen { stdOut in
allOut += stdOut; didReceiveOutput(.out(stdOut))
} didReceiveStandardErrorData: { stdErr in
allErr += stdErr; didReceiveOutput(.err(stdErr))
task.listen { incoming in
output.out += incoming; didReceiveOutput(incoming, .stdOut)
} didReceiveStandardErrorData: { incoming in
output.err += incoming; didReceiveOutput(incoming, .stdErr)
}
return try await withCheckedThrowingContinuation({ continuation in
@@ -138,11 +136,11 @@ class SystemShell: Shellable {
timer?.invalidate()
if !allErr.isEmpty {
return continuation.resume(returning: (process, .err(allErr)))
if !output.err.isEmpty {
return continuation.resume(returning: (process, .err(output.err)))
}
return continuation.resume(returning: (process, .out(allOut)))
return continuation.resume(returning: (process, .out(output.out)))
}
timer = Timer.scheduledTimer(withTimeInterval: timeout, repeats: false) { _ in

View File

@@ -27,7 +27,7 @@ public class TestableShell: Shellable {
func attach(
_ command: String,
didReceiveOutput: @escaping (ShellOutput) -> Void,
didReceiveOutput: @escaping (String, ShellStream) -> Void,
withTimeout timeout: TimeInterval
) async throws -> (Process, ShellOutput) {
return (Process(), self.sync(command))
@@ -41,22 +41,17 @@ public class TestableShell: Shellable {
}
}
// TODO: Test env shell output should be modeled differently
// So the possible outcome is either:
// 1. Immediate with almost zero delay `.instant("string")`
// 2. Delayed but then all at once: `.delay(300, "string")`
// 3. A stream of data spread over multiple seconds: `.multiple([.delay(300, "hello"), .delay(300, "bye")])`
struct FakeShellOutput {
let delay: TimeInterval
let output: ShellOutput
let output: String
let stream: ShellStream
static func instant(_ stdOut: String, _ stdErr: String? = nil) -> FakeShellOutput {
return FakeShellOutput(delay: 0, output: ShellOutput(out: stdOut, err: stdErr ?? ""))
static func instant(_ output: String, _ stream: ShellStream = .stdOut) -> FakeShellOutput {
return FakeShellOutput(delay: 0, output: output, stream: stream)
}
static func delayed(_ delay: TimeInterval, _ stdOut: String, _ stdErr: String? = nil) -> FakeShellOutput {
return FakeShellOutput(delay: delay, output: ShellOutput(out: stdOut, err: stdErr ?? ""))
static func delayed(_ delay: TimeInterval, _ output: String, _ stream: ShellStream = .stdOut) -> FakeShellOutput {
return FakeShellOutput(delay: delay, output: output, stream: stream)
}
}
@@ -67,37 +62,33 @@ struct BatchFakeShellOutput {
Outputs the fake shell output as expected.
*/
public func output(
didReceiveOutput: @escaping (ShellOutput) -> Void,
didReceiveOutput: @escaping (String, ShellStream) -> Void,
ignoreDelay: Bool = false
) async -> ShellOutput {
var allOut: String = ""
var allErr: String = ""
var output = ShellOutput(out: "", err: "")
Task {
self.items.forEach { fakeShellOutput in
let delay = UInt64(fakeShellOutput.delay * 1_000_000_000)
try await Task.sleep(nanoseconds: delay)
for item in items {
if !ignoreDelay {
let delay = UInt64(item.delay * 1_000_000_000)
try! await Task.sleep(nanoseconds: delay)
}
allOut += fakeShellOutput.output.out
if fakeShellOutput.output.hasError {
allErr += fakeShellOutput.output.err
}
if item.stream == .stdErr {
output.err += item.output
} else if item.stream == .stdOut {
output.out += item.output
}
}
return ShellOutput(
out: allOut,
err: allErr
)
return output
}
/**
For testing purposes (and speed) we may omit the delay, regardless of its timespan.
*/
public func outputInstantaneously(
didReceiveOutput: @escaping (ShellOutput) -> Void
) -> ShellOutput {
self.output(didReceiveOutput: didReceiveOutput, ignoreDelay: true)
didReceiveOutput: @escaping (String, ShellStream) -> Void = { _, _ in }
) async -> ShellOutput {
return await self.output(didReceiveOutput: didReceiveOutput, ignoreDelay: true)
}
}