summaryrefslogtreecommitdiff
path: root/spec/lib/puppet_spec
diff options
context:
space:
mode:
authorJeff McCune <jeff@puppetlabs.com>2011-08-08 16:58:14 -0700
committerJeff McCune <jeff@puppetlabs.com>2011-08-08 16:58:40 -0700
commit33887f9be50c4fd94bbd08d7c00d9b3d97e29d21 (patch)
treeb7c5b8cd3595b1db0a835f31e551f5baf6b2c0ed /spec/lib/puppet_spec
parente8fb6917d102d8a45d5682b79f33b1ac0d52d73b (diff)
parentaa27fc76c7d5fa090ea1d47027856c3e70c6ae8f (diff)
downloadpuppet-stdlib-33887f9be50c4fd94bbd08d7c00d9b3d97e29d21.tar.gz
puppet-stdlib-33887f9be50c4fd94bbd08d7c00d9b3d97e29d21.tar.bz2
Merge branch 'issue/master/8797_puppetlabs-functions_merge'
Closes pull request #12 Reviewed-by: Jeff McCune Verified all spec tests pass using rspec **/*_spec.rb * issue/master/8797_puppetlabs-functions_merge: (164 commits) * Moved kwalify to puppetlabs-kwalify project * Re-arranged tests in line with puppetlabs-stdlib Prep for stdlib merge * Renamed load_yaml & load_json to parseyaml & parsejson * Renamed is_valid_* functions and remove the 'valid_' Fix some ruby 1.9.2 issues. (#3) Provide documentation for remaining functions. (#3) Apply missing documentation to more functions. Remove rand. Some improvements to values_at tests. (#1) provide some more detailed tests for a number of functions. Removed date stub since this functinality is available in strftime anyway. (#2) fix is_string finally so it also makes sure numbers return false. (#2) unstub is_valid_domain_name Added doc strings for first five functions Removed join_with_prefix. (#2) unstub is_valid_mac_address. Allow sort for strings. Count functionality overlaps with size - so removing it. Removed crontab functions instead of unstubbing them. Removed load_variables. load_yaml is sufficient to solve this problem on its own. Remove is_valid_netmask instead of unstubbing. Doesn't seem like a sensible function on its own. (#2) unstub is_numeric function. ...
Diffstat (limited to 'spec/lib/puppet_spec')
-rwxr-xr-xspec/lib/puppet_spec/files.rb53
-rwxr-xr-xspec/lib/puppet_spec/fixtures.rb28
-rw-r--r--spec/lib/puppet_spec/matchers.rb87
-rwxr-xr-xspec/lib/puppet_spec/verbose.rb9
4 files changed, 177 insertions, 0 deletions
diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb
new file mode 100755
index 0000000..30fb4fc
--- /dev/null
+++ b/spec/lib/puppet_spec/files.rb
@@ -0,0 +1,53 @@
+require 'fileutils'
+require 'tempfile'
+
+# A support module for testing files.
+module PuppetSpec::Files
+ # This code exists only to support tests that run as root, pretty much.
+ # Once they have finally been eliminated this can all go... --daniel 2011-04-08
+ if Puppet.features.posix? then
+ def self.in_tmp(path)
+ path =~ /^\/tmp/ or path =~ /^\/var\/folders/
+ end
+ elsif Puppet.features.microsoft_windows?
+ def self.in_tmp(path)
+ tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp"))
+ path =~ /^#{tempdir}/
+ end
+ else
+ fail "Help! Can't find in_tmp for this platform"
+ end
+
+ def self.cleanup
+ $global_tempfiles ||= []
+ while path = $global_tempfiles.pop do
+ fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
+
+ begin
+ FileUtils.rm_r path, :secure => true
+ rescue Errno::ENOENT
+ # nothing to do
+ end
+ end
+ end
+
+ def tmpfile(name)
+ # Generate a temporary file, just for the name...
+ source = Tempfile.new(name)
+ path = source.path
+ source.close!
+
+ # ...record it for cleanup,
+ $global_tempfiles ||= []
+ $global_tempfiles << File.expand_path(path)
+
+ # ...and bam.
+ path
+ end
+
+ def tmpdir(name)
+ path = tmpfile(name)
+ FileUtils.mkdir_p(path)
+ path
+ end
+end
diff --git a/spec/lib/puppet_spec/fixtures.rb b/spec/lib/puppet_spec/fixtures.rb
new file mode 100755
index 0000000..7f6bc2a
--- /dev/null
+++ b/spec/lib/puppet_spec/fixtures.rb
@@ -0,0 +1,28 @@
+module PuppetSpec::Fixtures
+ def fixtures(*rest)
+ File.join(PuppetSpec::FIXTURE_DIR, *rest)
+ end
+ def my_fixture_dir
+ callers = caller
+ while line = callers.shift do
+ next unless found = line.match(%r{/spec/(.*)_spec\.rb:})
+ return fixtures(found[1])
+ end
+ fail "sorry, I couldn't work out your path from the caller stack!"
+ end
+ def my_fixture(name)
+ file = File.join(my_fixture_dir, name)
+ unless File.readable? file then
+ fail Puppet::DevError, "fixture '#{name}' for #{my_fixture_dir} is not readable"
+ end
+ return file
+ end
+ def my_fixtures(glob = '*', flags = 0)
+ files = Dir.glob(File.join(my_fixture_dir, glob), flags)
+ unless files.length > 0 then
+ fail Puppet::DevError, "fixture '#{glob}' for #{my_fixture_dir} had no files!"
+ end
+ block_given? and files.each do |file| yield file end
+ files
+ end
+end
diff --git a/spec/lib/puppet_spec/matchers.rb b/spec/lib/puppet_spec/matchers.rb
new file mode 100644
index 0000000..77f5803
--- /dev/null
+++ b/spec/lib/puppet_spec/matchers.rb
@@ -0,0 +1,87 @@
+require 'stringio'
+
+########################################################################
+# Backward compatibility for Jenkins outdated environment.
+module RSpec
+ module Matchers
+ module BlockAliases
+ alias_method :to, :should unless method_defined? :to
+ alias_method :to_not, :should_not unless method_defined? :to_not
+ alias_method :not_to, :should_not unless method_defined? :not_to
+ end
+ end
+end
+
+
+########################################################################
+# Custom matchers...
+RSpec::Matchers.define :have_matching_element do |expected|
+ match do |actual|
+ actual.any? { |item| item =~ expected }
+ end
+end
+
+
+RSpec::Matchers.define :exit_with do |expected|
+ actual = nil
+ match do |block|
+ begin
+ block.call
+ rescue SystemExit => e
+ actual = e.status
+ end
+ actual and actual == expected
+ end
+ failure_message_for_should do |block|
+ "expected exit with code #{expected} but " +
+ (actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
+ end
+ failure_message_for_should_not do |block|
+ "expected that exit would not be called with #{expected}"
+ end
+ description do
+ "expect exit with #{expected}"
+ end
+end
+
+
+RSpec::Matchers.define :have_printed do |expected|
+ match do |block|
+ $stderr = $stdout = StringIO.new
+
+ begin
+ block.call
+ ensure
+ $stdout.rewind
+ @actual = $stdout.read
+
+ $stdout = STDOUT
+ $stderr = STDERR
+ end
+
+ if @actual then
+ case expected
+ when String
+ @actual.include? expected
+ when Regexp
+ expected.match @actual
+ else
+ raise ArgumentError, "No idea how to match a #{@actual.class.name}"
+ end
+ end
+ end
+
+ failure_message_for_should do |actual|
+ if actual.nil? then
+ "expected #{expected.inspect}, but nothing was printed"
+ else
+ "expected #{expected.inspect} to be printed; got:\n#{actual}"
+ end
+ end
+
+ description do
+ "expect #{expected.inspect} to be printed"
+ end
+
+ diffable
+end
diff --git a/spec/lib/puppet_spec/verbose.rb b/spec/lib/puppet_spec/verbose.rb
new file mode 100755
index 0000000..d9834f2
--- /dev/null
+++ b/spec/lib/puppet_spec/verbose.rb
@@ -0,0 +1,9 @@
+# Support code for running stuff with warnings disabled.
+module Kernel
+ def with_verbose_disabled
+ verbose, $VERBOSE = $VERBOSE, nil
+ result = yield
+ $VERBOSE = verbose
+ return result
+ end
+end