Skip to content

Commit

Permalink
Implement fact memoization
Browse files Browse the repository at this point in the history
Subsequent calls to on_supported_os with the same options should result
in the same facts. This can greatly speed up testing by reducing calls
to FacterDB.
  • Loading branch information
ekohl committed Sep 4, 2020
1 parent 7ef35f3 commit 297231e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 297231e

Please sign in to comment.