Skip to content
This repository was archived by the owner on Jun 19, 2020. It is now read-only.

Commit ffc83aa

Browse files
(FACT-2535) Allow interpolation of Facter.fact('fact_name') (#435)
1 parent fa6fd17 commit ffc83aa

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

lib/models/resolved_fact.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ def legacy?
2222
def core?
2323
type == :core
2424
end
25+
26+
def to_s
27+
@value.to_s
28+
end
2529
end
2630
end

spec/facter/facter_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@
166166
expect(result).to be_instance_of(Facter::ResolvedFact).and(having_attributes(value: 'Ubuntu'))
167167
end
168168

169+
it 'can be interpolated' do
170+
user_query = 'os.name'
171+
172+
allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact])
173+
allow(fact_collection_spy)
174+
.to receive(:build_fact_collection!)
175+
.with([os_fact])
176+
.and_return(fact_collection_spy)
177+
allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return('Ubuntu')
178+
# rubocop:disable Style/UnneededInterpolation
179+
expect("#{Facter.fact(user_query)}").to eq('Ubuntu')
180+
# rubocop:enable Style/UnneededInterpolation
181+
end
182+
169183
it 'return no value' do
170184
user_query = 'os.name'
171185

spec/facter/model/resolved_fact_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
it 'responds to core? method with false' do
2424
expect(resolved_fact.core?).to be(true)
2525
end
26+
27+
# rubocop:disable Style/UnneededInterpolation
28+
it 'can be interpolated' do
29+
expect("#{resolved_fact}").to eq('fact_value')
30+
end
31+
32+
it 'interpolation of nil value will be empty string' do
33+
resolved = Facter::ResolvedFact.new('fact_name', nil)
34+
expect("#{resolved}").to eq('')
35+
end
36+
# rubocop:enable Style/UnneededInterpolation
2637
end
2738

2839
context 'when is an invalid type' do

0 commit comments

Comments
 (0)