Skip to content

Commit

Permalink
Merge pull request #46 from sportngin/cli-error-fix
Browse files Browse the repository at this point in the history
Errors from CLI commands should throw and exception
  • Loading branch information
bbergstrom committed Mar 23, 2015
2 parents ea15b2d + db56eff commit c40480a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test/tmp
test/version_tmp
tmp
log/
.idea

# YARD artifacts
.yardoc
Expand Down
1 change: 1 addition & 0 deletions lib/octopolo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def self.perform(command, say_command = true)
# and then perform it
if Open3.respond_to?(:capture3)
output, error, status = Open3.capture3(command)
raise "exit_status=#{status.exitstatus}; stderr=#{error}" unless status.success?
else
# Only necessary as long as we use 1.8.7, which doesn't have Open3.capture3
output = `#{command}`
Expand Down
13 changes: 11 additions & 2 deletions spec/octopolo/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module Octopolo
let(:result) { "result" }
let(:error) { "error message" }
let(:exception_message) { "Error with something" }
let(:status_success) { double("status", "success?" => true, :exitstatus => 0) }
let(:status_error) { double("status", "success?" => nil, :exitstatus => 1) }

it "passes the given command to the shell" do
subject.should_receive(:say).with(command)
Open3.should_receive(:capture3).with(command).and_return([result, nil])
Open3.should_receive(:capture3).with(command).and_return([result, nil, status_success])
subject.should_receive(:say).with(result)
subject.perform(command).should == result
end
Expand All @@ -26,13 +28,20 @@ module Octopolo
subject.perform(command).should == result
end

it "should handle errors gracefully" do
it "should handle exception gracefully" do
subject.should_receive(:say).with(command)
Open3.should_receive(:capture3).with(command).and_raise(exception_message)
subject.should_receive(:say).with("Unable to perform '#{command}': #{exception_message}")
subject.perform(command).should be_nil
end

it "should handle errors gracefully" do
subject.should_receive(:say).with(command)
Open3.should_receive(:capture3).with(command).and_return([result, "kaboom", status_error])
subject.should_receive(:say).with("Unable to perform '#{command}': exit_status=1; stderr=kaboom")
subject.perform(command).should be_nil
end

it "should not speak the command if told not to" do
subject.should_receive(:say).with(command).never
subject.perform(command, false)
Expand Down

0 comments on commit c40480a

Please sign in to comment.