@@ -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
0 commit comments