]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(MODULES-707) chomp() fails because generate() no longer returns a string
authorMark Chappell <mchappel@redhat.com>
Tue, 22 Apr 2014 07:36:28 +0000 (09:36 +0200)
committerMark Chappell <mchappel@redhat.com>
Mon, 22 Sep 2014 17:49:50 +0000 (19:49 +0200)
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

34 files changed:
lib/puppet/parser/functions/bool2num.rb
lib/puppet/parser/functions/capitalize.rb
lib/puppet/parser/functions/chomp.rb
lib/puppet/parser/functions/chop.rb
lib/puppet/parser/functions/downcase.rb
lib/puppet/parser/functions/empty.rb
lib/puppet/parser/functions/fqdn_rotate.rb
lib/puppet/parser/functions/lstrip.rb
lib/puppet/parser/functions/reverse.rb
lib/puppet/parser/functions/rstrip.rb
lib/puppet/parser/functions/shuffle.rb
lib/puppet/parser/functions/strip.rb
lib/puppet/parser/functions/swapcase.rb
lib/puppet/parser/functions/unique.rb
lib/puppet/parser/functions/upcase.rb
lib/puppet/parser/functions/uriescape.rb
lib/puppet/parser/functions/zip.rb
spec/functions/bool2num_spec.rb
spec/functions/capitalize_spec.rb
spec/functions/chomp_spec.rb
spec/functions/chop_spec.rb
spec/functions/downcase_spec.rb
spec/functions/empty_spec.rb
spec/functions/fqdn_rotate_spec.rb
spec/functions/lstrip_spec.rb
spec/functions/reverse_spec.rb
spec/functions/rstrip_spec.rb
spec/functions/shuffle_spec.rb
spec/functions/strip_spec.rb
spec/functions/swapcase_spec.rb
spec/functions/unique_spec.rb
spec/functions/upcase_spec.rb
spec/functions/uriescape_spec.rb
spec/functions/zip_spec.rb

index 9a07a8a11929be7a72eb6757551ed3687874f69e..b32a4e87d799eceff1c57355b2d6c9e4eb327b09 100644 (file)
@@ -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
index 640d00b82fa681297d48437a252d64a5c92e512d..98b2d16c97dd40855566b5ce46f956d77b2a9694 100644 (file)
@@ -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
index 4564a000abecd0dc1b2952fd34e5123bbe4fdf81..c55841e3c389f64625f13aacc0aec02d9cefed97 100644 (file)
@@ -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
index f242af39ce831681fa14ff2930643166c0e45254..b24ab785601f95b4cad3adeb9d5a4f4a0604fa30 100644 (file)
@@ -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
index 4066d210f771c30406853eef5528950a8429ae92..040b84f568437ba4b6596fff0d2edc9cf9186552 100644 (file)
@@ -12,9 +12,8 @@ Converts the case of a string or all strings in an array to lower case.
       "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
index 80ebb86b891d3ba83e24fe1cd070d1ab3c7afa79..cca620fae12a5311d672774263a9f65161c66db8 100644 (file)
@@ -12,9 +12,8 @@ Returns true if the variable is empty.
       "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
index 6558206055f13897030f3550b9b5d7a798c4a00a..7f4d37d01c0f73496d08836063d526030cd83a59 100644 (file)
@@ -12,10 +12,9 @@ Rotates an array a random number of times based on a nodes fqdn.
       "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
index 3a64de337666ea77aeb1dfa2156e43f15df328f1..624e4c8464b9d10713653870c1fad1dbb4ed4c6b 100644 (file)
@@ -12,9 +12,8 @@ Strips leading spaces to the left of a string.
       "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
index fe048690cdddddbc25582d04ac0f174be4883a24..7f1018f677697f2a37cbbf09a0ce33f0266d9805 100644 (file)
@@ -12,9 +12,8 @@ Reverses the order of a string or array.
       "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
index 29b0998205f37fed5357f65a2fd6409d37e4ba7b..0cf8d222c4e3f6adb3db491200f1e85d48b3670a 100644 (file)
@@ -12,9 +12,8 @@ Strips leading spaces to the right of the string.
       "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
index 18134ab633ea962a41016be3d5ac795e60077670..30c663dbe18d726eb371a1c03f7db4deb0b5cc2c 100644 (file)
@@ -12,9 +12,8 @@ Randomizes the order of a string or array elements.
       "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
index 5f4630d7de97a1f382f127c5b82a210cfaf1e06a..3fac47d531d5eb3e4c989769afb9d0120c13a148 100644 (file)
@@ -19,9 +19,8 @@ Would result in: "aaa"
       "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
index b9e663253389f7c6453047a95021bb9ebabbab50..eb7fe137d8a2f7a5ad89931db27f24f579ebec9a 100644 (file)
@@ -18,9 +18,8 @@ Would result in: "AbCd"
       "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
