summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-10-05 13:38:36 -0700
committerJohn Keiser <john@johnkeiser.com>2015-10-05 13:38:36 -0700
commit0053c17bba34090083a34395657ff1ba1616e213 (patch)
tree3f0234a43a070b46eebf62ed1f68a2c423f61e2e
parentdef2fe69b82fb8f0f5feb6634f63d6a3dc78ca85 (diff)
downloadchef-0053c17bba34090083a34395657ff1ba1616e213.tar.gz
Fix dispatch when there are different receivers
with different numbers of arguments. Fixes https://github.com/chef/chef-dk/issues/546)
-rw-r--r--lib/chef/event_dispatch/dispatcher.rb7
-rw-r--r--spec/unit/event_dispatch/dispatcher_spec.rb47
2 files changed, 50 insertions, 4 deletions
diff --git a/lib/chef/event_dispatch/dispatcher.rb b/lib/chef/event_dispatch/dispatcher.rb
index 966a3f32ec..f3e55539a9 100644
--- a/lib/chef/event_dispatch/dispatcher.rb
+++ b/lib/chef/event_dispatch/dispatcher.rb
@@ -32,8 +32,11 @@ class Chef
mth = s.method(method_name)
# Trim arguments to match what the subscriber expects to allow
# adding new arguments without breaking compat.
- args = args.take(mth.arity) if mth.arity < args.size && mth.arity >= 0
- mth.call(*args)
+ if mth.arity < args.size && mth.arity >= 0
+ mth.call(*args.take(mth.arity))
+ else
+ mth.call(*args)
+ end
end
end
diff --git a/spec/unit/event_dispatch/dispatcher_spec.rb b/spec/unit/event_dispatch/dispatcher_spec.rb
index 1014feea89..5a06e1d6d1 100644
--- a/spec/unit/event_dispatch/dispatcher_spec.rb
+++ b/spec/unit/event_dispatch/dispatcher_spec.rb
@@ -73,8 +73,51 @@ describe Chef::EventDispatch::Dispatcher do
expect(event_sink.synchronized_cookbook_args).to eq ["apache2"]
end
end
-
end
-end
+ context "when two event sinks have different arguments for an event" do
+ let(:event_sink_1) do
+ Class.new(Chef::EventDispatch::Base) do
+ attr_reader :synchronized_cookbook_args
+ def synchronized_cookbook(cookbook_name)
+ @synchronized_cookbook_args = [cookbook_name]
+ end
+ end.new
+ end
+ let(:event_sink_2) do
+ Class.new(Chef::EventDispatch::Base) do
+ attr_reader :synchronized_cookbook_args
+ def synchronized_cookbook(cookbook_name, cookbook)
+ @synchronized_cookbook_args = [cookbook_name, cookbook]
+ end
+ end.new
+ end
+
+ context "and the one with fewer arguments comes first" do
+ before do
+ dispatcher.register(event_sink_1)
+ dispatcher.register(event_sink_2)
+ end
+ it "trims the arugment list" do
+ cookbook_version = double("cookbook_version")
+ dispatcher.synchronized_cookbook("apache2", cookbook_version)
+ expect(event_sink_1.synchronized_cookbook_args).to eq ["apache2"]
+ expect(event_sink_2.synchronized_cookbook_args).to eq ["apache2", cookbook_version]
+ end
+ end
+
+ context "and the one with fewer arguments comes last" do
+ before do
+ dispatcher.register(event_sink_2)
+ dispatcher.register(event_sink_1)
+ end
+ it "trims the arugment list" do
+ cookbook_version = double("cookbook_version")
+ dispatcher.synchronized_cookbook("apache2", cookbook_version)
+ expect(event_sink_1.synchronized_cookbook_args).to eq ["apache2"]
+ expect(event_sink_2.synchronized_cookbook_args).to eq ["apache2", cookbook_version]
+ end
+ end
+ end
+end