Release prep v9.1.0
[puppet-modules/puppetlabs-apt.git] / spec / spec_helper.rb
1 # frozen_string_literal: true
2
3 RSpec.configure do |c|
4   c.mock_with :rspec
5 end
6
7 require 'puppetlabs_spec_helper/module_spec_helper'
8 require 'rspec-puppet-facts'
9
10 require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb'))
11
12 include RspecPuppetFacts
13
14 default_facts = {
15   puppetversion: Puppet.version,
16   facterversion: Facter.version,
17 }
18
19 default_fact_files = [
20   File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')),
21   File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')),
22 ]
23
24 default_fact_files.each do |f|
25   next unless File.exist?(f) && File.readable?(f) && File.size?(f)
26
27   begin
28     default_facts.merge!(YAML.safe_load(File.read(f), permitted_classes: [], permitted_symbols: [], aliases: true))
29   rescue StandardError => e
30     RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}"
31   end
32 end
33
34 # read default_facts and merge them over what is provided by facterdb
35 default_facts.each do |fact, value|
36   add_custom_fact fact, value
37 end
38
39 RSpec.configure do |c|
40   c.default_facts = default_facts
41   c.before :each do
42     # set to strictest setting for testing
43     # by default Puppet runs at warning level
44     Puppet.settings[:strict] = :warning
45     Puppet.settings[:strict_variables] = true
46   end
47   c.filter_run_excluding(bolt: true) unless ENV['GEM_BOLT']
48   c.after(:suite) do
49     RSpec::Puppet::Coverage.report!(0)
50   end
51
52   # Filter backtrace noise
53   backtrace_exclusion_patterns = [
54     %r{spec_helper},
55     %r{gems},
56   ]
57
58   if c.respond_to?(:backtrace_exclusion_patterns)
59     c.backtrace_exclusion_patterns = backtrace_exclusion_patterns
60   elsif c.respond_to?(:backtrace_clean_patterns)
61     c.backtrace_clean_patterns = backtrace_exclusion_patterns
62   end
63 end
64
65 # Ensures that a module is defined
66 # @param module_name Name of the module
67 def ensure_module_defined(module_name)
68   module_name.split('::').reduce(Object) do |last_module, next_module|
69     last_module.const_set(next_module, Module.new) unless last_module.const_defined?(next_module, false)
70     last_module.const_get(next_module, false)
71   end
72 end
73
74 # 'spec_overrides' from sync.yml will appear below this line