Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix issue 37 using Cocaine

  • Loading branch information...
commit 866f9467045aeb57f92589451a1bd16875941d60 1 parent a4c81b9
Marcos Tapajós tapajos authored
1  kumade.gemspec
View
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency('heroku', '~> 2.0')
s.add_dependency('thor', '~> 0.14')
s.add_dependency('rake', '>= 0.8.7')
+ s.add_dependency('cocaine', '>= 0.2.0')
s.add_development_dependency('rake', '>= 0.8.7')
s.add_development_dependency('rspec', '~> 2.6.0')
26 lib/kumade/base.rb
View
@@ -2,22 +2,26 @@
module Kumade
class Base < Thor::Shell::Color
+ attr_reader :pretending
def initialize
super()
end
- def run_or_error(commands, error_message)
- all_commands = [commands].flatten.join(' && ')
- if @pretending
- say_status(:run, all_commands)
- else
- error(error_message) unless run(all_commands)
+ def run_or_error(command, error_message)
+ say_status(:run, command)
+ if !pretending
+ error(error_message) unless run(command)
end
end
-
- def run(command, config = {})
- say_status :run, command
- config[:capture] ? `#{command}` : system("#{command}")
+
+ def run(command)
+ line = Cocaine::CommandLine.new(command)
+ begin
+ line.run
+ true
+ rescue Cocaine::ExitStatusError => e
+ false
+ end
end
def error(message)
@@ -29,4 +33,4 @@ def success(message)
say("==> #{message}", :green)
end
end
-end
+end
11 lib/kumade/git.rb
View
@@ -1,3 +1,4 @@
+require 'cocaine'
module Kumade
class Git < Base
attr_reader :environment
@@ -21,7 +22,7 @@ def push(branch, remote = 'origin', force = false)
command << remote
command << branch
command = command.join(" ")
- run_or_error([command], "Failed to push #{branch} -> #{remote}")
+ run_or_error(command, "Failed to push #{branch} -> #{remote}")
success("Pushed #{branch} -> #{remote}")
end
@@ -32,12 +33,12 @@ def create(branch)
end
def delete(branch_to_delete, branch_to_checkout)
- run_or_error(["git checkout #{branch_to_checkout}", "git branch -D #{branch_to_delete}"],
+ run_or_error("git checkout #{branch_to_checkout} && git branch -D #{branch_to_delete}",
"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}'"],
+ run_or_error "git checkout -b #{branch} && git add -f #{dir} && git commit -m '#{commit_message}'",
"Cannot deploy: #{error_output}"
success success_output
end
@@ -55,7 +56,7 @@ def remote_exists?(remote_name)
end
def dirty?
- ! system("git diff --exit-code")
+ !run("git diff --exit-code")
end
def ensure_clean_git
@@ -67,7 +68,7 @@ def ensure_clean_git
end
def branch_exist?(branch)
- system("git show-ref #{branch}")
+ run("git show-ref #{branch}")
end
end
end
59 spec/kumade/base_spec.rb
View
@@ -16,3 +16,62 @@
lambda{ subject.error("I'm an error!") }.should raise_error(Kumade::DeploymentError)
end
end
+
+describe Kumade::Base, "#run_or_error" do
+ let(:command) { "command" }
+ let(:error_message) { "error_message" }
+
+ before(:each) do
+ subject.should_receive(:say_status).with(:run, command)
+ end
+
+ context "when pretending" do
+ it "should never call run" do
+ subject.should_receive(:pretending).and_return(true)
+ subject.should_receive(:run).never
+ subject.run_or_error(command, error_message)
+ end
+ end
+
+ context "when not pretending" do
+
+ before(:each) do
+ subject.should_receive(:pretending).and_return(false)
+ end
+
+ context "with success" do
+ it "should call not call error" do
+ subject.should_receive(:run).and_return(true)
+ subject.should_receive(:error).never
+ subject.run_or_error(command, error_message)
+ end
+ end
+
+ context "without success" do
+ it "should call CommandLine.run and error with error_message" do
+ subject.should_receive(:run).and_return(false)
+ subject.should_receive(:error).with(error_message)
+ subject.run_or_error(command, error_message)
+ end
+ end
+ end
+end
+
+describe Kumade::Base, "#run" do
+ let(:comand_line_mock) { mock("Cocaine::CommandLine") }
+ let(:command) { "command" }
+
+ before(:each) do
+ Cocaine::CommandLine.should_receive(:new).with(command).and_return(comand_line_mock)
+ end
+
+ it "should return true when success" do
+ comand_line_mock.should_receive(:run)
+ subject.run(command).should be_true
+ end
+
+ it "should return false when not success" do
+ comand_line_mock.should_receive(:run).and_raise(Cocaine::ExitStatusError)
+ subject.run(command).should be_false
+ end
+end
38 spec/kumade/git_spec.rb
View
@@ -49,3 +49,41 @@
Kumade::Git.environments.should == ["staging"]
end
end
+
+describe Kumade::Git, "#branch_exist?" do
+ let(:comand_line_mock) { mock("Cocaine::CommandLine") }
+ let(:branch) { "branch" }
+ let(:environment) { "staging" }
+
+ subject { Kumade::Git.new(false, environment) }
+
+ before(:each) do
+ Cocaine::CommandLine.should_receive(:new).with("git show-ref #{branch}").and_return(comand_line_mock)
+ end
+
+ it "should return true when branch exist" do
+ comand_line_mock.should_receive(:run)
+ subject.branch_exist?("branch").should be_true
+ end
+
+ it "should return false if branch doesn't exist" do
+ comand_line_mock.should_receive(:run).and_raise(Cocaine::ExitStatusError)
+ subject.branch_exist?("branch").should be_false
+ end
+end
+
+describe Kumade::Git, "#dirty?" do
+ let(:environment) { "staging" }
+
+ subject { Kumade::Git.new(false, environment) }
+
+ it "should return true when dirty" do
+ subject.should_receive(:run).with("git diff --exit-code").and_return(false)
+ subject.dirty?.should be_true
+ end
+
+ it "should return false when not dirty" do
+ subject.should_receive(:run).with("git diff --exit-code").and_return(true)
+ subject.dirty?.should be_false
+ end
+end
1  spec/spec_helper.rb
View
@@ -3,6 +3,7 @@
require 'rake'
require 'stringio'
require 'aruba/api'
+require 'cocaine'
module GitRemoteHelpers
def force_add_heroku_remote(remote_name)
Please sign in to comment.
Something went wrong with that request. Please try again.