Skip to content

Commit

Permalink
Merge pull request #67 from Carthage/fix-stderr-output
Browse files Browse the repository at this point in the history
[TaskEvent] Fix standard error handling
  • Loading branch information
mdiep authored Jul 17, 2016
2 parents 44dcde2 + 2c66aa8 commit dd8e927
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions ReactiveTask/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError
}

return SignalProducer { observer, disposable in
func startAggregating(producer: Pipe.ReadProducer) -> Pipe.ReadProducer {
func startAggregating(producer: Pipe.ReadProducer, chunk: (NSData) -> TaskEvent<NSData>) -> Pipe.ReadProducer {
let aggregated = MutableProperty<Aggregation?>(nil)

producer.startWithSignal { signal, signalDisposable in
disposable += signalDisposable

let aggregate = NSMutableData()
signal.observe(Observer(next: { data in
observer.sendNext(.StandardOutput(data))
observer.sendNext(chunk(data))
aggregate.appendData(data)
}, failed: { error in
observer.sendFailed(error)
Expand All @@ -476,8 +476,8 @@ public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError
.flatMap(.Concat) { $0.producer }
}

let stdoutAggregated = startAggregating(stdoutProducer)
let stderrAggregated = startAggregating(stderrProducer)
let stdoutAggregated = startAggregating(stdoutProducer, chunk: TaskEvent.StandardOutput)
let stderrAggregated = startAggregating(stderrProducer, chunk: TaskEvent.StandardError)

rawTask.standardOutput = stdoutPipe.writeHandle
rawTask.standardError = stderrPipe.writeHandle
Expand Down
8 changes: 4 additions & 4 deletions ReactiveTaskTests/TaskSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ class TaskSpec: QuickSpec {
}

it("should launch a task that writes to stderr") {
let aggregated = NSMutableData()
let result = launchTask(Task("/usr/bin/stat", arguments: [ "not-a-real-file" ]))
.reduce(NSMutableData()) { aggregated, event in
.reduce(aggregated) { aggregated, event in
if case let .StandardError(data) = event {
aggregated.appendData(data)
}
Expand All @@ -61,9 +62,8 @@ class TaskSpec: QuickSpec {
.single()

expect(result).notTo(beNil())
if let data = result?.value {
expect(NSString(data: data, encoding: NSUTF8StringEncoding)).to(equal("stat: not-a-real-file: stat: No such file or directory\n"))
}
expect(result?.error).notTo(beNil())
expect(NSString(data: aggregated, encoding: NSUTF8StringEncoding)).to(equal("stat: not-a-real-file: stat: No such file or directory\n"))
}

it("should launch a task with standard input") {
Expand Down

0 comments on commit dd8e927

Please sign in to comment.