From: Bart Vanbrabant <[email protected]>
Signed-off-by: James Turnbull <[email protected]> --- lib/puppet/parser/conflicthandler.rb | 12 +++++-- spec/unit/parser/conflicthandler.rb | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 spec/unit/parser/conflicthandler.rb diff --git a/lib/puppet/parser/conflicthandler.rb b/lib/puppet/parser/conflicthandler.rb index bdbb947..7a85def 100644 --- a/lib/puppet/parser/conflicthandler.rb +++ b/lib/puppet/parser/conflicthandler.rb @@ -28,7 +28,7 @@ module Puppet::Parser @handlers ||= {} type = symbolize(type) - if handler(name, type) + if handler?(name, type) raise Puppet::DevError, "Conflict handler for %s already defined" % type end @@ -42,7 +42,8 @@ module Puppet::Parser end # Does a handler exist with the given name and type - def handler(name, type) + def handler?(name, type) + type = symbolize(type) return (handlers.has_key? name and handlers[name].has_key? type) end @@ -56,7 +57,7 @@ module Puppet::Parser else handler = resource["conflicthandler"] type = resource.type.downcase.to_sym - return unless handler(handler, type) + return unless handler?(handler, type) cls = @handlers[handler][type] instance = cls.new(resource) @@ -79,6 +80,11 @@ module Puppet::Parser def add_finish(&block) send(:define_method, :finish, &block) end + + # remove all defined handlers (used for tests) + def clear_handlers + @handlers = {} + end end attr_reader :conflicts, :resource diff --git a/spec/unit/parser/conflicthandler.rb b/spec/unit/parser/conflicthandler.rb new file mode 100644 index 0000000..f57fbf0 --- /dev/null +++ b/spec/unit/parser/conflicthandler.rb @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser::ConflictHandler do + after(:each) do + Puppet::Parser::ConflictHandler.clear_handlers + end + + describe "when creating new conflicthandlers" do + it "should create a new ConflictHandler subclass for that type" do + Puppet::Parser::ConflictHandler.newconflicthandler("file", "test") {} + + Puppet::Parser::ConflictHandler.handler?("test", "file").should be_true + end + + it "should raise an exception when a duplicate handler is created for a type" do + Puppet::Parser::ConflictHandler.newconflicthandler("file", "test") {} + lambda { Puppet::Parser::ConflictHandler.newconflicthandler("file", "test") {} }.should raise_error + end + end + + describe "when handling conflicts a conflicthandler" do + it "should return a conflicthandler instance for the resource and have a finish method" do + Puppet::Parser::ConflictHandler.newconflicthandler("file", "test") {} + + resource = {} + resource.stubs(:ref).returns resource + resource.stubs(:type).returns "file" + resource["conflicthandler"] = "test" + + handler = Puppet::Parser::ConflictHandler.get_handler(resource) + handler.should respond_to(:finish) + end + + it "should execute the code block" do + Puppet::Parser::ConflictHandler.newconflicthandler("file", "test") do + conflicts.length.should == 1 + resource["other"] = conflicts[0] + end + + resource = {} + resource.stubs(:ref).returns resource + resource.stubs(:type).returns "file" + resource["conflicthandler"] = "test" + + handler = Puppet::Parser::ConflictHandler.get_handler(resource) + + other = "conflict" + + handler.add_conflict(other) + handler.finish() + + resource.has_key?("other").should be_true + resource["other"].should equal(other) + end + end +end -- 1.6.0.4 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
