Skip to content

Commit 2655b2e

Browse files
author
jordanbreen28
committed
(feat) - Add merge_facts option
1 parent 977acc2 commit 2655b2e

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ To do this, pass a lambda as the value for the custom fact. The lambda is passed
443443
add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']}" }
444444
```
445445

446+
#### Merge into existing facts
447+
448+
You can also supply an optional input `:merge_facts` to the `add_custom_fact` method.
449+
450+
This allows you to merge facts values into a fact, if the fact is already present in the facts hash as oppose to overwriting the fact value.
451+
452+
```ruby
453+
add_custom_fact 'identity', { 'user' => "test_user" }, :merge_facts => true
454+
```
455+
456+
Will result in a hash of the identity fact like the below:
457+
458+
```ruby
459+
{
460+
"gid"=>0,
461+
"group"=>"root",
462+
"privileged"=>true,
463+
"uid"=>0,
464+
"user"=>"test_user"
465+
}
466+
```
467+
446468
### Supplying Custom External Facts through FacterDB
447469
Rspec-puppet-facts uses a gem called facterdb that contains many fact sets of various combinations that are pre generated. Rspec-puppet-facts queries
448470
facterdb to pull out a specific fact set to use when testing.

lib/rspec-puppet-facts.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,13 @@ def self.with_custom_facts(os, facts)
229229
next if fact[:options][:confine] && !fact[:options][:confine].include?(os)
230230
next if fact[:options][:exclude] && fact[:options][:exclude].include?(os)
231231

232-
if fact[:value].respond_to?(:call)
233-
facts = facts.deep_merge!({name.to_sym => fact[:value].call(os, facts)})
232+
key = name.to_sym
233+
value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
234+
# if merge_facts passed, merge supplied facts into facts hash
235+
if fact[:options][:merge_facts]
236+
facts = facts.deep_merge!({key => value})
234237
else
235-
facts = facts.deep_merge!({name.to_sym => fact[:value]})
238+
facts[key] = value
236239
end
237240
end
238241

spec/rspec_puppet_facts_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,22 @@
904904
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
905905
end
906906

907+
it 'deep merges fact and values' do
908+
add_custom_fact 'identity', { 'user' => "test_user" }, :merge_facts => true
909+
expect(subject['redhat-7-x86_64'][:identity]).to eq({
910+
"gid"=>0,
911+
"group"=>"root",
912+
"privileged"=>true,
913+
"uid"=>0,
914+
"user"=>"test_user"
915+
})
916+
end
917+
918+
it 'overwrites fact and values' do
919+
add_custom_fact 'identity', { 'user' => 'root' }
920+
expect(subject['redhat-7-x86_64'][:identity]).to eq({ 'user' => 'root' })
921+
end
922+
907923
it 'confines a fact to a particular operating system' do
908924
add_custom_fact 'root_home', '/root', :confine => 'redhat-7-x86_64'
909925
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'

0 commit comments

Comments
 (0)