Skip to content

Commit baf75ac

Browse files
Merge pull request #19 from GabrielNagy/facter-impl-consistency
Ensure FacterImpl consistency between example groups
2 parents fa9b1ef + 614347f commit baf75ac

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/rspec-puppet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def self.current_example
4343
c.add_setting :default_node_params, :default => {}
4444
c.add_setting :default_trusted_facts, :default => {}
4545
c.add_setting :default_trusted_external_data, :default => {}
46-
c.add_setting :facter_implementation, :default => 'facter'
46+
c.add_setting :facter_implementation, :default => :facter
4747
c.add_setting :hiera_config, :default => Puppet::Util::Platform.actually_windows? ? 'c:/nul/' : '/dev/null'
4848
c.add_setting :parser, :default => 'current'
4949
c.add_setting :trusted_node_data, :default => false

lib/rspec-puppet/adapters.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,16 @@ class Adapter40 < Base
113113
#
114114
# @api private
115115
#
116-
# Set the FacterImpl constant to the given Facter implementation.
117-
# The method noops if the constant is already set
116+
# Set the FacterImpl constant to the given Facter implementation or noop
117+
# if the constant is already set. If a proc is given, it will only be
118+
# called if FacterImpl is not defined.
118119
#
119-
# @param impl [Object]
120+
# @param impl [Object, Proc] An object or a proc that implements the Facter API
120121
def set_facter_impl(impl)
121-
Object.send(:const_set, :FacterImpl, impl) unless defined? FacterImpl
122+
return if defined?(FacterImpl)
123+
124+
impl = impl.call if impl.is_a?(Proc)
125+
Object.send(:const_set, :FacterImpl, impl)
122126
end
123127

124128
def setup_puppet(example_group)
@@ -237,11 +241,13 @@ def setup_puppet(example_group)
237241
case RSpec.configuration.facter_implementation.to_sym
238242
when :rspec
239243
if supports_facter_runtime?
240-
Puppet.runtime[:facter] = proc { RSpec::Puppet::FacterTestImpl.new }
241-
set_facter_impl(Puppet.runtime[:facter])
244+
# Lazily instantiate FacterTestImpl here to optimize memory
245+
# allocation, as the proc will only be called if FacterImpl is unset
246+
set_facter_impl(proc { RSpec::Puppet::FacterTestImpl.new })
247+
Puppet.runtime[:facter] = FacterImpl
242248
else
243249
warn "Facter runtime implementations are not supported in Puppet #{Puppet.version}, continuing with facter_implementation 'facter'"
244-
RSpec.configuration.facter_implementation = 'facter'
250+
RSpec.configuration.facter_implementation = :facter
245251
set_facter_impl(Facter)
246252
end
247253
when :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)