Module: PuppetlabsSpec::Fixtures

Defined in:
lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#fixtures(*rest) ⇒ Object

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



6
7
8
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', line 6

def fixtures(*rest)
  File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
end

#my_fixture(name) ⇒ Object

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



24
25
26
27
28
29
30
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', line 24

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

#my_fixture_dirObject

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



13
14
15
16
17
18
19
20
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', line 13

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) ⇒ Object

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



34
35
36
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', line 34

def my_fixture_read(name)
  File.read(my_fixture(name))
end

#my_fixtures(glob = '*', flags = 0) ⇒ Object

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



40
41
42
43
44
45
46
47
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', line 40

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