puppetlabs_spec_helper 2.6.2 → 4.0.1

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/README.md CHANGED
@@ -59,7 +59,7 @@ Host github.com
59
59
 
60
60
  ```
61
61
 
62
- Note: parallel downloads is only available for repositories and not forge modules.
62
+ Note: parallel downloads are available for repositories and forge modules.
63
63
 
64
64
  ### Parallel tests
65
65
  It is also possible to use the `parallel_tests` Gem via the `:parallel_spec` Rake task to run rspec commands in parallel on groups of spec files.
@@ -79,8 +79,7 @@ And then to run spec tests in parallel:
79
79
  Issues
80
80
  ======
81
81
 
82
- Please file issues against this project at the [Puppet Labs Issue
83
- Tracker](https://tickets.puppetlabs.com/browse/MODULES)
82
+ Please file issues against this project at the [Puppet Labs Issue Tracker](https://tickets.puppetlabs.com/browse/MODULES)
84
83
 
85
84
  The Long Version
86
85
  ----------------
@@ -103,10 +102,10 @@ This project is intended to serve two purposes:
103
102
  To Use this Project
104
103
  ===================
105
104
 
106
- The most common usage scenario is that you will check out the 'master'
105
+ The most common usage scenario is that you will check out the 'main'
107
106
  branch of this project from github, and install it as a rubygem.
108
107
  There should be few or no cases where you would want to have any other
109
- branch of this project besides master/HEAD.
108
+ branch of this project besides main/HEAD.
110
109
 
111
110
  Running on non-current ruby versions
112
111
  ------------------------------------
@@ -142,9 +141,6 @@ A number of the Puppet parser features, controlled via configuration during a
142
141
  normal puppet run, can be controlled by exporting specific environment
143
142
  variables for the spec run. These are:
144
143
 
145
- * ``FUTURE_PARSER`` - set to "yes" to enable the [future parser](http://docs.puppetlabs.com/puppet/latest/reference/experiments_future.html),
146
- the equivalent of setting [parser=future](http://docs.puppetlabs.com/references/latest/configuration.html#parser)
147
- in puppet.conf.
148
144
  * ``STRICT_VARIABLES`` - set to "yes" to enable set to strict variable checking when using Puppet versions between 3.5 and 4.0;
149
145
  set to "no" to disable strict variable checking on Puppet versions 4.0, and later.
150
146
  See [strict_variables](http://docs.puppetlabs.com/references/latest/configuration.html#strictvariables)
@@ -153,14 +149,18 @@ variables for the spec run. These are:
153
149
  to set the order of unrelated resources when applying a catalog. Leave unset for the default
154
150
  behavior, currently "random". This is equivalent to setting [ordering](http://docs.puppetlabs.com/references/latest/configuration.html#ordering)
155
151
  in puppet.conf.
156
- * ``STRINGIFY_FACTS`` - set to "no" to enable [structured facts](http://docs.puppetlabs.com/facter/2.0/fact_overview.html#writing-structured-facts),
157
- otherwise leave unset to retain the current default behavior. This is equivalent to setting
158
- [stringify_facts=false](http://docs.puppetlabs.com/references/latest/configuration.html#stringifyfacts)
159
- in puppet.conf.
160
- * ``TRUSTED_NODE_DATA`` - set to "yes" to enable [the $facts hash and trusted node data](http://docs.puppetlabs.com/puppet/latest/reference/lang_facts_and_builtin_vars.html),
161
- which enabled ``$facts`` and ``$trusted`` hashes. This is equivalent to setting
162
- [trusted_node_data=true](http://docs.puppetlabs.com/references/latest/configuration.html#trustednodedata)
163
- in puppet.conf.
152
+ * The following options only affect Puppet 3.x:
153
+ * ``FUTURE_PARSER`` _(Puppet 3.x, starting with 3.2)_ - set to "yes" to enable the [future parser](https://docs.puppet.com/puppet/3/experiments_future.html),
154
+ the equivalent of setting [parser=future](https://puppet.com/docs/puppet/3.8/configuration.html#parser)
155
+ in puppet.conf.
156
+ * ``TRUSTED_NODE_DATA`` _(Puppet 3.x, starting with 3.4)_ - set to "yes" to enable [the $facts hash and trusted node data](https://puppet.com/docs/puppet/3.8/lang_facts_and_builtin_vars.html#trusted-facts),
157
+ which enabled ``$facts`` and ``$trusted`` hashes. This is equivalent to setting
158
+ [trusted_node_data=true](https://puppet.com/docs/puppet/3.8/configuration.html#trustednodedata)
159
+ in puppet.conf.
160
+ * ``STRINGIFY_FACTS`` _(Puppet 3.x, starting with 3.5)_ - set to "no" to enable [structured facts](http://docs.puppetlabs.com/facter/2.0/fact_overview.html#writing-structured-facts),
161
+ otherwise leave unset to retain the current default behavior. This is equivalent to setting
162
+ [stringify_facts=false](https://puppet.com/docs/puppet/3.8/configuration.html#stringifyfacts)
163
+ in puppet.conf.
164
164
 
165
165
  As an example, to run spec tests with the future parser, strict variable checking,
166
166
  and manifest ordering, you would:
@@ -191,32 +191,40 @@ file named `.fixtures.yml` in the root of the project. You can specify a alterna
191
191
  You can use the `MODULE_WORKING_DIR` environment variable to specify a diffent location when installing module fixtures via the forge. By default the
192
192
  working directory is `<module directory>/spec/fixtures/work-dir`.
193
193
 
194
- When specifying the repo source of the fixture you have a few options as to which revision of the codebase you wish to use.
194
+ When specifying the repo source of the fixture you have a few options as to which revision of the codebase you wish to use, and optionally, the puppet versions where the fixture is needed.
195
195
 
196
- * repo - the url to the repo
197
- * scm - options include git or hg. This is an optional step as the helper code will figure out which scm is used.
196
+ * `repo` - the url to the repo
197
+ * `scm` - options include git or hg. This is an optional step as the helper code will figure out which scm is used.
198
198
 
199
199
  ```yaml
