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

Commit

Permalink
Fix deployer specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tapajos committed Aug 29, 2011
1 parent e4c165d commit 91989a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 161 deletions.
18 changes: 9 additions & 9 deletions lib/kumade/deployer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Kumade
class Deployer < Thor::Shell::Color
DEPLOY_BRANCH = "deploy"
attr_reader :environment, :pretending
attr_reader :environment, :pretending, :git

def initialize(environment = 'staging', pretending = false, cedar = false)
super()
Expand All @@ -27,12 +27,12 @@ def pre_deploy
end

def sync_github
@git.push(@branch)
git.push(@branch)
end

def sync_heroku
@git.create(DEPLOY_BRANCH)
@git.push("#{DEPLOY_BRANCH}:master", environment, true)
git.create(DEPLOY_BRANCH)
git.push("#{DEPLOY_BRANCH}:master", environment, true)
end

def heroku_migrate
Expand All @@ -43,7 +43,7 @@ def heroku_migrate
end

def post_deploy
@git.delete(DEPLOY_BRANCH, @branch)
git.delete(DEPLOY_BRANCH, @branch)
end

def heroku(command, app)
Expand All @@ -57,7 +57,7 @@ def heroku(command, app)
end

def ensure_clean_git
@git.ensure_clean_git
git.ensure_clean_git
end

def package_assets
Expand Down Expand Up @@ -90,7 +90,7 @@ def package_with_more
else
begin
run "bundle exec rake more:generate"
if @git.git_dirty?
if git.git_dirty?
success(success_message)
git_add_and_commit_all_assets_in(more_assets_path)
end
Expand All @@ -106,7 +106,7 @@ def invoke_custom_task
end

def git_add_and_commit_all_assets_in(dir)
@git.add_and_commit_all_in(dir, DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
git.add_and_commit_all_in(dir, DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
end

def jammit_assets_path
Expand Down Expand Up @@ -168,7 +168,7 @@ def success(message)
end

def ensure_heroku_remote_exists
if @git.remote_exists?(environment)
if git.remote_exists?(environment)
if app_name = Kumade.app_for(environment)
success("#{environment} is a Heroku remote")
else
Expand Down
178 changes: 26 additions & 152 deletions spec/kumade/deployer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,121 +61,29 @@
end

describe Kumade::Deployer, "#sync_github" do
before { subject.stub(:say) }

it "calls `git push`" do
subject.should_receive(:run).
with("git push origin master").
and_return(true)
before { subject.stub(:git).and_return(@git_mock = mock()) }
it "should call @git.push" do
@git_mock.should_receive(:push).with("master")
subject.sync_github
end

context "when `git push` fails" do
before { subject.stub(:run => false) }

it "prints an error message" do
subject.should_receive(:error).with("Failed to push master -> origin")

subject.sync_github
end
end

context "when syncing github succeeds" do
before { subject.stub(:run => true) }

it "does not raise an error" do
subject.should_not_receive(:error)
subject.sync_github
end

it "prints a success message" do
subject.should_receive(:success).with("Pushed master -> origin")

subject.sync_github
end
end
end

describe Kumade::Deployer, "#sync_heroku" do
let(:environment) { 'my-env' }
subject { Kumade::Deployer.new(environment) }
before { subject.stub(:say) }

context "when deploy branch exists" do
it "should calls `git push -f`" do
subject.stub(:branch_exist?).with("deploy").and_return(true)
subject.should_receive(:run).
with("git push -f #{environment} deploy:master").
and_return(true)
subject.sync_heroku
end
end

context "when deploy branch doesn't exists" do
it "should calls `git branch deploy` and `git push -f`" do
subject.stub(:branch_exist?).with("deploy").and_return(false)
subject.should_receive(:run).
with("git branch deploy").
and_return(true)
subject.should_receive(:run).
with("git push -f #{environment} deploy:master").
and_return(true)
subject.sync_heroku
end
end

context "when syncing to heroku fails" do
before do
subject.stub(:run => false)
end

it "prints an error" do
subject.should_receive(:error).twice
subject.sync_heroku
end
end

context "when syncing to heroku succeeds" do
before do
subject.stub(:run => true)
subject.stub(:say)
end

it "does not raise an error" do
subject.should_not_receive(:error)
subject.sync_heroku
end

it "prints a success message" do
subject.should_receive(:success).
with("Pushed master -> #{environment}")

subject.sync_heroku
end
before { subject.stub(:git).and_return(@git_mock = mock()) }
it "should call git.create and git.push" do
@git_mock.should_receive(:create).with("deploy")
@git_mock.should_receive(:push).with("deploy:master", environment, true)
subject.sync_heroku
end
end

describe Kumade::Deployer, "#ensure_clean_git" do
before { subject.stub(:say) }

context "when git is dirty" do
before { subject.stub(:git_dirty? => true) }

it "prints an error" do
subject.should_receive(:error).with("Cannot deploy: repo is not clean.")
subject.ensure_clean_git
end
end

context "when git is clean" do
before { subject.stub(:git_dirty? => false) }

it "prints a success message" do
subject.should_not_receive(:error)
subject.should_receive(:success).with("Git repo is clean")

subject.ensure_clean_git
end
before { subject.stub(:git).and_return(@git_mock = mock()) }
it "should call git.ensure_clean_git" do
@git_mock.should_receive(:ensure_clean_git)
subject.ensure_clean_git
end
end

Expand Down Expand Up @@ -261,7 +169,7 @@
end

context "with updated assets" do
before { subject.stub(:git_dirty? => true) }
before { subject.stub(:git => mock(:git_dirty? => true)) }

it "prints the correct message" do
subject.should_receive(:success).with("Packaged assets with Jammit")
Expand Down Expand Up @@ -319,14 +227,14 @@
context "with changed assets" do
it "prints a success message" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => true)
subject.stub(:git => mock(:git_dirty? => true))
subject.should_receive(:success).with("Packaged assets with More")

subject.package_with_more
end

it "calls git_add_and_commit_all_assets_in if assets were added" do
subject.stub(:git_dirty? => true,
subject.stub(:git => mock(:git_dirty? => true),
:more_assets_path => 'blerg')
subject.stub(:run).with("bundle exec rake more:generate")
subject.should_receive(:git_add_and_commit_all_assets_in).
Expand All @@ -340,15 +248,15 @@
context "with no changed assets" do
it "prints no message" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => false)
subject.stub(:git => mock(:git_dirty? => false))
subject.should_not_receive(:say)

subject.package_with_more
end

it "does not call git_add_and_commit_all_more_assets" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => false)
subject.stub(:git => mock(:git_dirty? => false))
subject.should_not_receive(:git_add_and_commit_all_assets_in)

