diff options
-rw-r--r-- | lib/puppet/defaults.rb | 4 | ||||
-rw-r--r-- | lib/puppet/parser/ast/casestatement.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/ast/leaf.rb | 10 | ||||
-rw-r--r-- | lib/puppet/parser/ast/selector.rb | 4 | ||||
-rwxr-xr-x | spec/unit/parser/ast/casestatement.rb | 38 | ||||
-rwxr-xr-x | spec/unit/parser/ast/leaf.rb | 4 | ||||
-rwxr-xr-x | spec/unit/parser/ast/selector.rb | 16 | ||||
-rwxr-xr-x | test/language/ast/casestatement.rb | 103 | ||||
-rwxr-xr-x | test/language/ast/selector.rb | 61 |
9 files changed, 40 insertions, 202 deletions
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index fa1b31c73..2f397f495 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -707,10 +707,6 @@ module Puppet ) setdefaults(:main, - :casesensitive => [false, - "Whether matching in case statements and selectors - should be case-sensitive. Case insensitivity is - handled by downcasing all values before comparison."], :external_nodes => ["none", "An external command that can produce node information. The output must be a YAML dump of a hash, and that hash must have one or both of diff --git a/lib/puppet/parser/ast/casestatement.rb b/lib/puppet/parser/ast/casestatement.rb index 64298cac3..720ef240b 100644 --- a/lib/puppet/parser/ast/casestatement.rb +++ b/lib/puppet/parser/ast/casestatement.rb @@ -20,7 +20,7 @@ class Puppet::Parser::AST default = nil @options.each do |option| option.eachopt do |opt| - return option.safeevaluate(scope) if opt.evaluate_match(value, scope, :file => file, :line => line, :sensitive => Puppet[:casesensitive]) + return option.safeevaluate(scope) if opt.evaluate_match(value, scope) end default = option if option.default? diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb index caf1d13a5..30c4a958f 100644 --- a/lib/puppet/parser/ast/leaf.rb +++ b/lib/puppet/parser/ast/leaf.rb @@ -11,12 +11,12 @@ class Puppet::Parser::AST end # evaluate ourselves, and match - def evaluate_match(value, scope, options = {}) + def evaluate_match(value, scope) obj = self.safeevaluate(scope) - if options[:sensitive] - obj = obj.downcase if obj.respond_to?(:downcase) - value = value.downcase if value.respond_to?(:downcase) - end + + obj = obj.downcase if obj.respond_to?(:downcase) + value = value.downcase if value.respond_to?(:downcase) + # "" == undef for case/selector/if obj == value or (obj == "" and value == :undef) end diff --git a/lib/puppet/parser/ast/selector.rb b/lib/puppet/parser/ast/selector.rb index ce834b63b..d27773c4b 100644 --- a/lib/puppet/parser/ast/selector.rb +++ b/lib/puppet/parser/ast/selector.rb @@ -15,8 +15,6 @@ class Puppet::Parser::AST # Get our parameter. paramvalue = @param.safeevaluate(scope) - sensitive = Puppet[:casesensitive] - default = nil unless @values.instance_of? AST::ASTArray or @values.instance_of? Array @@ -26,7 +24,7 @@ class Puppet::Parser::AST # Then look for a match in the options. @values.each do |obj| # short circuit asap if we have a match - return obj.value.safeevaluate(scope) if obj.param.evaluate_match(paramvalue, scope, :file => file, :line => line, :sensitive => sensitive) + return obj.value.safeevaluate(scope) if obj.param.evaluate_match(paramvalue, scope) # Store the default, in case it's necessary. default = obj if obj.param.is_a?(Default) diff --git a/spec/unit/parser/ast/casestatement.rb b/spec/unit/parser/ast/casestatement.rb index 657648e9d..c2e9a6929 100755 --- a/spec/unit/parser/ast/casestatement.rb +++ b/spec/unit/parser/ast/casestatement.rb @@ -57,13 +57,6 @@ describe Puppet::Parser::AST::CaseStatement do @casestmt.evaluate(@scope) end - it "should evaluate_match with sensitive parameter" do - Puppet.stubs(:[]).with(:casesensitive).returns(true) - @opval1.expects(:evaluate_match).with { |*arg| arg[2][:sensitive] == true } - - @casestmt.evaluate(@scope) - end - it "should return the first matching evaluated option" do @opval2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true) @option2.stubs(:safeevaluate).with(@scope).returns(:result) @@ -130,4 +123,35 @@ describe Puppet::Parser::AST::CaseStatement do end end + + it "should match if any of the provided options evaluate as true" do + ast = nil + AST = Puppet::Parser::AST + + tests = { + "one" => %w{a b c}, + "two" => %w{e f g} + } + options = tests.collect do |result, values| + values = values.collect { |v| AST::Leaf.new :value => v } + AST::CaseOpt.new(:value => AST::ASTArray.new(:children => values), + :statements => AST::Leaf.new(:value => result)) + end + options << AST::CaseOpt.new(:value => AST::Default.new(:value => "default"), + :statements => AST::Leaf.new(:value => "default")) + + ast = nil + param = AST::Variable.new(:value => "testparam") + ast = AST::CaseStatement.new(:test => param, :options => options) + + tests.each do |should, values| + values.each do |value| + @scope = Puppet::Parser::Scope.new() + @scope.setvar("testparam", value) + result = ast.evaluate(@scope) + + result.should == should + end + end + end end diff --git a/spec/unit/parser/ast/leaf.rb b/spec/unit/parser/ast/leaf.rb index 2bb374702..d5534debb 100755 --- a/spec/unit/parser/ast/leaf.rb +++ b/spec/unit/parser/ast/leaf.rb @@ -32,7 +32,7 @@ describe Puppet::Parser::AST::Leaf do @leaf.stubs(:safeevaluate).with(@scope).returns(@value) @value.expects(:downcase).returns("value") - @leaf.evaluate_match("value", @scope, :sensitive => true) + @leaf.evaluate_match("value", @scope) end it "should match undef if value is an empty string" do @@ -45,7 +45,7 @@ describe Puppet::Parser::AST::Leaf do parameter = stub 'parameter' parameter.expects(:downcase).returns("value") - @leaf.evaluate_match(parameter, @scope, :sensitive => true) + @leaf.evaluate_match(parameter, @scope) end end diff --git a/spec/unit/parser/ast/selector.rb b/spec/unit/parser/ast/selector.rb index f9a1efe6c..23989b902 100755 --- a/spec/unit/parser/ast/selector.rb +++ b/spec/unit/parser/ast/selector.rb @@ -86,22 +86,6 @@ describe Puppet::Parser::AST::Selector do @selector.evaluate(@scope) end - it "should transmit the sensitive parameter to evaluate_match" do - Puppet.stubs(:[]).with(:casesensitive).returns(:sensitive) - @param1.expects(:evaluate_match).with { |*arg| arg[2][:sensitive] == :sensitive } - - @selector.evaluate(@scope) - end - - it "should transmit the AST file and line to evaluate_match" do - @selector.file = :file - @selector.line = :line - @param1.expects(:evaluate_match).with { |*arg| arg[2][:file] == :file and arg[2][:line] == :line } - - @selector.evaluate(@scope) - end - - it "should evaluate the matching param" do @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true) diff --git a/test/language/ast/casestatement.rb b/test/language/ast/casestatement.rb deleted file mode 100755 index 2711455f2..000000000 --- a/test/language/ast/casestatement.rb +++ /dev/null @@ -1,103 +0,0 @@ -#!/usr/bin/env ruby -# -# Created by Luke A. Kanies on 2006-12-22. -# Copyright (c) 2006. All rights reserved. - -require File.dirname(__FILE__) + '/../../lib/puppettest' - -require 'puppettest' -require 'puppettest/parsertesting' - -class TestCaseStatement < Test::Unit::TestCase - include PuppetTest - include PuppetTest::ParserTesting - AST = Puppet::Parser::AST - - class ActiveAST < FakeAST - def self.clear - $evaluated = [] - end - def evaluate - $evaluated ||= [] - $evaluated << @evaluate - end - end - - def test_evaluate - ast = nil - scope = mkscope - param = nameobj("MyParam") - - hash = { - "myparam" => ActiveAST.new("lower"), - "MyParam" => ActiveAST.new("upper"), - true => ActiveAST.new(true) - } - options = ["myparam", "MyParam"].collect do |p| - AST::CaseOpt.new(:value => FakeAST.new(p), :statements => hash[p]) - end - assert_nothing_raised do - ast = AST::CaseStatement.new(:test => param, :options => options) - end - - # Start out case-sensitive - Puppet[:casesensitive] = true - - result = nil - assert_nothing_raised do - result = ast.evaluate scope - end - assert(result, "did not get valid result") - assert_equal(["upper"], $evaluated, "Did not match case-sensitively") - assert(! hash["myparam"].evaluated?, "lower value was evaluated even though it did not match") - - # Now try it case-insensitive - Puppet[:casesensitive] = false - $evaluated.clear - hash["MyParam"].reset - assert_nothing_raised do - result = ast.evaluate scope - end - assert(result, "did not get valid result") - assert_equal(["lower"], result, "Did not match case-insensitively") - assert(! hash["MyParam"].evaluated?, "upper value was evaluated even though it did not match") - end - - # #522 - test that case statements with multiple values work as - # expected, where any true value suffices. - def test_multiple_values - ast = nil - - tests = { - "one" => %w{a b c}, - "two" => %w{e f g} - } - options = tests.collect do |result, values| - values = values.collect { |v| AST::Leaf.new :value => v } - AST::CaseOpt.new(:value => AST::ASTArray.new(:children => values), - :statements => AST::Leaf.new(:value => result)) - end - options << AST::CaseOpt.new(:value => AST::Default.new(:value => "default"), - :statements => AST::Leaf.new(:value => "default")) - - ast = nil - param = AST::Variable.new(:value => "testparam") - assert_nothing_raised do - ast = AST::CaseStatement.new(:test => param, :options => options) - end - result = nil - tests.each do |should, values| - values.each do |value| - result = nil - scope = mkscope - scope.setvar("testparam", value) - assert_nothing_raised do - result = ast.evaluate(scope) - end - - assert_equal(should, result, "Got incorrect result for %s" % value) - end - end - end -end - diff --git a/test/language/ast/selector.rb b/test/language/ast/selector.rb deleted file mode 100755 index d2b1622d2..000000000 --- a/test/language/ast/selector.rb +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env ruby -# -# Created by Luke A. Kanies on 2006-12-22. -# Copyright (c) 2006. All rights reserved. - -require File.dirname(__FILE__) + '/../../lib/puppettest' - -require 'puppettest' -require 'puppettest/parsertesting' - -class TestSelector < Test::Unit::TestCase - include PuppetTest - include PuppetTest::ParserTesting - AST = Puppet::Parser::AST - - def test_evaluate - scope = mkscope - upperparam = nameobj("MYPARAM") - lowerparam = nameobj("myparam") - - should = {"MYPARAM" => "upper", "myparam" => "lower"} - - maker = Proc.new do - { - :default => AST::ResourceParam.new(:param => AST::Default.new(:value => "default"), :value => FakeAST.new("default")), - :lower => AST::ResourceParam.new(:param => FakeAST.new("myparam"), :value => FakeAST.new("lower")), - :upper => AST::ResourceParam.new(:param => FakeAST.new("MYPARAM"), :value => FakeAST.new("upper")), - } - - end - - # Start out case-sensitive - Puppet[:casesensitive] = true - - %w{MYPARAM myparam}.each do |str| - param = nameobj(str) - params = maker.call() - sel = AST::Selector.new(:param => param, :values => params.values) - result = nil - assert_nothing_raised { result = sel.evaluate(scope) } - assert_equal(should[str], result, "did not case-sensitively match %s" % str) - end - - # then insensitive - Puppet[:casesensitive] = false - - %w{MYPARAM myparam}.each do |str| - param = nameobj(str) - params = maker.call() - - # Delete the upper value, since we don't want it to match - # and it introduces a hash-ordering bug in testing. - params.delete(:upper) - sel = AST::Selector.new(:param => param, :values => params.values) - result = nil - assert_nothing_raised { result = sel.evaluate(scope) } - assert_equal("lower", result, "did not case-insensitively match %s" % str) - end - end -end - |