Permalink
Browse files

Run silently until an error occurs.

  • Loading branch information...
1 parent 7c8abe1 commit c93ddd46bc476467d18962f6eef9ef6ec44ee01f @jyurek jyurek committed Sep 2, 2011
View
@@ -1,11 +1,13 @@
require 'rake'
require 'thor'
+require 'stringio'
require 'kumade/base'
require 'kumade/git'
require 'kumade/deployer'
require 'kumade/runner'
require 'kumade/railtie'
+require 'kumade/deployment_error'
module Kumade
end
View
@@ -20,11 +20,11 @@ def run(command, config = {})
def error(message)
say("==> ! #{message}", :red)
- exit 1
+ raise Kumade::DeploymentError.new(message)
end
def success(message)
say("==> #{message}", :green)
end
end
-end
+end
@@ -0,0 +1,4 @@
+module Kumade
+ class DeploymentError < StandardError
+ end
+end
View
@@ -6,21 +6,23 @@ class << self
attr_reader :environment
end
- def self.run(args=ARGV, out=$stdout)
+ def self.run(args=ARGV, out=StringIO.new)
@out = out
@options = parse_arguments!(args)
@environment = args.shift || 'staging'
- deploy
+ swapping_stdout_for(@out) do
+ deploy
+ end
end
def self.deploy
if pretending?
- @out.puts "==> In Pretend Mode"
+ puts "==> In Pretend Mode"
end
- @out.puts "==> Deploying to: #{environment}"
+ puts "==> Deploying to: #{environment}"
Deployer.new(environment, pretending?, @options[:cedar]).deploy
- @out.puts "==> Deployed to: #{environment}"
+ puts "==> Deployed to: #{environment}"
end
def self.parse_arguments!(args)
@@ -37,19 +39,34 @@ def self.parse_arguments!(args)
end
opts.on_tail('-v', '--version', 'Show version') do
- @out.puts "kumade #{Kumade::VERSION}"
+ puts "kumade #{Kumade::VERSION}"
exit
end
opts.on_tail('-h', '--help', 'Show this message') do
- @out.puts opts
+ puts opts
exit
end
end.parse!(args)
options
end
+ def self.swapping_stdout_for(io)
+ begin
+ $real_stdout = $stdout
+ $stdout = io unless pretending?
+ yield
+ rescue Kumade::DeploymentError
+ unless pretending?
+ io.rewind
+ $real_stdout.print(io.read)
+ end
+ ensure
+ $stdout = $real_stdout
+ end
+ end
+
def self.pretending?
@options[:pretend]
end
View
@@ -10,4 +10,9 @@
it "exists" do
subject.should respond_to(:error)
end
+
+ it "prints its message and raises its message" do
+ subject.should_receive(:say).with("==> ! I'm an error!", :red)
+ lambda{ subject.error("I'm an error!") }.should raise_error(Kumade::DeploymentError)
+ end
end
@@ -453,4 +453,9 @@ class More
git_mock.should_receive(:delete).with('deploy', 'master')
subject.post_deploy
end
+
+ it "prints its message and raises its message" do
+ subject.should_receive(:say).with("==> ! I'm an error!", :red)
+ lambda{ subject.error("I'm an error!") }.should raise_error(Kumade::DeploymentError)
+ end
end
View
@@ -37,3 +37,40 @@
end
end
end
+
+describe Kumade::Runner do
+ it 'does not let anything get printed' do
+ stdout = $stdout
+ stdout.should_not_receive(:print)
+ output = StringIO.new
+
+ Kumade::Runner.swapping_stdout_for(output) do
+ $stdout.puts "Hello, you can't see me."
+ end
+
+ output.rewind
+ output.read.should == "Hello, you can't see me.\n"
+ end
+
+ it 'dumps the output stash to real stdout when an error happens' do
+ stdout = $stdout
+ stdout.should_receive(:print)
+ output = StringIO.new
+
+ Kumade::Runner.swapping_stdout_for(output) do
+ $stdout.puts "Hello, you can see me!"
+ raise Kumade::DeploymentError.new("error")
+ end
+ end
+
+ it 'prints everything in pretend mode' do
+ stdout = $stdout
+ stdout.should_receive(:puts)
+ output = StringIO.new
+ Kumade::Runner.should_receive(:pretending?).and_return(true)
+
+ Kumade::Runner.swapping_stdout_for(output) do
+ $stdout.puts "Hello, you can see me!"
+ end
+ end
+end

0 comments on commit c93ddd4

Please sign in to comment.