From e8bc37c8dc203cab481efedd71237c151882c007 Mon Sep 17 00:00:00 2001 From: Vatsal Manot Date: Tue, 29 Oct 2024 00:03:10 +0530 Subject: [PATCH] Update package --- .../Intramodular/Process/_AsyncProcess.swift | 54 ++++++++++--------- Tests/_AsyncProcessTests.swift | 17 ++++-- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Sources/Merge/Intramodular/Process/_AsyncProcess.swift b/Sources/Merge/Intramodular/Process/_AsyncProcess.swift index dc77b9d..a0a2562 100644 --- a/Sources/Merge/Intramodular/Process/_AsyncProcess.swift +++ b/Sources/Merge/Intramodular/Process/_AsyncProcess.swift @@ -36,7 +36,7 @@ public class _AsyncProcess: Logging { #if os(macOS) public init( existingProcess: Process?, - options: [_AsyncProcess.Option] + options: [_AsyncProcess.Option]? ) throws { let options: Set<_AsyncProcess.Option> = Set(options ?? []) @@ -287,35 +287,39 @@ extension _AsyncProcess { try await readData() } - try? await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - @MutexProtected - var didResume: Bool = false - - process.terminationHandler = { (process: Process) in - Task.detached(priority: .userInitiated) { - await Task.yield() - - if let terminationError = process.terminationError { - continuation.resume(throwing: terminationError) - } else { - assert(!process.isRunning) + do { + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + @MutexProtected + var didResume: Bool = false + + process.terminationHandler = { (process: Process) in + Task.detached(priority: .userInitiated) { + await Task.yield() + + if let terminationError = process.terminationError { + continuation.resume(throwing: terminationError) + } else { + assert(!process.isRunning) + + continuation.resume() + } - continuation.resume() + $didResume.assignedValue = true } + } + + do { + _willRunRightAfterThis() - $didResume.assignedValue = true + try process.run() + } catch { + continuation.resume(throwing: error) } - } - - do { - _willRunRightAfterThis() - try process.run() - } catch { - continuation.resume(throwing: error) + _didJustExit(didResume: { didResume }) } - - _didJustExit(didResume: { didResume }) + } catch { + runtimeIssue(error) } try await readStdoutStderrTask.value @@ -451,7 +455,7 @@ extension _AsyncProcess { } private func __handleData(_ data: Data, forPipe pipe: Pipe) async throws { - guard data.isEmpty else { + guard !data.isEmpty else { return } diff --git a/Tests/_AsyncProcessTests.swift b/Tests/_AsyncProcessTests.swift index f65c9db..312a350 100644 --- a/Tests/_AsyncProcessTests.swift +++ b/Tests/_AsyncProcessTests.swift @@ -10,15 +10,15 @@ import XCTest final class _AsyncProcessTests: XCTestCase { func testEcho() async throws { for _ in 0..<10 { - let process = _AsyncProcess(existingProcess: Process(command: "echo hello"), options: []) + let process = try _AsyncProcess(existingProcess: Process(command: "echo hello"), options: []) let result: Process.RunResult = try await process.run() - + XCTAssert(result.stdoutString == "hello") } for _ in 0..<10 { - let process = _AsyncProcess(existingProcess: Process(command: "echo hello"), options: []) + let process = try _AsyncProcess(existingProcess: Process(command: "echo hello"), options: []) try await process.start() @@ -27,4 +27,15 @@ final class _AsyncProcessTests: XCTestCase { XCTAssert(result.stdoutString == "hello") } } + + func testFoo() async throws { + let process = try await _AsyncProcess( + command: "echo Hello", + options: [._forwardStdoutStderr] + ) + + let result = try await process.run() + + XCTAssertEqual(result.stdoutString!, "Hello") + } }