index 8844a7418526da26046c3d810134f61ea726213d..cf770f3b44af2776f8039bba646a040f376e17d8 100644 (file)
@@ -28,9 +28,8 @@ This returns:
       "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
index fe6cadc3cced1b88ab530c2db2501a0cdcd8050d..4302b29e7480177c124d7be2af9e7cfdce9670dd 100644 (file)
@@ -20,9 +20,8 @@ Will return:
       "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
index 0d81de5d108ce96096ee689c0b3b927801dc8a66..a486eee50d798d7c5c0ebff71e40173e62e1c291 100644 (file)
@@ -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
index 2b56e9ca07089716970d1d09a8dcdbb2ded230f2..00266a4c589296327b9403a7d01b00fac2a847bb 100644 (file)
@@ -30,10 +30,8 @@ Would result in:
     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
index fbf461b1409115f420a544b5bf362e714731f096..3904d7e404214fe6e04a6906031060e05fbc0af0 100755 (executable)
@@ -17,8 +17,22 @@ describe "the bool2num function" do
     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
index 0cc2d760b7a30a28e591d75c4b37f90266015045..fd0e92ba26702d2fdc1e693196250c79d0ddd60d 100755 (executable)
@@ -16,4 +16,13 @@ describe "the capitalize function" do
     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
index d2ae287494034639b6197c6ef05957ef6dd71fad..b1e1e60f35e21249705a3294744d64cf199d4c59 100755 (executable)
@@ -16,4 +16,13 @@ describe "the chomp function" do
     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
index d9dbb88a51708133f15625f15015a70c1ef25918..c8a19519a98d0c159f6ac6417b81d48abbb70b1a 100755 (executable)
@@ -16,4 +16,13 @@ describe "the chop function" do
     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
index a844780b1b0075be2717bd8e8e65e91ae817d762..edebc44f1475360b9b172a842542a9626af495cd 100755 (executable)
@@ -21,4 +21,13 @@ describe "the downcase function" do
     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
index 1f2ace4caf779304cee67ca5f980e270242c4ba1..6a97c4cd3ba073fab1fded6d2e3a6749a8e1ba18 100755 (executable)
@@ -20,4 +20,13 @@ describe "the empty function" do
     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
index b2dc1f5a3f117b645d6831e173e4bb86916d3760..40057d4f7473c9c57583f25f6b2412e08b34abc4 100755 (executable)
@@ -30,4 +30,14 @@ describe "the fqdn_rotate function" do
      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
index 7025f97b838326a9b99a33dc3464e195b5ff1e99..68cca1c5d269fe276ea2f0f9d610cf0f0b3ac9f7 100755 (executable)
@@ -16,4 +16,13 @@ describe "the lstrip function" do
     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
index bfeabfb84353d142806b6ad793a627cf5b357d1c..1f047943fe63ceaa74a5e92e632868c53ddc6eb8 100755 (executable)
@@ -16,4 +16,13 @@ describe "the reverse function" do
     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
index 81321d76637549e2181e6bd6c49e246a8376c3e4..f6b483896f10c9cac8810423d6bb166092e8f4f1 100755 (executable)
@@ -21,4 +21,13 @@ describe "the rstrip function" do
     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
index ee0e2ffb22fd1f22a74d33d20cdcb07becd355f8..a62c1fb5e3f2799d505286649599f4f843e4b437 100755 (executable)
@@ -21,4 +21,13 @@ describe "the shuffle function" do
     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
index e228761d016478dd15758ca38fe6aeb4aeaead07..4ac8daf8638288cae5eb5104306a822757e3a110 100755 (executable)
@@ -15,4 +15,13 @@ describe "the strip function" do
     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
index c6838ab4e36e5567717e52930d2c63f22ed7cd5d..791d1dfaebc0c6974dc3f9f9ea7e4987f04628fe 100755 (executable)
@@ -16,4 +16,13 @@ describe "the swapcase function" do
     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
index 8ec1464eb4be5b167d5d8006e9a34165068f04c6..7cd3a566f38e90e2a19a085dc4bd50e4b5c023d5 100755 (executable)
@@ -21,4 +21,13 @@ describe "the unique function" do
     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
index 78e55ddf1e2c5bc2eff049c90ac9bb36e44e7b8e..3cf8b055249922505c009c10fb66321e76894319 100755 (executable)
@@ -21,4 +21,13 @@ describe "the upcase function" do
     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
index c44e9c19b05cec4e75020b5ff827543416e88363..2321e5aba8aa9f62cd546a0714ef2662bca98315 100755 (executable)
@@ -21,4 +21,13 @@ describe "the uriescape function" do
     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
index 744bdd7868968e089e57ecc163cb135583225995..f265fcee49a3be49259d3ca23067b84c22eca811 100755 (executable)
@@ -11,5 +11,21 @@ describe "the zip function" do
   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