|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -# This module provides some helper methods to assist with fixtures. It's |
4 |
| -# methods are designed to help when you have a conforming fixture layout so we |
5 |
| -# get project consistency. |
6 |
| -module PuppetlabsSpec::Fixtures |
7 |
| - # Returns the joined path of the global FIXTURE_DIR plus any path given to it |
8 |
| - def fixtures(*rest) |
9 |
| - File.join(PuppetlabsSpec::FIXTURE_DIR, *rest) |
10 |
| - end |
11 |
| - |
12 |
| - # Returns the path to your relative fixture dir. So if your spec test is |
13 |
| - # <project>/spec/unit/facter/foo_spec.rb then your relative dir will be |
14 |
| - # <project>/spec/fixture/unit/facter/foo |
15 |
| - def my_fixture_dir |
16 |
| - callers = caller |
17 |
| - while (line = callers.shift) |
18 |
| - next unless (found = line.match(%r{/spec/(.*)_spec\.rb:})) |
19 |
| - |
20 |
| - return fixtures(found[1]) |
| 3 | +module PuppetlabsSpec |
| 4 | + # This module provides some helper methods to assist with fixtures. It's |
| 5 | + # methods are designed to help when you have a conforming fixture layout so we |
| 6 | + # get project consistency. |
| 7 | + module Fixtures |
| 8 | + # Returns the joined path of the global FIXTURE_DIR plus any path given to it |
| 9 | + def fixtures(*rest) |
| 10 | + File.join(PuppetlabsSpec::FIXTURE_DIR, *rest) |
21 | 11 | end
|
22 |
| - raise "sorry, I couldn't work out your path from the caller stack!" |
23 |
| - end |
24 | 12 |
|
25 |
| - # Given a name, returns the full path of a file from your relative fixture |
26 |
| - # dir as returned by my_fixture_dir. |
27 |
| - def my_fixture(name) |
28 |
| - file = File.join(my_fixture_dir, name) |
29 |
| - unless File.readable? file |
30 |
| - raise "fixture '#{name}' for #{my_fixture_dir} is not readable" |
| 13 | + # Returns the path to your relative fixture dir. So if your spec test is |
| 14 | + # <project>/spec/unit/facter/foo_spec.rb then your relative dir will be |
| 15 | + # <project>/spec/fixture/unit/facter/foo |
| 16 | + def my_fixture_dir |
| 17 | + callers = caller |
| 18 | + while (line = callers.shift) |
| 19 | + next unless (found = line.match(%r{/spec/(.*)_spec\.rb:})) |
| 20 | + |
| 21 | + return fixtures(found[1]) |
| 22 | + end |
| 23 | + raise "sorry, I couldn't work out your path from the caller stack!" |
31 | 24 | end
|
32 | 25 |
|
33 |
| - file |
34 |
| - end |
| 26 | + # Given a name, returns the full path of a file from your relative fixture |
| 27 | + # dir as returned by my_fixture_dir. |
| 28 | + def my_fixture(name) |
| 29 | + file = File.join(my_fixture_dir, name) |
| 30 | + unless File.readable? file |
| 31 | + raise "fixture '#{name}' for #{my_fixture_dir} is not readable" |
| 32 | + end |
35 | 33 |
|
36 |
| - # Return the contents of the file using read when given a name. Uses |
37 |
| - # my_fixture to work out the relative path. |
38 |
| - def my_fixture_read(name) |
39 |
| - File.read(my_fixture(name)) |
40 |
| - end |
| 34 | + file |
| 35 | + end |
41 | 36 |
|
42 |
| - # Provides a block mechanism for iterating across the files in your fixture |
43 |
| - # area. |
44 |
| - def my_fixtures(glob = '*', flags = 0, &block) |
45 |
| - files = Dir.glob(File.join(my_fixture_dir, glob), flags) |
46 |
| - if files.empty? |
47 |
| - raise "fixture '#{glob}' for #{my_fixture_dir} had no files!" |
| 37 | + # Return the contents of the file using read when given a name. Uses |
| 38 | + # my_fixture to work out the relative path. |
| 39 | + def my_fixture_read(name) |
| 40 | + File.read(my_fixture(name)) |
48 | 41 | end
|
49 | 42 |
|
50 |
| - block && files.each(&block) |
51 |
| - files |
| 43 | + # Provides a block mechanism for iterating across the files in your fixture |
| 44 | + # area. |
| 45 | + def my_fixtures(glob = '*', flags = 0, &block) |
| 46 | + files = Dir.glob(File.join(my_fixture_dir, glob), flags) |
| 47 | + if files.empty? |
| 48 | + raise "fixture '#{glob}' for #{my_fixture_dir} had no files!" |
| 49 | + end |
| 50 | + |
| 51 | + block && files.each(&block) |
| 52 | + files |
| 53 | + end |
52 | 54 | end
|
53 | 55 | end
|
0 commit comments