Skip to content

Commit 37b805e

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 37b805e

File tree

6 files changed

+96
-0
lines changed

6 files changed

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

lib/rspec-puppet/matchers.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'rspec-puppet/matchers/run'
77
require 'rspec-puppet/matchers/count_generic'
88
require 'rspec-puppet/matchers/dynamic_matchers'
9+
require 'rspec-puppet/matchers/fact_matchers'
910
require 'rspec-puppet/matchers/type_matchers'
1011
require 'rspec-puppet/matchers/allow_value'
1112
require 'rspec-puppet/matchers/raise_error'
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module RSpec::Puppet
4+
module FactMatchers
5+
extend RSpec::Matchers::DSL
6+
7+
matcher :have_value do |expected|
8+
match { |actual| actual.value == expected }
9+
end
10+
end
11+
end

spec/facts/custom_spec.rb

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