Sha256: 2831e20c82e7a78fd94b7fa2ce1918b0435a5913f0be95ea8281cefb67f8a4c2

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 KB

Contents

# 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
      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

  # 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
      raise "fixture '#{name}' for #{my_fixture_dir} is not readable"
    end
    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)
    if files.empty?
      raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
    end
    block_given? && files.each { |file| yield file }
    files
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
puppetlabs_spec_helper-2.15.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.14.1 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.14.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.13.1 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.13.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.12.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.11.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.10.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.9.1 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
puppetlabs_spec_helper-2.9.0 lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb