Skip to content

Commit

Permalink
prep spec_helper.rb for PDK upgrade
Browse files Browse the repository at this point in the history
Move spec_helper customizations to spec_helper_local and .sync.yml.
  • Loading branch information
rrotter committed Dec 13, 2024
1 parent 18cd87c commit 7144e4b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 62 deletions.
11 changes: 11 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 4 additions & 62 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' }
Expand All @@ -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
19 changes: 19 additions & 0 deletions spec/spec_helper/deep_merge.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions spec/spec_helper/stub_loader.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'faker'
require 'spec_helper/deep_merge'
require 'spec_helper/stub_loader'

0 comments on commit 7144e4b

Please sign in to comment.