From 0804121719e4bde856723e19e8ff8bf6f9c2d55d Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Thu, 8 May 2014 14:43:06 -0700 Subject: [PATCH] Fix the stdlib functions that fail tests --- lib/puppet/parser/functions/floor.rb | 7 ++++++- lib/puppet/parser/functions/is_domain_name.rb | 3 +++ lib/puppet/parser/functions/is_float.rb | 3 +++ lib/puppet/parser/functions/is_function_available.rb | 3 +++ spec/acceptance/getparam_spec.rb | 2 +- spec/acceptance/is_float_spec.rb | 6 +++--- spec/acceptance/is_string_spec.rb | 2 +- 7 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb index a40192349..9a6f014d7 100644 --- a/lib/puppet/parser/functions/floor.rb +++ b/lib/puppet/parser/functions/floor.rb @@ -8,7 +8,12 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "floor(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 - arg = arguments[0] + begin + arg = Float(arguments[0]) + rescue TypeError, ArgumentError => e + raise(Puppet::ParseError, "floor(): Wrong argument type " + + "given (#{arguments[0]} for Numeric)") + end raise(Puppet::ParseError, "floor(): Wrong argument type " + "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb index 5826dc0d9..b3fee965a 100644 --- a/lib/puppet/parser/functions/is_domain_name.rb +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -20,6 +20,9 @@ module Puppet::Parser::Functions label_min_length=1 label_max_length=63 + # Only allow string types + return false unless domain.is_a?(String) + # Allow ".", it is the top level domain return true if domain == '.' diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 911f3c2dc..a2da94385 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -15,6 +15,9 @@ module Puppet::Parser::Functions value = arguments[0] + # Only allow Numeric or String types + return false unless value.is_a?(Numeric) or value.is_a?(String) + if value != value.to_f.to_s and !value.is_a? Float then return false else diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb index 6cbd35c3d..6da82c8c1 100644 --- a/lib/puppet/parser/functions/is_function_available.rb +++ b/lib/puppet/parser/functions/is_function_available.rb @@ -15,6 +15,9 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + # Only allow String types + return false unless arguments[0].is_a?(String) + function = Puppet::Parser::Functions.function(arguments[0].to_sym) function.is_a?(String) and not function.empty? end diff --git a/spec/acceptance/getparam_spec.rb b/spec/acceptance/getparam_spec.rb index a84b2d12e..91fc9a00f 100755 --- a/spec/acceptance/getparam_spec.rb +++ b/spec/acceptance/getparam_spec.rb @@ -13,7 +13,7 @@ notice(inline_template('getparam is <%= @o.inspect %>')) EOS - apply_manifest(pp, :expect_changes => true) do |r| + apply_manifest(pp, :catch_failures => true) do |r| expect(r.stdout).to match(/getparam is true/) end end diff --git a/spec/acceptance/is_float_spec.rb b/spec/acceptance/is_float_spec.rb index 79b01f065..338ba58d4 100755 --- a/spec/acceptance/is_float_spec.rb +++ b/spec/acceptance/is_float_spec.rb @@ -7,7 +7,7 @@ pp = <<-EOS $a = ['aaa.com','bbb','ccc'] $o = is_float($a) - notice(inline_template('is_floats is <%= @o.inspect %>')) + notice(inline_template('is_float is <%= @o.inspect %>')) EOS apply_manifest(pp, :catch_failures => true) do |r| @@ -18,7 +18,7 @@ pp = <<-EOS $a = true $o = is_float($a) - notice(inline_template('is_floats is <%= @o.inspect %>')) + notice(inline_template('is_float is <%= @o.inspect %>')) EOS apply_manifest(pp, :catch_failures => true) do |r| @@ -75,7 +75,7 @@ EOS apply_manifest(pp, :catch_failures => true) do |r| - expect(r.stdout).to match(/is_floats is false/) + expect(r.stdout).to match(/is_float is false/) end end end diff --git a/spec/acceptance/is_string_spec.rb b/spec/acceptance/is_string_spec.rb index bda6cd7f6..94d8e9678 100755 --- a/spec/acceptance/is_string_spec.rb +++ b/spec/acceptance/is_string_spec.rb @@ -50,7 +50,7 @@ EOS apply_manifest(pp, :catch_failures => true) do |r| - expect(r.stdout).to match(/is_string is true/) + expect(r.stdout).to match(/is_string is false/) end end it 'is_strings floats' do