rspec-puppetlabs-facts 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ PUPPET_VERSIONS_PATH = File.join(__dir__, 'ext', 'puppet_agent_facter_versions.json')
6
+
7
+ LEFTOVER_PATTERN = /RspecPuppetFacts|rspec-puppet-facts|voxpupuli|openfact/i
8
+ LEFTOVER_ALLOWED_TOKENS = %r{voxpupuli-rubocop|voxpupuli/rubocop}i
9
+ LEFTOVER_ALLOWED_FILES = %w[
10
+ README.md
11
+ HISTORY.md
12
+ CHANGELOG.md
13
+ LICENSE
14
+ scripts/rename-to-puppetlabs.sh
15
+ Rakefile
16
+ ].freeze
17
+
18
+ require 'rspec/core/rake_task'
19
+
20
+ RSpec::Core::RakeTask.new(:spec) do |t|
21
+ t.exclude_pattern = 'spec/spec_helper_acceptance.rb,spec/acceptance/**'
22
+ end
23
+
24
+ namespace :acceptance do
25
+ desc 'Run acceptance tests against current code'
26
+ RSpec::Core::RakeTask.new(:local) do |t|
27
+ t.pattern = 'spec/acceptance/**/*_spec.rb'
28
+ end
29
+ end
30
+
31
+ begin
32
+ require 'yard'
33
+ YARD::Rake::YardocTask.new
34
+ rescue LoadError
35
+ # yard is optional
36
+ end
37
+
38
+ desc 'Produce Commit history since last tag'
39
+ task :dump_commit do
40
+ last_tag = `git describe --tags $(git rev-list --tags --max-count=1)`.chomp
41
+ puts "############ Commits since #{last_tag} ######"
42
+ puts `git log --pretty=format:"- %s" #{last_tag}..HEAD`
43
+ end
44
+
45
+ namespace :puppet_versions do
46
+ desc 'updates the vendored list of puppet versions & components'
47
+ task :update do
48
+ warn 'The rake task is disabled since the 6.0.0 Release. Please see the README.md'
49
+ exit 1
50
+ end
51
+
52
+ desc 'runs all tests and verifies vendored component list'
53
+ task :test do
54
+ warn 'The rake task is disabled since the 6.0.0 Release. Please see the README.md'
55
+ exit 1
56
+ end
57
+ end
58
+
59
+ def tracked_files
60
+ files = `git ls-files -z`.split("\0")
61
+ abort 'check_leftovers: git ls-files returned no files' unless Process.last_status.success? && !files.empty?
62
+ files
63
+ end
64
+
65
+ def leftover_hits(path)
66
+ File.foreach(path).with_index(1).filter_map do |line, number|
67
+ text = line.scrub.gsub(LEFTOVER_ALLOWED_TOKENS, '')
68
+ "#{path}:#{number}: #{line.scrub.strip}" if text.match?(LEFTOVER_PATTERN)
69
+ end
70
+ end
71
+
72
+ desc 'Fail on leftover upstream names outside the allowlist'
73
+ task :check_leftovers do
74
+ files = tracked_files.reject { |path| LEFTOVER_ALLOWED_FILES.include?(path) }
75
+ hits = files.select { |path| File.file?(path) }.flat_map { |path| leftover_hits(path) }
76
+ abort "Leftover upstream names found (allowlist in Rakefile):\n#{hits.join("\n")}" unless hits.empty?
77
+
78
+ puts 'check_leftovers: OK'
79
+ end
80
+
81
+ begin
82
+ require 'voxpupuli/rubocop/rake'
83
+ rescue LoadError
84
+ # the voxpupuli-rubocop gem is optional
85
+ end
86
+
87
+ desc 'Run rspec, rubocop and the leftover-name check'
88
+ task default: %i[spec rubocop check_leftovers]
@@ -0,0 +1,74 @@
1
+ {
2
+ "9.1.4": "4.23.0",
3
+ "9.1.3": "4.23.0",
4
+ "9.1.0": "4.22.0",
5
+ "9.0.0": "4.21.0",
6
+ "8.22.3": "4.23.0",
7
+ "8.22.2": "4.23.0",
8
+ "8.22.0": "4.22.0",
9
+ "8.21.0": "4.21.0",
10
+ "8.20.0": "4.20.0",
11
+ "8.19.0": "4.19.0",
12
+ "8.18.0": "4.18.0",
13
+ "8.17.0": "4.17.0",
14
+ "8.16.0": "4.16.0",
15
+ "8.15.0": "4.15.0",
16
+ "8.14.0": "4.14.0",
17
+ "8.13.1": "4.13.0",
18
+ "8.13.0": "4.13.0",
19
+ "8.12.0": "4.12.0",
20
+ "8.11.0": "4.11.0",
21
+ "8.10.0": "4.10.0",
22
+ "8.9.0": "4.9.0",
23
+ "8.8.1": "4.8.0",
24
+ "8.8.0": "4.8.0",
25
+ "8.7.0": "4.7.1",
26
+ "8.6.0": "4.7.0",
27
+ "8.5.1": "4.6.1",
28
+ "8.5.0": "4.6.0",
29
+ "8.4.0": "4.5.2",
30
+ "8.3.1": "4.5.1",
31
+ "8.3.0": "4.5.1",
32
+ "8.2.0": "4.4.2",
33
+ "8.1.0": "4.4.1",
34
+ "8.0.0": "4.4.0",
35
+ "7.34.0": "4.10.0",
36
+ "7.33.0": "4.9.0",
37
+ "7.32.1": "4.8.0",
38
+ "7.32.0": "4.8.0",
39
+ "7.31.0": "4.7.1",
40
+ "7.30.0": "4.7.0",
41
+ "7.29.1": "4.6.1",
42
+ "7.29.0": "4.6.0",
43
+ "7.28.0": "4.5.2",
44
+ "7.27.0": "4.5.1",
45
+ "7.26.0": "4.4.2",
46
+ "7.25.0": "4.4.1",
47
+ "7.24.0": "4.3.1",
48
+ "7.23.0": "4.3.0",
49
+ "7.21.0": "4.2.14",
50
+ "7.20.0": "4.2.13",
51
+ "7.19.0": "4.2.12",
52
+ "7.18.0": "4.2.11",
53
+ "7.17.0": "4.2.10",
54
+ "7.16.0": "4.2.8",
55
+ "7.15.0": "4.2.8",
56
+ "7.14.0": "4.2.7",
57
+ "7.13.1": "4.2.6",
58
+ "7.13.0": "4.2.6",
59
+ "7.12.1": "4.2.5",
60
+ "7.12.0": "4.2.5",
61
+ "7.11.0": "4.2.4",
62
+ "7.10.0": "4.2.3",
63
+ "7.9.0": "4.2.2",
64
+ "7.8.0": "4.2.1",
65
+ "7.7.0": "4.2.0",
66
+ "7.6.1": "4.1.1",
67
+ "7.6.0": "4.1.0",
68
+ "7.5.0": "4.0.52",
69
+ "7.4.1": "4.0.51",
70
+ "7.4.0": "4.0.51",
71
+ "7.3.0": "4.0.49",
72
+ "7.1.0": "4.0.47",
73
+ "7.0.0": "4.0.46"
74
+ }
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecPuppetlabsFacts
4
+ # This module contains the current version constant
5
+ module Version
6
+ # The current version of this gem
7
+ STRING = '7.0.0'
8
+ end
9
+ end
@@ -0,0 +1,440 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'facter'
4
+ require 'puppetlabs/facterdb'
5
+ require 'json'
6
+ require 'deep_merge'
7
+
8
+ # The purpose of this module is to simplify the Puppet
9
+ # module's RSpec tests by looping through all supported
10
+ # OS'es and their facts data which is received from the FacterDB.
11
+ module RspecPuppetlabsFacts
12
+ FACTS_CACHE = {}
13
+
14
+ # Use the provided options or the data from the metadata.json file
15
+ # to find a set of matching facts in the FacterDB.
16
+ # OS names and facts can be used in the Puppet RSpec tests
17
+ # to run the examples against all supported facts combinations.
18
+ #
19
+ # The list of received OS facts can also be filtered by the SPEC_FACTS_OS
20
+ # environment variable. For example, if the variable is set to "debian"
21
+ # only the OS names which start with "debian" will be returned. It allows a
22
+ # user to quickly run the tests only on a single facts set without any
23
+ # file modifications.
24
+ #
25
+ # @return [Hash <String => Hash>]
26
+ # @param [Hash] opts
27
+ # @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. x86_64
28
+ # @option opts [Array<Hash>] :supported_os If this options is provided the data
29
+ # @option opts [String] :facterversion the facter version of which to
30
+ # select facts from, e.g.: '3.6'
31
+ # will be used instead of the "operatingsystem_support" section if the metadata file
32
+ # even if the file is missing.
33
+ def on_supported_os(opts = {})
34
+ opts[:hardwaremodels] ||= ['x86_64']
35
+ opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
36
+ opts[:supported_os] ||= RspecPuppetlabsFacts.meta_supported_os
37
+ opts[:facterversion] ||= RSpec.configuration.default_facter_version
38
+
39
+ # This should list all variables that on_supported_os_implementation uses
40
+ cache_key = [
41
+ opts.to_s,
42
+ RspecPuppetlabsFacts.custom_facts.to_s,
43
+ RspecPuppetlabsFacts.spec_facts_os_filter,
44
+ RspecPuppetlabsFacts.spec_facts_strict?,
45
+ ]
46
+
47
+ result = FACTS_CACHE[cache_key] ||= on_supported_os_implementation(opts)
48
+
49
+ # Marshalling is used to get unique instances which is needed for test
50
+ # isolation when facts are overridden.
51
+ Marshal.load(Marshal.dump(result))
52
+ end
53
+
54
+ # The real implementation of on_supported_os.
55
+ #
56
+ # Generating facts is slow - this allows memoization of the facts between
57
+ # multiple calls.
58
+ #
59
+ # @api private
60
+ def on_supported_os_implementation(opts = {})
61
+ unless /\A\d+\.\d+(?:\.\d+)*\z/.match?(facterversion = opts[:facterversion])
62
+ raise ArgumentError, ":facterversion must be in the format 'n.n' or 'n.n.n' (n is numeric), not '#{facterversion}'"
63
+ end
64
+
65
+ filter = []
66
+ opts[:supported_os].map do |os_sup|
67
+ if os_sup['operatingsystemrelease']
68
+ Array(os_sup['operatingsystemrelease']).map do |operatingsystemmajrelease|
69
+ opts[:hardwaremodels].each do |hardwaremodel|
70
+ os_release_filter = "/^#{Regexp.escape(operatingsystemmajrelease.split(' ')[0])}/"
71
+ case os_sup['operatingsystem']
72
+ when /BSD/i
73
+ hardwaremodel = 'amd64'
74
+ when /Solaris/i
75
+ hardwaremodel = 'i86pc'
76
+ when /AIX/i
77
+ hardwaremodel = '/^IBM,.*/'
78
+ os_release_filter = if operatingsystemmajrelease =~ /\A(\d+)\.(\d+)\Z/
79
+ "/^#{$~[1]}#{$~[2]}00-/"
80
+ else
81
+ "/^#{operatingsystemmajrelease}-/"
82
+ end
83
+ when /Windows/i
84
+ hardwaremodel = 'x86_64'
85
+ os_sup['operatingsystem'] = os_sup['operatingsystem'].downcase
86
+ operatingsystemmajrelease = operatingsystemmajrelease[/\A(?:Server )?(.+)/i, 1]
87
+
88
+ # force quoting because windows releases can contain spaces
89
+ os_release_filter = "\"#{operatingsystemmajrelease}\""
90
+ when /Amazon/i
91
+ # Tighten the regex for Amazon Linux 2 so that we don't pick up Amazon Linux 2016 or 2017 facts
92
+ os_release_filter = '/^2$/' if operatingsystemmajrelease == '2'
93
+ end
94
+
95
+ filter << {
96
+ 'os.name' => os_sup['operatingsystem'],
97
+ 'os.release.full' => os_release_filter,
98
+ 'os.hardware' => hardwaremodel,
99
+ }
100
+ end
101
+ end
102
+ else
103
+ opts[:hardwaremodels].each do |hardwaremodel|
104
+ filter << {
105
+ 'os.name' => os_sup['operatingsystem'],
106
+ 'os.hardware' => hardwaremodel,
107
+ }
108
+ end
109
+ end
110
+ end
111
+
112
+ strict_requirement = RspecPuppetlabsFacts.facter_version_to_strict_requirement(facterversion)
113
+
114
+ loose_requirement = RspecPuppetlabsFacts.facter_version_to_loose_requirement(facterversion)
115
+ received_facts = []
116
+
117
+ # FacterDB may have newer versions of facter data for which it contains a subset of all possible
118
+ # facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to
119
+ # cycle through and downgrade Facter versions per platform type until we find matching Facter data.
120
+ facterversion_key = RSpec.configuration.facterdb_string_keys ? 'facterversion' : :facterversion
121
+ filter.each do |filter_spec|
122
+ versions = Puppetlabs::FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h do |facts|
123
+ [Gem::Version.new(facts[facterversion_key]), facts]
124
+ end
125
+
126
+ version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }
127
+
128
+ unless version
129
+ version, facts = versions.select { |v, _f| loose_requirement =~ v }.max_by { |v, _f| v } if loose_requirement
130
+ next unless version
131
+
132
+ raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, aborting" if RspecPuppetlabsFacts.spec_facts_strict?
133
+
134
+ RspecPuppetlabsFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, using v#{version} instead"
135
+ end
136
+
137
+ received_facts << facts
138
+ end
139
+
140
+ unless received_facts.any?
141
+ RspecPuppetlabsFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
142
+ return {}
143
+ end
144
+
145
+ os_facts_hash = {}
146
+ received_facts.map do |facts|
147
+ os_fact = RSpec.configuration.facterdb_string_keys ? facts['os'] : facts[:os]
148
+ unless os_fact
149
+ RspecPuppetlabsFacts.warning "No os fact was found in FacterDB for: #{facts}"
150
+ next
151
+ end
152
+
153
+ os = "#{os_fact['name'].downcase}-#{os_fact['release']['major']}-#{os_fact['hardware']}"
154
+ next if RspecPuppetlabsFacts.spec_facts_os_filter && !os.start_with?(RspecPuppetlabsFacts.spec_facts_os_filter)
155
+
156
+ facts.merge! RspecPuppetlabsFacts.common_facts
157
+ os_facts_hash[os] = RspecPuppetlabsFacts.with_custom_facts(os, facts)
158
+ end
159
+
160
+ os_facts_hash
161
+ end
162
+
163
+ # @api private
164
+ def stringify_keys(hash)
165
+ hash.to_h { |k, v| [k.to_s, v.is_a?(Hash) ? stringify_keys(v) : v] }
166
+ end
167
+
168
+ # Register a custom fact that will be included in the facts hash.
169
+ # If it should be limited to a particular OS, pass a :confine option
170
+ # that contains the operating system(s) to confine to. If it should
171
+ # be excluded on a particular OS, use :exclude.
172
+ #
173
+ # @param [String] name Fact name
174
+ # @param [String,Proc] value Fact value. If proc, takes 2 params: os and facts hash
175
+ # @param [Hash] opts
176
+ # @option opts [String,Array<String>] :confine The applicable OS's
177
+ # @option opts [String,Array<String>] :exclude OS's to exclude
178
+ #
179
+ def add_custom_fact(name, value, options = {})
180
+ options[:confine] = [options[:confine]] if options[:confine].is_a?(String)
181
+ options[:exclude] = [options[:exclude]] if options[:exclude].is_a?(String)
182
+
183
+ RspecPuppetlabsFacts.register_custom_fact(name, value, options)
184
+ end
185
+
186
+ # Adds a custom fact to the @custom_facts variable.
187
+ #
188
+ # @param [String] name Fact name
189
+ # @param [String,Proc] value Fact value. If proc, takes 2 params: os and facts hash
190
+ # @param [Hash] opts
191
+ # @option opts [String,Array<String>] :confine The applicable OS's
192
+ # @option opts [String,Array<String>] :exclude OS's to exclude
193
+ # @api private
194
+ def self.register_custom_fact(name, value, options)
195
+ @custom_facts ||= {}
196
+ name = RSpec.configuration.facterdb_string_keys ? name.to_s : name.to_sym
197
+ @custom_facts[name] = { options: options, value: value }
198
+ end
199
+
200
+ # Adds any custom facts according to the rules defined for the operating
201
+ # system with the given facts.
202
+ # @param [String] os Name of the operating system
203
+ # @param [Hash] facts Facts hash
204
+ # @return [Hash] facts Facts hash with custom facts added
205
+ # @api private
206
+ def self.with_custom_facts(os, facts)
207
+ return facts unless @custom_facts
208
+
209
+ @custom_facts.each do |name, fact|
210
+ next if fact[:options][:confine] && !fact[:options][:confine].include?(os)
211
+ next if fact[:options][:exclude] && fact[:options][:exclude].include?(os)
212
+
213
+ value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
214
+ # if merge_facts passed, merge supplied facts into facts hash
215
+ if fact[:options][:merge_facts]
216
+ facts.deep_merge!({ name => value })
217
+ else
218
+ facts[name] = value
219
+ end
220
+ end
221
+
222
+ facts
223
+ end
224
+
225
+ # Get custom facts
226
+ # @return [nil,Hash]
227
+ # @api private
228
+ def self.custom_facts
229
+ @custom_facts
230
+ end
231
+
232
+ # If provided this filter can be used to limit the set
233
+ # of retrieved facts only to the matched OS names.
234
+ # The value is being taken from the SPEC_FACTS_OS environment
235
+ # variable and
236
+ # @return [nil,String]
237
+ # @api private
238
+ def self.spec_facts_os_filter
239
+ ENV.fetch('SPEC_FACTS_OS', nil)
240
+ end
241
+
242
+ # If SPEC_FACTS_STRICT is set to `yes`, RspecPuppetlabsFacts will error on missing FacterDB entries, instead of warning & skipping the tests, or using an older facter version.
243
+ # @return [Boolean]
244
+ # @api private
245
+ def self.spec_facts_strict?
246
+ ENV['SPEC_FACTS_STRICT'] == 'yes'
247
+ end
248
+
249
+ # These facts are common for all OS'es and will be
250
+ # added to the facts retrieved from the FacterDB
251
+ # @api private
252
+ # @return [Hash <Symbol => String>]
253
+ def self.common_facts
254
+ return @common_facts if @common_facts
255
+
256
+ # from Facter itself
257
+ @common_facts = {
258
+ rubysitedir: RbConfig::CONFIG['sitelibdir'],
259
+ rubyversion: RUBY_VERSION,
260
+ }
261
+
262
+ begin
263
+ require 'puppet'
264
+ rescue LoadError
265
+ warning 'Could not load Puppet'
266
+ else
267
+ # from Puppet.initialize_facts
268
+ @common_facts[:puppetversion] = Puppet.version.to_s
269
+ # from Puppet::Node::Facts#add_local_facts
270
+ @common_facts[:implementation] = Puppet.implementation if Puppet.respond_to?(:implementation)
271
+ end
272
+
273
+ @common_facts[:mco_version] = MCollective::VERSION if mcollective?
274
+
275
+ if augeas?
276
+ @common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
277
+ end
278
+ @common_facts = stringify_keys(@common_facts) if RSpec.configuration.facterdb_string_keys
279
+
280
+ @common_facts
281
+ end
282
+
283
+ # Determine if the Augeas gem is available.
284
+ # @api private
285
+ # @return [Boolean] true if the augeas gem could be loaded.
286
+ # :nocov:
287
+ def self.augeas?
288
+ require 'augeas'
289
+ true
290
+ rescue LoadError
291
+ false
292
+ end
293
+ # :nocov:
294
+
295
+ # Determine if the mcollective gem is available
296
+ # @api private
297
+ # @return [Boolean] true if the mcollective gem could be loaded.
298
+ # :nocov:
299
+ def self.mcollective?
300
+ require 'mcollective'
301
+ true
302
+ rescue LoadError
303
+ false
304
+ end
305
+ # :nocov:
306
+
307
+ # Get the "operatingsystem_support" structure from
308
+ # the parsed metadata.json file
309
+ # @raise [StandardError] if there is no "operatingsystem_support"
310
+ # in the metadata
311
+ # @return [Array<Hash>]
312
+ # @api private
313
+ def self.meta_supported_os
314
+ unless metadata['operatingsystem_support'].is_a? Array
315
+ raise StandardError, 'Unknown operatingsystem support in the metadata file!'
316
+ end
317
+
318
+ metadata['operatingsystem_support']
319
+ end
320
+
321
+ # Read the metadata file and parse
322
+ # its JSON content.
323
+ # @raise [StandardError] if the metadata file is missing
324
+ # @return [Hash]
325
+ # @api private
326
+ def self.metadata
327
+ return @metadata if @metadata
328
+ unless File.file? metadata_file
329
+ raise StandardError, "Can't find metadata.json... dunno why"
330
+ end
331
+
332
+ content = File.read metadata_file
333
+ @metadata = JSON.parse content
334
+ end
335
+
336
+ # This file contains the Puppet module's metadata
337
+ # @return [String]
338
+ # @api private
339
+ def self.metadata_file
340
+ 'metadata.json'
341
+ end
342
+
343
+ # Print a warning message to the console
344
+ # @param message [String]
345
+ # @api private
346
+ def self.warning(message)
347
+ warn message
348
+ end
349
+
350
+ # Reset the memoization
351
+ # to make the saved structures
352
+ # be generated again
353
+ # @api private
354
+ def self.reset
355
+ @custom_facts = nil
356
+ @common_facts = nil
357
+ @metadata = nil
358
+ end
359
+
360
+ # Construct the strict facter version requirement
361
+ # @return [Gem::Requirement] The version requirement to match
362
+ # @api private
363
+ def self.facter_version_to_strict_requirement(version)
364
+ Gem::Requirement.new(facter_version_to_strict_requirement_string(version))
365
+ end
366
+
367
+ # Construct the strict facter version requirement string
368
+ # @return [String] The version requirement to match
369
+ # @api private
370
+ def self.facter_version_to_strict_requirement_string(version)
371
+ if /\A[0-9]+(\.[0-9]+)*\Z/.match?(version)
372
+ # Interpret 3 as ~> 3.0
373
+ "~> #{version}.0"
374
+ else
375
+ version
376
+ end
377
+ end
378
+
379
+ # Construct the loose facter version requirement
380
+ # @return [Optional[Gem::Requirement]] The version requirement to match
381
+ # @api private
382
+ def self.facter_version_to_loose_requirement(version)
383
+ string = facter_version_to_loose_requirement_string(version)
384
+ Gem::Requirement.new(string) if string
385
+ end
386
+
387
+ # Construct the facter version requirement string
388
+ # @return [String] The version requirement to match
389
+ # @api private
390
+ def self.facter_version_to_loose_requirement_string(version)
391
+ if (m = /\A(?<major>[0-9]+)\.(?<minor>[0-9]+)(?:\.(?<patch>[0-9]+))?\Z/.match(version))
392
+ # Interpret 3.1 as < 3.2 and 3.2.1 as < 3.3
393
+ "< #{m[:major]}.#{m[:minor].to_i + 1}"
394
+ elsif /\A[0-9]+\Z/.match?(version)
395
+ # Interpret 3 as < 4
396
+ "< #{version.to_i + 1}"
397
+ else # rubocop:disable Style/EmptyElse
398
+ # This would be the same as the strict requirement
399
+ nil
400
+ end
401
+ end
402
+
403
+ def self.facter_version_for_puppet_version(puppet_version)
404
+ return Facter.version if puppet_version.nil?
405
+
406
+ json_path = File.expand_path(File.join(__dir__, '..', 'ext', 'puppet_agent_facter_versions.json'))
407
+ unless File.file?(json_path) && File.readable?(json_path)
408
+ warning "#{json_path} does not exist or is not readable, defaulting to Facter #{Facter.version}"
409
+ return Facter.version
410
+ end
411
+
412
+ version_map = JSON.parse(File.read(json_path)).map do |puppet, facter|
413
+ [Gem::Version.new(puppet), facter]
414
+ end
415
+
416
+ puppet_gem_version = Gem::Version.new(puppet_version)
417
+ applicable_versions = version_map.select { |p, _| puppet_gem_version >= p }
418
+ if applicable_versions.empty?
419
+ warning "Unable to find Puppet #{puppet_version} in #{json_path}, defaulting to Facter #{Facter.version}"
420
+ return Facter.version
421
+ end
422
+
423
+ applicable_versions.max_by { |p, _| p }.last
424
+ rescue JSON::ParserError
425
+ warning "#{json_path} contains invalid JSON, defaulting to Facter #{Facter.version}"
426
+ Facter.version
427
+ end
428
+ end
429
+
430
+ RSpec.configure do |c|
431
+ begin
432
+ require 'puppet'
433
+ rescue LoadError
434
+ puppet_version = nil
435
+ else
436
+ puppet_version = Puppet.version
437
+ end
438
+ c.add_setting :default_facter_version, default: RspecPuppetlabsFacts.facter_version_for_puppet_version(puppet_version)
439
+ c.add_setting :facterdb_string_keys, default: false
440
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
4
+ require 'rspec-puppetlabs-facts/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'rspec-puppetlabs-facts'
8
+ s.version = RspecPuppetlabsFacts::Version::STRING
9
+ s.authors = ['Puppet Labs', 'Vox Pupuli']
10
+ s.homepage = 'https://github.com/puppetlabs/rspec-puppetlabs-facts'
11
+ s.summary = 'Standard facts fixtures for Puppet, using Puppet Labs facter'
12
+ s.description = 'Contains facts from many Facter 4.x versions on many operating systems for rspec-puppet tests, ' \
13
+ 'sourced from puppetlabs-facterdb. Forked from the Vox Pupuli gem at version 6.3.1.'
14
+ s.licenses = 'Apache-2.0'
15
+
16
+ s.required_ruby_version = '>= 3.2.0'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20
+
21
+ s.add_development_dependency 'mime-types', '~> 3.5', '>= 3.5.2'
22
+ s.add_development_dependency 'rake', '~> 13.1'
23
+ s.add_development_dependency 'rspec', '~> 3.12'
24
+ s.add_development_dependency 'yard', '~> 0.9.34'
25
+
26
+ s.add_development_dependency 'voxpupuli-rubocop', '~> 5.2.0'
27
+
28
+ s.add_dependency 'deep_merge', '~> 1.2'
29
+
30
+ s.add_dependency 'facter', '>= 4', '< 5'
31
+ s.add_dependency 'puppetlabs-facterdb', '~> 0.1'
32
+ end