Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Use Kumade::CommandLine to run commands.
Browse files Browse the repository at this point in the history
This commit also gives full test coverage to the Git class.
  • Loading branch information
Gabe Berke-Williams committed Sep 23, 2011
1 parent 6652197 commit 72424c0
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 193 deletions.
1 change: 1 addition & 0 deletions lib/kumade.rb
Expand Up @@ -13,6 +13,7 @@ module Kumade
autoload :NoopPackager, "kumade/packagers/noop_packager"
autoload :PackagerList, "kumade/packager_list"
autoload :RakeTaskRunner, "kumade/rake_task_runner"
autoload :CommandLine, "kumade/command_line"

def self.configuration
@@configuration ||= Configuration.new
Expand Down
17 changes: 0 additions & 17 deletions lib/kumade/base.rb
Expand Up @@ -6,23 +6,6 @@ def initialize
super()
end

def run_or_error(command, error_message)
say_status(:run, command)
if ! Kumade.configuration.pretending?
error(error_message) unless run(command)
end
end

def run(command)
line = Cocaine::CommandLine.new(command)
begin
line.run
true
rescue Cocaine::ExitStatusError => e
false
end
end

def error(message)
say("==> ! #{message}", :red)
raise Kumade::DeploymentError.new(message)
Expand Down
41 changes: 41 additions & 0 deletions lib/kumade/command_line.rb
@@ -0,0 +1,41 @@
require 'cocaine'

module Kumade
class CommandFailedError < RuntimeError
end

class CommandLine < Base
def initialize(command_to_run)
super()
@command_line = Cocaine::CommandLine.new(command_to_run)
end

def run_or_error(error_message = nil)
if run_with_status
true
else
error(error_message)
end
end

def run_with_status
say_status(:run, command)
Kumade.configuration.pretending? || run
end

def run
begin
@command_line.run
true
rescue Cocaine::ExitStatusError, Cocaine::CommandNotFoundError
false
end
end

private

def command
@command_line.command
end
end
end
26 changes: 15 additions & 11 deletions lib/kumade/git.rb
Expand Up @@ -20,25 +20,27 @@ def push(branch, remote = 'origin', force = false)
command << remote
command << branch
command = command.join(" ")
run_or_error(command, "Failed to push #{branch} -> #{remote}")

command_line = CommandLine.new(command)
command_line.run_or_error("Failed to push #{branch} -> #{remote}")
success("Pushed #{branch} -> #{remote}")
end

def create(branch)
unless branch_exist?(branch)
run_or_error("git branch #{branch}", "Failed to create #{branch}")
unless has_branch?(branch)
CommandLine.new("git branch #{branch}").run_or_error("Failed to create #{branch}")
end
end

def delete(branch_to_delete, branch_to_checkout)
run_or_error("git checkout #{branch_to_checkout} && git branch -D #{branch_to_delete}",
"Failed to clean up #{branch_to_delete} branch")
command_line = CommandLine.new("git checkout #{branch_to_checkout} && git branch -D #{branch_to_delete}")
command_line.run_or_error("Failed to clean up #{branch_to_delete} branch")
end

def add_and_commit_all_in(dir, branch, commit_message, success_output, error_output)
run_or_error "git checkout -b #{branch} && git add -f #{dir} && git commit -m '#{commit_message}'",
"Cannot deploy: #{error_output}"
success success_output
command_line = CommandLine.new("git checkout -b #{branch} && git add -f #{dir} && git commit -m '#{commit_message}'")
command_line.run_or_error("Cannot deploy: #{error_output}")
success(success_output)
end

def current_branch
Expand All @@ -54,7 +56,7 @@ def remote_exists?(remote_name)
end

def dirty?
!run("git diff --exit-code")
! CommandLine.new("git diff --exit-code").run
end

def ensure_clean_git
Expand All @@ -65,8 +67,10 @@ def ensure_clean_git
end
end

def branch_exist?(branch)
run("git show-ref #{branch}")
private

def has_branch?(branch)
CommandLine.new("git show-ref #{branch}").run
end
end
end
24 changes: 16 additions & 8 deletions lib/kumade/heroku.rb
Expand Up @@ -26,21 +26,29 @@ def delete_deploy_branch
end

def heroku(command)
heroku_command = if cedar?
"bundle exec heroku run"
else
"bundle exec heroku"
end
run_or_error("#{heroku_command} #{command} --remote #{Kumade.configuration.environment}",
"Failed to run #{command} on Heroku")
full_heroku_command = "#{bundle_exec_heroku} #{command} --remote #{Kumade.configuration.environment}"
command_line = CommandLine.new(full_heroku_command)
command_line.run_or_error("Failed to run #{command} on Heroku")
end

def cedar?
return @cedar unless @cedar.nil?

@cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{Kumade.configuration.environment}").run.split("\n").grep(/\*/).any? do |line|
command_line = CommandLine.new("bundle exec heroku stack --remote #{Kumade.configuration.environment}")

@cedar = command_line.run_or_error.split("\n").grep(/\*/).any? do |line|
line.include?("cedar")
end
end

private

def bundle_exec_heroku
if cedar?
"bundle exec heroku run"
else
"bundle exec heroku"
end
end
end
end
82 changes: 0 additions & 82 deletions spec/kumade/base_spec.rb
Expand Up @@ -15,85 +15,3 @@
STDOUT.should have_received(:puts).with(regexp_matches(/I'm an error!/))
end
end

describe Kumade::Base, "#run_or_error" do
let(:command) { "dummy command" }
let(:error_message) { "dummy error message" }

before do
STDOUT.stubs(:puts)
end

context "when pretending" do
before do
Kumade.configuration.pretending = true
subject.stubs(:run)
end

it "does not run the command" do
subject.run_or_error("dummy command", "dummy error message")

subject.should have_received(:run).never
STDOUT.should have_received(:puts).with(regexp_matches(/#{command}/))
end
end

context "when not pretending" do
context "when it runs successfully" do
before do
Cocaine::CommandLine.stubs(:new).returns(stub(:run))
end

it "does not print an error" do
subject.run_or_error(command, error_message)

STDOUT.should have_received(:puts).with(regexp_matches(/#{error_message}/)).never
end
end

context "when it does not run successfully " do
let(:failing_command_line) { stub("Failing Cocaine::CommandLine") }

before do
subject.stubs(:error)
failing_command_line.stubs(:run).raises(Cocaine::ExitStatusError)
Cocaine::CommandLine.stubs(:new).returns(failing_command_line)
end

it "prints an error message" do
subject.run_or_error(command, error_message)

subject.should have_received(:error).with(error_message)
end
end
end
end

describe Kumade::Base, "#run" do
let(:command_line) { stub("Cocaine::CommandLine") }
let(:command) { "command" }

before do
Cocaine::CommandLine.stubs(:new).with(command).returns(command_line)
end

context "when not successful" do
before do
command_line.stubs(:run)
end

it "returns true" do
subject.run(command).should == true
end
end

context "when successful" do
before do
command_line.stubs(:run).raises(Cocaine::ExitStatusError)
end

it "returns false" do
subject.run(command).should == false
end
end
end
109 changes: 109 additions & 0 deletions spec/kumade/command_line_spec.rb
@@ -0,0 +1,109 @@
require 'spec_helper'

describe Kumade::CommandLine, "#run_or_error" do
let(:command_line) { stub("Cocaine::CommandLine instance", :run => nil) }
subject { Kumade::CommandLine.new("echo") }

before do
subject.stubs(:error)
subject.stubs(:say_status)

Cocaine::CommandLine.stubs(:new).returns(command_line)
end

context "when pretending" do
before do
Kumade.configuration.pretending = true
end

it "does not run the command" do
subject.run_or_error

command_line.should have_received(:run).never
end
end

context "when successful" do
before do
Kumade.configuration.pretending = false
command_line.stubs(:run => true)
end

it "returns true" do
subject.run_or_error.should be_true
end
end

context "when unsuccessful" do
subject { Kumade::CommandLine.new("BAD COMMAND") }
before do
Kumade.configuration.pretending = false
end

it "prints an error message" do
subject.run_or_error("something bad")

subject.should have_received(:error).with("something bad")
end
end
end

describe Kumade::CommandLine, "#run_with_status" do
let(:command) { "echo" }
let(:command_line) { stub("Cocaine::CommandLine instance", :run => nil, :command => command) }
subject { Kumade::CommandLine.new(command) }

before do
Cocaine::CommandLine.stubs(:new).returns(command_line)
subject.stubs(:say_status)
end

it "prints the command" do
subject.run_with_status

subject.should have_received(:say_status).with(:run, "echo").once
end

context "when pretending" do
before { Kumade.configuration.pretending = true }

it "does not run the command" do
subject.run_with_status

command_line.should have_received(:run).never
end

it "returns true" do
subject.run_with_status.should be_true
end
end

context "when not pretending" do
before { Kumade.configuration.pretending = false }

it "runs the command" do
subject.run_with_status

command_line.should have_received(:run).once
end
end
end

describe Kumade::CommandLine, "#run" do
context "when successful" do
subject { Kumade::CommandLine.new("echo") }

it "returns true" do
subject.run.should == true
end
end

context "when unsuccessful" do
let(:bad_command) { "grep FAKE NOT_A_FILE" }
subject { Kumade::CommandLine.new("#{bad_command} 2>/dev/null") }

it "returns false" do
subject.run.should be_false
end
end
end

0 comments on commit 72424c0

Please sign in to comment.