Skip to content

Commit c5c63dd

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 b478497 commit c5c63dd

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

lib/rspec-puppet/example.rb

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'rspec-puppet/support'
44
require 'rspec-puppet/example/define_example_group'
55
require 'rspec-puppet/example/class_example_group'
6+
require 'rspec-puppet/example/fact_example_group'
67
require 'rspec-puppet/example/function_example_group'
78
require 'rspec-puppet/example/host_example_group'
89
require 'rspec-puppet/example/type_example_group'
@@ -19,6 +20,7 @@ def c.rspec_puppet_include(group, type, file_path)
1920

2021
c.rspec_puppet_include RSpec::Puppet::DefineExampleGroup, :define, %w[spec defines]
2122
c.rspec_puppet_include RSpec::Puppet::ClassExampleGroup, :class, %w[spec classes]
23+
c.rspec_puppet_include RSpec::Puppet::FactExampleGroup, :define, %w[spec facts]
2224
c.rspec_puppet_include RSpec::Puppet::FunctionExampleGroup, :puppet_function, %w[spec functions]
2325
c.rspec_puppet_include RSpec::Puppet::HostExampleGroup, :host, %w[spec hosts]
2426
c.rspec_puppet_include RSpec::Puppet::TypeExampleGroup, :type, %w[spec types]
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

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