@@ -113,12 +113,16 @@ class Adapter40 < Base
113
113
#
114
114
# @api private
115
115
#
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.
118
119
#
119
- # @param impl [Object]
120
+ # @param impl [Object, Proc] An object or a proc that implements the Facter API
120
121
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 )
122
126
end
123
127
124
128
def setup_puppet ( example_group )
@@ -237,7 +241,9 @@ def setup_puppet(example_group)
237
241
case RSpec . configuration . facter_implementation . to_sym
238
242
when :rspec
239
243
if supports_facter_runtime?
240
- set_facter_impl ( RSpec ::Puppet ::FacterTestImpl . new )
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 } )
241
247
Puppet . runtime [ :facter ] = FacterImpl
242
248
else
243
249
warn "Facter runtime implementations are not supported in Puppet #{ Puppet . version } , continuing with facter_implementation 'facter'"
0 commit comments