From 14378caa6724dbee49238f58203cc77692d713c3 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Wed, 9 May 2007 05:24:21 +0000 Subject: [PATCH] Added support for load and exit callbacks, which get invoked when all recipes have been loaded and when all requested tasks have been executed git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6709 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- CHANGELOG | 2 ++ lib/capistrano/callback.rb | 4 ++-- lib/capistrano/cli/execute.rb | 2 ++ lib/capistrano/configuration/callbacks.rb | 6 ++++-- test/cli/execute_test.rb | 11 +++++++++++ test/configuration/callbacks_test.rb | 14 ++++++++++++++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 328802e9..619d3a50 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added support for load and exit callbacks, which get invoked when all recipes have been loaded and when all requested tasks have been executed [Jamis Buck] + * Added support for start and finish callbacks, which get invoked when any task is called via the command-line [Jamis Buck] * Make `capify' understand simple command-line switches [Jamis Buck] diff --git a/lib/capistrano/callback.rb b/lib/capistrano/callback.rb index 1e12e41f..f2dfb488 100644 --- a/lib/capistrano/callback.rb +++ b/lib/capistrano/callback.rb @@ -10,9 +10,9 @@ module Capistrano end def applies_to?(task) - if only.any? + if task && only.any? return only.include?(task.fully_qualified_name) - elsif except.any? + elsif task && except.any? return !except.include?(task.fully_qualified_name) else return true diff --git a/lib/capistrano/cli/execute.rb b/lib/capistrano/cli/execute.rb index 894cc3fd..42a6f582 100644 --- a/lib/capistrano/cli/execute.rb +++ b/lib/capistrano/cli/execute.rb @@ -27,7 +27,9 @@ module Capistrano set_pre_vars(config) load_recipes(config) + config.trigger(:load) execute_requested_actions(config) + config.trigger(:exit) config rescue Exception => error diff --git a/lib/capistrano/configuration/callbacks.rb b/lib/capistrano/configuration/callbacks.rb index 1c1b7e78..acda5c06 100644 --- a/lib/capistrano/configuration/callbacks.rb +++ b/lib/capistrano/configuration/callbacks.rb @@ -115,10 +115,12 @@ module Capistrano # Trigger the named event for the named task. All associated callbacks # will be fired, in the order they were defined. - def trigger(event, task) + def trigger(event, task=nil) pending = Array(callbacks[event]).select { |c| c.applies_to?(task) } if pending.any? - logger.trace "triggering #{event} callbacks for `#{task.fully_qualified_name}'" + msg = "triggering #{event} callbacks" + msg << " for `#{task.fully_qualified_name}'" if task + logger.trace(msg) pending.each { |callback| callback.call } end end diff --git a/test/cli/execute_test.rb b/test/cli/execute_test.rb index 6bf5e9cd..2715631e 100644 --- a/test/cli/execute_test.rb +++ b/test/cli/execute_test.rb @@ -18,6 +18,7 @@ class CLIExecuteTest < Test::Unit::TestCase @config = stub(:logger => @logger) @config.stubs(:set) @config.stubs(:load) + @config.stubs(:trigger) @cli.stubs(:instantiate_configuration).returns(@config) end @@ -83,6 +84,16 @@ class CLIExecuteTest < Test::Unit::TestCase @cli.execute! end + def test_execute_should_call_load_and_exit_triggers + @cli.options[:actions] = %w(first second) + @config.expects(:find_and_execute_task).with("first", :before => :start, :after => :finish) + @config.expects(:find_and_execute_task).with("second", :before => :start, :after => :finish) + @config.expects(:trigger).never + @config.expects(:trigger).with(:load) + @config.expects(:trigger).with(:exit) + @cli.execute! + end + def test_execute_should_call_handle_error_when_exceptions_occur @config.expects(:load).raises(Exception, "boom") @cli.expects(:handle_error).with { |e,| Exception === e } diff --git a/test/configuration/callbacks_test.rb b/test/configuration/callbacks_test.rb index f4e2c58c..8eb353f1 100644 --- a/test/configuration/callbacks_test.rb +++ b/test/configuration/callbacks_test.rb @@ -136,6 +136,20 @@ class ConfigurationCallbacksTest < Test::Unit::TestCase @config.trigger(:before, task) end + def test_trigger_without_task_should_invoke_all_callbacks_for_that_event + task = stub(:fully_qualified_name => "any:old:thing") + @config.on(:before, :first) + @config.on(:before, "second:third", :except => "any:old:thing") + @config.on(:before, "this:too", :except => "any:other:thing") + @config.on(:after, :another, "and:another") + @config.expects(:find_and_execute_task).with(:first) + @config.expects(:find_and_execute_task).with("second:third") + @config.expects(:find_and_execute_task).with("this:too") + @config.expects(:find_and_execute_task).with(:another).never + @config.expects(:find_and_execute_task).with("and:another").never + @config.trigger(:before) + end + def test_execute_task_without_named_hooks_should_just_call_task ns = stub("namespace", :default_task => nil, :name => "old", :fully_qualified_name => "any:old") task = stub(:fully_qualified_name => "any:old:thing", :name => "thing", :namespace => ns)