summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-10-02 15:06:47 +0200
committerGuido Günther <agx@sigxcpu.org>2014-10-02 18:47:26 +0200
commitb71d5617db54c55afa89cba1f003d30fa88af2a9 (patch)
tree297b948ee9b480b57eb055d5df894965b0ea32bf
parent165d5daf93f394c8eff20f77f699f782000596e5 (diff)
Add validate_nonemptyarray_msg
this makes is consistent with other validate_nonempty* functions
-rw-r--r--README.markdown16
-rw-r--r--lib/puppet/parser/functions/validate_nonemptyarray_msg.rb30
-rw-r--r--spec/unit/puppet/parser/functions/validate_nonemptyarray_msg_spec.rb45
3 files changed, 91 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index a89a7fe..03a7284 100644
--- a/README.markdown
+++ b/README.markdown
@@ -70,3 +70,19 @@ The following values will fail, causing compilation to abort:
validate_nonemptyarray('some_string')
$undefined = undef
validate_nonemptyarray($undefined)
+
+validate_nonemptyarray_msg
+--------------------------
+Validate that the passed value is an array data structure. Abort catalog
+compilation if any value fails this check. Also abort if any of the arrays
+is empty. On failure it prints the message given as the second argument.
+
+The following values will pass:
+
+ $my_array = [ 'one', 'two' ]
+ validate_nonemptyarray_msg($my_array, 'foo')
+
+The following values will fail, causing compilation to abort with the given msg:
+
+ validate_nonemptyarray_msg(true, 'Not a nonempty array')
+ validate_nonemptyarray_msg([], 'Not a nonempty array')
diff --git a/lib/puppet/parser/functions/validate_nonemptyarray_msg.rb b/lib/puppet/parser/functions/validate_nonemptyarray_msg.rb
new file mode 100644
index 0000000..4a787dd
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_nonemptyarray_msg.rb
@@ -0,0 +1,30 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_nonemptyarray_msg, :doc => <<-'ENDHEREDOC') do |args|
+ Validate that the passed value is an array data structure. Abort catalog
+ compilation if any value fails this check. Also abort if any of the arrays
+ is empty. On failure it prints the message given as the second argument.
+
+ The following values will pass:
+
+ $my_array = [ 'one', 'two' ]
+ validate_nonemptyarray_msg($my_array, 'foo')
+
+ The following values will fail, causing compilation to abort with the given msg:
+
+ validate_nonemptyarray_msg(true, 'Not a nonempty array')
+ validate_nonemptyarray_msg([], 'Not a nonempty array')
+
+ ENDHEREDOC
+
+ unless args.length == 2 then
+ raise Puppet::ParseError, ("validate_nonemptyarray(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ arg = args[0]
+ msg = args[1]
+ unless arg.is_a?(Array) and arg.length > 0
+ raise Puppet::ParseError, (msg)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_nonemptyarray_msg_spec.rb b/spec/unit/puppet/parser/functions/validate_nonemptyarray_msg_spec.rb
new file mode 100644
index 0000000..f6a54a1
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_nonemptyarray_msg_spec.rb
@@ -0,0 +1,45 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_nonemptyarray_msg) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+ describe 'when calling validate_nonemptyarray_msg from puppet' do
+
+ %w{ true false }.each do |the_string|
+ it "should not compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptyarray_msg('#{the_string}', 'a msg')"
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /a msg/)
+ end
+
+ it "should not compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptyarray_msg(#{the_string}, 'a msg')"
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /a msg/)
+ end
+ end
+
+ it "should compile when a array argument is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = [ 'foo', 'bar' ]
+ validate_nonemptyarray_msg($foo, 'a msg')
+ ENDofPUPPETcode
+ scope.compiler.compile
+ end
+
+ it "should not compile when an undef variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = undef
+ validate_nonemptyarray_msg($foo, 'a msg')
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /a msg/)
+ end
+
+ it "should not compile when with an empty array" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = []
+ validate_nonemptyarray_msg($foo, 'a msg')
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /a msg/)
+ end
+ end
+end