From e2d7f3bb89a91d3aff6f9810d69bd84bc82ffb29 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 22 Apr 2014 09:36:28 +0200 Subject: [PATCH] (MODULES-707) chomp() fails because generate() no longer returns a string We need to use unless value.is_a?(String) || value.is_a?(Array) rather than klass = value.class unless [String, Array].include?(klass) because the klass version enforces type checking which is too strict, and does not allow us to accept objects wich have extended String (or Array). For example, generate() function now returns Puppet::Util::Execution::ProcessOutput which is just a very simple extension of String. While this in it's self was not intentional (PUP-2306) it is not unreasonable to cope with objects which extend Strings --- lib/puppet/parser/functions/bool2num.rb | 3 +-- lib/puppet/parser/functions/capitalize.rb | 3 +-- lib/puppet/parser/functions/chomp.rb | 3 +-- lib/puppet/parser/functions/chop.rb | 3 +-- lib/puppet/parser/functions/downcase.rb | 3 +-- lib/puppet/parser/functions/empty.rb | 3 +-- lib/puppet/parser/functions/fqdn_rotate.rb | 3 +-- lib/puppet/parser/functions/lstrip.rb | 3 +-- lib/puppet/parser/functions/reverse.rb | 3 +-- lib/puppet/parser/functions/rstrip.rb | 3 +-- lib/puppet/parser/functions/shuffle.rb | 3 +-- lib/puppet/parser/functions/strip.rb | 3 +-- lib/puppet/parser/functions/swapcase.rb | 3 +-- lib/puppet/parser/functions/unique.rb | 3 +-- lib/puppet/parser/functions/upcase.rb | 3 +-- lib/puppet/parser/functions/uriescape.rb | 3 +-- lib/puppet/parser/functions/zip.rb | 4 +--- spec/functions/bool2num_spec.rb | 18 ++++++++++++++++-- spec/functions/capitalize_spec.rb | 9 +++++++++ spec/functions/chomp_spec.rb | 9 +++++++++ spec/functions/chop_spec.rb | 9 +++++++++ spec/functions/downcase_spec.rb | 9 +++++++++ spec/functions/empty_spec.rb | 9 +++++++++ spec/functions/fqdn_rotate_spec.rb | 10 ++++++++++ spec/functions/lstrip_spec.rb | 9 +++++++++ spec/functions/reverse_spec.rb | 9 +++++++++ spec/functions/rstrip_spec.rb | 9 +++++++++ spec/functions/shuffle_spec.rb | 9 +++++++++ spec/functions/strip_spec.rb | 9 +++++++++ spec/functions/swapcase_spec.rb | 9 +++++++++ spec/functions/unique_spec.rb | 9 +++++++++ spec/functions/upcase_spec.rb | 9 +++++++++ spec/functions/uriescape_spec.rb | 9 +++++++++ spec/functions/zip_spec.rb | 16 ++++++++++++++++ 34 files changed, 185 insertions(+), 37 deletions(-) diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index 9a07a8a11..b32a4e87d 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -15,10 +15,9 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) + unless value.is_a?(String) || value.is_a?(FalseClass) || value.is_a?(TrueClass) raise(Puppet::ParseError, 'bool2num(): Requires either ' + 'boolean or string to work with') end diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index 640d00b82..98b2d16c9 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -13,9 +13,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'capitalize(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index 4564a000a..c55841e3c 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chomp(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index f242af39c..b24ab7856 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -16,9 +16,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chop(): Requires either an ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 4066d210f..040b84f56 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'downcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index 80ebb86b8..cca620fae 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, Hash, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) raise(Puppet::ParseError, 'empty(): Requires either ' + 'array, hash or string to work with') end diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 655820605..7f4d37d01 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -12,10 +12,9 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class require 'digest/md5' - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index 3a64de337..624e4c846 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'lstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index fe048690c..7f1018f67 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'reverse(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 29b099820..0cf8d222c 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'rstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 18134ab63..30c663dbe 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -12,9 +12,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'shuffle(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index 5f4630d7d..3fac47d53 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -19,9 +19,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'strip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index b9e663253..eb7fe137d 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -18,9 +18,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'swapcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index 8844a7418..cf770f3b4 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -28,9 +28,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'unique(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index fe6cadc3c..4302b29e7 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -20,9 +20,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'upcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 0d81de5d1..a486eee50 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'uriescape(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 2b56e9ca0..00266a4c5 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -30,10 +30,8 @@ module Puppet::Parser::Functions flatten = arguments[2] if arguments[2] if flatten - klass = flatten.class - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) + unless flatten.is_a?(String) || flatten.is_a?(FalseClass) || flatten.is_a?(TrueClass) raise(Puppet::ParseError, 'zip(): Requires either ' + 'boolean or string to work with') end diff --git a/spec/functions/bool2num_spec.rb b/spec/functions/bool2num_spec.rb index fbf461b14..3904d7e40 100755 --- a/spec/functions/bool2num_spec.rb +++ b/spec/functions/bool2num_spec.rb @@ -17,8 +17,22 @@ expect(result).to(eq(1)) end - it "should convert false to 0" do - result = scope.function_bool2num([false]) + it "should convert 'true' to 1" do + result = scope.function_bool2num(['true']) + result.should(eq(1)) + end + + it "should convert 'false' to 0" do + result = scope.function_bool2num(['false']) expect(result).to(eq(0)) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('true') + result = scope.function_bool2num([value]) + result.should(eq(1)) + end end diff --git a/spec/functions/capitalize_spec.rb b/spec/functions/capitalize_spec.rb index 0cc2d760b..fd0e92ba2 100755 --- a/spec/functions/capitalize_spec.rb +++ b/spec/functions/capitalize_spec.rb @@ -16,4 +16,13 @@ result = scope.function_capitalize(["abc"]) expect(result).to(eq("Abc")) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_capitalize([value]) + result.should(eq('Abc')) + end end diff --git a/spec/functions/chomp_spec.rb b/spec/functions/chomp_spec.rb index d2ae28749..b1e1e60f3 100755 --- a/spec/functions/chomp_spec.rb +++ b/spec/functions/chomp_spec.rb @@ -16,4 +16,13 @@ result = scope.function_chomp(["abc\n"]) expect(result).to(eq("abc")) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("abc\n") + result = scope.function_chomp([value]) + result.should(eq("abc")) + end end diff --git a/spec/functions/chop_spec.rb b/spec/functions/chop_spec.rb index d9dbb88a5..c8a19519a 100755 --- a/spec/functions/chop_spec.rb +++ b/spec/functions/chop_spec.rb @@ -16,4 +16,13 @@ result = scope.function_chop(["asdf\n"]) expect(result).to(eq("asdf")) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("abc\n") + result = scope.function_chop([value]) + result.should(eq('abc')) + end end diff --git a/spec/functions/downcase_spec.rb b/spec/functions/downcase_spec.rb index a844780b1..edebc44f1 100755 --- a/spec/functions/downcase_spec.rb +++ b/spec/functions/downcase_spec.rb @@ -21,4 +21,13 @@ result = scope.function_downcase(["asdf asdf"]) expect(result).to(eq("asdf asdf")) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("ASFD") + result = scope.function_downcase([value]) + result.should(eq('asfd')) + end end diff --git a/spec/functions/empty_spec.rb b/spec/functions/empty_spec.rb index 1f2ace4ca..6a97c4cd3 100755 --- a/spec/functions/empty_spec.rb +++ b/spec/functions/empty_spec.rb @@ -20,4 +20,13 @@ result = scope.function_empty(['asdf']) expect(result).to(eq(false)) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new() + result = scope.function_empty([value]) + result.should(eq(true)) + end end diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb index b2dc1f5a3..40057d4f7 100755 --- a/spec/functions/fqdn_rotate_spec.rb +++ b/spec/functions/fqdn_rotate_spec.rb @@ -30,4 +30,14 @@ val2 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"]) expect(val1).not_to eql(val2) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") + value = AlsoString.new("asdf") + result = scope.function_fqdn_rotate([value]) + result.size.should(eq(4)) + end end diff --git a/spec/functions/lstrip_spec.rb b/spec/functions/lstrip_spec.rb index 7025f97b8..68cca1c5d 100755 --- a/spec/functions/lstrip_spec.rb +++ b/spec/functions/lstrip_spec.rb @@ -16,4 +16,13 @@ result = scope.function_lstrip([" asdf"]) expect(result).to(eq('asdf')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new(" asdf") + result = scope.function_lstrip([value]) + result.should(eq("asdf")) + end end diff --git a/spec/functions/reverse_spec.rb b/spec/functions/reverse_spec.rb index bfeabfb84..1f047943f 100755 --- a/spec/functions/reverse_spec.rb +++ b/spec/functions/reverse_spec.rb @@ -16,4 +16,13 @@ result = scope.function_reverse(["asdfghijkl"]) expect(result).to(eq('lkjihgfdsa')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdfghjkl') + result = scope.function_reverse([value]) + result.should(eq('lkjhgfdsa')) + end end diff --git a/spec/functions/rstrip_spec.rb b/spec/functions/rstrip_spec.rb index 81321d766..f6b483896 100755 --- a/spec/functions/rstrip_spec.rb +++ b/spec/functions/rstrip_spec.rb @@ -21,4 +21,13 @@ result = scope.function_rstrip([["a ","b ", "c "]]) expect(result).to(eq(['a','b','c'])) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdf ') + result = scope.function_rstrip([value]) + result.should(eq('asdf')) + end end diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb index ee0e2ffb2..a62c1fb5e 100755 --- a/spec/functions/shuffle_spec.rb +++ b/spec/functions/shuffle_spec.rb @@ -21,4 +21,13 @@ result = scope.function_shuffle(["adfs"]) expect(result.split("").sort.join("")).to(eq("adfs")) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdf') + result = scope.function_shuffle([value]) + result.size.should(eq(4)) + end end diff --git a/spec/functions/strip_spec.rb b/spec/functions/strip_spec.rb index e228761d0..4ac8daf86 100755 --- a/spec/functions/strip_spec.rb +++ b/spec/functions/strip_spec.rb @@ -15,4 +15,13 @@ result = scope.function_strip([" ab cd "]) expect(result).to(eq('ab cd')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new(' as df ') + result = scope.function_strip([value]) + result.should(eq('as df')) + end end diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb index c6838ab4e..791d1dfae 100755 --- a/spec/functions/swapcase_spec.rb +++ b/spec/functions/swapcase_spec.rb @@ -16,4 +16,13 @@ result = scope.function_swapcase(["aaBBccDD"]) expect(result).to(eq('AAbbCCdd')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("aaBBccDD") + result = scope.function_swapcase([value]) + result.should(eq("AAbbCCdd")) + end end diff --git a/spec/functions/unique_spec.rb b/spec/functions/unique_spec.rb index 8ec1464eb..7cd3a566f 100755 --- a/spec/functions/unique_spec.rb +++ b/spec/functions/unique_spec.rb @@ -21,4 +21,13 @@ result = scope.function_unique([["a","a","b","b","c"]]) expect(result).to(eq(['a','b','c'])) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('aabbc') + result = scope.function_unique([value]) + result.should(eq('abc')) + end end diff --git a/spec/functions/upcase_spec.rb b/spec/functions/upcase_spec.rb index 78e55ddf1..3cf8b0552 100755 --- a/spec/functions/upcase_spec.rb +++ b/spec/functions/upcase_spec.rb @@ -21,4 +21,13 @@ result = scope.function_upcase(["ABC"]) expect(result).to(eq('ABC')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_upcase([value]) + result.should(eq('ABC')) + end end diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index c44e9c19b..2321e5aba 100755 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -21,4 +21,13 @@ result = scope.function_uriescape(["ABCdef"]) expect(result).to(eq('ABCdef')) end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_uriescape([value]) + result.should(eq('abc')) + end end diff --git a/spec/functions/zip_spec.rb b/spec/functions/zip_spec.rb index 744bdd786..f265fcee4 100755 --- a/spec/functions/zip_spec.rb +++ b/spec/functions/zip_spec.rb @@ -11,5 +11,21 @@ it "should be able to zip an array" do result = scope.function_zip([['1','2','3'],['4','5','6']]) expect(result).to(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + result = scope.function_zip([['1','2','3'],['4','5','6'], false]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + end + + it "should be able to zip an array and flatten" do + result = scope.function_zip([['1','2','3'],['4','5','6'], true]) + result.should(eq(["1", "4", "2", "5", "3", "6"])) + end + + it "should accept objects which extend String for the second argument" do + class AlsoString < String + end + + value = AlsoString.new('false') + result = scope.function_zip([['1','2','3'],['4','5','6'],value]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) end end