puppetlabs_spec_helper 2.13.1 → 3.0.0

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,14 @@ 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
+ Dir.entries(dir).reject { |f| f =~ %r{^\.} }.collect { |f| File.join(dir, f, 'spec', 'lib') }
58
+ end
59
+ components.flatten.each do |d|
60
+ $LOAD_PATH << d if FileTest.directory?(d) && !$LOAD_PATH.include?(d)
61
+ end
62
+
53
63
  RSpec.configure do |c|
54
64
  c.environmentpath = spec_path if Puppet.version.to_f >= 4.0
55
65
  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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'rake'
3
5
  require 'rspec/core/rake_task'
@@ -16,6 +18,24 @@ rescue LoadError
16
18
  # ignore
17
19
  end
18
20
 
21
+ begin
22
+ require 'puppet_blacksmith/rake_tasks'
23
+ rescue LoadError
24
+ # ignore
25
+ end
26
+
27
+ begin
28
+ require 'github_changelog_generator/task'
29
+ rescue LoadError
30
+ # ignore
31
+ end
32
+
33
+ begin
34
+ require 'puppet-strings/tasks'
35
+ rescue LoadError
36
+ # ignore
37
+ end
38
+
19
39
  parallel_tests_loaded = false
20
40
  begin
21
41
  require 'parallel_tests'
@@ -29,12 +49,13 @@ task default: [:help]
29
49
  pattern = 'spec/{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit}/**/*_spec.rb'
30
50
 
31
51
  RSpec::Core::RakeTask.new(:spec_standalone) do |t, args|
32
- t.rspec_opts = ['--color']
52
+ t.rspec_opts = []
33
53
  t.rspec_opts << ENV['CI_SPEC_OPTIONS'] unless ENV['CI_SPEC_OPTIONS'].nil?
34
54
  if ENV['CI_NODE_TOTAL'] && ENV['CI_NODE_INDEX']
35
55
  ci_total = ENV['CI_NODE_TOTAL'].to_i
36
56
  ci_index = ENV['CI_NODE_INDEX'].to_i
37
57
  raise "CI_NODE_INDEX must be between 1-#{ci_total}" unless ci_index >= 1 && ci_index <= ci_total
58
+
38
59
  files = Rake::FileList[pattern].to_a
39
60
  per_node = (files.size / ci_total.to_f).ceil
40
61
  t.pattern = if args.extras.nil? || args.extras.empty?
@@ -90,16 +111,17 @@ end
90
111
  desc 'Parallel spec tests'
91
112
  task :parallel_spec_standalone do |_t, args|
92
113
  raise 'Add the parallel_tests gem to Gemfile to enable this task' unless parallel_tests_loaded
114
+
93
115
  if Rake::FileList[pattern].to_a.empty?
94
116
  warn 'No files for parallel_spec to run against'
95
117
  else
96
- begin
97
- args = ['-t', 'rspec']
98
- args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
99
- args.concat(Rake::FileList[pattern].to_a)
100
118
 
101
- ParallelTests::CLI.new.run(args)
102
- end
119
+ args = ['-t', 'rspec']
120
+ args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
121
+ args.concat(Rake::FileList[pattern].to_a)
122
+
123
+ ParallelTests::CLI.new.run(args)
124
+
103
125
  end
104
126
  end
105
127
 
@@ -124,14 +146,15 @@ namespace :build do
124
146
  desc 'Build Puppet module with PDK'
125
147
  task :pdk do
126
148
  begin
149
+ require 'pdk/util'
127
150
  require 'pdk/module/build'
128
151
 
129
- path = PDK::Module::Build.invoke(:force => true, :'target-dir' => File.join(Dir.pwd, 'pkg'))
152
+ path = PDK::Module::Build.invoke(force: true, 'target-dir': File.join(Dir.pwd, 'pkg'))
130
153
  puts "Module built: #{path}"
131
154
  rescue LoadError
132
155
  _ = `pdk --version`
133
156
  unless $CHILD_STATUS.success?
134
- $stderr.puts 'Unable to build module. Please install PDK or add the `pdk` gem to your Gemfile.'
157
+ warn 'Unable to build module. Please install PDK or add the `pdk` gem to your Gemfile.'
135
158
  abort
136
159
  end
137
160
 
@@ -149,52 +172,34 @@ require 'puppet-lint/tasks/puppet-lint'
149
172
  # Must clear as it will not override the existing puppet-lint rake task since we require to import for
150
173
  # the PuppetLint::RakeTask
151
174
  Rake::Task[:lint].clear
152
- # Relative is not able to be set within the context of PuppetLint::RakeTask
175
+ # Utilize PuppetLint global configuration so that these settings can be tweaked by
176
+ # spec_helper.rb in an individual module
153
177
  PuppetLint.configuration.relative = true
