Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Errors Should Not Be Rescued #47

Merged
merged 6 commits into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/octopolo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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?
raise "command=#{command}; 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 All @@ -36,8 +36,6 @@ def self.perform(command, say_command = true)
say output if say_command
# return the output of the command
output
rescue => e
say "Unable to perform '#{command}': #{e.message}"
end

# Public: Perform the command, but do not print out the command
Expand Down
7 changes: 4 additions & 3 deletions lib/octopolo/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ def self.reserved_branch?
# Public: Check out the given branch name
#
# branch_name - The name of the branch to check out
def self.check_out branch_name
# do_after_pull - Should a pull be done after checkout?
def self.check_out(branch_name, do_after_pull=true)
fetch
perform "checkout #{branch_name}"
pull
pull if do_after_pull
unless current_branch == branch_name
raise CheckoutFailed, "Failed to check out '#{branch_name}'"
end
Expand All @@ -87,7 +88,7 @@ def self.check_out branch_name
def self.new_branch(new_branch_name, source_branch_name)
fetch
perform("branch --no-track #{new_branch_name} origin/#{source_branch_name}")
check_out new_branch_name
check_out(new_branch_name, false)
perform("push --set-upstream origin #{new_branch_name}")
end

Expand Down
11 changes: 5 additions & 6 deletions spec/octopolo/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ module Octopolo
subject.perform(command).should == result
end

it "should handle exception gracefully" do
it "should raise exception" 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
expect { subject.perform(command) }.to raise_error(RuntimeError, exception_message)
end

it "should handle errors gracefully" do
it "should raise errors from command" 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
expect { subject.perform(command) }
.to raise_error(RuntimeError, "command=#{command}; exit_status=1; stderr=kaboom")
end

it "should not speak the command if told not to" do
Expand Down
10 changes: 9 additions & 1 deletion spec/octopolo/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ module Octopolo
Git.check_out name
end

it "checks out the given branch name without after pull" do
Git.should_receive(:fetch)
Git.should_receive(:perform).with("checkout #{name}")
Git.should_not_receive(:pull)
Git.should_receive(:current_branch) { name }
Git.check_out(name, false)
end

it "raises an exception if the current branch is not the requested branch afterward" do
Git.should_receive(:fetch)
Git.should_receive(:perform)
Expand Down Expand Up @@ -345,7 +353,7 @@ module Octopolo
it "creates and pushes a new branch from the source branch" do
Git.should_receive(:fetch)
Git.should_receive(:perform).with("branch --no-track #{new_branch_name} origin/#{source_branch_name}")
Git.should_receive(:check_out).with(new_branch_name)
Git.should_receive(:check_out).with(new_branch_name, false)
Git.should_receive(:perform).with("push --set-upstream origin #{new_branch_name}")

Git.new_branch(new_branch_name, source_branch_name)
Expand Down