Skip to content

Commit

Permalink
(bug) - Prevent duplicated fact keys
Browse files Browse the repository at this point in the history
Previously, when registering a custom fact with rspec-puppet-facts, the
fact key would be converted to a string. This lead to duplicate facts
being present in the facts hash, as default fact keys are stored as sym.

To fix this, we remove a `to_s` conversion on the key in the
register_custom_fact method, unless the rspec config stringify keys is true.
  • Loading branch information
jordanbreen28 committed Jan 29, 2024
1 parent edb6b22 commit 7f83f77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def add_custom_fact(name, value, options = {})
# @api private
def self.register_custom_fact(name, value, options)
@custom_facts ||= {}
@custom_facts[name.to_s] = {:options => options, :value => value}
name = RSpec.configuration.facterdb_string_keys ? name.to_s : name.to_sym
@custom_facts[name] = {:options => options, :value => value}
end

# Adds any custom facts according to the rules defined for the operating
Expand Down
22 changes: 16 additions & 6 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -901,24 +901,34 @@

it 'adds a simple fact and value' do
add_custom_fact 'root_home', '/root'
expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
end

it 'overwrites fact and values' do
add_custom_fact :identity, { 'user' => 'root' }
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'
expect(subject['redhat-6-x86_64']['root_home']).to be_nil
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
expect(subject['redhat-6-x86_64'][:root_home]).to be_nil
end

it 'excludes a fact from a particular operating system' do
add_custom_fact 'root_home', '/root', :exclude => 'redhat-7-x86_64'
expect(subject['redhat-7-x86_64']['root_home']).to be_nil
expect(subject['redhat-6-x86_64']['root_home']).to eq '/root'
expect(subject['redhat-7-x86_64'][:root_home]).to be_nil
expect(subject['redhat-6-x86_64'][:root_home]).to eq '/root'
end

it 'takes a proc as a value' do
add_custom_fact 'root_home', ->(_os, _facts) { '/root' }
expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
end

it 'accepts sym key and stores as sym' do
add_custom_fact :root_home, ->(_os, _facts) { '/root' }
expect(subject['redhat-7-x86_64'][:root_home]).to eq '/root'
end
end

Expand Down

0 comments on commit 7f83f77

Please sign in to comment.