Skip to content

Commit

Permalink
Collect facts iteratively
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ekohl committed Jul 4, 2023
1 parent 35f5209 commit 0c9397f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,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 }

Check failure on line 124 in lib/rspec-puppet-facts.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - `facts`.
.max_by { |facts| Gem::Version.new(facts[:facterversion]) }

Check failure on line 125 in lib/rspec-puppet-facts.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - `facts`.

next unless facts
version = facts[:facterversion]

unless version == facterversion
if RspecPuppetFacts.spec_facts_strict?
Expand All @@ -131,10 +135,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 {}
Expand Down

0 comments on commit 0c9397f

Please sign in to comment.