From 8cbd7936e87dff3803e7b7fec686994ac9654b2e Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Sat, 11 Apr 2020 16:09:59 +0200 Subject: [PATCH] Memoize rspec-puppet-facts Collection of facts can be slow. By memoizing subsequent calls are spec up greatly. In puppet-nginx test collection was reduced from 1 minute 52 seconds down to 15 seconds. That has 9 calls to on_supported_os. --- lib/voxpupuli/test/spec_helper.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/voxpupuli/test/spec_helper.rb b/lib/voxpupuli/test/spec_helper.rb index 7f15ed7..ccc3518 100644 --- a/lib/voxpupuli/test/spec_helper.rb +++ b/lib/voxpupuli/test/spec_helper.rb @@ -28,6 +28,15 @@ def suggest_facter_version require 'rspec-puppet-facts' include RspecPuppetFacts +# Generating facts is slow - this memoizes the facts between multiple classes. +# Marshalling is used to get unique instances which helps when tests overrides +# facts. +FACTS_CACHE = {} +def on_supported_os(opts = {}) + result = FACTS_CACHE[opts.to_s] ||= super(opts) + Marshal.load(Marshal.dump(result)) +end + RSpec.configure do |config| config.default_facter_version = suggest_facter_version