Skip to content

Commit

Permalink
(feat) - add overwrite_facts option
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbreen28 committed Jul 20, 2023
1 parent c5f5fc8 commit 0121a50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ def self.with_custom_facts(os, facts)
next if fact[:options][:confine] && !fact[:options][:confine].include?(os)
next if fact[:options][:exclude] && fact[:options][:exclude].include?(os)

if fact[:value].respond_to?(:call)
# if overwrite_fact passed, replace entire fact in facts hash with values supplied
if fact[:options][:overwrite_fact]
facts[name.to_sym] = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
elsif fact[:value].respond_to?(:call)
facts = facts.deep_merge!({name.to_sym => fact[:value].call(os, facts)})
else
facts = facts.deep_merge!({name.to_sym => fact[:value]})
Expand Down
16 changes: 16 additions & 0 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,22 @@
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
end

it 'deep merges fact and values' do
add_custom_fact 'identity', { 'user' => "test_user" }
expect(subject['redhat-7-x86_64'][:identity]).to eq({
"gid"=>0,
"group"=>"root",
"privileged"=>true,
"uid"=>0,
"user"=>"test_user"
})
end

it 'overwrites fact and values' do
add_custom_fact 'identity', { 'user' => 'root' }, :overwrite_fact => true
expect(subject['redhat-7-x86_64'][:identity]).to eq({ 'user' => 'root' })
end

it 'confines a fact to a particular operating system' do
add_custom_fact 'root_home', '/root', :confine => 'redhat-7-x86_64'
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
Expand Down

0 comments on commit 0121a50

Please sign in to comment.