Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Oct 28, 2024
1 parent 07731ee commit e8bc37c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
54 changes: 29 additions & 25 deletions Sources/Merge/Intramodular/Process/_AsyncProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [])

Expand Down Expand Up @@ -287,35 +287,39 @@ extension _AsyncProcess {
try await readData()
}

try? await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
@MutexProtected
var didResume: Bool = false

process.terminationHandler = { (process: Process) in
Task<Void, Never>.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<Void, Error>) in
@MutexProtected
var didResume: Bool = false

process.terminationHandler = { (process: Process) in
Task<Void, Never>.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
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 14 additions & 3 deletions Tests/_AsyncProcessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
}
}

0 comments on commit e8bc37c

Please sign in to comment.