subject.package_with_more
Expand All @@ -369,29 +277,11 @@
end

describe Kumade::Deployer, "#git_add_and_commit_all_assets_in" do
before do
subject.stub(:run => true)
subject.stub(:say)
end

it "prints a success message" do
subject.should_receive(:success).with("Added and committed all assets")

subject.git_add_and_commit_all_assets_in('blerg')
end

it "runs the correct commands" do
subject.should_receive(:run).
with("git checkout -b deploy && git add -f blerg && git commit -m 'Compiled assets'")

subject.git_add_and_commit_all_assets_in('blerg')
end

it "prints an error if it could not add and commit assets" do
subject.stub(:run => false)
subject.should_receive(:error).with("Cannot deploy: couldn't commit assets")

subject.git_add_and_commit_all_assets_in('blerg')
before {subject.stub(:git => @git_mock = mock())}

it "should call git.add_and_commit_all_in" do
@git_mock.should_receive(:add_and_commit_all_in).with("dir", 'deploy', 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
subject.git_add_and_commit_all_assets_in("dir")
end
end

Expand Down Expand Up @@ -534,22 +424,6 @@ class More
end
end

describe Kumade::Deployer, "#remote_exists?" do
let(:remote_name){ 'staging' }

before { force_add_heroku_remote(remote_name, 'i-am-a-heroku-app') }

it "returns true if the remote exists" do
subject.remote_exists?(remote_name).should be_true
end

it "returns false if the remote does not exist" do
remove_remote(remote_name)

subject.remote_exists?(remote_name).should be_false
end
end

describe Kumade::Deployer, "#heroku" do
let(:app_name){ 'sushi' }

Expand Down Expand Up @@ -585,10 +459,10 @@ class More
end

describe Kumade::Deployer, "#post_deploy" do
before { subject.stub(:run => true, :say => true) }

it "cleans up the deploy branch" do
subject.should_receive(:run).with('git checkout master && git branch -D deploy')
before { subject.stub(:git => @git_mock = mock()) }
it "should call git.delete" do
@git_mock.should_receive(:delete).with('deploy', 'master')
subject.post_deploy
end
end

0 comments on commit 91989a1

Please sign in to comment.