From 4d80729a00e5e2cb60078c61372b6a24316f79ba Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Mon, 9 Nov 2020 19:35:08 +0100 Subject: [PATCH] Collect facts iteratively Rather than getting the facts for every filter spec and then discarding that, this stores the found facts in an array and uses that. This eliminates a call to FacterDB::get_facts with a very complex filter. A quick test in puppet-nginx reduces loading of tests by about 2 seconds (from ~9 to ~7). --- lib/rspec-puppet-facts.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/rspec-puppet-facts.rb b/lib/rspec-puppet-facts.rb index 661a3d75..ef0a1872 100644 --- a/lib/rspec-puppet-facts.rb +++ b/lib/rspec-puppet-facts.rb @@ -113,14 +113,18 @@ def on_supported_os_implementation(opts = {}) end facterversion_obj = Gem::Version.new(facterversion) + received_facts = [] # FacterDB may have newer versions of facter data for which it contains a subset of all possible # facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to # cycle through and downgrade Facter versions per platform type until we find matching Facter data. filter.each do |filter_spec| - version = FacterDB.get_facts(filter_spec).map { |facts| Gem::Version.new(facts[:facterversion]) }.sort.reverse.detect { |v| v <= facterversion_obj } - next unless version - version = version.to_s + facts = FacterDB.get_facts(filter_spec) + .select { |facts| Gem::Version.new(facts[:facterversion]) <= facterversion_obj } + .max_by { |facts| Gem::Version.new(facts[:facterversion]) } + + next unless facts + version = facts[:facterversion] unless version == facterversion if RspecPuppetFacts.spec_facts_strict? @@ -130,10 +134,9 @@ def on_supported_os_implementation(opts = {}) RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, using v#{version} instead" end - filter_spec[:facterversion] = "/\\A#{Regexp.escape(version)}/" + received_facts << facts end - received_facts = FacterDB::get_facts(filter) unless received_facts.any? RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}" return {}