Skip to content

Commit

Permalink
Added a dry-run option to see what commands will be run without actua…
Browse files Browse the repository at this point in the history
…lly running them
  • Loading branch information
pgr0ss committed Jul 19, 2008
1 parent ad0a1c2 commit 7279a38
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/capistrano/cli/execute.rb
Expand Up @@ -23,6 +23,7 @@ def execute
def execute!
config = instantiate_configuration
config.debug = options[:debug]
config.dry_run = options[:dry_run]
config.logger.level = options[:verbose]

set_pre_vars(config)
Expand Down
4 changes: 4 additions & 0 deletions lib/capistrano/cli/options.rb
Expand Up @@ -31,6 +31,10 @@ def option_parser #:nodoc:
"Prompts before each remote command execution."
) { |value| options[:debug] = true }

opts.on("-n", "--dry-run",
"Prints out commands without running them."
) { |value| options[:dry_run] = true }

opts.on("-e", "--explain TASK",
"Displays help (if available) for the task."
) { |value| options[:explain] = value }
Expand Down
3 changes: 2 additions & 1 deletion lib/capistrano/configuration.rb
Expand Up @@ -19,10 +19,11 @@ module Capistrano
# define roles, and set configuration variables.
class Configuration
# The logger instance defined for this configuration.
attr_accessor :debug, :logger
attr_accessor :debug, :logger, :dry_run

def initialize #:nodoc:
@debug = false
@dry_run = false
@logger = Logger.new
end

Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/configuration/actions/invocation.rb
Expand Up @@ -46,7 +46,7 @@ def run(cmd, options={}, &block)
block ||= self.class.default_io_proc
logger.debug "executing #{cmd.strip.inspect}"

return if debug && continue_execution(cmd) == false
return if dry_run || (debug && continue_execution(cmd) == false)

options = add_default_command_options(options)

Expand Down
2 changes: 1 addition & 1 deletion test/cli/execute_test.rb
Expand Up @@ -15,7 +15,7 @@ def initialize
def setup
@cli = MockCLI.new
@logger = stub_everything
@config = stub(:logger => @logger, :debug= => nil)
@config = stub(:logger => @logger, :debug= => nil, :dry_run= => nil)
@config.stubs(:set)
@config.stubs(:load)
@config.stubs(:trigger)
Expand Down
12 changes: 12 additions & 0 deletions test/cli/options_test.rb
Expand Up @@ -30,6 +30,18 @@ def test_parse_options_with_d_should_set_debug_option
assert @cli.options[:debug]
end

def test_parse_options_with_n_should_set_dry_run_option
@cli.args << "-n"
@cli.parse_options!
assert @cli.options[:dry_run]
end

def test_parse_options_with_dry_run_should_set_dry_run_option
@cli.args << "--dry-run"
@cli.parse_options!
assert @cli.options[:dry_run]
end

def test_parse_options_with_e_should_set_explain_option
@cli.args << "-e" << "sample"
@cli.parse_options!
Expand Down
7 changes: 7 additions & 0 deletions test/configuration/actions/invocation_test.rb
Expand Up @@ -5,6 +5,7 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase
class MockConfig
attr_reader :options
attr_accessor :debug
attr_accessor :dry_run

def initialize
@options = {}
Expand Down Expand Up @@ -40,6 +41,12 @@ def test_run_options_should_be_passed_to_execute_on_servers
@config.run "ls", :foo => "bar"
end

def test_run_will_return_if_dry_run
@config.expects(:dry_run).returns(true)
@config.expects(:execute_on_servers).never
@config.run "ls", :foo => "bar"
end

def test_run_without_block_should_use_default_io_proc
@config.expects(:execute_on_servers).yields(%w(s1 s2 s3).map { |s| server(s) })
@config.expects(:sessions).returns(Hash.new { |h,k| h[k] = k.host.to_sym }).times(3)
Expand Down

0 comments on commit 7279a38

Please sign in to comment.