puppetlabs_spec_helper 2.15.0 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec-puppet'
2
4
  require 'puppetlabs_spec_helper/puppet_spec_helper'
3
5
  require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
@@ -50,6 +52,15 @@ if ENV['SIMPLECOV'] == 'yes'
50
52
  end
51
53
  end
52
54
 
55
+ # Add all spec lib dirs to LOAD_PATH
56
+ components = module_path.split(File::PATH_SEPARATOR).collect do |dir|
57
+ next unless Dir.exist? dir
58
+ Dir.entries(dir).reject { |f| f =~ %r{^\.} }.collect { |f| File.join(dir, f, 'spec', 'lib') }
59
+ end
60
+ components.flatten.each do |d|
61
+ $LOAD_PATH << d if FileTest.directory?(d) && !$LOAD_PATH.include?(d)
62
+ end
63
+
53
64
  RSpec.configure do |c|
54
65
  c.environmentpath = spec_path if Puppet.version.to_f >= 4.0
55
66
  c.module_path = module_path
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppetlabs_spec_helper/puppetlabs_spec_helper'
2
4
 
3
5
  # Don't want puppet getting the command line arguments for rake or autotest
@@ -45,7 +47,7 @@ require 'puppetlabs_spec_helper/puppetlabs_spec/files'
45
47
  # to compatibility mode for older versions of puppet.
46
48
  begin
47
49
  require 'puppet/test/test_helper'
48
- rescue LoadError => err
50
+ rescue LoadError
49
51
  end
50
52
 
51
53
  # This is just a utility class to allow us to isolate the various version-specific
@@ -58,7 +60,7 @@ module Puppet
58
60
  # Puppet's Settings singleton object, and other fun implementation details
59
61
  # that code external to puppet should really never know about.
60
62
  def self.initialize_via_fallback_compatibility(config)
61
- $stderr.puts('Warning: you appear to be using an older version of puppet; spec_helper will use fallback compatibility mode.')
63
+ warn('Warning: you appear to be using an older version of puppet; spec_helper will use fallback compatibility mode.')
62
64
  config.before :all do
63
65
  # nothing to do for now
64
66
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'tempfile'
3
5
  require 'pathname'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This module provides some helper methods to assist with fixtures. It's
2
4
  # methods are designed to help when you have a conforming fixture layout so we
3
5
  # get project consistency.
@@ -14,6 +16,7 @@ module PuppetlabsSpec::Fixtures
14
16
  callers = caller
15
17
  while line = callers.shift
16
18
  next unless found = line.match(%r{/spec/(.*)_spec\.rb:})
19
+
17
20
  return fixtures(found[1])
18
21
  end
19
22
  raise "sorry, I couldn't work out your path from the caller stack!"
@@ -26,6 +29,7 @@ module PuppetlabsSpec::Fixtures
26
29
  unless File.readable? file
27
30
  raise "fixture '#{name}' for #{my_fixture_dir} is not readable"
28
31
  end
32
+
29
33
  file
30
34
  end
31
35
 
@@ -37,12 +41,13 @@ module PuppetlabsSpec::Fixtures
37
41
 
38
42
  # Provides a block mechanism for iterating across the files in your fixture
39
43
  # area.
40
- def my_fixtures(glob = '*', flags = 0)
44
+ def my_fixtures(glob = '*', flags = 0, &block)
41
45
  files = Dir.glob(File.join(my_fixture_dir, glob), flags)
42
46
  if files.empty?
43
47
  raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
44
48
  end
45
- block_given? && files.each { |file| yield file }
49
+
50
+ block_given? && files.each(&block)
46
51
  files
47
52
  end
48
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'stringio'
2
4
  require 'rspec/expectations'
3
5
 
@@ -6,8 +8,8 @@ require 'rspec/expectations'
6
8
  module RSpec
7
9
  module Matchers
8
10
  module BlockAliases
9
- if method_defined? :should
10
- alias to should unless method_defined? :to
11
+ if method_defined?(:should) && !method_defined?(:to)
12
+ alias to should
11
13
  end
12
14
  if method_defined? :should_not
