Skip to content

Commit a346ced

Browse files
committed
Ensure FacterImpl consistency between example groups
Using the 'rspec' facter implementation we could sometimes get in a case where the values of FacterImpl and Puppet.runtime[:facter] diverged between example groups. Because rspec-puppet overrides facts using the FacterImpl constant (which is only set once), Puppet.runtime[:facter] would point to a different instance of FacterTestImpl with no available facts, causing calls to Puppet.runtime[:facter].value to fail. To prevent this from happening, set Puppet.runtime[:facter] to the value of FacterImpl; this way we make sure Puppet.runtime[:facter] and FacterImpl operate on the same instance of FacterTestImpl.
1 parent fa9b1ef commit a346ced

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/rspec-puppet/adapters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ def setup_puppet(example_group)
237237
case RSpec.configuration.facter_implementation.to_sym
238238
when :rspec
239239
if supports_facter_runtime?
240-
Puppet.runtime[:facter] = proc { RSpec::Puppet::FacterTestImpl.new }
241-
set_facter_impl(Puppet.runtime[:facter])
240+
set_facter_impl(RSpec::Puppet::FacterTestImpl.new)
241+
Puppet.runtime[:facter] = FacterImpl
242242
else
243243
warn "Facter runtime implementations are not supported in Puppet #{Puppet.version}, continuing with facter_implementation 'facter'"
244244
RSpec.configuration.facter_implementation = 'facter'

spec/unit/adapters_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ def context_double(options = {})
224224
expect(FacterImpl).to be_kind_of(RSpec::Puppet::FacterTestImpl)
225225
end
226226

227+
it 'ensures consistency of FacterImpl in subsequent example groups' do
228+
context = context_double
229+
230+
# Pretend that FacterImpl is already initialized from a previous example group
231+
Puppet.runtime[:facter] = RSpec::Puppet::FacterTestImpl.new
232+
Object.send(:const_set, :FacterImpl, Puppet.runtime[:facter])
233+
234+
allow(RSpec.configuration).to receive(:facter_implementation).and_return('rspec')
235+
subject.setup_puppet(context)
236+
expect(FacterImpl).to eq(Puppet.runtime[:facter])
237+
end
238+
227239
it 'raises if given an unsupported option' do
228240
context = context_double
229241
allow(RSpec.configuration).to receive(:facter_implementation).and_return('salam')

0 commit comments

Comments
 (0)