From 828b17122f84444f5a389e9908992dc1fa7afa9c Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Wed, 19 Jul 2023 13:58:56 +0100 Subject: [PATCH] (maint) - Add deep_merge gem dependency --- lib/rspec-puppet-facts.rb | 13 +++---------- rspec-puppet-facts.gemspec | 1 + 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/rspec-puppet-facts.rb b/lib/rspec-puppet-facts.rb index 0effa154..2d9e1e48 100644 --- a/lib/rspec-puppet-facts.rb +++ b/lib/rspec-puppet-facts.rb @@ -2,6 +2,7 @@ require 'facter' require 'facterdb' require 'json' +require 'deep_merge' # The purpose of this module is to simplify the Puppet # module's RSpec tests by looping through all supported @@ -229,9 +230,9 @@ def self.with_custom_facts(os, facts) next if fact[:options][:exclude] && fact[:options][:exclude].include?(os) if fact[:value].respond_to?(:call) - facts = facts.deep_merge({name.to_sym => fact[:value].call(os, facts)}) + facts = facts.deep_merge!({name.to_sym => fact[:value].call(os, facts)}) else - facts = facts.deep_merge({name.to_sym => fact[:value]}) + facts = facts.deep_merge!({name.to_sym => fact[:value]}) end end @@ -408,11 +409,3 @@ def self.facter_version_for_puppet_version(puppet_version) :default => RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version) c.add_setting :facterdb_string_keys, :default => false end - -# monkey patch the Hash Class to add deep_merge -class ::Hash - def deep_merge(second) - merger = proc { |_, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 } - merge(second.to_h, &merger) - end -end diff --git a/rspec-puppet-facts.gemspec b/rspec-puppet-facts.gemspec index 6698f7a7..97bddcb5 100644 --- a/rspec-puppet-facts.gemspec +++ b/rspec-puppet-facts.gemspec @@ -26,6 +26,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'voxpupuli-rubocop', '~> 2.0' s.add_runtime_dependency 'facter' + s.add_runtime_dependency 'deep_merge', '~> 1.2' s.add_runtime_dependency 'facterdb', '>= 0.5.0' s.add_runtime_dependency 'puppet' end