diff --git a/lib/puppet/parser/functions/include.rb b/lib/puppet/parser/functions/include.rb index 1e12a6a88..5e24e4942 100644 --- a/lib/puppet/parser/functions/include.rb +++ b/lib/puppet/parser/functions/include.rb @@ -1,24 +1,29 @@ # Include the specified classes Puppet::Parser::Functions::newfunction(:include, :doc => "Evaluate one or more classes.") do |vals| - vals = [vals] unless vals.is_a?(Array) + if vals.is_a?(Array) + # Protect against array inside array + vals = vals.flatten + else + vals = [vals] + end # The 'false' disables lazy evaluation. klasses = compiler.evaluate_classes(vals, self, false) missing = vals.find_all do |klass| ! klasses.include?(klass) end unless missing.empty? # Throw an error if we didn't evaluate all of the classes. str = "Could not find class" str += "es" if missing.length > 1 str += " " + missing.join(", ") if n = namespaces and ! n.empty? and n != [""] str += " in namespaces #{@namespaces.join(", ")}" end self.fail Puppet::ParseError, str end end diff --git a/spec/unit/parser/functions/include_spec.rb b/spec/unit/parser/functions/include_spec.rb index 5f86049a8..6802905a0 100755 --- a/spec/unit/parser/functions/include_spec.rb +++ b/spec/unit/parser/functions/include_spec.rb @@ -1,40 +1,52 @@ #!/usr/bin/env rspec require 'spec_helper' describe "the 'include' function" do before :all do Puppet::Parser::Functions.autoloader.loadall end before :each do Puppet::Node::Environment.stubs(:current).returns(nil) @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo")) @scope = Puppet::Parser::Scope.new(:compiler => @compiler) end it "should exist" do Puppet::Parser::Functions.function("include").should == "function_include" end it "should include a single class" do inc = "foo" @compiler.expects(:evaluate_classes).with {|klasses,parser,lazy| klasses == [inc]}.returns([inc]) @scope.function_include("foo") end it "should include multiple classes" do inc = ["foo","bar"] @compiler.expects(:evaluate_classes).with {|klasses,parser,lazy| klasses == inc}.returns(inc) @scope.function_include(["foo","bar"]) end + it "should include multiple classes passed in an array" do + inc = ["foo","bar"] + @compiler.expects(:evaluate_classes).with {|klasses,parser,lazy| klasses == inc}.returns(inc) + @scope.function_include([["foo","bar"]]) + end + + it "should flatten nested arrays" do + inc = ["foo","bar","baz"] + @compiler.expects(:evaluate_classes).with {|klasses,parser,lazy| klasses == inc}.returns(inc) + @scope.function_include([["foo","bar"],"baz"]) + end + it "should not lazily evaluate the included class" do @compiler.expects(:evaluate_classes).with {|klasses,parser,lazy| lazy == false}.returns("foo") @scope.function_include("foo") end it "should raise if the class is not found" do @scope.stubs(:source).returns(true) expect { @scope.function_include("nosuchclass") }.to raise_error Puppet::Error end end