200
200
  scm: git
201
201
  scm: hg
202
202
  ```
203
203
 
204
- * target - the directory name to clone the repo into ie. `target: mymodule` defaults to the repo name (Optional)
205
- * subdir - directory to be removed from the cloned repo. Its contents will be moved to the root directory (Optional)
206
- * ref - used to specify the tag name like version hash of commit (Optional)
204
+ * `target` - the directory name to clone the repo into ie. `target: mymodule` defaults to the repo name (Optional)
205
+ * `subdir` - directory to be removed from the cloned repo. Its contents will be moved to the root directory (Optional)
206
+ * `ref` - used to specify the tag name (like version) or commit hash to be checked out (Optional). Branch names should use the `branch` option instead.
207
207
 
208
208
  ```yaml
209
209
  ref: 1.0.0
210
210
  ref: 880fca52c
211
211
  ```
212
- * branch - used to specify the branch name you want to use ie. `branch: development`
213
- * flags - additional flags passed to the module installer (both puppet and scm)
212
+ * `branch` - used to specify the branch name you want to use ie. `branch: development`
213
+ * `flags` - additional flags passed to the module installer (both puppet and scm)
214
214
 
215
215
  ```yaml
216
216
  flags: --verbose
217
217
  ```
218
+ * `puppet_version` - versions of puppet for which the fixture should be installed. Ruby version constraints are supported. Only works when the `semantic_puppet` gem is available (shipped with puppet 4.0 and up, by default).
219
+
220
+ ```yaml
221
+ puppet_version: '>= 6.0.0'
222
+ ```
223
+
224
+ **Notes:**
218
225
 
219
- **Note:** ref and branch can be used together to get a specific revision on a specific branch
226
+ * `ref` and `branch` can be used together to get a specific revision on a specific branch
227
+ * Top level `defaults` option could be used to set global options
220
228
 
221
229
  Fixtures Examples
222
230
  -----------------
@@ -252,7 +260,7 @@ fixtures:
252
260
  target: "spec/fixtures/control_repos"
253
261
  ```
254
262
 
255
- Specify that the git tag `2.4.2` of `stdlib' should be checked out:
263
+ Specify that the git tag `2.4.2` of `stdlib` should be checked out:
256
264
 
257
265
  ```yaml
258
266
  fixtures:
@@ -263,6 +271,16 @@ fixtures:
263
271
  ref: "2.6.0"
264
272
  ```
265
273
 
274
+ Only install the `yumrepo_core` module when testing against Puppet 6 or greater:
275
+
276
+ ```yaml
277
+ fixtures:
278
+ repositories:
279
+ yumrepo_core:
280
+ repo: "git://github.com/puppetlabs/puppetlabs-yumrepo_core"
281
+ puppet_version: ">= 6.0.0"
282
+ ```
283
+
266
284
  Move manifests and siblings to root directory when they are inside a `code` directory:
267
285
 
268
286
  ```yaml
