summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/spec.opts6
-rw-r--r--spec/spec_helper.rb31
-rw-r--r--spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb71
3 files changed, 108 insertions, 0 deletions
diff --git a/spec/spec.opts b/spec/spec.opts
new file mode 100644
index 0000000..91cd642
--- /dev/null
+++ b/spec/spec.opts
@@ -0,0 +1,6 @@
+--format
+s
+--colour
+--loadby
+mtime
+--backtrace
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..a63422d
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,31 @@
+dir = File.expand_path(File.dirname(__FILE__))
+$LOAD_PATH.unshift File.join(dir, 'lib')
+
+# So everyone else doesn't have to include this base constant.
+module PuppetSpec
+ FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR)
+end
+
+require 'puppet'
+require 'rspec-puppet'
+require 'simplecov'
+require 'puppetlabs_spec_helper/module_spec_helper'
+require 'mocha/setup'
+
+
+SimpleCov.start do
+ add_filter "/spec/"
+end
+
+
+RSpec.configure do |config|
+ config.before :each do
+ # Ensure that we don't accidentally cache facts and environment between
+ # test cases. This requires each example group to explicitly load the
+ # facts being exercised with something like
+ # Facter.collection.loader.load(:ipaddress)
+ Facter::Util::Loader.any_instance.stubs(:load_all)
+ Facter.clear
+ Facter.clear_messages
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb b/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb
new file mode 100644
index 0000000..41b9dcd
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb
@@ -0,0 +1,71 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_nonemptystring) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe 'when calling validate_nonemptystring from puppet' do
+
+ %w{ foo bar baz }.each do |the_string|
+
+ it "should compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptystring('#{the_string}')"
+ scope.compiler.compile
+ end
+
+ it "should compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptystring(#{the_string})"
+ scope.compiler.compile
+ end
+
+ end
+
+ %w{ true false }.each do |the_string|
+ it "should compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptystring('#{the_string}')"
+ scope.compiler.compile
+ end
+
+ it "should not compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptystring(#{the_string})"
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should compile when multiple string arguments are passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = 'one'
+ $bar = 'two'
+ validate_nonemptystring($foo, $bar)
+ ENDofPUPPETcode
+ scope.compiler.compile
+ end
+
+ it "should not compile when an explicitly undef variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = undef
+ validate_nonemptystring($foo)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ it "should not compile when an undefined variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ validate_nonemptystring($foobarbazishouldnotexist)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ it "should not compile when one string is empty" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = 'one'
+ $bar = ''
+ $baz = 'three'
+ validate_nonemptystring($foo, $bar)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ end
+end