Skip to content

Commit

Permalink
(feat) - Add merge_facts option
Browse files Browse the repository at this point in the history
This commit adds an option to the add_custom_fact method, which allows
the user to specify whether they wish to deep_merge facts.

This allows the user to determine whether they wish to completely
overwrite a fact, or simply merge values into the existing fact in a
given fact hash.
  • Loading branch information
jordanbreen28 committed Jan 26, 2024
1 parent 05d0f65 commit c8e1111
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,28 @@ To do this, pass a lambda as the value for the custom fact. The lambda is passed
add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']}" }
```

#### Merge into existing facts

You can also supply an optional input `:merge_facts` to the `add_custom_fact` method.

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.

```ruby
add_custom_fact :identity, { 'user' => 'test_user' }, :merge_facts => true
```

Will result in a hash of the identity fact like the below:

```ruby
{
"gid"=>0,
"group"=>"root",
"privileged"=>true,
"uid"=>0,
"user"=>"test_user"
}
```

### Supplying Custom External Facts through FacterDB
Rspec-puppet-facts uses a gem called facterdb that contains many fact sets of various combinations that are pre generated. Rspec-puppet-facts queries
facterdb to pull out a specific fact set to use when testing.
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def self.with_custom_facts(os, facts)
value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
# if merge_facts passed, merge supplied facts into facts hash
if fact[:options][:merge_facts]
facts = facts.deep_merge!({key => value})
facts.deep_merge!({name => value})
else
facts[key] = value
facts[name] = value
end
end

Expand Down

0 comments on commit c8e1111

Please sign in to comment.