diff --git a/lib/rspec-puppet/facter_impl.rb b/lib/rspec-puppet/facter_impl.rb index d37a7518e..f1ecfd225 100644 --- a/lib/rspec-puppet/facter_impl.rb +++ b/lib/rspec-puppet/facter_impl.rb @@ -8,7 +8,13 @@ def initialize end def value(fact_name) - @facts[fact_name.to_s] + fact_name.to_s.split('.').reduce(@facts) do |sub_hash, key| + if sub_hash.is_a?(Hash) && sub_hash.key?(key) + sub_hash[key] + else + return nil + end + end end def clear diff --git a/spec/unit/facter_impl_spec.rb b/spec/unit/facter_impl_spec.rb index 3f6ef321c..22e55ae92 100644 --- a/spec/unit/facter_impl_spec.rb +++ b/spec/unit/facter_impl_spec.rb @@ -10,6 +10,7 @@ 'int_fact' => 3, 'true_fact' => true, 'false_fact' => false, + 'os' => { 'name' => 'my_os', 'release' => { 'major' => '42' } } } end @@ -19,6 +20,7 @@ facter_impl.add(:int_fact) { setcode { 3 } } facter_impl.add(:true_fact) { setcode { true } } facter_impl.add(:false_fact) { setcode { false } } + facter_impl.add(:os) { setcode { { 'name' => 'my_os', 'release' => { 'major' => '42' }} } } end describe 'noop methods' do @@ -49,6 +51,24 @@ it 'retrieves a fact of type FalseClass' do expect(facter_impl.value(:false_fact)).to eq(false) end + + context 'when using dot-notation' do + it 'retrieves a child fact using dot-notation' do + expect(facter_impl.value('os.name')).to eq('my_os') + end + + it 'retrieves a hash child fact using dot-notation' do + expect(facter_impl.value('os.release')).to eq({ 'major' => '42' }) + end + + it 'retrieves a deeply nested child fact using dot-notation' do + expect(facter_impl.value('os.release.major')).to eq('42') + end + + it 'returns nil if a child fact is missing' do + expect(facter_impl.value('os.release.unknown_subkey')).to eq(nil) + end + end end describe '#to_hash' do