diff options
-rw-r--r-- | lib/puppet/parser/functions/extlookup.rb (renamed from ext/extlookup.rb) | 24 | ||||
-rwxr-xr-x | spec/unit/parser/functions/extlookup_spec.rb | 85 |
2 files changed, 93 insertions, 16 deletions
diff --git a/ext/extlookup.rb b/lib/puppet/parser/functions/extlookup.rb index d87583ba7..ee230e7ce 100644 --- a/ext/extlookup.rb +++ b/lib/puppet/parser/functions/extlookup.rb @@ -82,11 +82,11 @@ require 'csv' module Puppet::Parser::Functions newfunction(:extlookup, :type => :rvalue) do |args| key = args[0] - default = "_ExtUNSET_" - datafile = "_ExtUNSET_" - default = args[1] if args[1] - datafile = args[2] if args[2] + default = args[1] + datafile = args[2] + + raise Puppet::ParseError, ("extlookup(): wrong number of arguments (#{args.length}; must be <= 3)") if args.length > 3 extlookup_datadir = lookupvar('extlookup_datadir') extlookup_precedence = Array.new @@ -123,12 +123,13 @@ module Puppet::Parser::Functions datafiles << extlookup_datadir + "/#{d}.csv" end - desired = "_ExtUNSET_" + desired = nil datafiles.each do |file| + parser = Puppet::Parser::Parser.new(environment) parser.watch_file(file) if File.exists?(file) - if desired == "_ExtUNSET_" + if desired.nil? if File.exists?(file) result = CSV.read(file).find_all do |r| r[0] == key @@ -167,15 +168,6 @@ module Puppet::Parser::Functions end end - # don't accidently return nil's and such rather throw a parse error - if desired == "_ExtUNSET_" && default == "_ExtUNSET_" - raise Puppet::ParseError, "No match found for '#{key}' in any data file during extlookup()" - else - desired = default if desired == "_ExtUNSET_" - end - - desired + desired || default or raise Puppet::ParseError, "No match found for '#{key}' in any data file during extlookup()" end end - -# vi:tabstop=4:expandtab:ai diff --git a/spec/unit/parser/functions/extlookup_spec.rb b/spec/unit/parser/functions/extlookup_spec.rb new file mode 100755 index 000000000..bf2880345 --- /dev/null +++ b/spec/unit/parser/functions/extlookup_spec.rb @@ -0,0 +1,85 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' +require 'tempfile' + +describe "the extlookup function" do + + before :each do + @scope = Puppet::Parser::Scope.new + + @scope.stubs(:environment).returns(Puppet::Node::Environment.new('production')) + end + + it "should exist" do + Puppet::Parser::Functions.function("extlookup").should == "function_extlookup" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_extlookup([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 3 arguments" do + lambda { @scope.function_extlookup(["foo", "bar", "baz", "gazonk"]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return the default" do + result = @scope.function_extlookup([ "key", "default"]) + result.should == "default" + end + + it "should lookup the key in a supplied datafile" do + t = Tempfile.new('extlookup.csv') do + t.puts 'key,value' + t.puts 'nonkey,nonvalue' + t.close + + result = @scope.function_extlookup([ "key", "default", t.path]) + result.should == "value" + end + end + + it "should return an array if the datafile contains more than two columns" do + t = Tempfile.new('extlookup.csv') do + t.puts 'key,value1,value2' + t.puts 'nonkey,nonvalue,nonvalue' + t.close + + result = @scope.function_extlookup([ "key", "default", t.path]) + result.should == ["value1", "value2"] + end + end + + it "should raise an error if there's no matching key and no default" do + t = Tempfile.new('extlookup.csv') do + t.puts 'key,value' + t.puts 'nonkey,nonvalue' + t.close + + result = @scope.function_extlookup([ "key", nil, t.path]) + result.should == "value" + end + end + + describe "should look in $extlookup_datadir for data files listed by $extlookup_precedence" do + before do + @scope.stubs(:lookupvar).with('extlookup_datadir').returns("/tmp") + @scope.stubs(:lookupvar).with('extlookup_precedence').returns(["one","two"]) + File.open("/tmp/one.csv","w"){|one| one.puts "key,value1" } + File.open("/tmp/two.csv","w") do |two| + two.puts "key,value2" + two.puts "key2,value_two" + end + end + + it "when the key is in the first file" do + result = @scope.function_extlookup([ "key" ]) + result.should == "value1" + end + + it "when the key is in the second file" do + result = @scope.function_extlookup([ "key2" ]) + result.should == "value_two" + end + end +end |