diff --git a/lib/capistrano/cli/execute.rb b/lib/capistrano/cli/execute.rb index a0fb6771..ae36d0f1 100644 --- a/lib/capistrano/cli/execute.rb +++ b/lib/capistrano/cli/execute.rb @@ -23,6 +23,7 @@ module Capistrano def execute! config = instantiate_configuration config.debug = options[:debug] + config.dry_run = options[:dry_run] config.logger.level = options[:verbose] set_pre_vars(config) diff --git a/lib/capistrano/cli/options.rb b/lib/capistrano/cli/options.rb index 568f45f8..c1b97970 100644 --- a/lib/capistrano/cli/options.rb +++ b/lib/capistrano/cli/options.rb @@ -31,6 +31,10 @@ module Capistrano "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 } diff --git a/lib/capistrano/configuration.rb b/lib/capistrano/configuration.rb index c55bc5ac..97044a6c 100644 --- a/lib/capistrano/configuration.rb +++ b/lib/capistrano/configuration.rb @@ -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 diff --git a/lib/capistrano/configuration/actions/invocation.rb b/lib/capistrano/configuration/actions/invocation.rb index b1734531..923e5ac2 100644 --- a/lib/capistrano/configuration/actions/invocation.rb +++ b/lib/capistrano/configuration/actions/invocation.rb @@ -46,7 +46,7 @@ module Capistrano 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) diff --git a/test/cli/execute_test.rb b/test/cli/execute_test.rb index 256bd30e..90bbd396 100644 --- a/test/cli/execute_test.rb +++ b/test/cli/execute_test.rb @@ -15,7 +15,7 @@ class CLIExecuteTest < Test::Unit::TestCase 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) diff --git a/test/cli/options_test.rb b/test/cli/options_test.rb index 89ec02f2..a32368d3 100644 --- a/test/cli/options_test.rb +++ b/test/cli/options_test.rb @@ -30,6 +30,18 @@ class CLIOptionsTest < Test::Unit::TestCase 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! diff --git a/test/configuration/actions/invocation_test.rb b/test/configuration/actions/invocation_test.rb index 13171f38..6e09495f 100644 --- a/test/configuration/actions/invocation_test.rb +++ b/test/configuration/actions/invocation_test.rb @@ -5,6 +5,7 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase class MockConfig attr_reader :options attr_accessor :debug + attr_accessor :dry_run def initialize @options = {} @@ -40,6 +41,12 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase @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)