From 4732676548f91a43bc67ea0b70abdd34cae093c1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Tue, 3 Apr 2012 11:46:20 -0400 Subject: add a "step" argument to range() This patch adds an optional "step" argument to the stdlib range() function. There is no change to the default behavior of the function; however, passing a numeric "step" argument invokes the Ruby Range#step method, e.g. range("0", "9", "2") returns [0,2,4,6,8] --- lib/puppet/parser/functions/range.rb | 10 ++++++++- spec/unit/puppet/parser/functions/range_spec.rb | 30 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 6e85422..03ab9e9 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -18,6 +18,13 @@ Will return: [0,1,2,3,4,5,6,7,8,9] range("a", "c") Will return: ["a","b","c"] + +Passing a third argument will cause the generated range to step by that +interval, e.g. + + range("0", "9", "2") + +Will return: [0,2,4,6,8] EOS ) do |arguments| @@ -28,6 +35,7 @@ Will return: ["a","b","c"] if arguments.size > 1 start = arguments[0] stop = arguments[1] + step = arguments[2].nil? ? 1 : arguments[2].to_i.abs type = '..' # We select simplest type for Range available in Ruby ... @@ -62,7 +70,7 @@ Will return: ["a","b","c"] when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... end - result = range.collect { |i| i } # Get them all ... Pokemon ... + result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... return result end diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 8c2446a..24cc391 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -23,9 +23,39 @@ describe "the range function" do result.should(eq(['a','b','c','d'])) end + it "should return a letter range given a step of 1" do + result = @scope.function_range(["a","d","1"]) + result.should(eq(['a','b','c','d'])) + end + + it "should return a stepped letter range" do + result = @scope.function_range(["a","d","2"]) + result.should(eq(['a','c'])) + end + + it "should return a stepped letter range given a negative step" do + result = @scope.function_range(["1","4","-2"]) + result.should(eq(['a','c'])) + end + it "should return a number range" do result = @scope.function_range(["1","4"]) result.should(eq([1,2,3,4])) end + it "should return a number range given a step of 1" do + result = @scope.function_range(["1","4","1"]) + result.should(eq([1,2,3,4])) + end + + it "should return a stepped number range" do + result = @scope.function_range(["1","4","2"]) + result.should(eq([1,3])) + end + + it "should return a stepped number range given a negative step" do + result = @scope.function_range(["1","4","-2"]) + result.should(eq([1,3])) + end + end -- cgit v1.2.3 From cf37a128a0939a6ba842d735a664b957905cbd87 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Wed, 22 May 2013 17:10:45 -0700 Subject: Add functions to validate ipv4 and ipv6 addresses --- .../parser/functions/validate_ipv4_address.rb | 41 ++++++++++++++ .../parser/functions/validate_ipv6_address.rb | 42 ++++++++++++++ .../parser/functions/validate_ipv4_address_spec.rb | 64 ++++++++++++++++++++++ .../parser/functions/validate_ipv6_address_spec.rb | 62 +++++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_ipv4_address.rb create mode 100644 lib/puppet/parser/functions/validate_ipv6_address.rb create mode 100644 spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb create mode 100644 spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb new file mode 100644 index 0000000..2de1454 --- /dev/null +++ b/lib/puppet/parser/functions/validate_ipv4_address.rb @@ -0,0 +1,41 @@ +module Puppet::Parser::Functions + + newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC + Validate that all values passed are valid IPv4 addresses. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_ip = "1.2.3.4" + validate_ipv4_address($my_ip) + validate_bool("8.8.8.8", "172.16.0.1", $my_ip) + + The following values will fail, causing compilation to abort: + + $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ] + validate_ipv4_address($some_array) + + ENDHEREDOC + ) do |args| + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless arg.is_a?(String) + raise Puppet::ParseError, "#{arg.inspect} is not a string." + end + + begin + unless IPAddr.new(arg).ipv4? + raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." + end + rescue ArgumentError + raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." + end + end + + end + +end diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb new file mode 100644 index 0000000..d270b4d --- /dev/null +++ b/lib/puppet/parser/functions/validate_ipv6_address.rb @@ -0,0 +1,42 @@ +module Puppet::Parser::Functions + + newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC + Validate that all values passed are valid IPv6 addresses. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_ip = "3ffe:505:2" + validate_ipv6_address(1) + validate_ipv6_address($my_ip) + validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip) + + The following values will fail, causing compilation to abort: + + $some_array = [ true, false, "garbage string", "1.2.3.4" ] + validate_ipv6_address($some_array) + + ENDHEREDOC + ) do |args| + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless arg.is_a?(String) + raise Puppet::ParseError, "#{arg.inspect} is not a string." + end + + begin + unless IPAddr.new(arg).ipv6? + raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." + end + rescue ArgumentError + raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." + end + end + + end + +end diff --git a/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb new file mode 100644 index 0000000..85536d3 --- /dev/null +++ b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env/ruby -S rspec + +require "spec_helper" + +describe Puppet::Parser::Functions.function(:validate_ipv4_address) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe "when calling validate_ipv4_address from puppet" do + describe "when given IPv4 address strings" do + it "should compile with one argument" do + Puppet[:code] = "validate_ipv4_address('1.2.3.4')" + scope.compiler.compile + end + + it "should compile with multiple arguments" do + Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')" + scope.compiler.compile + end + end + + describe "when given an IPv6 address" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address('3ffe:505')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) + end + end + + describe "when given other strings" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address('hello', 'world')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) + end + end + + describe "when given numbers" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address(1, 2)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/) + end + end + + describe "when given booleans" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address(true, false)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a string/) + end + end + + it "should not compile when no arguments are passed" do + Puppet[:code] = "validate_ipv4_address()" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end +end diff --git a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb new file mode 100644 index 0000000..bf3c966 --- /dev/null +++ b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb @@ -0,0 +1,62 @@ +#! /usr/bin/env/ruby -S rspec + +require "spec_helper" + +describe Puppet::Parser::Functions.function(:validate_ipv6_address) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe "when calling validate_ipv6_address from puppet" do + describe "when given IPv6 address strings" do + it "should compile with one argument" do + Puppet[:code] = "validate_ipv6_address('3ffe:505:2')" + scope.compiler.compile + end + + it "should compile with multiple arguments" do + Puppet[:code] = "validate_ipv6_address('3ffe:505:2', '3ffe:505:1')" + scope.compiler.compile + end + end + + describe "when given an ipv4 address" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address('1.2.3.4')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end + end + + describe "when given other strings" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address('hello', 'world')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end + end + + describe "when given numbers" do + it "should compile" do + Puppet[:code] = "validate_ipv6_address(1, 2)" + scope.compiler.compile + end + end + + describe "when given booleans" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address(true, false)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a string/) + end + end + + it "should not compile when no arguments are passed" do + Puppet[:code] = "validate_ipv6_address()" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end +end -- cgit v1.2.3 From 9a41f07e3b7f1ad44ac5260814867591f13a9d3e Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Wed, 22 May 2013 17:19:03 -0700 Subject: Ruby 2.0 introduces IPAddr::InvalidAddressError --- lib/puppet/parser/functions/validate_ipv4_address.rb | 9 ++++++++- lib/puppet/parser/functions/validate_ipv6_address.rb | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb index 2de1454..fc02748 100644 --- a/lib/puppet/parser/functions/validate_ipv4_address.rb +++ b/lib/puppet/parser/functions/validate_ipv4_address.rb @@ -18,6 +18,13 @@ module Puppet::Parser::Functions ENDHEREDOC ) do |args| + require "ipaddr" + rescuable_exceptions = [ ArgumentError ] + + if defined?(IPAddr::InvalidAddressError) + rescuable_exceptions << IPAddr::InvalidAddressError + end + unless args.length > 0 then raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)") end @@ -31,7 +38,7 @@ module Puppet::Parser::Functions unless IPAddr.new(arg).ipv4? raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." end - rescue ArgumentError + rescue *rescuable_exceptions raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." end end diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb index d270b4d..b0f2558 100644 --- a/lib/puppet/parser/functions/validate_ipv6_address.rb +++ b/lib/puppet/parser/functions/validate_ipv6_address.rb @@ -19,6 +19,13 @@ module Puppet::Parser::Functions ENDHEREDOC ) do |args| + require "ipaddr" + rescuable_exceptions = [ ArgumentError ] + + if defined?(IPAddr::InvalidAddressError) + rescuable_exceptions << IPAddr::InvalidAddressError + end + unless args.length > 0 then raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)") end @@ -32,7 +39,7 @@ module Puppet::Parser::Functions unless IPAddr.new(arg).ipv6? raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." end - rescue ArgumentError + rescue *rescuable_exceptions raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." end end -- cgit v1.2.3 From 5d5796a7d5da68b74b4325adb47a907b8579bb26 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Wed, 22 May 2013 17:37:08 -0700 Subject: Update ipv6 examples --- .../unit/puppet/parser/functions/validate_ipv6_address_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb index bf3c966..c74e8cd 100644 --- a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb @@ -8,12 +8,12 @@ describe Puppet::Parser::Functions.function(:validate_ipv6_address) do describe "when calling validate_ipv6_address from puppet" do describe "when given IPv6 address strings" do it "should compile with one argument" do - Puppet[:code] = "validate_ipv6_address('3ffe:505:2')" + Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')" scope.compiler.compile end it "should compile with multiple arguments" do - Puppet[:code] = "validate_ipv6_address('3ffe:505:2', '3ffe:505:1')" + Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')" scope.compiler.compile end end @@ -37,9 +37,11 @@ describe Puppet::Parser::Functions.function(:validate_ipv6_address) do end describe "when given numbers" do - it "should compile" do + it "should not compile" do Puppet[:code] = "validate_ipv6_address(1, 2)" - scope.compiler.compile + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) end end -- cgit v1.2.3 From e0fd7299f60690e67bf5488ed2a04102a550aec0 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Wed, 22 May 2013 17:43:37 -0700 Subject: Don't run certain tests under 1.8.7 --- .../puppet/parser/functions/validate_ipv6_address_spec.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb index c74e8cd..1fe5304 100644 --- a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb @@ -36,12 +36,15 @@ describe Puppet::Parser::Functions.function(:validate_ipv6_address) do end end - describe "when given numbers" do - it "should not compile" do - Puppet[:code] = "validate_ipv6_address(1, 2)" - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + # 1.8.7 is EOL'd and also absolutely insane about ipv6 + unless RUBY_VERSION == '1.8.7' + describe "when given numbers" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address(1, 2)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end end end -- cgit v1.2.3 From 1fcb854f155cd0d63f88a9c792e60555ddffcc16 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:11:50 -0700 Subject: (maint) split up range spec with describe blocks --- spec/unit/puppet/parser/functions/range_spec.rb | 81 +++++++++++++------------ 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 5eb290f..c7dab2e 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -12,54 +12,59 @@ describe "the range function" do lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) end - it "should return a letter range" do - result = scope.function_range(["a","d"]) - result.should(eq(['a','b','c','d'])) - end + describe 'with a letter range' do + it "should return a letter range" do + result = scope.function_range(["a","d"]) + result.should(eq(['a','b','c','d'])) + end - it "should return a letter range given a step of 1" do - result = scope.function_range(["a","d","1"]) - result.should(eq(['a','b','c','d'])) - end + it "should return a letter range given a step of 1" do + result = scope.function_range(["a","d","1"]) + result.should(eq(['a','b','c','d'])) + end - it "should return a stepped letter range" do - result = scope.function_range(["a","d","2"]) - result.should(eq(['a','c'])) - end + it "should return a stepped letter range" do + result = scope.function_range(["a","d","2"]) + result.should(eq(['a','c'])) + end - it "should return a stepped letter range given a negative step" do - result = scope.function_range(["a","d","-2"]) - result.should(eq(['a','c'])) + it "should return a stepped letter range given a negative step" do + result = scope.function_range(["a","d","-2"]) + result.should(eq(['a','c'])) + end end - it "should return a number range" do - result = scope.function_range(["1","4"]) - result.should(eq([1,2,3,4])) - end + describe 'with a number range' do + it "should return a number range" do + result = scope.function_range(["1","4"]) + result.should(eq([1,2,3,4])) + end - it "should work with padded hostname like strings" do - expected = ("host01".."host10").to_a - scope.function_range(["host01","host10"]).should eq expected - end + it "should return a number range given a step of 1" do + result = scope.function_range(["1","4","1"]) + result.should(eq([1,2,3,4])) + end - it "should coerce zero padded digits to integers" do - expected = (0..10).to_a - scope.function_range(["00", "10"]).should eq expected - end + it "should return a stepped number range" do + result = scope.function_range(["1","4","2"]) + result.should(eq([1,3])) + end - it "should return a number range given a step of 1" do - result = scope.function_range(["1","4","1"]) - result.should(eq([1,2,3,4])) + it "should return a stepped number range given a negative step" do + result = scope.function_range(["1","4","-2"]) + result.should(eq([1,3])) + end end - it "should return a stepped number range" do - result = scope.function_range(["1","4","2"]) - result.should(eq([1,3])) - end + describe 'with a numeric-like string range' do + it "should work with padded hostname like strings" do + expected = ("host01".."host10").to_a + scope.function_range(["host01","host10"]).should eq expected + end - it "should return a stepped number range given a negative step" do - result = scope.function_range(["1","4","-2"]) - result.should(eq([1,3])) + it "should coerce zero padded digits to integers" do + expected = (0..10).to_a + scope.function_range(["00", "10"]).should eq expected + end end - end -- cgit v1.2.3 From dd0a4220d591f778e828f98e73ac67ed9e3b97c4 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:13:20 -0700 Subject: (maint) Use present tense in range_spec messages --- spec/unit/puppet/parser/functions/range_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index c7dab2e..07b6f11 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -4,65 +4,65 @@ require 'spec_helper' describe "the range function" do let(:scope) { PuppetlabsSpec::PuppetInternals.scope } - it "should exist" do + it "exists" do Puppet::Parser::Functions.function("range").should == "function_range" end - it "should raise a ParseError if there is less than 1 arguments" do + it "raises a ParseError if there is less than 1 arguments" do lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) end describe 'with a letter range' do - it "should return a letter range" do + it "returns a letter range" do result = scope.function_range(["a","d"]) result.should(eq(['a','b','c','d'])) end - it "should return a letter range given a step of 1" do + it "returns a letter range given a step of 1" do result = scope.function_range(["a","d","1"]) result.should(eq(['a','b','c','d'])) end - it "should return a stepped letter range" do + it "returns a stepped letter range" do result = scope.function_range(["a","d","2"]) result.should(eq(['a','c'])) end - it "should return a stepped letter range given a negative step" do + it "returns a stepped letter range given a negative step" do result = scope.function_range(["a","d","-2"]) result.should(eq(['a','c'])) end end describe 'with a number range' do - it "should return a number range" do + it "returns a number range" do result = scope.function_range(["1","4"]) result.should(eq([1,2,3,4])) end - it "should return a number range given a step of 1" do + it "returns a number range given a step of 1" do result = scope.function_range(["1","4","1"]) result.should(eq([1,2,3,4])) end - it "should return a stepped number range" do + it "returns a stepped number range" do result = scope.function_range(["1","4","2"]) result.should(eq([1,3])) end - it "should return a stepped number range given a negative step" do + it "returns a stepped number range given a negative step" do result = scope.function_range(["1","4","-2"]) result.should(eq([1,3])) end end describe 'with a numeric-like string range' do - it "should work with padded hostname like strings" do + it "works with padded hostname like strings" do expected = ("host01".."host10").to_a scope.function_range(["host01","host10"]).should eq expected end - it "should coerce zero padded digits to integers" do + it "coerces zero padded digits to integers" do expected = (0..10).to_a scope.function_range(["00", "10"]).should eq expected end -- cgit v1.2.3 From 65380bcdfbe91f85013d8a925cdd719826209ce4 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:14:28 -0700 Subject: (maint) Clean up range_spec error expectation Replace `lambda` with `expect` for making an error expectation Add an explicit error message in expectation --- spec/unit/puppet/parser/functions/range_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 07b6f11..426903c 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -9,7 +9,7 @@ describe "the range function" do end it "raises a ParseError if there is less than 1 arguments" do - lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) + expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/ end describe 'with a letter range' do -- cgit v1.2.3 From 77768e5d8df7fa37707b75934fc47be9bb4e3989 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:17:00 -0700 Subject: (maint) Remove syntax decoration from range_spec --- spec/unit/puppet/parser/functions/range_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 426903c..0e1ad37 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -15,44 +15,44 @@ describe "the range function" do describe 'with a letter range' do it "returns a letter range" do result = scope.function_range(["a","d"]) - result.should(eq(['a','b','c','d'])) + result.should eq ['a','b','c','d'] end it "returns a letter range given a step of 1" do result = scope.function_range(["a","d","1"]) - result.should(eq(['a','b','c','d'])) + result.should eq ['a','b','c','d'] end it "returns a stepped letter range" do result = scope.function_range(["a","d","2"]) - result.should(eq(['a','c'])) + result.should eq ['a','c'] end it "returns a stepped letter range given a negative step" do result = scope.function_range(["a","d","-2"]) - result.should(eq(['a','c'])) + result.should eq ['a','c'] end end describe 'with a number range' do it "returns a number range" do result = scope.function_range(["1","4"]) - result.should(eq([1,2,3,4])) + result.should eq [1,2,3,4] end it "returns a number range given a step of 1" do result = scope.function_range(["1","4","1"]) - result.should(eq([1,2,3,4])) + result.should eq [1,2,3,4] end it "returns a stepped number range" do result = scope.function_range(["1","4","2"]) - result.should(eq([1,3])) + result.should eq [1,3] end it "returns a stepped number range given a negative step" do result = scope.function_range(["1","4","-2"]) - result.should(eq([1,3])) + result.should eq [1,3] end end -- cgit v1.2.3 From 7f98203f1863288675d8fa48e2830efca5e26324 Mon Sep 17 00:00:00 2001 From: Chris Boot Date: Thu, 27 Jun 2013 17:51:36 +0100 Subject: ensure_resource: fix documentation typo --- README.markdown | 2 +- lib/puppet/parser/functions/ensure_resource.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 0e40f51..2a1ddfd 100644 --- a/README.markdown +++ b/README.markdown @@ -258,7 +258,7 @@ resource. This example only creates the resource if it does not already exist: - ensure_resource('user, 'dan', {'ensure' => 'present' }) + ensure_resource('user', 'dan', {'ensure' => 'present' }) If the resource already exists but does not match the specified parameters, this function will attempt to recreate the resource leading to a duplicate diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index a9a1733..05e5593 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -13,7 +13,7 @@ resource. This example only creates the resource if it does not already exist: - ensure_resource('user, 'dan', {'ensure' => 'present' }) + ensure_resource('user', 'dan', {'ensure' => 'present' }) If the resource already exists but does not match the specified parameters, this function will attempt to recreate the resource leading to a duplicate -- cgit v1.2.3 From 964a9ad6193b0dd243a44ddae1509655fc9e9fb8 Mon Sep 17 00:00:00 2001 From: Alex O'Rielly Date: Fri, 21 Jun 2013 17:33:44 -0500 Subject: (#21416) Allow file_line to match multiple lines Without this commit the file_line type will outright fail if multiple lines match the given regex. This commit allows the file_line type and provider to optionally match and modify all matching lines. Changeset rebased into a single commit by Adrien Thebo --- lib/puppet/provider/file_line/ruby.rb | 5 ++-- lib/puppet/type/file_line.rb | 5 ++++ spec/unit/puppet/provider/file_line/ruby_spec.rb | 33 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index a3219d3..3cb9f6e 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,4 +1,3 @@ - Puppet::Type.type(:file_line).provide(:ruby) do def exists? @@ -35,8 +34,8 @@ Puppet::Type.type(:file_line).provide(:ruby) do def handle_create_with_match() regex = resource[:match] ? Regexp.new(resource[:match]) : nil match_count = lines.select { |l| regex.match(l) }.size - if match_count > 1 - raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" + if match_count > 1 && resource[:multiple].to_s != 'true' + raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" end File.open(resource[:path], 'w') do |fh| lines.each do |l| diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index f71a4bc..14946bb 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -37,6 +37,11 @@ Puppet::Type.newtype(:file_line) do 'if a match is found, we replace that line rather than adding a new line.' end + newparam(:multiple) do + desc 'An optional value to determine if match can change multiple lines.' + newvalues(true, false) + end + newparam(:line) do desc 'The line to be appended to the file located by the path parameter.' end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 7857d39..648c05b 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -61,6 +61,39 @@ describe provider_class do File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end + it 'should replace all lines that matches' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'foo = bar', + :match => '^foo\s*=.*$', + :multiple => true + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") + end + @provider.exists?.should be_nil + @provider.create + File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar") + end + + it 'should raise an error with invalid values' do + expect { + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'foo = bar', + :match => '^foo\s*=.*$', + :multiple => 'asgadga' + } + ) + }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./) + end + it 'should replace a line that matches' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2") -- cgit v1.2.3 From b2e23dc65bd9851dcb6ad60ffca1acbc70b617e1 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Tue, 2 Jul 2013 11:18:42 +0100 Subject: Adjust to use default URI.escape escape list Conform to RFC per comments on: https://github.com/puppetlabs/puppetlabs-stdlib/pull/164 Conflicts: lib/puppet/parser/functions/uriescape.rb spec/unit/puppet/parser/functions/uriescape_spec.rb --- lib/puppet/parser/functions/uriescape.rb | 3 +-- spec/unit/puppet/parser/functions/uriescape_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 67b93a6..0d81de5 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -15,7 +15,6 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - unsafe = ":/?#[]@!$&'()*+,;= " unless [Array, String].include?(klass) raise(Puppet::ParseError, 'uriescape(): Requires either ' + @@ -26,7 +25,7 @@ module Puppet::Parser::Functions # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i } else - result = URI.escape(value,unsafe) + result = URI.escape(value) end return result diff --git a/spec/unit/puppet/parser/functions/uriescape_spec.rb b/spec/unit/puppet/parser/functions/uriescape_spec.rb index 371de46..7211c88 100644 --- a/spec/unit/puppet/parser/functions/uriescape_spec.rb +++ b/spec/unit/puppet/parser/functions/uriescape_spec.rb @@ -13,8 +13,8 @@ describe "the uriescape function" do end it "should uriescape a string" do - result = scope.function_uriescape([":/?#[]@!$&'()*+,;= "]) - result.should(eq('%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%20')) + result = scope.function_uriescape([":/?#[]@!$&'()*+,;= \"{}"]) + result.should(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D')) end it "should do nothing if a string is already safe" do -- cgit v1.2.3 From 206941520467a5cbf1ba4131c68c4814b5ab181a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Mon, 29 Jul 2013 11:30:47 +0200 Subject: added delete_values() and delete_undef_values() functions --- README.markdown | 27 +++++++++++++++++ lib/puppet/parser/functions/delete_undef_values.rb | 34 ++++++++++++++++++++++ lib/puppet/parser/functions/delete_values.rb | 27 +++++++++++++++++ .../parser/functions/delete_undef_values_spec.rb | 29 ++++++++++++++++++ .../puppet/parser/functions/delete_values_spec.rb | 30 +++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 lib/puppet/parser/functions/delete_undef_values.rb create mode 100644 lib/puppet/parser/functions/delete_values.rb create mode 100644 spec/unit/puppet/parser/functions/delete_undef_values_spec.rb create mode 100644 spec/unit/puppet/parser/functions/delete_values_spec.rb diff --git a/README.markdown b/README.markdown index 2a1ddfd..1e51e1d 100644 --- a/README.markdown +++ b/README.markdown @@ -202,6 +202,33 @@ Deletes a determined indexed value from an array. Would return: ['a','c'] +- *Type*: rvalue + +delete_values +------------- +Deletes all instances of a given value from a hash. + +*Examples:* + + delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B') + +Would return: {'a'=>'A','c'=>'C','B'=>'D'} + + +delete_undef_values +------------------- +Deletes all instances of the undef value from an array or hash. + +*Examples:* + + $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false}) + +Would return: {a => 'A', b => '', d => false} + + $array = delete_undef_values(['A','',undef,false]) + +Would return: ['A','',false] + - *Type*: rvalue difference diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb new file mode 100644 index 0000000..4eecab2 --- /dev/null +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:delete_undef_values, :type => :rvalue, :doc => <<-EOS +Returns a copy of input hash or array with all undefs deleted. + +*Examples:* + + $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false}) + +Would return: {a => 'A', b => '', d => false} + + $array = delete_undef_values(['A','',undef,false]) + +Would return: ['A','',false] + + EOS + ) do |args| + + raise(Puppet::ParseError, + "delete_undef_values(): Wrong number of arguments given " + + "(#{args.size})") if args.size < 1 + + result = args[0] + if result.is_a?(Hash) + result.delete_if {|key, val| val.equal? :undef} + elsif result.is_a?(Array) + result.delete :undef + else + raise(Puppet::ParseError, + "delete_undef_values(): Wrong argument type #{args[0].class} " + + "for first argument") + end + result + end +end diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb new file mode 100644 index 0000000..2fcbd16 --- /dev/null +++ b/lib/puppet/parser/functions/delete_values.rb @@ -0,0 +1,27 @@ +module Puppet::Parser::Functions + newfunction(:delete_values, :type => :rvalue, :doc => <<-EOS +Deletes all instances of a given value from a hash. + +*Examples:* + + delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B') + +Would return: {'a'=>'A','c'=>'C','B'=>'D'} + + EOS + ) do |arguments| + + raise(Puppet::ParseError, + "delete_values(): Wrong number of arguments given " + + "(#{arguments.size} of 2)") if arguments.size != 2 + + hash = arguments[0] + item = arguments[1] + + if not hash.is_a?(Hash) + raise(TypeError, "delete_values(): First argument must be a Hash. " + \ + "Given an" " argument of class #{hash.class}.") + end + hash.delete_if { |key, val| item == val } + end +end diff --git a/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb b/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb new file mode 100644 index 0000000..0536641 --- /dev/null +++ b/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete_undef_values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("delete_undef_values").should == "function_delete_undef_values" + end + + it "should raise a ParseError if there is less than 1 argument" do + lambda { scope.function_delete_undef_values([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if the argument is not Array nor Hash" do + lambda { scope.function_delete_undef_values(['']) }.should( raise_error(Puppet::ParseError)) + lambda { scope.function_delete_undef_values([nil]) }.should( raise_error(Puppet::ParseError)) + end + + it "should delete all undef items from Array and only these" do + result = scope.function_delete_undef_values([['a',:undef,'c','undef']]) + result.should(eq(['a','c','undef'])) + end + + it "should delete all undef items from Hash and only these" do + result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}]) + result.should(eq({'a'=>'A','c'=>'C','d'=>'undef'})) + end +end diff --git a/spec/unit/puppet/parser/functions/delete_values_spec.rb b/spec/unit/puppet/parser/functions/delete_values_spec.rb new file mode 100644 index 0000000..e15c366 --- /dev/null +++ b/spec/unit/puppet/parser/functions/delete_values_spec.rb @@ -0,0 +1,30 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete_values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("delete_values").should == "function_delete_values" + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + lambda { scope.function_delete_values([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there are greater than 2 arguments" do + lambda { scope.function_delete([[], 'foo', 'bar']) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a TypeError if the argument is not a hash" do + lambda { scope.function_delete_values([1,'bar']) }.should( raise_error(TypeError)) + lambda { scope.function_delete_values(['foo','bar']) }.should( raise_error(TypeError)) + lambda { scope.function_delete_values([[],'bar']) }.should( raise_error(TypeError)) + end + + it "should delete all instances of a value from a hash" do + result = scope.function_delete_values([{ 'a'=>'A', 'b'=>'B', 'B'=>'C', 'd'=>'B' },'B']) + result.should(eq({ 'a'=>'A', 'B'=>'C' })) + end + +end -- cgit v1.2.3 From 0206d367c05a7fb2c3bbd7d547e1306541acbef6 Mon Sep 17 00:00:00 2001 From: Martin Hellmich Date: Fri, 28 Jun 2013 18:03:37 +0200 Subject: changed the validate_slength function to accept a min length An optional third parameter can be given a min length. The function then only passes successfully, if all strings are in the range min_length <= string <= max_length update and fix function and unit tests the check for the minlength has to be written differently because 0 values should be possible. We now check a) if the input is convertible, and throw a ParseError and b) if the input .is_a?(Numeric) and ask for a positive number it's not as clean as for maxlength, but keeps a similar behaviour refined the error checking for the min length try to convert to Integer(args[2]) and fail, if it's not possible changed the tests accordingly to the new parameter checking --- lib/puppet/parser/functions/validate_slength.rb | 31 ++++++++++++++++------ .../parser/functions/validate_slength_spec.rb | 14 +++++++++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index fdcc0a2..e0ba43b 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -2,23 +2,26 @@ module Puppet::Parser::Functions newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args| Validate that the first argument is a string (or an array of strings), and - less/equal to than the length of the second argument. It fails if the first - argument is not a string or array of strings, and if arg 2 is not convertable - to a number. + less/equal to than the length of the second argument. An optional third + parameter can be given a the minimum length. It fails if the first + argument is not a string or array of strings, and if arg 2 and arg 3 are + not convertable to a number. The following values will pass: validate_slength("discombobulate",17) validate_slength(["discombobulate","moo"],17) + validate_slength(["discombobulate","moo"],17,3) The following valueis will not: validate_slength("discombobulate",1) validate_slength(["discombobulate","thermometer"],5) + validate_slength(["discombobulate","moo"],17,10) ENDHEREDOC - raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2 + raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)") unless args.length == 2 or args.length == 3 unless (args[0].is_a?(String) or args[0].is_a?(Array)) raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}") @@ -27,19 +30,31 @@ module Puppet::Parser::Functions begin max_length = args[1].to_i rescue NoMethodError => e - raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the length parameter to an integer - sorry: " + e.message ) + raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message ) + end + + unless args.length == 2 + begin + min_length = Integer(args[2]) + rescue StandardError => e + raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message ) + end + else + min_length = 0 end raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0 + raise Puppet::ParseError, ("validate_slength(): please pass a positive number as min_length") unless min_length >= 0 + raise Puppet::ParseError, ("validate_slength(): please pass a min length that is smaller than the maximum") unless min_length <= max_length case args[0] when String - raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been less than or equal to #{max_length} characters") unless args[0].length <= max_length + raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters") unless args[0].length <= max_length and min_length <= arg.length when Array args[0].each do |arg| if arg.is_a?(String) - unless ( arg.is_a?(String) and arg.length <= max_length ) - raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been less than or equal to #{max_length} characters") + unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length) + raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters") end else raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}") diff --git a/spec/unit/puppet/parser/functions/validate_slength_spec.rb b/spec/unit/puppet/parser/functions/validate_slength_spec.rb index eccf908..a870c63 100755 --- a/spec/unit/puppet/parser/functions/validate_slength_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_slength_spec.rb @@ -26,8 +26,20 @@ describe "the validate_slength function" do expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/)) end + it "should raise a ParseError if argument 3 doesn't convert to a fixnum" do + expect { scope.function_validate_slength(["moo",2,["3"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) + end + + it "should raise a ParseError if argument 3 converted, but to 0, e.g. a string" do + expect { scope.function_validate_slength(["moo",2,"monkey"]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) + end + it "should fail if string greater then size" do - expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been less than or equal to/)) + expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been between 0 and 2/)) + end + + it "should fail if the min length is larger than the max length" do + expect { scope.function_validate_slength(["test", 10, 15]) }.to(raise_error(Puppet::ParseError, /pass a min length that is smaller than the max/)) end it "should fail if you pass an array of something other than strings" do -- cgit v1.2.3 From 77625e6d8fa48771f99c0a3c667c23a6d547017a Mon Sep 17 00:00:00 2001 From: Hubert Date: Thu, 8 Aug 2013 12:56:12 +0200 Subject: Fix validate_slength, arg.length should be args[0].length During a puppet run an error will be thrown and a puppet run will fail completely (when using validate_slength): undefined local variable or method `arg' for # --- lib/puppet/parser/functions/validate_slength.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index e0ba43b..339a21d 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -49,7 +49,7 @@ module Puppet::Parser::Functions case args[0] when String - raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters") unless args[0].length <= max_length and min_length <= arg.length + raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters") unless args[0].length <= max_length and min_length <= args[0].length when Array args[0].each do |arg| if arg.is_a?(String) -- cgit v1.2.3 From 2ba5404b16911c6a926e66a5efbe0b164cb6b658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Thu, 8 Aug 2013 16:56:32 +0200 Subject: minor corrections to delete_values() --- lib/puppet/parser/functions/delete_values.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index 2fcbd16..17b9d37 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -15,12 +15,11 @@ Would return: {'a'=>'A','c'=>'C','B'=>'D'} "delete_values(): Wrong number of arguments given " + "(#{arguments.size} of 2)") if arguments.size != 2 - hash = arguments[0] - item = arguments[1] + hash, item = arguments if not hash.is_a?(Hash) raise(TypeError, "delete_values(): First argument must be a Hash. " + \ - "Given an" " argument of class #{hash.class}.") + "Given an argument of class #{hash.class}.") end hash.delete_if { |key, val| item == val } end -- cgit v1.2.3 From 4ad1da83f9326b883ef97a3642a889af068137e4 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:00:27 -0700 Subject: (maint) Remove unneeded parens around exceptions --- lib/puppet/parser/functions/validate_slength.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 339a21d..22c9d52 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -21,47 +21,47 @@ module Puppet::Parser::Functions ENDHEREDOC - raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)") unless args.length == 2 or args.length == 3 + raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3 unless (args[0].is_a?(String) or args[0].is_a?(Array)) - raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}") + raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}" end begin max_length = args[1].to_i rescue NoMethodError => e - raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message ) + raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message end unless args.length == 2 begin min_length = Integer(args[2]) rescue StandardError => e - raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message ) + raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message end else min_length = 0 end - raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0 - raise Puppet::ParseError, ("validate_slength(): please pass a positive number as min_length") unless min_length >= 0 - raise Puppet::ParseError, ("validate_slength(): please pass a min length that is smaller than the maximum") unless min_length <= max_length + raise Puppet::ParseError, "validate_slength(): please pass a positive number as max_length" unless max_length > 0 + raise Puppet::ParseError, "validate_slength(): please pass a positive number as min_length" unless min_length >= 0 + raise Puppet::ParseError, "validate_slength(): please pass a min length that is smaller than the maximum" unless min_length <= max_length case args[0] when String - raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters") unless args[0].length <= max_length and min_length <= args[0].length + raise Puppet::ParseError, "validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters" unless args[0].length <= max_length and min_length <= args[0].length when Array args[0].each do |arg| if arg.is_a?(String) unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length) - raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters") + raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters" end else - raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}") + raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}" end end else - raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}") + raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}" end end end -- cgit v1.2.3 From 2cfa408909cd8a1d465e76d0487fea204f55b844 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:08:35 -0700 Subject: (maint) Explicitly unpack validate_slength args --- lib/puppet/parser/functions/validate_slength.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 22c9d52..8c8569a 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -23,19 +23,21 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3 - unless (args[0].is_a?(String) or args[0].is_a?(Array)) - raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}" + input, max_length, min_length = *args + + unless (input.is_a?(String) or input.is_a?(Array)) + raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{input.class}" end begin - max_length = args[1].to_i + max_length = max_length.to_i rescue NoMethodError => e raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message end unless args.length == 2 begin - min_length = Integer(args[2]) + min_length = Integer(min_length) rescue StandardError => e raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message end @@ -47,11 +49,11 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "validate_slength(): please pass a positive number as min_length" unless min_length >= 0 raise Puppet::ParseError, "validate_slength(): please pass a min length that is smaller than the maximum" unless min_length <= max_length - case args[0] + case input when String - raise Puppet::ParseError, "validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters" unless args[0].length <= max_length and min_length <= args[0].length + raise Puppet::ParseError, "validate_slength(): #{input.inspect} is #{input.length} characters. It should have been between #{min_length} and #{max_length} characters" unless input.length <= max_length and min_length <= input.length when Array - args[0].each do |arg| + input.each do |arg| if arg.is_a?(String) unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length) raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters" @@ -61,7 +63,7 @@ module Puppet::Parser::Functions end end else - raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}" + raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{input.class}" end end end -- cgit v1.2.3 From e63715ddaf7c2e5a742ce29e5e159b6031918963 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:20:46 -0700 Subject: (maint) reword error messages for validate_slength --- lib/puppet/parser/functions/validate_slength.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 8c8569a..68054b8 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -26,20 +26,20 @@ module Puppet::Parser::Functions input, max_length, min_length = *args unless (input.is_a?(String) or input.is_a?(Array)) - raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{input.class}" + raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got a #{input.class}" end begin max_length = max_length.to_i rescue NoMethodError => e - raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message + raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got a #{max_length.class}" end unless args.length == 2 begin min_length = Integer(min_length) rescue StandardError => e - raise Puppet::ParseError, "validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message + raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got a #{min_length.class}" end else min_length = 0 -- cgit v1.2.3 From 6df05cbc2d6d4d85774de7ca16f1fc557f69516d Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:27:56 -0700 Subject: (maint) clean up validate_slength argument validation --- lib/puppet/parser/functions/validate_slength.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 68054b8..83c7ed0 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -30,24 +30,26 @@ module Puppet::Parser::Functions end begin - max_length = max_length.to_i - rescue NoMethodError => e - raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got a #{max_length.class}" + max_length = Integer(max_length) + raise ArgumentError if max_length <= 0 + rescue ArgumentError, TypeError + raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got #{max_length}:#{max_length.class}" end - unless args.length == 2 + if min_length begin min_length = Integer(min_length) - rescue StandardError => e - raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got a #{min_length.class}" + raise ArgumentError if min_length < 0 + rescue ArgumentError, TypeError + raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got #{min_length}:#{min_length.class}" end else min_length = 0 end - raise Puppet::ParseError, "validate_slength(): please pass a positive number as max_length" unless max_length > 0 - raise Puppet::ParseError, "validate_slength(): please pass a positive number as min_length" unless min_length >= 0 - raise Puppet::ParseError, "validate_slength(): please pass a min length that is smaller than the maximum" unless min_length <= max_length + if min_length > max_length + raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument" + end case input when String -- cgit v1.2.3 From b41883933c1dbf1a3c9fa7fce65e273246b474ee Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:50:55 -0700 Subject: (maint) collapse String/Array validation into shared lambda --- lib/puppet/parser/functions/validate_slength.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 83c7ed0..259df5a 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -51,21 +51,23 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument" end + validator = lambda do |str| + unless str.length <= max_length and str.length >= min_length + raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}" + end + end + case input when String - raise Puppet::ParseError, "validate_slength(): #{input.inspect} is #{input.length} characters. It should have been between #{min_length} and #{max_length} characters" unless input.length <= max_length and min_length <= input.length + validator.call(input) when Array - input.each do |arg| + input.each_with_index do |arg, pos| if arg.is_a?(String) - unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length) - raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters" - end + validator.call(arg) else - raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}" + raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}" end end - else - raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{input.class}" end end end -- cgit v1.2.3 From 1950b605fb7acaf055b922433a74c429794ee375 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:51:36 -0700 Subject: (maint) reindent case statement to match standard indentation --- lib/puppet/parser/functions/validate_slength.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 259df5a..34dfcf2 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -58,16 +58,16 @@ module Puppet::Parser::Functions end case input - when String - validator.call(input) - when Array - input.each_with_index do |arg, pos| - if arg.is_a?(String) - validator.call(arg) - else - raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}" - end + when String + validator.call(input) + when Array + input.each_with_index do |arg, pos| + if arg.is_a?(String) + validator.call(arg) + else + raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}" end + end end end end -- cgit v1.2.3 From 200e585ea78a5f3587ca35bb0fe453c0670ed65c Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 12:48:46 -0700 Subject: (maint) refactor validate_slength tests --- .../parser/functions/validate_slength_spec.rb | 77 ++++++++++++---------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/spec/unit/puppet/parser/functions/validate_slength_spec.rb b/spec/unit/puppet/parser/functions/validate_slength_spec.rb index 6ed1b0f..851835f 100755 --- a/spec/unit/puppet/parser/functions/validate_slength_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_slength_spec.rb @@ -9,52 +9,59 @@ describe "the validate_slength function" do Puppet::Parser::Functions.function("validate_slength").should == "function_validate_slength" end - it "should raise a ParseError if there is less than 2 arguments" do - expect { scope.function_validate_slength([]) }.to(raise_error(Puppet::ParseError)) - expect { scope.function_validate_slength(["asdf"]) }.to(raise_error(Puppet::ParseError)) - end + describe "validating the input argument types" do + it "raises an error if there are less than two arguments" do + expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/ + end - it "should raise a ParseError if argument 2 doesn't convert to a fixnum" do - expect { scope.function_validate_slength(["moo",["2"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) - end + it "raises an error if there are more than three arguments" do + expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/ + end - it "should raise a ParseError if argument 2 converted, but to 0, e.g. a string" do - expect { scope.function_validate_slength(["moo","monkey"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/)) - end + it "raises an error if the first argument is not a string" do + expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/ + end - it "should raise a ParseError if argument 2 converted, but to 0" do - expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/)) - end + it "raises an error if the second argument cannot be cast to an Integer" do + expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/ + end - it "should raise a ParseError if argument 3 doesn't convert to a fixnum" do - expect { scope.function_validate_slength(["moo",2,["3"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) - end + it "raises an error if the third argument cannot be cast to an Integer" do + expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/ + end - it "should raise a ParseError if argument 3 converted, but to 0, e.g. a string" do - expect { scope.function_validate_slength(["moo",2,"monkey"]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) + it "raises an error if the second argument is smaller than the third argument" do + expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/ + end end - it "should fail if string greater then size" do - expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been between 0 and 2/)) - end + describe "validating the input string length" do + describe "when the input is a string" do + it "fails validation if the string is larger than the max length" do + expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/ + end - it "should fail if the min length is larger than the max length" do - expect { scope.function_validate_slength(["test", 10, 15]) }.to(raise_error(Puppet::ParseError, /pass a min length that is smaller than the max/)) - end + it "fails validation if the string is less than the min length" do + expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/ + end - it "should fail if you pass an array of something other than strings" do - expect { scope.function_validate_slength([["moo",["moo"],Hash.new["moo" => 7]], 7]) }.to(raise_error(Puppet::ParseError, /is not a string, it's a/)) - end + it "doesn't raise an error if the string is under the max length" do + scope.function_validate_slength(['input', 10]) + end - it "should fail if you pass something other than a string or array" do - expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/)) - end + it "doesn't raise an error if the string is equal to the max length" do + scope.function_validate_slength(['input', 5]) + end - it "should not fail if string is smaller or equal to size" do - expect { scope.function_validate_slength(["test", 5]) }.to_not(raise_error(Puppet::ParseError)) - end + it "doesn't raise an error if the string is equal to the min length" do + scope.function_validate_slength(['input', 10, 5]) + end + end - it "should not fail if array of string is are all smaller or equal to size" do - expect { scope.function_validate_slength([["moo","foo","bar"], 5]) }.to_not(raise_error(Puppet::ParseError)) + describe "when the input is an array" do + it "fails validation if one of the array elements is not a string" do + expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/ + end + end end end -- cgit v1.2.3 From 24911db44ca086b00ce543f7da08e5176f7c93a8 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 12:52:17 -0700 Subject: (maint) Validate input argument in a single location --- lib/puppet/parser/functions/validate_slength.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 34dfcf2..7d534f3 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -25,10 +25,6 @@ module Puppet::Parser::Functions input, max_length, min_length = *args - unless (input.is_a?(String) or input.is_a?(Array)) - raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got a #{input.class}" - end - begin max_length = Integer(max_length) raise ArgumentError if max_length <= 0 @@ -62,12 +58,14 @@ module Puppet::Parser::Functions validator.call(input) when Array input.each_with_index do |arg, pos| - if arg.is_a?(String) + if arg.is_a? String validator.call(arg) else - raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}" + raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got #{arg.class}" end end + else + raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got #{input.class}" end end end -- cgit v1.2.3