Skip to content

Commit cafb92b

Browse files
committed
Lazily instantiate FacterTestImpl
1 parent a346ced commit cafb92b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/rspec-puppet/adapters.rb

Lines changed: 11 additions & 5 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,7 +241,9 @@ def setup_puppet(example_group)
237241
case RSpec.configuration.facter_implementation.to_sym
238242
when :rspec
239243
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 })
241247
Puppet.runtime[:facter] = FacterImpl
242248
else
243249
warn "Facter runtime implementations are not supported in Puppet #{Puppet.version}, continuing with facter_implementation 'facter'"

0 commit comments

Comments
 (0)