module PuppetlabsSpec::Fixtures

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.

Public Instance Methods

fixtures(*rest) click to toggle source

Returns the joined path of the global FIXTURE_DIR plus any path given to it

# File lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb, line 9
def fixtures(*rest)
  File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
end
my_fixture(name) click to toggle source

Given a name, returns the full path of a file from your relative fixture dir as returned by my_fixture_dir.

# File lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb, line 28
def my_fixture(name)
  file = File.join(my_fixture_dir, name)
  raise "fixture '#{name}' for #{my_fixture_dir} is not readable" unless File.readable? file

  file
end
my_fixture_dir() click to toggle source

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

# File lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb, line 16
def my_fixture_dir
  callers = caller
  while (line = callers.shift)
    next unless (found = line.match(%r{/spec/(.*)_spec\.rb:}))

    return fixtures(found[1])
  end
  raise "sorry, I couldn't work out your path from the caller stack!"
end
my_fixture_read(name) click to toggle source

Return the contents of the file using read when given a name. Uses my_fixture to work out the relative path.

# File lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb, line 37
def my_fixture_read(name)
  File.read(my_fixture(name))
end
my_fixtures(glob = '*', flags = 0, &block) click to toggle source

Provides a block mechanism for iterating across the files in your fixture area.

# File lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb, line 43
def my_fixtures(glob = '*', flags = 0, &block)
  files = Dir.glob(File.join(my_fixture_dir, glob), flags)
  raise "fixture '#{glob}' for #{my_fixture_dir} had no files!" if files.empty?

  block && files.each(&block)
  files
end