diff --git a/lib/rspec-puppet-facts.rb b/lib/rspec-puppet-facts.rb index 53118f08..4d7515b2 100644 --- a/lib/rspec-puppet-facts.rb +++ b/lib/rspec-puppet-facts.rb @@ -7,6 +7,8 @@ # module's RSpec tests by looping through all supported # OS'es and their facts data which is received from the FacterDB. module RspecPuppetFacts + FACTS_CACHE = {} + # Use the provided options or the data from the metadata.json file # to find a set of matching facts in the FacterDB. # OS names and facts can be used in the Puppet RSpec tests @@ -27,6 +29,20 @@ module RspecPuppetFacts # will be used instead of the "operatingsystem_support" section if the metadata file # even if the file is missing. def on_supported_os(opts = {}) + result = FACTS_CACHE[opts.to_s] ||= on_supported_os_implementation(opts) + + # Marshalling is used to get unique instances which is needed for test + # isolation when facts are overridden. + Marshal.load(Marshal.dump(result)) + end + + # The real implementation of on_supported_os. + # + # Generating facts is slow - this allows memoization of the facts between + # multiple calls. + # + # @api private + def on_supported_os_implementation(opts = {}) opts[:hardwaremodels] ||= ['x86_64'] opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os