Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Heroku class

  • Loading branch information...
commit 82c75d2320ca350e41e08ace801c0d895d1288e3 1 parent 7c20edc
@gabebw gabebw authored
View
1  lib/kumade.rb
@@ -6,6 +6,7 @@ module Kumade
autoload :Railtie, "kumade/railtie"
autoload :DeploymentError, "kumade/deployment_error"
autoload :Configuration, "kumade/configuration"
+ autoload :Heroku, "kumade/heroku"
def self.configuration
@@configuration ||= Configuration.new
View
39 lib/kumade/deployer.rb
@@ -3,12 +3,12 @@
module Kumade
class Deployer < Base
- DEPLOY_BRANCH = "deploy"
- attr_reader :git
+ attr_reader :git, :heroku
def initialize
super()
@git = Git.new
+ @heroku = Heroku.new
@branch = @git.current_branch
end
@@ -16,8 +16,8 @@ def deploy
begin
ensure_heroku_remote_exists
pre_deploy
- sync_heroku
- heroku_migrate
+ heroku.sync
+ heroku.migrate_database
rescue
ensure
post_deploy
@@ -34,35 +34,8 @@ def sync_github
git.push(@branch)
end
- def sync_heroku
- git.create(DEPLOY_BRANCH)
- git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
- end
-
- def heroku_migrate
- heroku("rake db:migrate") unless Kumade.configuration.pretending?
- success("Migrated #{Kumade.configuration.environment}")
- end
-
def post_deploy
- git.delete(DEPLOY_BRANCH, @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")
- 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|
- line.include?("cedar")
- end
+ heroku.delete_deploy_branch
end
def ensure_clean_git
@@ -115,7 +88,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, Kumade::Heroku::DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
end
def jammit_assets_path
View
46 lib/kumade/heroku.rb
@@ -0,0 +1,46 @@
+require 'cocaine'
+
+module Kumade
+ class Heroku < Base
+ DEPLOY_BRANCH = "deploy"
+ attr_reader :git
+
+ def initialize
+ super()
+ @git = Git.new
+ @branch = @git.current_branch
+ end
+
+ def sync
+ git.create(DEPLOY_BRANCH)
+ git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
+ end
+
+ def migrate_database
+ heroku("rake db:migrate") unless Kumade.configuration.pretending?
+ success("Migrated #{Kumade.configuration.environment}")
+ end
+
+ def delete_deploy_branch
+ git.delete(DEPLOY_BRANCH, @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")
+ 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|
+ line.include?("cedar")
+ end
+ end
+ end
+end
View
156 spec/kumade/deployer_spec.rb
@@ -2,46 +2,6 @@
require 'jammit'
-# This should really be done by mocking out a Heroku class
-shared_context "when on Cedar" do
- let(:cocaine_mock) { mock("Cocaine::CommandLine") }
-
- before do
- Cocaine::CommandLine.should_receive(:new).
- with("bundle exec heroku stack --remote staging").
- and_return(cocaine_mock)
-
- cocaine_mock.should_receive(:run).and_return(%{
- aspen-mri-1.8.6
- bamboo-mri-1.9.2
- bamboo-ree-1.8.7
-* cedar (beta)
-})
- end
-end
-
-shared_context "when not on Cedar" do
- let(:cocaine_mock) { mock("Cocaine::CommandLine") }
-
- before do
- Cocaine::CommandLine.should_receive(:new).
- with("bundle exec heroku stack --remote staging").
- and_return(cocaine_mock)
- cocaine_mock.should_receive(:run).and_return(%{
- aspen-mri-1.8.6
-* bamboo-mri-1.9.2
- bamboo-ree-1.8.7
- cedar (beta)
-})
- end
-end
-
-describe Kumade::Deployer, "DEPLOY_BRANCH" do
- subject { Kumade::Deployer::DEPLOY_BRANCH }
-
- it { should == "deploy" }
-end
-
describe Kumade::Deployer, "#pre_deploy" do
let(:git) { subject.git }
@@ -58,20 +18,15 @@
let(:remote_name) { 'staging' }
before do
+ STDOUT.stub(:puts)
force_add_heroku_remote(remote_name)
end
- it "calls the correct methods in order" do
- subject.stub(:run)
- subject.stub(:post_deploy)
-
- %w(ensure_heroku_remote_exists
- pre_deploy
- sync_heroku
- heroku_migrate
- ).each do |command|
- subject.should_receive(command).ordered
- end
+ it "calls the correct methods" do
+ subject.should_receive(:pre_deploy)
+ subject.heroku.should_receive(:sync)
+ subject.heroku.should_receive(:migrate_database)
+ subject.should_receive(:post_deploy)
subject.deploy
end
@@ -83,7 +38,6 @@
subject.deploy
end
-
end
describe Kumade::Deployer, "#sync_github" do
@@ -93,26 +47,13 @@
`git checkout -b #{new_branch}`
end
- it "calls git.push with the current branch" do
+ it "pushes the current branch to github" do
subject.git.should_receive(:push).with(new_branch)
subject.sync_github
end
end
-describe Kumade::Deployer, "#sync_heroku" do
- let(:environment) { 'staging' }
-
- before do
- force_add_heroku_remote(environment)
- end
-
- it "creates and pushes the deploy branch" do
- subject.git.should_receive(:create).with("deploy")
- subject.git.should_receive(:push).with("deploy:master", environment, true)
- subject.sync_heroku
- end
-end
describe Kumade::Deployer, "#ensure_clean_git" do
it "calls git.ensure_clean_git" do
@@ -386,36 +327,11 @@ class More
end
end
-describe Kumade::Deployer, "#heroku_migrate" do
- let(:environment) { 'staging' }
-
- before do
- STDOUT.stub(:puts)
- force_add_heroku_remote(environment)
- end
-
- it "runs db:migrate with the correct app" do
- subject.should_receive(:heroku).with("rake db:migrate")
-
- subject.heroku_migrate
- end
-
- context "when pretending" do
- before do
- STDOUT.stub(:puts)
- Kumade.configuration.pretending = true
- end
-
- it "does not run heroku" do
- subject.should_not_receive(:heroku)
- subject.heroku_migrate
- end
-
- it "prints a message" do
- STDOUT.should_receive(:puts).with(/Migrated #{environment}/)
+describe Kumade::Deployer, "#post_deploy" do
+ it "deletes the deploy branch" do
+ Kumade::Git.any_instance.should_receive(:delete).with('deploy', 'master')
- subject.heroku_migrate
- end
+ subject.post_deploy
end
end
@@ -469,53 +385,3 @@ class More
end
end
-describe Kumade::Deployer, "#cedar?" do
- context "when on Cedar" do
- include_context "when on Cedar"
-
- it "returns true" do
- subject.cedar?.should == true
- end
- end
-
- context "when not on Cedar" do
- include_context "when not on Cedar"
-
- it "returns false" do
- subject.cedar?.should == false
- end
- end
-end
-
-describe Kumade::Deployer, "#heroku" do
- before { STDOUT.stub(:puts) }
-
- context "when on Cedar" do
- include_context "when on Cedar"
-
- it "runs commands with `run`" do
- Cocaine::CommandLine.should_receive(:new).
- with(/bundle exec heroku run rake/).
- and_return(stub(:run => true))
- subject.heroku("rake")
- end
- end
-
- context "when not on Cedar" do
- include_context "when not on Cedar"
-
- it "runs commands without `run`" do
- Cocaine::CommandLine.should_receive(:new).
- with(/bundle exec heroku rake/).
- and_return(stub(:run => true))
- subject.heroku("rake")
- end
- end
-end
-
-describe Kumade::Deployer, "#post_deploy" do
- it "calls git.delete" do
- subject.git.should_receive(:delete).with('deploy', 'master')
- subject.post_deploy
- end
-end
View
117 spec/kumade/heroku_spec.rb
@@ -0,0 +1,117 @@
+require 'spec_helper'
+
+describe Kumade::Heroku, "DEPLOY_BRANCH" do
+ subject { Kumade::Heroku::DEPLOY_BRANCH }
+
+ it { should == "deploy" }
+end
+
+describe Kumade::Heroku, "#sync" do
+ let(:environment) { 'staging' }
+
+ before do
+ force_add_heroku_remote(environment)
+ end
+
+ it "creates and pushes the deploy branch" do
+ subject.git.should_receive(:create).with("deploy")
+ subject.git.should_receive(:push).with("deploy:master", environment, true)
+ subject.sync
+ end
+end
+
+describe Kumade::Heroku, "#migrate_database" do
+ let(:environment) { 'staging' }
+
+ before do
+ STDOUT.stub(:puts)
+ force_add_heroku_remote(environment)
+ end
+
+ it "runs db:migrate with the correct app" do
+ subject.should_receive(:heroku).with("rake db:migrate")
+
+ subject.migrate_database
+ end
+
+ context "when pretending" do
+ before do
+ STDOUT.stub(:puts)
+ Kumade.configuration.pretending = true
+ end
+
+ it "does not run the command" do
+ subject.should_not_receive(:heroku)
+
+ subject.migrate_database
+ end
+
+ it "prints a message" do
+ STDOUT.should_receive(:puts).with(/Migrated #{environment}/)
+
+ subject.migrate_database
+ end
+ end
+end
+
+describe Kumade::Heroku, "#heroku" do
+ let(:command_line_instance) { stub("Cocaine::CommandLine instance", :run => true) }
+
+ before do
+ STDOUT.stub(:puts)
+ end
+
+ context "when on Cedar" do
+ include_context "when on Cedar"
+
+ it "runs commands with `run`" do
+ Cocaine::CommandLine.should_receive(:new).
+ with(/bundle exec heroku run/).
+ and_return(command_line_instance)
+
+ subject.heroku("rake")
+ end
+ end
+
+ context "when not on Cedar" do
+ include_context "when not on Cedar"
+
+ it "runs commands without `run`" do
+ Cocaine::CommandLine.should_receive(:new).
+ with(/bundle exec heroku rake/).
+ and_return(command_line_instance)
+
+ subject.heroku("rake")
+ end
+ end
+end
+
+describe Kumade::Heroku, "#cedar?" do
+ context "when on Cedar" do
+ include_context "when on Cedar"
+
+ it "returns true" do
+ subject.cedar?.should == true
+ end
+ end
+
+ context "when not on Cedar" do
+ include_context "when not on Cedar"
+
+ it "returns false" do
+ subject.cedar?.should == false
+ end
+ end
+end
+
+describe Kumade::Heroku, "#delete_deploy_branch" do
+ before { STDOUT.stub(:puts) }
+
+ it "deletes the deploy branch" do
+ Cocaine::CommandLine.should_receive(:new).
+ with("git checkout master && git branch -D deploy").
+ and_return(stub(:run => true))
+ subject.delete_deploy_branch
+ end
+end
+
View
4 spec/spec_helper.rb
@@ -18,6 +18,10 @@ def remove_remote(remote_name)
end
end
+
+spec_dir = Pathname.new(File.expand_path(File.dirname(__FILE__)))
+Dir[spec_dir.join('support', '**')].each {|f| require File.expand_path(f) }
+
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
View
32 spec/support/heroku.rb
@@ -0,0 +1,32 @@
+shared_context "when on Cedar" do
+ let(:cocaine_mock) { mock("Cocaine::CommandLine") }
+
+ before do
+ Cocaine::CommandLine.should_receive(:new).
+ with("bundle exec heroku stack --remote staging").
+ and_return(cocaine_mock)
+
+ cocaine_mock.should_receive(:run).and_return(%{
+ aspen-mri-1.8.6
+ bamboo-mri-1.9.2
+ bamboo-ree-1.8.7
+* cedar (beta)
+})
+ end
+end
+
+shared_context "when not on Cedar" do
+ let(:cocaine_mock) { mock("Cocaine::CommandLine") }
+
+ before do
+ Cocaine::CommandLine.should_receive(:new).
+ with("bundle exec heroku stack --remote staging").
+ and_return(cocaine_mock)
+ cocaine_mock.should_receive(:run).and_return(%{
+ aspen-mri-1.8.6
+* bamboo-mri-1.9.2
+ bamboo-ree-1.8.7
+ cedar (beta)
+})
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.