154
- PuppetLint::RakeTask.new(:lint) do |config|
155
- config.fail_on_warnings = true
156
- config.disable_checks = %w[
157
- 80chars
158
- 140chars
159
- class_inherits_from_params_class
160
- class_parameter_defaults
161
- disable_autoloader_layout
162
- documentation
163
- single_quote_string_with_variables
164
- ]
165
- config.ignore_paths = [
166
- '.vendor/**/*.pp',
167
- 'bundle/**/*.pp',
168
- 'pkg/**/*.pp',
169
- 'spec/**/*.pp',
170
- 'tests/**/*.pp',
171
- 'types/**/*.pp',
172
- 'vendor/**/*.pp',
173
- ]
178
+ PuppetLint.configuration.ignore_paths ||= []
179
+ PuppetLint.configuration.ignore_paths << '.vendor/**/*.pp'
180
+ PuppetLint.configuration.ignore_paths << 'bundle/**/*.pp'
181
+ PuppetLint.configuration.ignore_paths << 'pkg/**/*.pp'
182
+ PuppetLint.configuration.ignore_paths << 'spec/**/*.pp'
183
+ PuppetLint.configuration.ignore_paths << 'tests/**/*.pp'
184
+ PuppetLint.configuration.ignore_paths << 'types/**/*.pp'
185
+ PuppetLint.configuration.ignore_paths << 'vendor/**/*.pp'
186
+ puppet_lint_disable_checks = %w[
187
+ 80chars
188
+ 140chars
189
+ class_inherits_from_params_class
190
+ class_parameter_defaults
191
+ disable_autoloader_layout
192
+ documentation
193
+ single_quote_string_with_variables
194
+ ]
195
+ puppet_lint_disable_checks.each do |check|
196
+ PuppetLint.configuration.send("disable_#{check}")
174
197
  end
198
+ PuppetLint::RakeTask.new(:lint)
175
199
 
176
200
  desc 'Run puppet-lint and fix issues automatically'
177
201
  PuppetLint::RakeTask.new(:lint_fix) do |config|
178
- config.fail_on_warnings = true
179
202
  config.fix = true
180
- config.disable_checks = %w[
181
- 80chars
182
- 140chars
183
- class_inherits_from_params_class
184
- class_parameter_defaults
185
- disable_autoloader_layout
186
- documentation
187
- single_quote_string_with_variables
188
- ]
189
- config.ignore_paths = [
190
- '.vendor/**/*.pp',
191
- 'bundle/**/*.pp',
192
- 'pkg/**/*.pp',
193
- 'spec/**/*.pp',
194
- 'tests/**/*.pp',
195
- 'types/**/*.pp',
196
- 'vendor/**/*.pp',
197
- ]
198
203
  end
199
204
 
200
205
  require 'puppet-syntax/tasks/puppet-syntax'
@@ -202,7 +207,8 @@ PuppetSyntax.exclude_paths ||= []
202
207
  PuppetSyntax.exclude_paths << 'spec/fixtures/**/*'
203
208
  PuppetSyntax.exclude_paths << 'pkg/**/*'
204
209
  PuppetSyntax.exclude_paths << 'vendor/**/*'
205
- PuppetSyntax.exclude_paths << 'plans/*'
210
+ PuppetSyntax.exclude_paths << '.vendor/**/*'
211
+ PuppetSyntax.exclude_paths << 'plans/**/*'
206
212
  if Puppet.version.to_f < 4.0
207
213
  PuppetSyntax.exclude_paths << 'types/**/*'
208
214
  end
@@ -243,7 +249,7 @@ task :compute_dev_version do
243
249
  version = modinfo['version']
244
250
  elsif File.exist?('Modulefile')
245
251
  modfile = File.read('Modulefile')
