Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow Kumade::CLI to be initialized

  • Loading branch information...
commit 2879fbc5e2588aadedc72f7b9a31ae6e052eca1c 1 parent 28f79b2
@joshuaclayton joshuaclayton authored
Showing with 70 additions and 54 deletions.
  1. +1 −1  bin/kumade
  2. +36 −33 lib/kumade/cli.rb
  3. +33 −20 spec/kumade/cli_spec.rb
View
2  bin/kumade
@@ -2,4 +2,4 @@
require 'kumade'
-Kumade::CLI.run
+Kumade::CLI.new
View
69 lib/kumade/cli.rb
@@ -4,35 +4,57 @@
module Kumade
class CLI
class << self
- attr_reader :environment
+ attr_writer :deployer
+
+ def deployer
+ @deployer || Kumade::Deployer
+ end
end
- def self.run(args = ARGV, out = StringIO.new)
- @out = out
- @options = parse_arguments!(args)
+ def initialize(args = ARGV, out = StringIO.new)
+ @options = {}
+ parse_arguments!(args)
@environment = args.shift || 'staging'
- swapping_stdout_for(@out) do
+ self.class.swapping_stdout_for(out, pretending?) do
deploy
end
end
- def self.deploy
+ def self.swapping_stdout_for(io, pretending = false)
+ if pretending
+ yield
+ else
+ begin
+ real_stdout = $stdout
+ $stdout = io
+ yield
+ rescue Kumade::DeploymentError
+ io.rewind
+ real_stdout.print(io.read)
+ ensure
+ $stdout = real_stdout
+ end
+ end
+ end
+
+ private
+
+ def deploy
if pretending?
puts "==> In Pretend Mode"
end
- puts "==> Deploying to: #{environment}"
- Deployer.new(environment, pretending?).deploy
- puts "==> Deployed to: #{environment}"
+ puts "==> Deploying to: #{@environment}"
+ self.class.deployer.new(@environment, pretending?).deploy
+ puts "==> Deployed to: #{@environment}"
end
- def self.parse_arguments!(args)
- options = {}
+ def parse_arguments!(args)
OptionParser.new do |opts|
opts.banner = "Usage: kumade <environment> [options]"
opts.on("-p", "--pretend", "Pretend mode: print what kumade would do") do |p|
- options[:pretend] = p
+ @options[:pretend] = p
end
opts.on_tail('-v', '--version', 'Show version') do
@@ -45,29 +67,10 @@ def self.parse_arguments!(args)
exit
end
end.parse!(args)
-
- options
- end
-
- def self.swapping_stdout_for(io)
- if pretending?
- yield
- else
- begin
- real_stdout = $stdout
- $stdout = io
- yield
- rescue Kumade::DeploymentError
- io.rewind
- real_stdout.print(io.read)
- ensure
- $stdout = real_stdout
- end
- end
end
- def self.pretending?
- @options[:pretend]
+ def pretending?
+ !!@options[:pretend]
end
end
end
View
53 spec/kumade/cli_spec.rb
@@ -4,27 +4,44 @@
let(:out) { StringIO.new }
let(:environment) { 'my-environment' }
- subject { Kumade::CLI }
-
- %w(-p --pretend).each do |pretend_arg|
- it "sets pretend mode when run with #{pretend_arg}" do
- subject.stub(:deploy)
-
- subject.run([environment, pretend_arg], out)
- subject.pretending?.should be_true
+ let(:deployer) { stub("Deployer", :new => deployer_instance) }
+ let(:deployer_instance) { stub("DeployerInstance", :deploy => nil) }
+
+ before { Kumade::CLI.deployer = deployer }
+ after { Kumade::CLI.deployer = nil }
+
+ context "when pretending" do
+ %w(-p --pretend).each do |pretend_flag|
+ context pretend_flag do
+ subject { Kumade::CLI.new([pretend_flag, environment], out) }
+
+ it "deploys correctly" do
+ deployer.should_receive(:new).with(environment, true)
+ deployer_instance.should_receive(:deploy)
+ subject
+ end
+ end
end
end
- it "defaults to staging" do
- subject.stub(:deploy)
- subject.run([], out)
- subject.environment.should == 'staging'
+ context "running normally" do
+ subject { Kumade::CLI.new([environment], out) }
+
+ it "deploys correctly" do
+ deployer.should_receive(:new).with(environment, false)
+ deployer_instance.should_receive(:deploy)
+ subject
+ end
end
+end
- it "deploys" do
- Kumade::Deployer.any_instance.should_receive(:deploy)
+describe Kumade::CLI, ".deployer" do
+ after { Kumade::CLI.deployer = nil }
+ it { Kumade::CLI.deployer.should == Kumade::Deployer }
- subject.run
+ it "can override deployer" do
+ Kumade::CLI.deployer = "deployer!"
+ Kumade::CLI.deployer.should == "deployer!"
end
end
@@ -53,14 +70,10 @@
end
context "in pretend mode" do
- before do
- Kumade::CLI.should_receive(:pretending?).and_return(true)
- end
-
it 'prints everything' do
stdout.should_receive(:puts)
- Kumade::CLI.swapping_stdout_for(output) do
+ Kumade::CLI.swapping_stdout_for(output, true) do
$stdout.puts "Hello, you can see me!"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.