Skip to content

Commit

Permalink
(MODULES-707) chomp() fails because generate() no longer returns a st…
Browse files Browse the repository at this point in the history
…ring

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
  • Loading branch information
tremble committed Sep 22, 2014
1 parent b347cc8 commit e2d7f3b
Show file tree
Hide file tree
Showing 34 changed files with 185 additions and 37 deletions.
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/bool2num.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/capitalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/chomp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/chop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/downcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/fqdn_rotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/lstrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/reverse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/rstrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/shuffle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/strip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/swapcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/unique.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/upcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/uriescape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/puppet/parser/functions/zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions spec/functions/bool2num_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/capitalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/chomp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/chop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/downcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/empty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions spec/functions/fqdn_rotate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/lstrip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/reverse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/rstrip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/shuffle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/strip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/swapcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/functions/unique_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit e2d7f3b

Please sign in to comment.