summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-05-21 22:03:04 +0200
committerGuido Günther <agx@sigxcpu.org>2014-05-22 21:52:11 +0200
commit6b9bc57547e7bc84b17f33a4807f22be6bf91ecb (patch)
treebf2ceb234c0ac842c752222cc5f17a1978390457
parentc4abfdae1bcc4f24606467e43b738cc412f98741 (diff)
Add validate_nonemptystring_msg
This allows to print a custom message when the string is not valid but only accepts a single argument.
-rw-r--r--README.markdown18
-rw-r--r--lib/puppet/parser/functions/validate_nonemptystring_msg.rb31
-rw-r--r--spec/unit/puppet/parser/functions/validate_nonemptystring_msg_spec.rb52
3 files changed, 100 insertions, 1 deletions
diff --git a/README.markdown b/README.markdown
index 4761f9d..96b4b6e 100644
--- a/README.markdown
+++ b/README.markdown
@@ -25,6 +25,23 @@ The following values will fail, causing compilation to abort:
$undefined = undef
validate_nonemptystring($undefined)
+validate_nonemptystring_msg
+---------------------------
+Validate that the passed value is string data and not empty i.e. not
+undef or ''. Abort catalog compilation if it fails this check and
+print the message given as second argment.
+
+The following values will pass:
+
+ $my_string = "one two"
+ validate_nonemptystring_msg($my_string, 'Not a string')
+
+The following values will fail, causing compilation to abort and print
+'Not a string':
+
+ validate_nonemptystring_msg(true. 'Not a string')
+ validate_nonemptystring_msg([ 'some', 'array' ], 'Not a string')
+
validate_nonemptyarray
----------------------
Validate that all passed values are array data structures. Abort
@@ -43,4 +60,3 @@ The following values will fail, causing compilation to abort:
validate_nonemptyarray('some_string')
$undefined = undef
validate_nonemptyarray($undefined)
-
diff --git a/lib/puppet/parser/functions/validate_nonemptystring_msg.rb b/lib/puppet/parser/functions/validate_nonemptystring_msg.rb
new file mode 100644
index 0000000..f983a55
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_nonemptystring_msg.rb
@@ -0,0 +1,31 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_nonemptystring_msg, :doc => <<-'ENDHEREDOC') do |args|
+ Validate that the passed value is string data and not empty
+ i.e. not undef or ''. Abort catalog compilation if it fails this
+ check and print the message given as second argment.
+
+ The following values will pass:
+
+ $my_string = "one two"
+ validate_nonemptystring_msg($my_string, 'Not a string')
+
+ The following values will fail, causing compilation to abort and print
+ 'Not a string':
+
+ validate_nonemptystring_msg(true. 'Not a string')
+ validate_nonemptystring_msg([ 'some', 'array' ], 'Not a string')
+
+ ENDHEREDOC
+
+ unless args.length == 2 then
+ raise Puppet::ParseError, ("validate_nonemptystring_msg(): wrong number of arguments (#{args.length}; must be 2)")
+ end
+
+ str = args[0]
+ msg = args[1]
+ unless str.is_a?(String) and str != nil and str != '' and str != :undef
+ raise Puppet::ParseError, (msg)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_nonemptystring_msg_spec.rb b/spec/unit/puppet/parser/functions/validate_nonemptystring_msg_spec.rb
new file mode 100644
index 0000000..47988e4
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_nonemptystring_msg_spec.rb
@@ -0,0 +1,52 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_nonemptystring_msg) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe 'when calling validate_nonemptystring_msg from puppet' do
+
+ it "should compile when foo is a string" do
+ Puppet[:code] = "validate_nonemptystring_msg('foo', 'a msg')"
+ scope.compiler.compile
+ end
+
+
+ %w{ true false }.each do |the_string|
+ it "should compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptystring_msg('#{the_string}', 'a msg')"
+ scope.compiler.compile
+ end
+
+ it "should not compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptystring_msg(#{the_string}, 'a msg')"
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /msg/)
+ end
+ end
+
+ it "should not compile when an explicitly undef variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = undef
+ validate_nonemptystring_msg($foo, 'a msg')
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /msg/)
+ end
+
+ it "should not compile when an undefined variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ validate_nonemptystring_msg($foobarbazishouldnotexist, 'a msg')
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /msg/)
+ end
+
+ it "should not compile when the string is empty" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $bar = ''
+ validate_nonemptystring_msg($bar, 'a msg')
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /msg/)
+ end
+
+ end
+end