Skip to content

Commit

Permalink
Improve command failure reporting
Browse files Browse the repository at this point in the history
* Remove `#silence_build` method from `#exec.`
* Tell the user what happened when a command fails.
* Print both the command and error message for easier debugging.
* Capture both STDOUT and STDERR in #exec.

Commands may output important debugging information via either channel.
Use `Open3.capture2e` instead of `Kernel.system`. It appears to be
impossible to capture both `STDERR` and `STDOUT` simultaneously with the
latter.
  • Loading branch information
botandrose-machine authored and seanpdoyle committed Dec 3, 2015
1 parent 87c65e3 commit f911bf0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
master
------

* Improve command failure reporting. [#324]
* Use latest EmberCLI-generated asset files. [#316]
* Delete previous build output on application boot instead of on process exit.
[#308]

[#324]: https://github.com/thoughtbot/ember-cli-rails/pull/324
[#316]: https://github.com/thoughtbot/ember-cli-rails/pull/316
[#308]: https://github.com/thoughtbot/ember-cli-rails/pull/308

Expand Down
30 changes: 30 additions & 0 deletions lib/ember_cli/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "open3"

module EmberCli
class Runner
def initialize(env: {}, out:, err:, options: {})
@env = env
@out = out
@err = err
@options = options
end

def run!(command)
output, status = Open3.capture2e(@env, command, @options)

@out.write(output)

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

exit 1
end

true
end
end
end
28 changes: 10 additions & 18 deletions lib/ember_cli/shell.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "ember_cli/command"
require "ember_cli/runner"

module EmberCli
class Shell
Expand All @@ -13,7 +14,7 @@ def initialize(paths:, env: {}, options: {})
end

def compile
silence_build { exec ember.build }
exec ember.build
end

def build_and_watch
Expand Down Expand Up @@ -50,18 +51,17 @@ def test
attr_reader :ember, :env, :options, :paths

def spawn(command)
Kernel.spawn(env, command, process_options) || exit(1)
Kernel.spawn(env, command, chdir: paths.root.to_s, out: paths.log.to_s) ||
exit(1)
end

def exec(command)
Kernel.system(env, command, process_options) || exit(1)
end

def process_options
{
chdir: paths.root.to_s,
out: paths.log.to_s,
}
Runner.new(
options: { chdir: paths.root.to_s },
out: paths.log,
err: $stderr,
env: env,
).run!(command)
end

def running?
Expand All @@ -77,13 +77,5 @@ def lock_buildfile
def detach
Process.detach pid
end

def silence_build(&block)
if ENV.fetch("EMBER_CLI_RAILS_VERBOSE") { EmberCli.env.production? }
yield
else
silence_stream(STDOUT, &block)
end
end
end
end
34 changes: 34 additions & 0 deletions spec/lib/ember_cli/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "ember_cli/runner"

describe EmberCli::Runner do
describe "#run!" do
context "when the command fails" do
it "writes STDERR and STDOUT to `err`" do
out = StringIO.new
err = StringIO.new
runner = EmberCli::Runner.new(err: err, out: out)

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

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

expect(err.read).to match(/out\nerr/)
expect(out.read).to eq("out\nerr\n")
end
end

it "executes the command" do
out = StringIO.new
err = StringIO.new
runner = EmberCli::Runner.new(err: err, out: out)

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

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

expect(err.read).to be_empty
expect(out.read).to eq("out\n")
end
end
end

0 comments on commit f911bf0

Please sign in to comment.