aboutsummaryrefslogtreecommitdiff
path: root/spec/puppetlabs_spec/fixtures.rb
diff options
context:
space:
mode:
authorNan Liu <nan.liu@gmail.com>2012-05-01 16:20:12 -0700
committerNan Liu <nan.liu@gmail.com>2012-05-01 16:20:12 -0700
commit9bd4362615994e0025d220bfb34609a689720d2b (patch)
tree2a4f1450709f3559bf2b6cad95c7efe04011161b /spec/puppetlabs_spec/fixtures.rb
parentdf6c470fd39e6e4e92968d414ae9021b4ec67205 (diff)
parentc83ffff9493443845c1545240f75d8136c0ecdff (diff)
downloadpuppet-tftp-9bd4362615994e0025d220bfb34609a689720d2b.tar.gz
puppet-tftp-9bd4362615994e0025d220bfb34609a689720d2b.tar.bz2
Merge pull request #1 from nanliu/master
Add support files for initial module release.
Diffstat (limited to 'spec/puppetlabs_spec/fixtures.rb')
-rw-r--r--spec/puppetlabs_spec/fixtures.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/puppetlabs_spec/fixtures.rb b/spec/puppetlabs_spec/fixtures.rb
new file mode 100644
index 0000000..9ce0a6b
--- /dev/null
+++ b/spec/puppetlabs_spec/fixtures.rb
@@ -0,0 +1,49 @@
+# This module provides some helper methods to assist with fixtures. It's
+# methods are designed to help when you have a conforming fixture layout so we
+# get project consistency.
+module PuppetlabsSpec::Fixtures
+
+ # Returns the joined path of the global FIXTURE_DIR plus any path given to it
+ def fixtures(*rest)
+ File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
+ end
+
+ # Returns the path to your relative fixture dir. So if your spec test is
+ # <project>/spec/unit/facter/foo_spec.rb then your relative dir will be
+ # <project>/spec/fixture/unit/facter/foo
+ 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
+
+ # Given a name, returns the full path of a file from your relative fixture
+ # dir as returned by my_fixture_dir.
+ def my_fixture(name)
+ file = File.join(my_fixture_dir, name)
+ unless File.readable? file then
+ fail "fixture '#{name}' for #{my_fixture_dir} is not readable"
+ end
+ return file
+ end
+
+ # Return the contents of the file using read when given a name. Uses
+ # my_fixture to work out the relative path.
+ def my_fixture_read(name)
+ File.read(my_fixture(name))
+ end
+
+ # Provides a block mechanism for iterating across the files in your fixture
+ # area.
+ def my_fixtures(glob = '*', flags = 0)
+ files = Dir.glob(File.join(my_fixture_dir, glob), flags)
+ unless files.length > 0 then
+ fail "fixture '#{glob}' for #{my_fixture_dir} had no files!"
+ end
+ block_given? and files.each do |file| yield file end
+ files
+ end
+end