Skip to content

Commit

Permalink
Merge pull request #636 from jflan-dd/jflan/drain-stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
f-meloni authored Feb 1, 2025
2 parents 14cc1c5 + 4390d72 commit eac3e07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Replace deprecated properties and functions in Process [@417-72KI][] - [#634](https://github.com/danger/swift/pull/634)
- Support user type mannequin on for GitHub [@f-meloni][] - [#638](https://github.com/danger/swift/pull/638)
- Add Android support [@marcprux][] - [#635](https://github.com/danger/swift/pull/635)
- Drain stdout and stderr concurrently [@jflan-dd] - [#636](https://github.com/danger/swift/pull/636)

## 3.20.2

Expand Down
24 changes: 19 additions & 5 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ extension ShellExecuting {
}

public struct ShellExecutor: ShellExecuting {
/// Queue used to concurrently listen to both stdout and stderr
private let outputQueue = DispatchQueue(label: "ShellExecutor.outputQueue", attributes: .concurrent)

public init() {}

public func execute(_ command: String,
Expand Down Expand Up @@ -98,12 +101,23 @@ public struct ShellExecutor: ShellExecuting {
task.standardError = stderr
try task.run()

// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
let stdoutString = String(data: stdoutData, encoding: .utf8)!
let group = DispatchGroup()

var stdoutString: String!
var stderrData: Data!

outputQueue.async(group: group, qos: .userInitiated) {
// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
stdoutString = String(data: stdoutData, encoding: .utf8)!
}

outputQueue.async(group: group, qos: .userInitiated) {
// Read from STDERR to ensure the `Pipe` does not fill up
stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
}

// Read from STDERR to ensure the `Pipe` does not fill up
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
group.wait()

task.waitUntilExit()

Expand Down

0 comments on commit eac3e07

Please sign in to comment.