13
15
  alias to_not should_not unless method_defined? :to_not
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Initialize puppet for testing by loading the
2
4
  # 'puppetlabs_spec_helper/puppet_spec_helper' library
3
5
  require 'puppetlabs_spec_helper/puppet_spec_helper'
@@ -10,7 +12,7 @@ module PuppetlabsSpec
10
12
  def scope(parts = {})
11
13
  RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')
12
14
 
13
- if Puppet.version =~ %r{^2\.[67]}
15
+ if %r{^2\.[67]}.match?(Puppet.version)
14
16
  # loadall should only be necessary prior to 3.x
15
17
  # Please note, loadall needs to happen first when creating a scope, otherwise
16
18
  # you might receive undefined method `function_*' errors
@@ -21,7 +23,7 @@ module PuppetlabsSpec
21
23
  scope_parent = parts[:parent] || scope_compiler.topscope
22
24
  scope_resource = parts[:resource] || resource(type: :node, title: scope_compiler.node.name)
23
25
 
24
- scope = if Puppet.version =~ %r{^2\.[67]}
26
+ scope = if %r{^2\.[67]}.match?(Puppet.version)
25
27
  Puppet::Parser::Scope.new(compiler: scope_compiler)
26
28
  else
27
29
  Puppet::Parser::Scope.new(scope_compiler)
@@ -67,6 +69,7 @@ module PuppetlabsSpec
67
69
  # exists. This is a hack, but at least it's a hidden hack and not an
68
70
  # exposed hack.
69
71
  return nil unless Puppet::Parser::Functions.function(name)
72
+
70
73
  scope.method("function_#{name}".intern)
71
74
  end
72
75
  module_function :function_method
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Define the main module namespace for use by the helper modules
2
4
  module PuppetlabsSpec
3
5
  # FIXTURE_DIR represents the standard locations of all fixture data. Normally
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'rake'
3
5
  require 'rspec/core/rake_task'
4
6
  require 'tmpdir'
5
7
  require 'pathname'
6
8
  require 'puppetlabs_spec_helper/version'
7
- require 'puppetlabs_spec_helper/tasks/beaker'
8
9
  require 'puppetlabs_spec_helper/tasks/fixtures'
9
10
  require 'puppetlabs_spec_helper/tasks/check_symlinks'
10
11
  require 'English'
@@ -53,6 +54,7 @@ RSpec::Core::RakeTask.new(:spec_standalone) do |t, args|
53
54
  ci_total = ENV['CI_NODE_TOTAL'].to_i
54
55
  ci_index = ENV['CI_NODE_INDEX'].to_i
55
56
  raise "CI_NODE_INDEX must be between 1-#{ci_total}" unless ci_index >= 1 && ci_index <= ci_total
57
+
56
58
  files = Rake::FileList[pattern].to_a
57
59
  per_node = (files.size / ci_total.to_f).ceil
58
60
  t.pattern = if args.extras.nil? || args.extras.empty?
@@ -108,16 +110,17 @@ end
108
110
  desc 'Parallel spec tests'
109
111
  task :parallel_spec_standalone do |_t, args|
110
112
  raise 'Add the parallel_tests gem to Gemfile to enable this task' unless parallel_tests_loaded
113
+
111
114
  if Rake::FileList[pattern].to_a.empty?
112
115
  warn 'No files for parallel_spec to run against'
113
116
  else
114
- begin
115
- args = ['-t', 'rspec']
116
- args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
117
- args.concat(Rake::FileList[pattern].to_a)
118
117
 
119
- ParallelTests::CLI.new.run(args)
120
- end
118
+ args = ['-t', 'rspec']
119
+ args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
120
+ args.concat(Rake::FileList[pattern].to_a)
121
+
122
+ ParallelTests::CLI.new.run(args)
123
+
121
124
  end
122
125
  end
123
126
 
@@ -145,12 +148,12 @@ namespace :build do
145
148
  require 'pdk/util'
146
149
  require 'pdk/module/build'
147
150
 
148
- path = PDK::Module::Build.invoke(:force => true, :'target-dir' => File.join(Dir.pwd, 'pkg'))
151
+ path = PDK::Module::Build.invoke(force: true, 'target-dir': File.join(Dir.pwd, 'pkg'))
149
152
  puts "Module built: #{path}"
