Modified Registry.add_callback() to do nothing when called from a
disabled plug-in.

Paired-with: Jesse Wolfe <[email protected]>
Signed-off-by: Paul Berry <[email protected]>
---
Local-branch: maint/next/create_module_install_tasks
 lib/registry.rb           |   32 ++++++++++++++++++++++++++------
 spec/lib/registry_spec.rb |   31 +++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/lib/registry.rb b/lib/registry.rb
index a1f60f3..09f6d0b 100644
--- a/lib/registry.rb
+++ b/lib/registry.rb
@@ -8,13 +8,15 @@ class Registry
   end
 
   def add_callback( feature_name, hook_name, callback_name, value = nil, 
&block )
-    if block and value
-      raise "Cannot pass both a value and a block to add_callback"
-    elsif @registry[feature_name][hook_name][callback_name]
-      raise "Cannot redefine callback 
[#{feature_name.inspect},#{hook_name.inspect},#{callback_name}]"
-    end
+    disallow_uninstalled_plugins do
+      if block and value
+        raise "Cannot pass both a value and a block to add_callback"
+      elsif @registry[feature_name][hook_name][callback_name]
+        raise "Cannot redefine callback 
[#{feature_name.inspect},#{hook_name.inspect},#{callback_name}]"
+      end
 
-    @registry[feature_name][hook_name][callback_name] = value || block
+      @registry[feature_name][hook_name][callback_name] = value || block
+    end
   end
 
   def each_callback( feature_name, hook_name )
@@ -41,4 +43,22 @@ class Registry
       end
     end
   end
+
+  private
+
+  def installed_plugins
+    @installed_plugins ||= Dir.open(File.join(File.dirname(__FILE__), 
'../config/installed_plugins')).to_a.reject { |f|
+      f =~ /^\./
+    }
+  end
+
+  def disallow_uninstalled_plugins
+    caller.each do |call_source|
+      if call_source =~ %r{/vendor/plugins/([^/]+)/}
+        plugin_name = $1
+        return unless installed_plugins.include? plugin_name
+      end
+    end
+    yield
+  end
 end
diff --git a/spec/lib/registry_spec.rb b/spec/lib/registry_spec.rb
index ecb3020..735d04e 100644
--- a/spec/lib/registry_spec.rb
+++ b/spec/lib/registry_spec.rb
@@ -38,6 +38,37 @@ describe @registry do
       callbacks.first.call.should == "my block"
       callbacks.last.should == "foo bar baz"
     end
+
+    it "ignores calls from disabled plugins" do
+      @registry.stubs(:installed_plugins).returns(['foo'])
+      class << @registry
+        def caller
+          ['irrelevant/path.rb', '/x/y/z/vendor/plugins/bar/init.rb', 
'other/irrelevant/path.rb']
+        end
+      end
+      @registry.add_callback(:test, :hook, "0_callback") { "my callback" }
+      callbacks = []
+      @registry.each_callback(:test, :hook) do |callback|
+        callbacks << callback
+      end
+      callbacks.should == []
+    end
+
+    it "allows calls from enabled plugins" do
+      @registry.stubs(:installed_plugins).returns(['foo'])
+      class << @registry
+        def caller
+          ['irrelevant/path.rb', '/x/y/z/vendor/plugins/foo/init.rb', 
'other/irrelevant/path.rb']
+        end
+      end
+      @registry.add_callback(:test, :hook, "0_callback") { "my callback" }
+      callbacks = []
+      @registry.each_callback(:test, :hook) do |callback|
+        callbacks << callback
+      end
+      callbacks.length.should == 1
+      callbacks.first.call.should == "my callback"
+    end
   end
 
   describe "#each_callback" do
-- 
1.7.2

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to