Skip to content

Commit

Permalink
Modify Runner to use Open3.capture3
Browse files Browse the repository at this point in the history
[`Open3.capture3`][docs] captures `STDOUT` and `STDERR`, allowing the
`Runner` to manually redirect their output.

This commit modified `Runner` to **only** output errors to `STDERR`
(instead of to both `STDOUT` and `STDERR`).

[docs]: http://ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-capture3
  • Loading branch information
seanpdoyle committed Mar 18, 2016
1 parent f57ce82 commit 33e2ad4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
master
------

* Remove dependency on `tee`. Fixes bug [#417][#417].
* Only write errors to `STDERR`. [#421]
* Remove dependency on `tee`. Fixes bug [#417][#417]. [#420]

[#421]: https://github.com/thoughtbot/ember-cli-rails/issues/421
[#420]: https://github.com/thoughtbot/ember-cli-rails/issues/420
[#417]: https://github.com/thoughtbot/ember-cli-rails/issues/417

0.7.2
Expand Down
33 changes: 9 additions & 24 deletions lib/ember_cli/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@ def initialize(out:, err:, env: {}, options: {})
end

def run(command)
output, status = Open3.capture2e(env, command, options)
output, error, status = Open3.capture3(env, command, options)

write_to_out(output)
write(output, streams: out_streams)
write(error, streams: err_streams)

[output, status]
status
end

def run!(command)
output, status = run(command)

unless status.success?
write_to_err <<-MSG.strip_heredoc
ERROR: Failed command: `#{command}`
OUTPUT:
#{output}
MSG

exit status.exitstatus
run(command).tap do |status|
unless status.success?
exit status.exitstatus
end
end

true
end

protected
Expand All @@ -39,15 +32,7 @@ def run!(command)

private

def write_to_out(output)
write(out_streams, output)
end

def write_to_err(output)
write(out_streams + err_streams, output)
end

def write(streams, output)
def write(output, streams:)
streams.each do |stream|
stream.write(output)
end
Expand Down
34 changes: 26 additions & 8 deletions spec/lib/ember_cli/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
describe EmberCli::Runner do
describe "#run!" do
context "when the command fails" do
it "writes to all `err` and `out` streams" do
output_streams = Array.new(4) { StringIO.new }
it "writes output to `out` streams" do
output_streams = Array.new(2) { StringIO.new }
runner = EmberCli::Runner.new(
err: output_streams.first(2),
out: output_streams.last(2),
err: [],
out: output_streams,
)

expect { runner.run!("echo 'out'; echo 'err' > /dev/stderr; exit 1") }.
to raise_error(SystemExit)

output_streams.each(&:rewind)
ouput_strings = output_streams.each(&:rewind).map(&:read)

output_streams.each do |stream|
expect(stream.read).to match(/out\nerr/)
ouput_strings.each do |output|
expect(output).to eq("out\n")
end
end

it "writes errors to `err` streams" do
error_streams = Array.new(2) { StringIO.new }
runner = EmberCli::Runner.new(
err: error_streams,
out: [],
)

expect { runner.run!("echo 'out'; echo 'err' > /dev/stderr; exit 1") }.
to raise_error(SystemExit)

error_strings = error_streams.each(&:rewind).map(&:read)

error_strings.each do |error|
expect(error).to eq("err\n")
end
end
end
Expand All @@ -26,10 +43,11 @@
err = StringIO.new
runner = EmberCli::Runner.new(err: [err], out: [out])

runner.run!("echo 'out'")
status = runner.run!("echo 'out'")

[err, out].each(&:rewind)

expect(status).to be_success
expect(err.read).to be_empty
expect(out.read).to eq("out\n")
end
Expand Down

0 comments on commit 33e2ad4

Please sign in to comment.