150
153
  rescue LoadError
151
154
  _ = `pdk --version`
152
155
  unless $CHILD_STATUS.success?
153
- $stderr.puts 'Unable to build module. Please install PDK or add the `pdk` gem to your Gemfile.'
156
+ warn 'Unable to build module. Please install PDK or add the `pdk` gem to your Gemfile.'
154
157
  abort
155
158
  end
156
159
 
@@ -168,52 +171,34 @@ require 'puppet-lint/tasks/puppet-lint'
168
171
  # Must clear as it will not override the existing puppet-lint rake task since we require to import for
169
172
  # the PuppetLint::RakeTask
170
173
  Rake::Task[:lint].clear
171
- # Relative is not able to be set within the context of PuppetLint::RakeTask
174
+ # Utilize PuppetLint global configuration so that these settings can be tweaked by
175
+ # spec_helper.rb in an individual module
172
176
  PuppetLint.configuration.relative = true
173
- PuppetLint::RakeTask.new(:lint) do |config|
174
- config.fail_on_warnings = true
175
- config.disable_checks = %w[
176
- 80chars
177
- 140chars
178
- class_inherits_from_params_class
179
- class_parameter_defaults
180
- disable_autoloader_layout
181
- documentation
182
- single_quote_string_with_variables
183
- ]
184
- config.ignore_paths = [
185
- '.vendor/**/*.pp',
186
- 'bundle/**/*.pp',
187
- 'pkg/**/*.pp',
188
- 'spec/**/*.pp',
189
- 'tests/**/*.pp',
190
- 'types/**/*.pp',
191
- 'vendor/**/*.pp',
192
- ]
177
+ PuppetLint.configuration.ignore_paths ||= []
178
+ PuppetLint.configuration.ignore_paths << '.vendor/**/*.pp'
179
+ PuppetLint.configuration.ignore_paths << 'bundle/**/*.pp'
180
+ PuppetLint.configuration.ignore_paths << 'pkg/**/*.pp'
181
+ PuppetLint.configuration.ignore_paths << 'spec/**/*.pp'
182
+ PuppetLint.configuration.ignore_paths << 'tests/**/*.pp'
183
+ PuppetLint.configuration.ignore_paths << 'types/**/*.pp'
184
+ PuppetLint.configuration.ignore_paths << 'vendor/**/*.pp'
185
+ puppet_lint_disable_checks = %w[
186
+ 80chars
187
+ 140chars
188
+ class_inherits_from_params_class
189
+ class_parameter_defaults
190
+ disable_autoloader_layout
191
+ documentation
192
+ single_quote_string_with_variables
193
+ ]
194
+ puppet_lint_disable_checks.each do |check|
195
+ PuppetLint.configuration.send("disable_#{check}")
193
196
  end
197
+ PuppetLint::RakeTask.new(:lint)
194
198
 
195
199
  desc 'Run puppet-lint and fix issues automatically'
196
200
  PuppetLint::RakeTask.new(:lint_fix) do |config|
197
- config.fail_on_warnings = true
198
201
  config.fix = true
199
- config.disable_checks = %w[
200
- 80chars
201
- 140chars
202
- class_inherits_from_params_class
203
- class_parameter_defaults
204
- disable_autoloader_layout
205
- documentation
206
- single_quote_string_with_variables
207
- ]
208
- config.ignore_paths = [
209
- '.vendor/**/*.pp',
210
- 'bundle/**/*.pp',
211
- 'pkg/**/*.pp',
212
- 'spec/**/*.pp',
213
- 'tests/**/*.pp',
214
- 'types/**/*.pp',
215
- 'vendor/**/*.pp',
216
- ]
217
202
  end
218
203
 
219
204
  require 'puppet-syntax/tasks/puppet-syntax'
@@ -263,7 +248,7 @@ task :compute_dev_version do
263
248
  version = modinfo['version']
264
249
  elsif File.exist?('Modulefile')
265
250
  modfile = File.read('Modulefile')