@@ -300,6 +318,27 @@ fixtures:
300
318
  flags: "--verbose"
301
319
  ```
302
320
 
321
+ Use `defaults` to define global parameters:
322
+
323
+ ```yaml
324
+ defaults:
325
+ forge_modules:
326
+ flags: "--module_repository https://my_repo.com"
327
+ fixtures:
328
+ forge_modules:
329
+ stdlib:
330
+ repo: "puppetlabs/stdlib"
331
+ ref: "2.6.0"
332
+ repositories:
333
+ firewall:
334
+ repo: "git://github.com/puppetlabs/puppetlabs-firewall"
335
+ ref: "2.6.0"
336
+ ```
337
+
338
+ Fixture Loading
339
+ ---------------
340
+ Any module that has a `spec/lib` directory will be available on the ruby `LOAD_PATH` for tests to consume. This allows modules to provide additional helper code to be supplied. The [augeasprovider_core](https://github.com/hercules-team/augeasproviders_core) module has [some examples](https://github.com/hercules-team/augeasproviders_core/tree/master/spec/lib).
341
+
303
342
  Testing Parser Functions
304
343
  ========================
305
344
 
@@ -345,14 +384,18 @@ remembering to duplicate any existing environment variables:
345
384
  env:
346
385
  - FUTURE_PARSER=yes CI_NODE_TOTAL=2 CI_NODE_INDEX=1
347
386
  - FUTURE_PARSER=yes CI_NODE_TOTAL=2 CI_NODE_INDEX=2
348
-
387
+
349
388
  #### Running tests tagged with test tiers
350
- To run tests tagged with risk levels set the ``TEST_TIERS`` environment variable to a comma-separated list of
351
- the appropriate tiers.
389
+ To run tests tagged with risk levels set the ``TEST_TIERS`` environment variable to a comma-separated list of the appropriate tiers.
352
390
 
353
- For example: to run tests marked ``tier_high => true`` and ``tier_medium => true`` in the same test run set the
391
+ For example: to run tests marked ``tier_high => true`` and ``tier_medium => true`` in the same test run set the
354
392
  environment variable``TEST_TIERS=high,medium``
355
-
393
+
394
+ By default ``TEST_TIERS`` only accepts low, medium and high as valid tiers. If you would like to use your own keywords to set the environment variable ``TEST_TIERS_ALLOWED``.
395
+
396
+ For example: to use the keywords dev, rnd, staging and production you can set
397
+ ``TEST_TIERS_ALLOWED=dev,rnd,staging,production``. Then you would be able to run tests marked ``tier_dev => true``, ``tier_production => true`` with ``TEST_TIERS=dev,production``
398
+
356
399
  Note, if the ``TEST_TIERS`` environment variable is set to empty string or nil, all tiers will be executed.
357
400
 
358
401
 
@@ -364,80 +407,20 @@ See the documentation of [RSpec-Puppet](https://github.com/rodjek/rspec-puppet)
364
407
  for Puppet manifest coverage reports.
365
408
 
366
409
  Starting with Ruby 1.9, the *de facto* standard for Ruby code coverage is
367
- [SimpleCov](https://github.com/colszowka/simplecov).
368
- You can add it to your module like this:
369
-
370
- ```Ruby
371
- # First line of spec/spec_helper.rb
372
- require 'simplecov'
373
-
374
- SimpleCov.start do
375
- add_filter '/spec/'
376
- # Exclude bundled Gems in `/.vendor/`
377
- add_filter '/.vendor/'
378
- end
379
-
380
- require 'puppetlabs_spec_helper/module_spec_helper'
381
- # Further content
382
- ```
410
+ [SimpleCov](https://github.com/colszowka/simplecov). It is implemented as a Rake task in this gem.
383
411
 
384
- The reports will then be generated every time you invoke RSpec, e.g. via `rake spec`,
385
- and are written to `/coverage/`, which you should add to `.gitignore`.
412
+ To run code coverage:
386
413
 
387
- Remember to add `gem 'simplecov', require: false` to your `Gemfile`.
414
+ $ rake spec:simplecov
388
415
 
389
- Using Code Climate
390
- ------------------
416
+ Reports are written to `/coverage/`, which you should add to `.gitignore`.
391
417
 
392
- You can also use [Code Climate](https://codeclimate.com/) together with SimpleCov:
418
+ The reports can be generated every time you invoke RSpec, e.g. via `rake spec`,
419
+ You can enable it, set the following environment variable:s
393
420
 
394
- ```Ruby
395
- # First line of spec/spec_helper.rb
396
- require 'codeclimate-test-reporter'
421
+ ``SIMPLECOV=yes``
397
422
 
