forked from rodjek/rspec-puppet
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RSpec::Puppet | ||
# This module provides support for custom facts | ||
module FactExampleGroup | ||
include RSpec::Puppet::FactMatchers | ||
|
||
def subject | ||
setup_facter | ||
Facter.fact(self.class.top_level_description) | ||
end | ||
|
||
def rspec_puppet_cleanup | ||
Facter.clear | ||
# TODO: clean LOAD_PATH again? | ||
end | ||
|
||
private | ||
|
||
# TODO: duplicates adapter | ||
def modulepath | ||
if (rspec_modulepath = RSpec.configuration.module_path) | ||
rspec_modulepath.split(File::PATH_SEPARATOR) | ||
else | ||
Puppet[:environmentpath].split(File::PATH_SEPARATOR).map do |path| | ||
File.join(path, 'fixtures', 'modules') | ||
end | ||
end | ||
end | ||
|
||
def setup_facter | ||
# TODO: duplicates RSpec::Puppet::Support.setup_puppet | ||
modulepath.map do |d| | ||
Dir["#{d}/*/lib/facter"].entries.each do |entry| | ||
$LOAD_PATH << File.expand_path(File.dirname(entry)) | ||
end | ||
end | ||
|
||
Facter.clear | ||
|
||
return unless respond_to?(:facts) | ||
|
||
allow(Facter).to receive(:value).and_call_original | ||
|
||
facts.each do |fact, value| | ||
# TODO: Facter.fact(fact).value? | ||
allow(Facter).to receive(:value).with(fact.to_sym).and_return(value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module RSpec::Puppet | ||
module FactMatchers | ||
extend RSpec::Matchers::DSL | ||
|
||
matcher :have_value do |expected| | ||
match { |actual| actual.value == expected } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'custom' do | ||
it { is_expected.not_to be_nil } | ||
it { is_expected.to have_value('bar') } | ||
|
||
context 'with overridden' do | ||
let(:facts) do | ||
{ | ||
myfact: 'set' | ||
} | ||
end | ||
|
||
it { is_expected.to have_value('foo') } | ||
end | ||
|
||
context 'with unrelated fact overridden' do | ||
let(:facts) do | ||
{ | ||
kernel: 'unix' | ||
} | ||
end | ||
|
||
it { is_expected.to have_value('bar') } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Facter.add(:custom) do | ||
setcode { Facter.value(:myfact) ? 'foo' : 'bar' } | ||
end |