246
- version = modfile.match(%r{\nversion[ ]+['"](.*)['"]})[1]
252
+ version = modfile.match(%r{\nversion +['"](.*)['"]})[1]
247
253
  else
248
254
  raise 'Could not find a metadata.json or Modulefile! Cannot compute dev version without one or the other!'
249
255
  end
@@ -255,12 +261,12 @@ task :compute_dev_version do
255
261
  # If the branch is a release branch we append an 'r' into the new_version,
256
262
  # this is due to the release branch buildID conflicting with master branch when trying to push to the staging forge.
257
263
  # More info can be found at https://tickets.puppetlabs.com/browse/FM-6170
258
- new_version = if build = ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER']
264
+ new_version = if build = (ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER'])
259
265
  if branch.eql? 'release'
260
- '%s-%s%04d-%s' % [version, 'r', build, sha]
266
+ '%s-%s%04d-%s' % [version, 'r', build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
261
267
  else
262
- '%s-%04d-%s' % [version, build, sha]
263
- end
268
+ '%s-%04d-%s' % [version, build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
269
+ end
264
270
  else
265
271
  "#{version}-#{sha}"
266
272
  end
@@ -277,10 +283,7 @@ task :release_checks do
277
283
  else
278
284
  Rake::Task[:spec].invoke
279
285
  end
280
- Rake::Task['check:symlinks'].invoke
281
- Rake::Task['check:test_file'].invoke
282
- Rake::Task['check:dot_underscore'].invoke
283
- Rake::Task['check:git_ignore'].invoke
286
+ Rake::Task[:check].invoke
284
287
  end
285
288
 
286
289
  namespace :check do
@@ -321,6 +324,9 @@ namespace :check do
321
324
  end
322
325
  end
323
326
 
327
+ desc 'Run static pre release checks'
328
+ task check: ['check:symlinks', 'check:test_file', 'check:dot_underscore', 'check:git_ignore']
329
+
324
330
  desc 'Display the list of available rake tasks'
325
331
  task :help do
326
332
  system('rake -T')
@@ -339,25 +345,83 @@ rescue LoadError
339
345
  end
340
346
  end
341
347
 
342
- module_dir = Dir.pwd
343
- locales_dir = File.absolute_path('locales', module_dir)
344
- # if the task is allowed to run when the module does not have a locales directory,
345
- # the task is run in the puppet gem instead and creates a POT there.
346
- puts 'gettext-setup tasks will only be loaded if the locales/ directory is present' if Rake.verbose == true
347
- if File.exist? locales_dir
348
- begin
349
- spec = Gem::Specification.find_by_name 'gettext-setup'
350
- load "#{spec.gem_dir}/lib/tasks/gettext.rake"
351
- # Initialization requires a valid locales directory
352
- GettextSetup.initialize_config(locales_dir)
353
- namespace :module do
354
- desc 'Runs all tasks to build a modules POT file for internationalization'
355
- task :pot_gen do
356
- Rake::Task['gettext:pot'].invoke
357
- Rake::Task['gettext:metadata_pot'].invoke("#{module_dir}/metadata.json")
348
+ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_since_tag = nil, changelog_tag_pattern = 'v%s')
349
+ if Bundler.rubygems.find_name('github_changelog_generator').any?
350
+ # needed a place to hide these methods
351
+ # rubocop:disable Lint/NestedMethodDefinition
352
+ def changelog_user_from_metadata
353
+ result = JSON.parse(File.read('metadata.json'))['author']
354
+ raise 'unable to find the changelog_user in .sync.yml, or the author in metadata.json' if result.nil?
355
+
356
+ puts "GitHubChangelogGenerator user:#{result}"
357
+ result
358
+ end
359
+
360
+ def changelog_project_from_metadata
361
+ result = JSON.parse(File.read('metadata.json'))['name']
362
+ raise 'unable to find the changelog_project in .sync.yml or the name in metadata.json' if result.nil?
363
+
364
+ puts "GitHubChangelogGenerator project:#{result}"
365
+ result
366
+ end
367
+
368
+ def changelog_future_release
369
+ return unless Rake.application.top_level_tasks.include? 'changelog'
370
+
371
+ result = JSON.parse(File.read('metadata.json'))['version']
372
+ raise 'unable to find the future_release (version) in metadata.json' if result.nil?
373
+
374
+ puts "GitHubChangelogGenerator future_release:#{result}"
375
+ result
376
+ end
377
+ # rubocop:enable Lint/NestedMethodDefinition
378
+
379
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
380
+ if ENV['CHANGELOG_GITHUB_TOKEN'].nil?
381
+ raise "Set CHANGELOG_GITHUB_TOKEN environment variable eg 'export CHANGELOG_GITHUB_TOKEN=valid_token_here'"
358
382
  end
383
+
384
+ config.user = changelog_user || changelog_user_from_metadata
385
+ config.project = changelog_project || changelog_project_from_metadata
386
+ config.since_tag = changelog_since_tag if changelog_since_tag
387
+ config.future_release = changelog_tag_pattern % changelog_future_release.to_s
388
+ config.exclude_labels = ['maintenance']
389
+ config.header = "# Change log\n\nAll notable changes to this project will be documented in this file. " \
390
+ 'The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres ' \
391
+ 'to [Semantic Versioning](https://semver.org).'
392
+ config.add_pr_wo_labels = true
393
+ config.issues = false
394
+ config.merge_prefix = '### UNCATEGORIZED PRS; GO LABEL THEM'
395
+ config.configure_sections = {
396
+ 'Changed' => {
397
+ 'prefix' => '### Changed',
398
+ 'labels' => ['backwards-incompatible'],
399
+ },
400
+ 'Added' => {
401
+ 'prefix' => '### Added',
402
+ 'labels' => %w[feature enhancement],
403
+ },
404
+ 'Fixed' => {
405
+ 'prefix' => '### Fixed',
406
+ 'labels' => ['bugfix'],
407
+ },
408
+ }
409
+ end
410
+ else
411
+ desc 'Generate a Changelog from GitHub'
412
+ task :changelog do
413
+ raise <<~MESSAGE
414
+ The changelog tasks depends on unreleased features of the github_changelog_generator gem.
415
+ Please manually add it to your .sync.yml for now, and run `pdk update`:
416
+ ---
417
+ Gemfile:
418
+ optional:
419
+ ':development':
420
+ - gem: 'github_changelog_generator'
421
+ git: 'https://github.com/skywinder/github-changelog-generator'
422
+ ref: '20ee04ba1234e9e83eb2ffb5056e23d641c7a018'
423
+ condition: "Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2')"
424
+ MESSAGE
359
425
  end
360
- rescue Gem::LoadError
361
- puts 'No gettext-setup gem found, skipping GettextSetup config initialization' if Rake.verbose == true
362
426
  end
363
427
  end