398
- SimpleCov.formatters = [
399
- SimpleCov::Formatter::HTMLFormatter,
400
- CodeClimate::TestReporter::Formatter
401
- ]
402
-
403
- SimpleCov.start do
404
- add_filter '/spec/'
405
- # Exclude bundled Gems in `/.vendor/`
406
- add_filter '/.vendor/'
407
- end
408
-
409
- require 'puppetlabs_spec_helper/module_spec_helper'
410
- # Further content
411
- ```
412
-
413
- Remember to add `gem 'codeclimate-test-reporter', require: false` to your `Gemfile`.
414
-
415
- Using Coveralls
416
- ---------------
417
-
418
- You can also use [Coveralls](https://coveralls.io/) together with SimpleCov:
419
-
420
- ```Ruby
421
- # First line of spec/spec_helper.rb
422
- require 'simplecov'
423
- require 'coveralls'
424
-
425
- SimpleCov.formatters = [
426
- SimpleCov::Formatter::HTMLFormatter,
427
- Coveralls::SimpleCov::Formatter
428
- ]
429
-
430
- SimpleCov.start do
431
- add_filter '/spec/'
432
- # Exclude bundled Gems in `/.vendor/`
433
- add_filter '/.vendor/'
434
- end
435
-
436
- require 'puppetlabs_spec_helper/module_spec_helper'
437
- # Further content
438
- ```
439
-
440
- Remember to add `gem 'coveralls', require: false` to your `Gemfile`.
423
+ Remember to add the simplecov-console and codecov gems to your `Gemfile`. If you run `spec:simplecov` on Travis-CI or any of the other supported CI services, reports gets automatically uploaded to https://codecov.io/ .
441
424
 
442
425
  Some Notes for Windows Users
443
426
  ============================
@@ -451,7 +434,7 @@ This is related to a registry security setting requiring elevated privileges to
451
434
 
452
435
  Currently, there are two known approaches to get around this problem.
453
436
 
454
- - run your windows shell (cmd) as an Administrator
437
+ - run your windows shell (cmd) as an Administrator
455
438
  or
456
439
  - modify the registry entry settings to allow symbolic links to be created.
457
440
 
@@ -463,5 +446,52 @@ The following links may give you some insight into why...
463
446
 
464
447
  [Microsoft TechNet](https://technet.microsoft.com/en-us/library/cc754077.aspx)
465
448
 
449
+ mock_with
450
+ =========
451
+
452
+ There are two major mocking frameworks in modules test suites today: [mocha](https://rubygems.org/gems/mocha) and [rspec-mocks](https://rubygems.org/gems/rspec-mocks). We recommend that you choose rspec-mocks explicitly by specifying `mock_with`, either in your `spec_helper.rb` like so:
453
+
454
+ ```
455
+ RSpec.configure do |c|
456
+ c.mock_with :rspec
457
+ end
458
+ require 'puppetlabs_spec_helper/module_spec_helper'
459
+ ```
460
+
461
+ or by using Puppet Development Kit's [`mock_with` option in `.sync.yml`](https://github.com/puppetlabs/pdk-templates#specspec_helperrb) and `pdk update`.
462
+
463
+ You can also continue to use mocha by explicitly specifying `:mocha`, following the [mocha documentation](http://gofreerange.com/mocha/docs/).
464
+
465
+ Migration
466
+ ---------
467
+
468
+ To migrate from mocha to rspec-mocks, in many simple cases the following two kinds of changes are all you need:
469
+
470
+ Translate all stubs:
471
+ ```
472
+ context.stubs(:some_method).with(arguments).returns('value')
473
+ ```
474
+ to this:
475
+ ```
476
+ allow(context).to receive(:some_method).with(arguments).and_return('value')
477
+ ```
478
+
479
+ Translate all expectations:
480
+ ```
481
+ context.expects(:some_method).with(arguments).returns('value')
482
+ ```
483
+ to this:
484
+ ```
485
+ expect(context).to receive(:some_method).with(arguments).and_return('value')
486
+ ```
487
+
488
+ Rationale
489
+ ---------
490
+
491
+ * As a part of the RSpec project, rspec-mocks integration is better.
492
+ * mocha is extending base ruby objects with its `stubs`, and `expects` methods, polluting the global namespace, with a potential for subtle and gnarly errors.
493
+ * Currently both rspec-mocks and mocha get loaded, leading to test suites that use both.
494
+ * puppetlabs_spec_helper loads and configures mocha unconditionally, causing friction when a different mocking framework is wanted.
495
+ * mocha is an additional dependency to carry.
466
496
 
467
497
  EOF
data/Rakefile CHANGED
@@ -1,15 +1,45 @@
1
- # encoding: utf-8
2
- require "bundler/gem_tasks"
3
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
4
2
 
5
- task :default => :spec
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ def gem_present(name)
7
+ !Bundler.rubygems.find_name(name).empty?
8
+ end
6
9
 
7
10
  RSpec::Core::RakeTask.new(:spec) do |spec|
8
11
  spec.pattern = FileList['spec/**/*_spec.rb'].exclude('spec/fixtures/**/*_spec.rb')
9
12
  end
10
13
 
11
- require 'rubocop/rake_task'
12
- RuboCop::RakeTask.new
13
-
14
14
  require 'yard'
15
15
  YARD::Rake::YardocTask.new
16
+
17
+ default_tasks = [:spec]
18
+
19
+ if gem_present 'rubocop'
20
+ require 'rubocop/rake_task'
21
+ RuboCop::RakeTask.new
22
+ default_tasks.unshift(:rubocop)
23
+ end
24
+
25
+ task default: default_tasks
26
+
27
+ #### CHANGELOG ####
28
+ begin
29
+ require 'github_changelog_generator/task'
30
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
31
+ require 'puppetlabs_spec_helper/version'
32
+ config.since_tag = 'v2.8.0'
33
+ config.future_release = "v#{PuppetlabsSpecHelper::VERSION}"
34
+ config.header = "# Changelog\n\n" \
35
+ "All significant changes to this repo will be summarized in this file.\n"
36
+ # config.include_labels = %w[enhancement bug]
37
+ config.user = 'puppetlabs'
38
+ config.project = 'puppetlabs_spec_helper'
39
+ end
40
+ rescue LoadError
41
+ desc 'Install github_changelog_generator to get access to automatic changelog generation'
42
+ task :changelog do
43
+ raise 'Install github_changelog_generator to get access to automatic changelog generation'
44
+ end
45
+ end
@@ -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'
@@ -18,6 +20,46 @@ env_module_path = ENV['MODULEPATH']
18
20
  module_path = File.join(fixture_path, 'modules')
19
21
 
20
22
  module_path = [module_path, env_module_path].join(File::PATH_SEPARATOR) if env_module_path
23
+ if ENV['SIMPLECOV'] == 'yes'
24
+ begin
25
+ require 'simplecov'
26
+ require 'simplecov-console'
27
+ require 'codecov'
28
+
29
+ SimpleCov.formatters = [
30
+ SimpleCov::Formatter::HTMLFormatter,
31
+ SimpleCov::Formatter::Console,
32
+ SimpleCov::Formatter::Codecov,
33
+ ]
34
+ SimpleCov.start do
35
+ track_files 'lib/**/*.rb'
36
+ add_filter '/spec'
37
+
38
+ # do not track vendored files
39
+ add_filter '/vendor'
40
+ add_filter '/.vendor'
41
+
42
+ # do not track gitignored files
43
+ # this adds about 4 seconds to the coverage check
44
+ # this could definitely be optimized
45
+ add_filter do |f|
46
+ # system returns true if exit status is 0, which with git-check-ignore means file is ignored
47
+ system("git check-ignore --quiet #{f.filename}")
48
+ end
49
+ end
50
+ rescue LoadError
51
+ raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task'
52
+ end
53
+ end
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
21
63
 
22
64
  RSpec.configure do |c|
23
65
  c.environmentpath = spec_path if Puppet.version.to_f >= 4.0
@@ -26,7 +68,12 @@ RSpec.configure do |c|
26
68
  c.parser = 'future' if ENV['FUTURE_PARSER'] == 'yes'
27
69
 
28
70
  c.before :each do
29
- Puppet.features.stubs(:root?).returns(true)
71
+ if c.mock_framework.framework_name == :rspec
72
+ allow(Puppet.features).to receive(:root?).and_return(true)
73
+ else
74
+ Puppet.features.stubs(:root?).returns(true)
75
+ end
76
+
30
77
  # stringify_facts and trusted_node_data were removed in puppet4
31
78
  if Puppet.version.to_f < 4.0
32
79
  Puppet.settings[:stringify_facts] = false if ENV['STRINGIFY_FACTS'] == 'no'
@@ -1,14 +1,25 @@
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
4
6
  ARGV.clear
5
7
 
6
- # This is needed because we're using mocha with rspec instead of Test::Unit or MiniTest
7
- ENV['MOCHA_OPTIONS']='skip_integration'
8
-
9
8
  require 'puppet'
10
9
  require 'rspec/expectations'
11
- require 'mocha/api'
10
+
11
+ # Detect whether the module is overriding the choice of mocking framework
12
+ # @mock_framework is used since more than seven years, and we need to avoid
13
+ # `mock_framework`'s autoloading to distinguish between the default, and
14
+ # the module's choice.
15
+ # See also below in RSpec.configure
16
+ if RSpec.configuration.instance_variable_get(:@mock_framework).nil?
17
+ # This is needed because we're using mocha with rspec instead of Test::Unit or MiniTest
18
+ ENV['MOCHA_OPTIONS'] = 'skip_integration'
19
+
20
+ # Current versions of RSpec already load this for us, but who knows what's used out there?
21
+ require 'mocha/api'
22
+ end
12
23
 
13
24
  require 'pathname'
14
25
  require 'tmpdir'
@@ -36,7 +47,7 @@ require 'puppetlabs_spec_helper/puppetlabs_spec/files'
36
47
  # to compatibility mode for older versions of puppet.
37
48
  begin
38
49
  require 'puppet/test/test_helper'
39
- rescue LoadError => err
50
+ rescue LoadError
40
51
  end
41
52
 
42
53
  # This is just a utility class to allow us to isolate the various version-specific
@@ -49,7 +60,7 @@ module Puppet
49
60
  # Puppet's Settings singleton object, and other fun implementation details
50
61
  # that code external to puppet should really never know about.
51
62
  def self.initialize_via_fallback_compatibility(config)
52
- $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.')
53
64
  config.before :all do
54
65
  # nothing to do for now
55
66
  end
@@ -70,11 +81,11 @@ module Puppet
70
81
 
71
82
  # Set the confdir and vardir to gibberish so that tests
72
83
  # have to be correctly mocked.
73
- Puppet[:confdir] = "/dev/null"
74
- Puppet[:vardir] = "/dev/null"
84
+ Puppet[:confdir] = '/dev/null'
85
+ Puppet[:vardir] = '/dev/null'
75
86
 
76
87
  # Avoid opening ports to the outside world
77
- Puppet.settings[:bindaddress] = "127.0.0.1"
88
+ Puppet.settings[:bindaddress] = '127.0.0.1'
78
89
  end
79
90
 
80
91
  config.after :each do
@@ -82,7 +93,7 @@ module Puppet
82
93
 
83
94
  Puppet::Node::Environment.clear
84
95
  Puppet::Util::Storage.clear
85
- Puppet::Util::ExecutionStub.reset if Puppet::Util.constants.include? "ExecutionStub"
96
+ Puppet::Util::ExecutionStub.reset if Puppet::Util.constants.include? 'ExecutionStub'
86
97
 
87
98
  PuppetlabsSpec::Files.cleanup
88
99
  end
@@ -91,7 +102,7 @@ module Puppet
91
102
  end
92
103
 
93
104
  # JJM Hack to make the stdlib tests run in Puppet 2.6 (See puppet commit cf183534)
94
- if not Puppet.constants.include? "Test" then
105
+ unless Puppet.constants.include? 'Test'
95
106
  module Puppet::Test
96
107
  class LogCollector
97
108
  def initialize(logs)
@@ -104,7 +115,7 @@ if not Puppet.constants.include? "Test" then
104
115
  end
105
116
  end
106
117
  Puppet::Util::Log.newdesttype :log_collector do
107
- match "Puppet::Test::LogCollector"
118
+ match 'Puppet::Test::LogCollector'
108
119
 
109
120
  def initialize(messages)
110
121
  @messages = messages
@@ -118,11 +129,17 @@ end
118
129
 
119
130
  # And here is where we do the main rspec configuration / setup.
120
131
  RSpec.configure do |config|
121
- # Some modules depend on having mocha set up for them
122
- config.mock_with :mocha
132
+ # Detect whether the module is overriding the choice of mocking framework
133
+ # @mock_framework is used since more than seven years, and we need to avoid
134
+ # `mock_framework`'s autoloading to distinguish between the default, and
135
+ # the module's choice.
136
+ if config.instance_variable_get(:@mock_framework).nil?
137
+ RSpec.warn_deprecation('puppetlabs_spec_helper: defaults `mock_with` to `:mocha`. See https://github.com/puppetlabs/puppetlabs_spec_helper#mock_with to choose a sensible value for you')
138
+ config.mock_with :mocha
139
+ end
123
140
 
124
141
  # determine whether we can use the new API or not, and call the appropriate initializer method.
125
- if (defined?(Puppet::Test::TestHelper))
142
+ if defined?(Puppet::Test::TestHelper)
126
143
  # This case is handled by rspec-puppet since v1.0.0 (via 41257b33cb1f9ade4426b044f70be511b0c89112)
127
144
  else
128
145
  Puppet::PuppetSpecInitializer.initialize_via_fallback_compatibility(config)
@@ -158,5 +175,4 @@ RSpec.configure do |config|
158
175
  Puppet::Util::Log.close_all
159
176
  Puppet::Util::Log.level = @log_level
160
177
  end
161
-
162
178
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'tempfile'
3
5
  require 'pathname'
@@ -18,11 +20,11 @@ module PuppetlabsSpec::Files
18
20
 
19
21
  def self.cleanup
20
22
  $global_tempfiles ||= []
21
- while path = $global_tempfiles.pop do
22
- fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
23
+ while path = $global_tempfiles.pop
24
+ raise "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
23
25
 
24
26
  begin
25
- FileUtils.rm_r path, :secure => true
27
+ FileUtils.rm_r path, secure: true
26
28
  rescue Errno::ENOENT
27
29
  # nothing to do
28
30
  end
@@ -1,8 +1,9 @@
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.
4
6
  module PuppetlabsSpec::Fixtures
5
-
6
7
  # Returns the joined path of the global FIXTURE_DIR plus any path given to it
7
8
  def fixtures(*rest)
8
9
  File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
@@ -13,21 +14,23 @@ module PuppetlabsSpec::Fixtures
13
14
  # <project>/spec/fixture/unit/facter/foo
14
15
  def my_fixture_dir
15
16
  callers = caller
16
- while line = callers.shift do
17
+ while line = callers.shift
17
18
  next unless found = line.match(%r{/spec/(.*)_spec\.rb:})
19
+
18
20
  return fixtures(found[1])
19
21
  end
20
- fail "sorry, I couldn't work out your path from the caller stack!"
22
+ raise "sorry, I couldn't work out your path from the caller stack!"
21
23
  end
22
24
 
23
25
  # Given a name, returns the full path of a file from your relative fixture
24
26
  # dir as returned by my_fixture_dir.
25
27
  def my_fixture(name)
26
28
  file = File.join(my_fixture_dir, name)
27
- unless File.readable? file then
28
- fail "fixture '#{name}' for #{my_fixture_dir} is not readable"
29
+ unless File.readable? file
30
+ raise "fixture '#{name}' for #{my_fixture_dir} is not readable"
29
31
  end
30
- return file
32
+
33
+ file
31
34
  end
32
35
 
33
36
  # Return the contents of the file using read when given a name. Uses
@@ -38,12 +41,13 @@ module PuppetlabsSpec::Fixtures
38
41
 
39
42
  # Provides a block mechanism for iterating across the files in your fixture
40
43
  # area.
41
- def my_fixtures(glob = '*', flags = 0)
44
+ def my_fixtures(glob = '*', flags = 0, &block)
42
45
  files = Dir.glob(File.join(my_fixture_dir, glob), flags)
43
- unless files.length > 0 then
44
- fail "fixture '#{glob}' for #{my_fixture_dir} had no files!"
46
+ if files.empty?
47
+ raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
45
48
  end
46
- block_given? and files.each do |file| yield file end
49
+
50
+ block_given? && files.each(&block)
47
51
  files
48
52
  end
49
53
  end