Skip to content

Commit a88d275

Browse files
committed
Add infrastructure for testing custom facts
This adds testing of custom facts by placing them in spec/facts, similar to how classes have spec/classes and defines have spec/defines. The subject is also set in the same way. It is possible to stub other facts using a facts block and facts are properly cleared before and after a run.
1 parent 8532f22 commit a88d275

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

Diff for: lib/rspec-puppet/example.rb

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'rspec-puppet/example/type_alias_example_group'
88
require 'rspec-puppet/example/provider_example_group'
99
require 'rspec-puppet/example/application_example_group'
10+
require 'rspec-puppet/example/fact_example_group'
1011

1112
RSpec::configure do |c|
1213

@@ -27,6 +28,7 @@ def c.rspec_puppet_include(group, type, file_path)
2728
c.rspec_puppet_include RSpec::Puppet::TypeAliasExampleGroup, :type_alias, %w[spec type_aliases]
2829
c.rspec_puppet_include RSpec::Puppet::ProviderExampleGroup, :provider, %w[spec providers]
2930
c.rspec_puppet_include RSpec::Puppet::ApplicationExampleGroup, :application, %w[spec applications]
31+
c.rspec_puppet_include RSpec::Puppet::FactExampleGroup, :define, %w[spec facts]
3032

3133
# Hook for each example group type to remove any caches or instance variables, since they will remain
3234
# and cause a memory leak. Can't be assigned per type by :file_path, so check for its presence.

Diff for: lib/rspec-puppet/example/fact_example_group.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module RSpec::Puppet
2+
# This module provides support for custom facts
3+
module FactExampleGroup
4+
def subject
5+
setup_facter do
6+
Facter.fact(self.class.top_level_description)
7+
end
8+
end
9+
10+
# TODO: proper matcher with a description
11+
def have_value(value)
12+
have_attributes(value: value)
13+
end
14+
15+
def rspec_puppet_cleanup
16+
Facter.clear
17+
# TODO: clean LOAD_PATH again?
18+
end
19+
20+
private
21+
22+
# TODO: duplicates adapter
23+
def modulepath
24+
if rspec_modulepath = RSpec.configuration.module_path
25+
rspec_modulepath.split(File::PATH_SEPARATOR)
26+
else
27+
Puppet[:environmentpath].split(File::PATH_SEPARATOR).map do |path|
28+
File.join(path, 'fixtures', 'modules')
29+
end
30+
end
31+
end
32+
33+
def setup_facter
34+
Dir.mktmpdir do |dir|
35+
modulepath.map do |d|
36+
Dir["#{d}/*/lib/facter"].entries.each do |entry|
37+
$LOAD_PATH << File.expand_path(File.dirname(entry))
38+
end
39+
end
40+
41+
Facter.clear
42+
43+
if respond_to?(:facts)
44+
allow(Facter).to receive(:value).and_call_original
45+
46+
facts.each do |fact, value|
47+
# TODO: Facter.fact(fact).value?
48+
allow(Facter).to receive(:value).with(fact.to_sym).and_return(value)
49+
end
50+
end
51+
52+
yield
53+
end
54+
end
55+
end
56+
end

Diff for: spec/facts/custom_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper'
2+
3+
describe 'custom' do
4+
it { is_expected.not_to be_nil }
5+
it { is_expected.to have_value('bar') }
6+
7+
context 'with overridden' do
8+
let(:facts) do
9+
{
10+
myfact: 'set',
11+
}
12+
end
13+
14+
it { is_expected.to have_value('foo') }
15+
end
16+
17+
context 'with unrelated fact overridden' do
18+
let(:facts) do
19+
{
20+
kernel: 'unix',
21+
}
22+
end
23+
24+
it { is_expected.to have_value('bar') }
25+
end
26+
end
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Facter.add(:custom) do
2+
setcode { Facter.value(:myfact) ? 'foo' : 'bar' }
3+
end

0 commit comments

Comments
 (0)