diff --git a/.sync.yml b/.sync.yml index fdf310f0c..0034c7421 100644 --- a/.sync.yml +++ b/.sync.yml @@ -32,3 +32,14 @@ spec/default_facts.yml: - 2.5.0 vm_guests: - invalid_existing_guest +spec/spec_helper.rb: + spec_overrides: + - "module DefaultHieraConfig" + - " extend RSpec::SharedContext" + - " let(:hiera_config) { 'spec/fixtures/hiera/default_config.yaml' }" + - "end" + - "" + - "RSpec.configure do |c|" + - " c.default_facts = default_facts" + - " c.include DefaultHieraConfig" + - "end " # trailing space prevents PDK from skipping this line diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a538290a0..fc145a864 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,9 +8,10 @@ require 'puppetlabs_spec_helper/module_spec_helper' require 'rspec-puppet-utils' require 'rspec-puppet-facts' -require 'faker' include RspecPuppetFacts +require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) + default_facts = { puppetversion: Puppet.version, facterversion: Facter.version, @@ -33,6 +34,7 @@ default_facts.merge!(YAML.safe_load(File.read(default_module_facts_path))) end +# 'spec_overrides' from sync.yml will appear below this line module DefaultHieraConfig extend RSpec::SharedContext let(:hiera_config) { 'spec/fixtures/hiera/default_config.yaml' } @@ -41,64 +43,4 @@ module DefaultHieraConfig RSpec.configure do |c| c.default_facts = default_facts c.include DefaultHieraConfig -end - -# Stub the default dependency loader to do normal resolution except where overridden -def stub_loader! - allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).and_call_original -end - -# Stub the dependency loader to resolve a named function to a double of some type -# -# @param name [String] The name of the puppet function to stub -# @param dbl [RSpec double] Optional RSpeck double with `call` mocked; -# will be used instead of the block if supplied -# @yield [*args] A block that takes all parameters given to the puppet function; -# required if dbl is not supplied, and ignored if dbl is supplied -def stub_function(name, dbl = nil, &func) - func = dbl || func - stub = ->(_scope, *args, &block) do - func.call(*args, &block) - end - allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).with(:function, name).and_return(stub) -end - -def stub_with_call(name) - double(name).tap do |dbl| - dbl.define_singleton_method(:allow_call) do - allow(dbl).to receive(:call) - end - stub_function(name, dbl) - end -end - -def stub(name) - dbl = double(name) - - yield dbl - stub_function(name, dbl) -end - -def allow_call(dbl) - allow(dbl).to receive(:call) -end - -class Hash - # deep_merge copied with minor adaptation from: - # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/deep_mergeable.rb - def deep_merge(other, &block) - dup.deep_merge!(other, &block) - end - - def deep_merge!(other, &block) - merge!(other) do |key, this_val, other_val| - if this_val.is_a?(Hash) && other_val.is_a?(Hash) - this_val.deep_merge(other_val, &block) - elsif block_given? - block.call(key, this_val, other_val) - else - other_val - end - end - end -end +end diff --git a/spec/spec_helper/deep_merge.rb b/spec/spec_helper/deep_merge.rb new file mode 100644 index 000000000..ed4a0cb0e --- /dev/null +++ b/spec/spec_helper/deep_merge.rb @@ -0,0 +1,19 @@ +class Hash + # deep_merge copied with minor adaptation from: + # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/deep_mergeable.rb + def deep_merge(other, &block) + dup.deep_merge!(other, &block) + end + + def deep_merge!(other, &block) + merge!(other) do |key, this_val, other_val| + if this_val.is_a?(Hash) && other_val.is_a?(Hash) + this_val.deep_merge(other_val, &block) + elsif block_given? + block.call(key, this_val, other_val) + else + other_val + end + end + end +end diff --git a/spec/spec_helper/stub_loader.rb b/spec/spec_helper/stub_loader.rb new file mode 100644 index 000000000..1d0911a5d --- /dev/null +++ b/spec/spec_helper/stub_loader.rb @@ -0,0 +1,30 @@ +# Stub the default dependency loader to do normal resolution except where overridden +def stub_loader! + allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).and_call_original +end + +# Stub the dependency loader to resolve a named function to a double of some type +# +# @param name [String] The name of the puppet function to stub +# @param dbl [RSpec double] Optional RSpec double with `call` mocked; +# will be used instead of the block if supplied +# @yield [*args] A block that takes all parameters given to the puppet function; +# required if dbl is not supplied, and ignored if dbl is supplied +def stub_function(name, dbl = nil, &func) + func = dbl || func + stub = ->(_scope, *args, &block) do + func.call(*args, &block) + end + allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).with(:function, name).and_return(stub) +end + +def stub(name) + dbl = double(name) + + yield dbl + stub_function(name, dbl) +end + +def allow_call(dbl) + allow(dbl).to receive(:call) +end diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb new file mode 100644 index 000000000..0b4e553e2 --- /dev/null +++ b/spec/spec_helper_local.rb @@ -0,0 +1,3 @@ +require 'faker' +require 'spec_helper/deep_merge' +require 'spec_helper/stub_loader'