Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use Kumade::Configuration to handle environment and pretending.

  • Loading branch information...
commit 469761b791e9fbb20783b063ce9cabe2357e5665 1 parent 681bc8f
@gabebw gabebw authored
View
6 lib/kumade.rb
@@ -8,6 +8,10 @@ module Kumade
autoload :Configuration, "kumade/configuration"
def self.configuration
- @@configuration ||= Kumade::Configuration.new
+ @@configuration ||= Configuration.new
+ end
+
+ def self.configuration=(new_configuration)
+ @@configuration = new_configuration
end
end
View
5 lib/kumade/base.rb
@@ -2,14 +2,13 @@
module Kumade
class Base < Thor::Shell::Color
- attr_reader :pretending
def initialize
super()
end
def run_or_error(command, error_message)
say_status(:run, command)
- if !pretending
+ if ! Kumade.configuration.pretending?
error(error_message) unless run(command)
end
end
@@ -33,4 +32,4 @@ def success(message)
say("==> #{message}", :green)
end
end
-end
+end
View
22 lib/kumade/cli.rb
@@ -14,13 +14,15 @@ def deployer
def initialize(args = ARGV, out = StringIO.new)
@options = {}
parse_arguments!(args)
- @environment = args.shift || 'staging'
+
+ Kumade.configuration.pretending = !!@options[:pretend]
+ Kumade.configuration.environment = args.shift || 'staging'
self.class.swapping_stdout_for(out, print_output?) do
deploy
end
end
-
+
def self.swapping_stdout_for(io, print_output = false)
if print_output
yield
@@ -41,12 +43,12 @@ def self.swapping_stdout_for(io, print_output = false)
private
def deploy
- if pretending?
+ if Kumade.configuration.pretending?
puts "==> In Pretend Mode"
end
- puts "==> Deploying to: #{@environment}"
- self.class.deployer.new(@environment, pretending?).deploy
- puts "==> Deployed to: #{@environment}"
+ puts "==> Deploying to: #{Kumade.configuration.environment}"
+ self.class.deployer.new.deploy
+ puts "==> Deployed to: #{Kumade.configuration.environment}"
end
def parse_arguments!(args)
@@ -73,16 +75,12 @@ def parse_arguments!(args)
end.parse!(args)
end
- def pretending?
- !!@options[:pretend]
- end
-
def verbose?
@options[:verbose]
end
-
+
def print_output?
- pretending? || verbose?
+ Kumade.configuration.pretending? || verbose?
end
end
end
View
6 lib/kumade/configuration.rb
@@ -1,8 +1,8 @@
module Kumade
class Configuration
- def initialize
- @environment = 'staging'
- @pretending = false
+ def initialize(environment = 'staging', pretending = false )
+ @environment = environment
+ @pretending = pretending
end
def pretending?
View
34 lib/kumade/deployer.rb
@@ -4,14 +4,12 @@
module Kumade
class Deployer < Base
DEPLOY_BRANCH = "deploy"
- attr_reader :environment, :pretending, :git
+ attr_reader :git
- def initialize(environment = 'staging', pretending = false)
+ def initialize
super()
- @environment = environment
- @pretending = pretending
- @git = Git.new(environment, pretending)
- @branch = @git.current_branch
+ @git = Git.new
+ @branch = @git.current_branch
end
def deploy
@@ -38,12 +36,12 @@ def sync_github
def sync_heroku
git.create(DEPLOY_BRANCH)
- git.push("#{DEPLOY_BRANCH}:master", environment, true)
+ git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
end
def heroku_migrate
- heroku("rake db:migrate") unless pretending
- success("Migrated #{environment}")
+ heroku("rake db:migrate") unless Kumade.configuration.pretending?
+ success("Migrated #{Kumade.configuration.environment}")
end
def post_deploy
@@ -56,13 +54,13 @@ def heroku(command)
else
"bundle exec heroku"
end
- run_or_error("#{heroku_command} #{command} --remote #{environment}",
+ 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 #{environment}").run.split("\n").grep(/\*/).any? do |line|
+ @cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{Kumade.configuration.environment}").run.split("\n").grep(/\*/).any? do |line|
line.include?("cedar")
end
end
@@ -81,7 +79,7 @@ def package_with_jammit
begin
success_message = "Packaged assets with Jammit"
- if pretending
+ if Kumade.configuration.pretending?
success(success_message)
else
Jammit.package!
@@ -96,7 +94,7 @@ def package_with_jammit
def package_with_more
success_message = "Packaged assets with More"
- if pretending
+ if Kumade.configuration.pretending?
success(success_message)
else
begin
@@ -113,7 +111,7 @@ def package_with_more
def invoke_custom_task
success("Running kumade:before_asset_compilation task")
- Rake::Task["kumade:before_asset_compilation"].invoke unless pretending
+ Rake::Task["kumade:before_asset_compilation"].invoke unless Kumade.configuration.pretending?
end
def git_add_and_commit_all_assets_in(dir)
@@ -156,14 +154,14 @@ def custom_task?
end
def ensure_heroku_remote_exists
- if git.remote_exists?(environment)
+ if git.remote_exists?(Kumade.configuration.environment)
if git.heroku_remote?
- success("#{environment} is a Heroku remote")
+ success("#{Kumade.configuration.environment} is a Heroku remote")
else
- error(%{Cannot deploy: "#{environment}" remote does not point to Heroku})
+ error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
end
else
- error(%{Cannot deploy: "#{environment}" remote does not exist})
+ error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
end
end
end
View
12 lib/kumade/git.rb
@@ -1,15 +1,13 @@
require 'cocaine'
module Kumade
class Git < Base
- attr_reader :environment
- def initialize(environment = 'staging', pretending = false)
+ def initialize
super()
- @pretending = pretending
- @environment = environment
end
def heroku_remote?
- `git config --get remote.#{environment}.url`.strip =~ /^git@heroku\..+:(.+)\.git$/
+ remote_url = `git config --get remote.#{Kumade.configuration.environment}.url`.strip
+ !! remote_url.strip.match(/^git@heroku\..+:(.+)\.git$/)
end
def self.environments
@@ -48,7 +46,7 @@ def current_branch
end
def remote_exists?(remote_name)
- if @pretending
+ if Kumade.configuration.pretending?
true
else
`git remote` =~ /^#{remote_name}$/
@@ -60,7 +58,7 @@ def dirty?
end
def ensure_clean_git
- if ! @pretending && dirty?
+ if ! Kumade.configuration.pretending? && dirty?
error("Cannot deploy: repo is not clean.")
else
success("Git repo is clean")
View
38 spec/kumade/base_spec.rb
@@ -18,35 +18,33 @@
end
describe Kumade::Base, "#run_or_error" do
- let(:command) { "command" }
- let(:error_message) { "error_message" }
-
- before(:each) do
+ let(:command) { "dummy command" }
+ let(:error_message) { "dummy error message" }
+
+ before 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)
+ before do
+ Kumade.configuration.pretending = true
end
- end
-
- context "when not pretending" do
- before(:each) do
- subject.should_receive(:pretending).and_return(false)
+ it "does not run the command" do
+ subject.should_not_receive(:run)
+ subject.run_or_error("dummy command", "dummy error message")
end
+ end
+ context "when not pretending" do
context "with success" do
- it "should call not call error" do
+ it "should 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)
@@ -60,18 +58,18 @@
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
+end
View
38 spec/kumade/cli_spec.rb
@@ -3,7 +3,6 @@
describe Kumade::CLI do
let(:out) { StringIO.new }
let(:environment) { 'my-environment' }
-
let(:deployer) { stub("Deployer", :new => deployer_instance) }
let(:deployer_instance) { stub("DeployerInstance", :deploy => nil) }
@@ -12,11 +11,15 @@
context "when pretending" do
%w(-p --pretend).each do |pretend_flag|
+ subject { Kumade::CLI.new([pretend_flag, environment], out) }
+
context pretend_flag do
- subject { Kumade::CLI.new([pretend_flag, environment], out) }
+ it "sets pretending to true" do
+ subject
+ Kumade.configuration.pretending.should == true
+ end
- it "deploys correctly" do
- deployer.should_receive(:new).with(environment, true)
+ it "deploys" do
deployer_instance.should_receive(:deploy)
subject
end
@@ -24,11 +27,27 @@
end
end
+ context "with no command-line arguments" do
+ subject { Kumade::CLI.new([], out) }
+
+ it "sets the environment to staging" do
+ Kumade.configuration.environment.should == 'staging'
+ end
+
+ it "sets pretending to false" do
+ Kumade.configuration.pretending.should == false
+ end
+ end
+
context "running normally" do
subject { Kumade::CLI.new([environment], out) }
- it "deploys correctly" do
- deployer.should_receive(:new).with(environment, false)
+ it "sets pretending to false" do
+ subject
+ Kumade.configuration.pretending.should == false
+ end
+
+ it "deploys" do
deployer_instance.should_receive(:deploy)
subject
end
@@ -37,7 +56,10 @@
describe Kumade::CLI, ".deployer" do
after { Kumade::CLI.deployer = nil }
- it { Kumade::CLI.deployer.should == Kumade::Deployer }
+
+ it "sets the deployer to the Deployer class by default" do
+ Kumade::CLI.deployer.should == Kumade::Deployer
+ end
it "can override deployer" do
Kumade::CLI.deployer = "deployer!"
@@ -78,4 +100,4 @@
end
end
end
-end
+end
View
53 spec/kumade/configuration_spec.rb
@@ -1,37 +1,36 @@
require 'spec_helper'
-describe Kumade::Configuration do
- context "#pretending" do
- it "has read/write access for the pretending attribute" do
- subject.pretending = true
- subject.pretending.should == true
- end
- end
+describe Kumade::Configuration, "by default" do
+ its(:environment) { should == 'staging' }
+ it { should_not be_pretending }
+end
- context "pretending?" do
- it "returns false when not pretending" do
- subject.pretending = false
- subject.should_not be_pretending
- end
+describe Kumade::Configuration, "#pretending" do
+ it "has read/write access for the pretending attribute" do
+ subject.pretending = true
+ subject.pretending.should == true
+ end
+end
- it "returns true when pretending" do
- subject.pretending = true
- subject.should be_pretending
- end
+describe Kumade::Configuration, "#pretending?" do
+ it "returns false when not pretending" do
+ subject.pretending = false
+ subject.should_not be_pretending
+ end
- it "defaults to false" do
- subject.pretending.should == false
- end
+ it "returns true when pretending" do
+ subject.pretending = true
+ subject.should be_pretending
end
- context "#environment" do
- it "has read/write access for the environment attribute" do
- subject.environment = 'new-environment'
- subject.environment.should == 'new-environment'
- end
+ it "defaults to false" do
+ subject.pretending.should == false
+ end
+end
- it "defaults to staging" do
- subject.environment.should == 'staging'
- end
+describe Kumade::Configuration, "#environment" do
+ it "has read/write access for the environment attribute" do
+ subject.environment = 'new-environment'
+ subject.environment.should == 'new-environment'
end
end
View
27 spec/kumade/deployer_spec.rb
@@ -83,9 +83,10 @@
let(:environment) { 'my-env' }
let(:git_mock) { mock() }
- subject { Kumade::Deployer.new(environment) }
-
- before { subject.stub(:git => git_mock) }
+ before do
+ Kumade.configuration.environment = environment
+ subject.stub(:git => git_mock)
+ end
it "calls git.create and git.push" do
git_mock.should_receive(:create).with("deploy")
@@ -391,17 +392,14 @@ class More
describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
let(:environment) { 'staging' }
- let(:bad_environment) { 'bad' }
before do
subject.stub(:say)
force_add_heroku_remote(environment)
- `git remote add #{bad_environment} blerg@example.com`
+ Kumade.configuration.environment = environment
end
context "when the remote points to Heroku" do
- subject { Kumade::Deployer.new(environment) }
-
it "does not print an error" do
subject.should_not_receive(:error)
@@ -416,8 +414,9 @@ class More
end
context "when the remote does not exist" do
- subject { Kumade::Deployer.new(environment) }
- before { remove_remote(environment) }
+ before do
+ remove_remote(environment)
+ end
it "prints an error" do
subject.should_receive(:error).with(%{Cannot deploy: "#{environment}" remote does not exist})
@@ -427,7 +426,12 @@ class More
end
context "when the remote does not point to Heroku" do
- subject { Kumade::Deployer.new(bad_environment) }
+ let(:bad_environment) { 'bad' }
+
+ before do
+ `git remote add #{bad_environment} blerg@example.com`
+ Kumade.configuration.environment = bad_environment
+ end
it "prints an error" do
subject.should_receive(:error).with(%{Cannot deploy: "#{bad_environment}" remote does not point to Heroku})
@@ -475,8 +479,8 @@ class More
describe Kumade::Deployer, "#heroku" do
context "when on Cedar" do
- subject { Kumade::Deployer.new('staging', false) }
before { subject.stub(:cedar?).and_return(true) }
+
it "runs commands with `run`" do
subject.should_receive(:run_or_error).with("bundle exec heroku run rake --remote staging", //)
subject.heroku("rake")
@@ -484,7 +488,6 @@ class More
end
context "when not on Cedar" do
- subject { Kumade::Deployer.new('staging', false) }
before { subject.stub(:cedar?).and_return(false) }
it "runs commands without `run`" do
View
52 spec/kumade/git_spec.rb
@@ -1,31 +1,45 @@
require 'spec_helper'
describe Kumade::Git, "#heroku_remote?" do
- let(:environment) { 'staging' }
- let(:another_heroku_environment) { 'another_staging' }
- let(:not_a_heroku_env) { 'fake_heroku' }
- let(:not_a_heroku_url) { 'git@github.com:gabebw/kumade.git' }
- let(:another_heroku_url) { 'git@heroku.work:my-app.git' }
+ context "when the environment is a Heroku repository" do
+ let(:environment) { 'staging' }
- before do
- force_add_heroku_remote(environment)
- `git remote add #{not_a_heroku_env} #{not_a_heroku_url}`
- `git remote add #{another_heroku_environment} #{another_heroku_url}`
- end
+ before do
+ force_add_heroku_remote(environment)
+ Kumade.configuration.environment = environment
+ end
- after do
- remove_remote(environment)
- remove_remote(not_a_heroku_env)
- remove_remote(another_heroku_environment)
+ after { remove_remote(environment) }
+
+ its(:heroku_remote?) { should == true }
end
- it "returns true when the remote is a heroku repository" do
- Kumade::Git.new(environment).heroku_remote?.should be_true
- Kumade::Git.new(another_heroku_environment).heroku_remote?.should be_true
+ context "when the environment is a Heroku repository managed with heroku-accounts" do
+ let(:another_heroku_environment) { 'another_staging' }
+ let(:another_heroku_url) { 'git@heroku.work:my-app.git' }
+
+ before do
+ force_add_heroku_remote(another_heroku_environment)
+ Kumade.configuration.environment = another_heroku_environment
+ end
+
+ after { remove_remote(another_heroku_environment) }
+
+ its(:heroku_remote?) { should == true }
end
- it "returns false when the remote is not a heroku repository" do
- Kumade::Git.new('kumade').heroku_remote?.should be_false
+ context "when the environment is not a Heroku repository" do
+ let(:not_a_heroku_env) { 'fake_heroku' }
+ let(:not_a_heroku_url) { 'git@github.com:gabebw/kumade.git' }
+
+ before do
+ `git remote add #{not_a_heroku_env} #{not_a_heroku_url}`
+ Kumade.configuration.environment = not_a_heroku_env
+ end
+
+ after { remove_remote(not_a_heroku_env) }
+
+ its(:heroku_remote?) { should == false }
end
end
View
6 spec/kumade_spec.rb
@@ -10,3 +10,9 @@
end
end
+describe Kumade, ".configuration=" do
+ it "sets Kumade.configuration" do
+ Kumade.configuration = "new-value"
+ Kumade.configuration.should == "new-value"
+ end
+end
View
6 spec/spec_helper.rb
@@ -3,7 +3,9 @@
require 'kumade'
+# Since we autoload, these aren't require'd when we require kumade.
require 'rake'
+require 'cocaine'
module GitRemoteHelpers
def force_add_heroku_remote(remote_name)
@@ -36,4 +38,8 @@ def remove_remote(remote_name)
example.run
end
end
+
+ config.after do
+ Kumade.configuration = Kumade::Configuration.new
+ end
end

0 comments on commit 469761b

Please sign in to comment.
Something went wrong with that request. Please try again.