Skip to content

Commit d8a362b

Browse files
authored
Merge pull request #46 from alexjfisher/facter_impl_dot_notation
2 parents 612d8af + 074363e commit d8a362b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/rspec-puppet/facter_impl.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ def initialize
88
end
99

1010
def value(fact_name)
11-
@facts[fact_name.to_s]
11+
begin
12+
@facts.dig(*fact_name.to_s.split('.'))
13+
rescue TypeError
14+
nil
15+
end
1216
end
1317

1418
def clear

spec/unit/facter_impl_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'int_fact' => 3,
1111
'true_fact' => true,
1212
'false_fact' => false,
13+
'os' => { 'name' => 'my_os', 'release' => { 'major' => '42' } }
1314
}
1415
end
1516

@@ -19,6 +20,7 @@
1920
facter_impl.add(:int_fact) { setcode { 3 } }
2021
facter_impl.add(:true_fact) { setcode { true } }
2122
facter_impl.add(:false_fact) { setcode { false } }
23+
facter_impl.add(:os) { setcode { { 'name' => 'my_os', 'release' => { 'major' => '42' }} } }
2224
end
2325

2426
describe 'noop methods' do
@@ -49,6 +51,28 @@
4951
it 'retrieves a fact of type FalseClass' do
5052
expect(facter_impl.value(:false_fact)).to eq(false)
5153
end
54+
55+
context 'when using dot-notation' do
56+
it 'retrieves a child fact using dot-notation' do
57+
expect(facter_impl.value('os.name')).to eq('my_os')
58+
end
59+
60+
it 'retrieves a hash child fact using dot-notation' do
61+
expect(facter_impl.value('os.release')).to eq({ 'major' => '42' })
62+
end
63+
64+
it 'retrieves a deeply nested child fact using dot-notation' do
65+
expect(facter_impl.value('os.release.major')).to eq('42')
66+
end
67+
68+
it 'returns nil if a child fact is missing' do
69+
expect(facter_impl.value('os.release.unknown_subkey')).to eq(nil)
70+
end
71+
72+
it 'returns nil if trying to lookup into a string' do
73+
expect(facter_impl.value('os.name.foo')).to eq(nil)
74+
end
75+
end
5276
end
5377

5478
describe '#to_hash' do

0 commit comments

Comments
 (0)