266
- version = modfile.match(%r{\nversion[ ]+['"](.*)['"]})[1]
251
+ version = modfile.match(%r{\nversion +['"](.*)['"]})[1]
267
252
  else
268
253
  raise 'Could not find a metadata.json or Modulefile! Cannot compute dev version without one or the other!'
269
254
  end
@@ -273,14 +258,14 @@ task :compute_dev_version do
273
258
 
274
259
  # If we're in a CI environment include our build number
275
260
  # If the branch is a release branch we append an 'r' into the new_version,
276
- # this is due to the release branch buildID conflicting with master branch when trying to push to the staging forge.
261
+ # this is due to the release branch buildID conflicting with main branch when trying to push to the staging forge.
277
262
  # More info can be found at https://tickets.puppetlabs.com/browse/FM-6170
278
- new_version = if build = ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER']
263
+ new_version = if build = (ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER'])
279
264
  if branch.eql? 'release'
280
- '%s-%s%04d-%s' % [version, 'r', build, sha]
265
+ '%s-%s%04d-%s' % [version, 'r', build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
281
266
  else
282
- '%s-%04d-%s' % [version, build, sha]
283
- end
267
+ '%s-%04d-%s' % [version, build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
268
+ end
284
269
  else
285
270
  "#{version}-#{sha}"
286
271
  end
@@ -297,10 +282,7 @@ task :release_checks do
297
282
  else
298
283
  Rake::Task[:spec].invoke
299
284
  end
300
- Rake::Task['check:symlinks'].invoke
301
- Rake::Task['check:test_file'].invoke
302
- Rake::Task['check:dot_underscore'].invoke
303
- Rake::Task['check:git_ignore'].invoke
285
+ Rake::Task[:check].invoke
304
286
  end
305
287
 
306
288
  namespace :check do
@@ -341,6 +323,9 @@ namespace :check do
341
323
  end
342
324
  end
343
325
 
326
+ desc 'Run static pre release checks'
327
+ task check: ['check:symlinks', 'check:test_file', 'check:dot_underscore', 'check:git_ignore']
328
+
344
329
  desc 'Display the list of available rake tasks'
345
330
  task :help do
346
331
  system('rake -T')
@@ -351,6 +336,14 @@ begin
351
336
  RuboCop::RakeTask.new(:rubocop) do |task|
352
337
  # These make the rubocop experience maybe slightly less terrible
353
338
  task.options = ['-D', '-S', '-E']
339
+
340
+ # Use Rubocop's Github Actions formatter if possible
341
+ if ENV['GITHUB_ACTIONS'] == 'true'
342
+ rubocop_spec = Gem::Specification.find_by_name('rubocop')
343
+ if Gem::Version.new(rubocop_spec.version) >= Gem::Version.new('1.2')
344
+ task.formatters << 'github'
345
+ end
346
+ end
354
347
  end
355
348
  rescue LoadError
356
349
  desc 'rubocop is not available in this installation'
@@ -359,29 +352,6 @@ rescue LoadError
359
352
  end
360
353
  end
361
354
 
362
- module_dir = Dir.pwd
363
- locales_dir = File.absolute_path('locales', module_dir)
364
- # if the task is allowed to run when the module does not have a locales directory,
365
- # the task is run in the puppet gem instead and creates a POT there.
366
- puts 'gettext-setup tasks will only be loaded if the locales/ directory is present' if Rake.verbose == true
367
- if File.exist? locales_dir
368
- begin
369
- spec = Gem::Specification.find_by_name 'gettext-setup'
370
- load "#{spec.gem_dir}/lib/tasks/gettext.rake"
371
- # Initialization requires a valid locales directory
372
- GettextSetup.initialize(locales_dir)
373
- namespace :module do
374
- desc 'Runs all tasks to build a modules POT file for internationalization'
375
- task :pot_gen do
376
- Rake::Task['gettext:pot'].invoke
377
- Rake::Task['gettext:metadata_pot'].invoke("#{module_dir}/metadata.json")
378
- end
379
- end
380
- rescue Gem::LoadError
381
- puts 'No gettext-setup gem found, skipping GettextSetup config initialization' if Rake.verbose == true
382
- end
383
- end
384
-
385
355
  def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_since_tag = nil, changelog_tag_pattern = 'v%s')
386
356
  if Bundler.rubygems.find_name('github_changelog_generator').any?
387
357
  # needed a place to hide these methods
@@ -389,6 +359,7 @@ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_sin
389
359
  def changelog_user_from_metadata
390
360
  result = JSON.parse(File.read('metadata.json'))['author']
391
361
  raise 'unable to find the changelog_user in .sync.yml, or the author in metadata.json' if result.nil?
362
+
392
363
  puts "GitHubChangelogGenerator user:#{result}"
393
364
  result
394
365
  end
@@ -396,14 +367,17 @@ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_sin
396
367
  def changelog_project_from_metadata
397
368
  result = JSON.parse(File.read('metadata.json'))['name']
398
369
  raise 'unable to find the changelog_project in .sync.yml or the name in metadata.json' if result.nil?
370
+
399
371
  puts "GitHubChangelogGenerator project:#{result}"
400
372
  result
401
373
  end
402
374
 
403
375
  def changelog_future_release
404
376
  return unless Rake.application.top_level_tasks.include? 'changelog'
377
+
405
378
  result = JSON.parse(File.read('metadata.json'))['version']
406
379
  raise 'unable to find the future_release (version) in metadata.json' if result.nil?
380
+
407
381
  puts "GitHubChangelogGenerator future_release:#{result}"
408
382
  result
409
383
  end
@@ -413,6 +387,7 @@ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_sin
413
387
  if ENV['CHANGELOG_GITHUB_TOKEN'].nil?
414
388
  raise "Set CHANGELOG_GITHUB_TOKEN environment variable eg 'export CHANGELOG_GITHUB_TOKEN=valid_token_here'"
415
389
  end
390
+
416
391
  config.user = changelog_user || changelog_user_from_metadata
417
392
  config.project = changelog_project || changelog_project_from_metadata
418
393
  config.since_tag = changelog_since_tag if changelog_since_tag
@@ -442,18 +417,18 @@ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_sin
442
417
  else
443
418
  desc 'Generate a Changelog from GitHub'
444
419
  task :changelog do
445
- raise <<EOM
446
- The changelog tasks depends on unreleased features of the github_changelog_generator gem.
447
- Please manually add it to your .sync.yml for now, and run `pdk update`:
448
- ---
449
- Gemfile:
450
- optional:
451
- ':development':
452
- - gem: 'github_changelog_generator'
453
- git: 'https://github.com/skywinder/github-changelog-generator'
454
- ref: '20ee04ba1234e9e83eb2ffb5056e23d641c7a018'
455
- condition: "Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2')"
456
- EOM
420
+ raise <<~MESSAGE
421
+ The changelog tasks depends on unreleased features of the github_changelog_generator gem.
422
+ Please manually add it to your .sync.yml for now, and run `pdk update`:
423
+ ---
424
+ Gemfile:
425
+ optional:
426
+ ':development':
427
+ - gem: 'github_changelog_generator'
428
+ git: 'https://github.com/skywinder/github-changelog-generator'
429
+ ref: '20ee04ba1234e9e83eb2ffb5056e23d641c7a018'
430
+ condition: "Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2')"
431
+ MESSAGE
457
432
  end
458
433
  end
459
434
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathspec'
2
4
 
3
5
  module PuppetlabsSpecHelper; end
@@ -33,7 +35,7 @@ class PuppetlabsSpecHelper::Tasks::CheckSymlinks
33
35
  end
34
36
 
35
37
  def ignored?(path)
36
- path = path.to_s + '/' if File.directory?(path)
38
+ path = "#{path}/" if File.directory?(path)
37
39
 
38
40
  !ignore_pathspec.match_paths([path], Dir.pwd).empty?
39
41
  end
@@ -42,6 +44,7 @@ class PuppetlabsSpecHelper::Tasks::CheckSymlinks
42
44
  @ignore_pathspec ||= PathSpec.new(DEFAULT_IGNORED).tap do |pathspec|
43
45
  IGNORE_LIST_FILES.each do |f|
44
46
  next unless File.file?(f) && File.readable?(f)
47
+
45
48
  File.open(f, 'r') { |fd| pathspec.add(fd) }
46
49
  end
47
50
  end