Skip to content

Commit

Permalink
Merge pull request rodjek#19 from GabrielNagy/facter-impl-consistency
Browse files Browse the repository at this point in the history
Ensure FacterImpl consistency between example groups
  • Loading branch information
ciprianbadescu authored Dec 10, 2021
2 parents fa9b1ef + 614347f commit baf75ac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/rspec-puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.current_example
c.add_setting :default_node_params, :default => {}
c.add_setting :default_trusted_facts, :default => {}
c.add_setting :default_trusted_external_data, :default => {}
c.add_setting :facter_implementation, :default => 'facter'
c.add_setting :facter_implementation, :default => :facter
c.add_setting :hiera_config, :default => Puppet::Util::Platform.actually_windows? ? 'c:/nul/' : '/dev/null'
c.add_setting :parser, :default => 'current'
c.add_setting :trusted_node_data, :default => false
Expand Down
20 changes: 13 additions & 7 deletions lib/rspec-puppet/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ class Adapter40 < Base
#
# @api private
#
# Set the FacterImpl constant to the given Facter implementation.
# The method noops if the constant is already set
# Set the FacterImpl constant to the given Facter implementation or noop
# if the constant is already set. If a proc is given, it will only be
# called if FacterImpl is not defined.
#
# @param impl [Object]
# @param impl [Object, Proc] An object or a proc that implements the Facter API
def set_facter_impl(impl)
Object.send(:const_set, :FacterImpl, impl) unless defined? FacterImpl
return if defined?(FacterImpl)

impl = impl.call if impl.is_a?(Proc)
Object.send(:const_set, :FacterImpl, impl)
end

def setup_puppet(example_group)
Expand Down Expand Up @@ -237,11 +241,13 @@ def setup_puppet(example_group)
case RSpec.configuration.facter_implementation.to_sym
when :rspec
if supports_facter_runtime?
Puppet.runtime[:facter] = proc { RSpec::Puppet::FacterTestImpl.new }
set_facter_impl(Puppet.runtime[:facter])
# Lazily instantiate FacterTestImpl here to optimize memory
# allocation, as the proc will only be called if FacterImpl is unset
set_facter_impl(proc { RSpec::Puppet::FacterTestImpl.new })
Puppet.runtime[:facter] = FacterImpl
else
warn "Facter runtime implementations are not supported in Puppet #{Puppet.version}, continuing with facter_implementation 'facter'"
RSpec.configuration.facter_implementation = 'facter'
RSpec.configuration.facter_implementation = :facter
set_facter_impl(Facter)
end
when :facter
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/adapters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ def context_double(options = {})
expect(FacterImpl).to be_kind_of(RSpec::Puppet::FacterTestImpl)
end

it 'ensures consistency of FacterImpl in subsequent example groups' do
context = context_double

# Pretend that FacterImpl is already initialized from a previous example group
Puppet.runtime[:facter] = RSpec::Puppet::FacterTestImpl.new
Object.send(:const_set, :FacterImpl, Puppet.runtime[:facter])

allow(RSpec.configuration).to receive(:facter_implementation).and_return('rspec')
subject.setup_puppet(context)
expect(FacterImpl).to eq(Puppet.runtime[:facter])
end

it 'raises if given an unsupported option' do
context = context_double
allow(RSpec.configuration).to receive(:facter_implementation).and_return('salam')
Expand Down

0 comments on commit baf75ac

Please sign in to comment.