Skip to content

Commit eac3e07

Browse files
authored
Merge pull request #636 from jflan-dd/jflan/drain-stderr
2 parents 14cc1c5 + 4390d72 commit eac3e07

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Replace deprecated properties and functions in Process [@417-72KI][] - [#634](https://github.com/danger/swift/pull/634)
2424
- Support user type mannequin on for GitHub [@f-meloni][] - [#638](https://github.com/danger/swift/pull/638)
2525
- Add Android support [@marcprux][] - [#635](https://github.com/danger/swift/pull/635)
26+
- Drain stdout and stderr concurrently [@jflan-dd] - [#636](https://github.com/danger/swift/pull/636)
2627

2728
## 3.20.2
2829

Sources/DangerShellExecutor/ShellExecutor.swift

+19-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extension ShellExecuting {
5454
}
5555

5656
public struct ShellExecutor: ShellExecuting {
57+
/// Queue used to concurrently listen to both stdout and stderr
58+
private let outputQueue = DispatchQueue(label: "ShellExecutor.outputQueue", attributes: .concurrent)
59+
5760
public init() {}
5861

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

101-
// Pull out the STDOUT as a string because we'll need that regardless
102-
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
103-
let stdoutString = String(data: stdoutData, encoding: .utf8)!
104+
let group = DispatchGroup()
105+
106+
var stdoutString: String!
107+
var stderrData: Data!
108+
109+
outputQueue.async(group: group, qos: .userInitiated) {
110+
// Pull out the STDOUT as a string because we'll need that regardless
111+
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
112+
stdoutString = String(data: stdoutData, encoding: .utf8)!
113+
}
114+
115+
outputQueue.async(group: group, qos: .userInitiated) {
116+
// Read from STDERR to ensure the `Pipe` does not fill up
117+
stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
118+
}
104119

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

108122
task.waitUntilExit()
109123

0 commit comments

Comments
 (0)