rspec-puppet 2.7.0 → 2.12.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fe98cc153715d0f4a7ea74302e22db11b27f256b
4
- data.tar.gz: 0ff2d613a201b5b50779674bc126904555203c66
2
+ SHA256:
3
+ metadata.gz: c0983cc8fe444bc2035b1e58df39027ab7589cbeae175ace159a384dea8ef2cf
4
+ data.tar.gz: 8d91008e48102d7053911236a0173daa8daf64e94274a5f6046a45656770c8fa
5
5
  SHA512:
6
- metadata.gz: 24db31229795fcfd326243a0b2fc20a17c78b6ee772f28092f9afd917534a3dd307d688c3934ef590c857027944c6fc9432baea652c7c4ecfbd40e563eb89dd1
7
- data.tar.gz: 50fa9b5882460e10e4148c4b78372b3f366178738fe5fa92dfee522e8b4811a5378156623e0f6d13e030c912f9a5560d6f4051e0cd7aa7a249c59600a55fe20f
6
+ metadata.gz: 5c2a790b3d4e4f7dc14dce67ba3c1e2c635534962acfa8523390aa2b07389c216c7ffb1cbddccd5ebd0e627372267dad4f07c59293e5fd36ce3d659b9d0c35bd
7
+ data.tar.gz: 7c9d11d0c5047af4ad2dd7a7743c2e5ff341257f9f2036beabd4b23a88294bf22e8d7e282f1a95cef230a66e7b6e5e5f2c429fb95a265116dd341bc38698aaa7
data/CHANGELOG.md CHANGED
@@ -2,6 +2,135 @@
2
2
  All notable changes to this project will be documented in this file. This
3
3
  project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [2.12.0]
6
+
7
+ ### Added
8
+ * Handle nil autorequire results ([#22](https://github.com/puppetlabs/rspec-puppet/pull/22))
9
+ * Add the ability to use kind_of matchers ([#24](https://github.com/puppetlabs/rspec-puppet/pull/24))
10
+
11
+ ## [2.11.1]
12
+
13
+ ### Fixed
14
+ * Ensure FacterImpl consistency between example groups ([#19](https://github.com/puppetlabs/rspec-puppet/pull/19))
15
+
16
+ ## [2.11.0]
17
+
18
+ ### Added
19
+ * Add setting to use custom Facter implementation ([GH-16](https://github.com/puppetlabs/rspec-puppet/pull/16))
20
+
21
+ ## [2.10.0]
22
+ The release sees rspec-puppet move into the puppetlabs namespace
23
+
24
+ ### Added
25
+ * Add ruby 3 support ([GH-11](https://github.com/puppetlabs/rspec-puppet/pull/11))
26
+
27
+ ## [2.9.0]
28
+
29
+ ### Added
30
+ * Allow users to disable app_management for Puppet 4
31
+ * Added support for regexp arguments to Sensitive
32
+ * Handle all auto*, not just autorequire
33
+ * Set up loaders so that 4.x functions resolve properly
34
+
35
+ ## [2.8.0]
36
+
37
+ ### Breaking Changes
38
+ * As of the 2.8.0 release, the `rspec-puppet` project no longer guarantees compatibility
39
+ with Puppet 2.x or 3.x (running under Ruby 1.8.7) or Puppet 4.x (running under Ruby 1.9.3).
40
+ * This release adds support for the [`Sensitive`](https://puppet.com/docs/puppet/latest/lang_data_sensitive.html)
41
+ data type, however existing tests that were expecting `String` content may need to be updated
42
+ to wrap the expected value in the new `sensitive` helper:
43
+
44
+ ```ruby
45
+ # Old
46
+ it { is_expected.to contain_file('/etc/mysecret.conf').with_content("top secret\n") }
47
+
48
+ # New
49
+ it { is_expected.to contain_file('/etc/mysecret.conf').with_content(sensitive("top secret\n")) }
50
+ ```
51
+
52
+ ### Added
53
+ * Added support for [trusted external fact data](https://github.com/puppetlabs/rspec-puppet#specifying-trusted-external-data).
54
+ * Added the ability to exclude resources from the coverage report calculations using a regular expression.
55
+ (See [documentation](https://rspec-puppet.com/documentation/coverage/#excluded-resources) for an example.
56
+ * Added `have_unique_values_for_all` matcher to assert a specific resource parameter value is unique across
57
+ the entire catalogue.
58
+ (See [documentation](https://rspec-puppet.com/documentation/classes/#test-resource-parameter-values-for-uniqueness).)
59
+ * Added ability to customize module-layer Hiera configuration via new settings. See
60
+ [documentation](https://rspec-puppet.com/documentation/configuration/#disable_module_hiera) for details.
61
+
62
+ ### Changed
63
+ * `RSpec::Puppet::Cache` now evicts least recently used entries when it reaches max size.
64
+ * `rspec-puppet`'s implementation of `match_manifests` will no longer look in `init.pp` for class
65
+ declarations if a manifest file exactly matching the class name exists.
66
+
67
+ ### Fixed
68
+ * Resolved compatibility issues with Ruby 2.7.x and added Ruby 2.7.x to the test matrix.
69
+ * Resolved issues calculating coverage and reporting results when there are 0 tested resources.
70
+ * Resolved compatibility issue with `rspec-expectations` 3.10.0.
71
+
72
+ ## [2.7.10]
73
+
74
+ ### Fixed
75
+ * Fix issues with removal of `default_env` method in Puppet 6.17.0.
76
+
77
+ ## [2.7.9]
78
+
79
+ This release had unintended breaking changes and was withdrawn.
80
+
81
+ ## [2.7.8]
82
+
83
+ ### Fixed
84
+ * Fix cross-platform testing for Puppet >= 6.9.0 when there is no `ipaddress6`
85
+ fact defined.
86
+
87
+ ## [2.7.7]
88
+
89
+ ### Fixed
90
+ * Fix the support for rspec-expectations >= 3.8.5.
91
+
92
+ ### Changed
93
+ * Remove the rspec-expectations dependency limit introduced in 2.7.6.
94
+
95
+ ## [2.7.6]
96
+
97
+ ### Changed
98
+ * Limit rspec-expectations dependency to < 3.8.5 due to an incompatible
99
+ change.
100
+
101
+ ## [2.7.5]
102
+
103
+ ### Fixed
104
+ * Minor refactor to prevent the fix introduced in 2.7.4 from raising
105
+ a deprecation warning on latest RSpec.
106
+
107
+ ## [2.7.4]
108
+
109
+ ### Fixed
110
+ * Fix the resource coverage test so that rspec will exit non-zero if the
111
+ desired coverage is not met.
112
+
113
+ ## [2.7.3]
114
+
115
+ ### Fixed
116
+ * Puppet 6 deferred functions are now evaluated and resolved as part of the
117
+ catalogue compilation process.
118
+ * If running with parallel\_tests, the resources that are filtered out of the
119
+ resource coverage report are now taken into account when merging the final
120
+ report, fixing false negative results that can occur.
121
+
122
+ ## [2.7.2]
123
+
124
+ ### Changed
125
+ * Reverted the change introduced in 2.7.0 that reencoded resource parameter
126
+ values to modify their line endings.
127
+
128
+ ## [2.7.1]
129
+
130
+ ### Fixed
131
+ * Fixed a bug that prevented the platform pretending/stubbing logic from being
132
+ temporarily disabled when loading Ruby code.
133
+
5
134
  ## [2.7.0]
6
135
 
7
136
  ### Changed
@@ -447,13 +576,45 @@ Thanks to Adrien Thebo, Alex Harvey, Brian, Dan Bode, Dominic Cleal, Javier Pala
447
576
  ## 1.0.1 and earlier
448
577
  For changelog of versions 1.0.1 and earlier, see http://rspec-puppet.com/changelog/
449
578
 
450
- [2.x]: https://github.com/rodjek/rspec-puppet/compare/v2.5.0...master
451
- [2.5.0]: https://github.com/rodjek/rspec-puppet/compare/v2.4.0...v2.5.0
452
- [2.4.0]: https://github.com/rodjek/rspec-puppet/compare/v2.3.2...v2.4.0
453
- [2.3.2]: https://github.com/rodjek/rspec-puppet/compare/v2.3.1...v2.3.2
454
- [2.3.1]: https://github.com/rodjek/rspec-puppet/compare/v2.3.0...v2.3.1
455
- [2.3.0]: https://github.com/rodjek/rspec-puppet/compare/v2.2.0...v2.3.0
456
- [2.2.0]: https://github.com/rodjek/rspec-puppet/compare/v2.1.0...v2.2.0
457
- [2.1.0]: https://github.com/rodjek/rspec-puppet/compare/v2.0.1...v2.1.0
458
- [2.0.1]: https://github.com/rodjek/rspec-puppet/compare/v2.0.0...v2.0.1
459
- [2.0.0]: https://github.com/rodjek/rspec-puppet/compare/v1.0.1...v2.0.0
579
+ [2.x]: https://github.com/puppetlabs/rspec-puppet/compare/v2.11.1...master
580
+ [2.11.1]: https://github.com/puppetlabs/rspec-puppet/compare/v2.11.0..v2.11.1
581
+ [2.11.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.10.0...v2.11.0
582
+ [2.10.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.9.0...v2.10.0
583
+ [2.9.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.8.0...v2.9.0
584
+ [2.8.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.10...v2.8.0
585
+ [2.7.10]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.9...v2.7.10
586
+ [2.7.9]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.8...v2.7.9
587
+ [2.7.8]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.7...v2.7.8
588
+ [2.7.7]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.6...v2.7.7
589
+ [2.7.6]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.5...v2.7.6
590
+ [2.7.5]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.4...v2.7.5
591
+ [2.7.4]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.3...v2.7.4
592
+ [2.7.3]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.2...v2.7.3
593
+ [2.7.2]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.1...v2.7.2
594
+ [2.7.1]: https://github.com/puppetlabs/rspec-puppet/compare/v2.7.0...v2.7.1
595
+ [2.7.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.15...v2.7.0
596
+ [2.6.15]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.14...v2.6.15
597
+ [2.6.14]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.13...v2.6.14
598
+ [2.6.13]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.12...v2.6.13
599
+ [2.6.12]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.11...v2.6.12
600
+ [2.6.11]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.10...v2.6.11
601
+ [2.6.10]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.9...v2.6.10
602
+ [2.6.9]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.8...v2.6.9
603
+ [2.6.8]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.7...v2.6.8
604
+ [2.6.7]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.6...v2.6.7
605
+ [2.6.6]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.5...v2.6.6
606
+ [2.6.5]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.4...v2.6.5
607
+ [2.6.4]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.3...v2.6.4
608
+ [2.6.3]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.2...v2.6.3
609
+ [2.6.2]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.1...v2.6.2
610
+ [2.6.1]: https://github.com/puppetlabs/rspec-puppet/compare/v2.6.0...v2.6.1
611
+ [2.6.0]: https://github.com/puppetlabs/rspec-puppet/compare/2.5.0...v2.6.0
612
+ [2.5.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.4.0...v2.5.0
613
+ [2.4.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.3.2...v2.4.0
614
+ [2.3.2]: https://github.com/puppetlabs/rspec-puppet/compare/v2.3.1...v2.3.2
615
+ [2.3.1]: https://github.com/puppetlabs/rspec-puppet/compare/v2.3.0...v2.3.1
616
+ [2.3.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.2.0...v2.3.0
617
+ [2.2.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.1.0...v2.2.0
618
+ [2.1.0]: https://github.com/puppetlabs/rspec-puppet/compare/v2.0.1...v2.1.0
619
+ [2.0.1]: https://github.com/puppetlabs/rspec-puppet/compare/v2.0.0...v2.0.1
620
+ [2.0.0]: https://github.com/puppetlabs/rspec-puppet/compare/v1.0.1...v2.0.0
data/README.md CHANGED
@@ -2,6 +2,20 @@
2
2
  [![Build Status](https://travis-ci.org/rodjek/rspec-puppet.svg?branch=master)](https://travis-ci.org/rodjek/rspec-puppet)
3
3
  [![Coverage Status](https://coveralls.io/repos/rodjek/rspec-puppet/badge.svg?branch=master)](https://coveralls.io/r/rodjek/rspec-puppet?branch=master)
4
4
 
5
+ #### Table of Contents
6
+
7
+ * [Installation](#installation)
8
+ * [Starting out with a new module](#starting-out-with-a-new-module)
9
+ * [Configure manifests for Puppet 4](#configure-manifests-for-puppet-4)
10
+ * [Configuration](#configuration)
11
+ * [Naming conventions](#naming-conventions)
12
+ * [Example groups](#example-groups)
13
+ * [Defined Types, Classes & Applications](#defined-types-classes--applications)
14
+ * [Functions](#functions)
15
+ * [Hiera integration](#hiera-integration)
16
+ * [Producing coverage reports](#producing-coverage-reports)
17
+ * [Related projects](#related-projects)
18
+
5
19
  ## Installation
6
20
 
7
21
  gem install rspec-puppet
@@ -216,6 +230,20 @@ In some circumstances (e.g. where your nodename/certname is not the same as
216
230
  your FQDN), this behaviour is undesirable and can be disabled by changing this
217
231
  setting to `false`.
218
232
 
233
+ #### facter\_implementation
234
+ Type | Default | Puppet Version(s)
235
+ ------- | -------- | -----------------
236
+ String | `facter` | 6.25+, 7.12+
237
+
238
+ Configures rspec-puppet to use a specific Facter implementation for running
239
+ unit tests. If the `rspec` implementation is set and Puppet does not support
240
+ it, rspec-puppet will warn and fall back to the `facter` implementation.
241
+ Setting an unsupported option will make rspec-puppet raise an error.
242
+
243
+ * `facter` - Use the default implementation, honoring the Facter version specified in the Gemfile
244
+ * `rspec` - Use a custom hash-based implementation of Facter defined in
245
+ rspec-puppet (this provides a considerable gain in speed if tests are run with Facter 4)
246
+
219
247
  ## Naming conventions
220
248
 
221
249
  For clarity and consistency, I recommend that you use the following directory
@@ -759,6 +787,30 @@ RSpec.configure do |c|
759
787
  end
760
788
  ```
761
789
 
790
+ #### Specifying trusted external data
791
+
792
+ When testing with Puppet >= 6.14, the trusted facts hash will have an additional `external`
793
+ key for trusted external data.
794
+
795
+ By default, the test environment contains no trusted external data (as usually obtained from
796
+ trusted external commands and found in the `external` key). If you need to test against specific
797
+ trusted external data you can set those with a hash. The hash will then be available in
798
+ `$trusted['external']`
799
+
800
+ ```ruby
801
+ let(:trusted_external_data) { {'foo' => 'bar'} }
802
+ ```
803
+
804
+ You can also create a set of default trusted external data provided to all specs in your spec_helper:
805
+
806
+ ```ruby
807
+ RSpec.configure do |c|
808
+ c.default_trusted_external_data = {
809
+ 'foo' => 'bar'
810
+ }
811
+ end
812
+ ```
813
+
762
814
  #### Testing Exported Resources
763
815
 
764
816
  You can test if a resource was exported from the catalogue by using the
@@ -829,7 +881,7 @@ Puppet functions.
829
881
 
830
882
  ```ruby
831
883
  it 'should be able to do something' do
832
- subject.call(['foo']) == 'bar'
884
+ subject.execute('foo') == 'bar'
833
885
  end
834
886
  ```
835
887
 
@@ -869,11 +921,11 @@ either the `with_params` chain method in the `run` matcher
869
921
  it { is_expected.to run.with_params('foo', 'bar', ['baz']) }
870
922
  ```
871
923
 
872
- Or by using the `call` method on the subject directly
924
+ Or by using the `execute` method on the subject directly
873
925
 
874
926
  ```ruby
875
927
  it 'something' do
876
- subject.call(['foo', 'bar', ['baz']])
928
+ subject.execute('foo', 'bar', ['baz'])
877
929
  end
878
930
  ```
879
931
 
@@ -900,8 +952,8 @@ Or by using any of the existing RSpec matchers on the subject directly
900
952
 
901
953
  ```ruby
902
954
  it 'something' do
903
- subject.call(['foo']) == 'bar'
904
- subject.call(['baz']).should be_an Array
955
+ subject.execute('foo') == 'bar'
956
+ subject.execute('baz').should be_an Array
905
957
  end
906
958
  ```
907
959
 
@@ -919,8 +971,8 @@ Or by using the existing `raises_error` RSpec matcher
919
971
 
920
972
  ```ruby
921
973
  it 'something' do
922
- expect { subject.call(['a', 'b']) }.should raise_error(Puppet::ParseError)
923
- expect { subject.call(['a']) }.should_not raise_error(Puppet::ParseError)
974
+ expect { subject.execute('a', 'b') }.should raise_error(Puppet::ParseError)
975
+ expect { subject.execute('a') }.should_not raise_error(Puppet::ParseError)
924
976
  end
925
977
  ```
926
978
 
@@ -1,3 +1,5 @@
1
+ require 'rspec-puppet/facter_impl'
2
+
1
3
  module RSpec::Puppet
2
4
  module Adapters
3
5
 
@@ -108,6 +110,21 @@ module RSpec::Puppet
108
110
  end
109
111
 
110
112
  class Adapter40 < Base
113
+ #
114
+ # @api private
115
+ #
116
+ # Set the FacterImpl constant to the given Facter implementation or noop
117
+ # if the constant is already set. If a proc is given, it will only be
118
+ # called if FacterImpl is not defined.
119
+ #
120
+ # @param impl [Object, Proc] An object or a proc that implements the Facter API
121
+ def set_facter_impl(impl)
122
+ return if defined?(FacterImpl)
123
+
124
+ impl = impl.call if impl.is_a?(Proc)
125
+ Object.send(:const_set, :FacterImpl, impl)
126
+ end
127
+
111
128
  def setup_puppet(example_group)
112
129
  super
113
130
 
@@ -139,7 +156,8 @@ module RSpec::Puppet
139
156
  Puppet.push_context(
140
157
  {
141
158
  :environments => loader,
142
- :current_environment => env
159
+ :current_environment => env,
160
+ :loaders => (Puppet::Pops::Loaders.new(env) if Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0')),
143
161
  },
144
162
  "Setup rspec-puppet environments"
145
163
  )
@@ -185,6 +203,12 @@ module RSpec::Puppet
185
203
  end
186
204
 
187
205
  class Adapter4X < Adapter40
206
+ def setup_puppet(example_group)
207
+ super
208
+
209
+ set_facter_impl(Facter)
210
+ end
211
+
188
212
  def settings_map
189
213
  super.concat([
190
214
  [:trusted_server_facts, :trusted_server_facts]
@@ -193,12 +217,60 @@ module RSpec::Puppet
193
217
  end
194
218
 
195
219
  class Adapter6X < Adapter40
220
+ #
221
+ # @api private
222
+ #
223
+ # Check to see if Facter runtime implementations are supported in the
224
+ # current Puppet version
225
+ #
226
+ # @return [Boolean] true if runtime implementations are supported
227
+ def supports_facter_runtime?
228
+ unless defined?(@supports_facter_runtime)
229
+ begin
230
+ Puppet.runtime[:facter]
231
+ @supports_facter_runtime = true
232
+ rescue
233
+ @supports_facter_runtime = false
234
+ end
235
+ end
236
+
237
+ @supports_facter_runtime
238
+ end
239
+
240
+ def setup_puppet(example_group)
241
+ case RSpec.configuration.facter_implementation.to_sym
242
+ when :rspec
243
+ if supports_facter_runtime?
244
+ # Lazily instantiate FacterTestImpl here to optimize memory
245
+ # allocation, as the proc will only be called if FacterImpl is unset
246
+ set_facter_impl(proc { RSpec::Puppet::FacterTestImpl.new })
247
+ Puppet.runtime[:facter] = FacterImpl
248
+ else
249
+ warn "Facter runtime implementations are not supported in Puppet #{Puppet.version}, continuing with facter_implementation 'facter'"
250
+ RSpec.configuration.facter_implementation = :facter
251
+ set_facter_impl(Facter)
252
+ end
253
+ when :facter
254
+ set_facter_impl(Facter)
255
+ else
256
+ raise "Unsupported facter_implementation '#{RSpec.configuration.facter_implementation}'"
257
+ end
258
+
259
+ super
260
+ end
261
+
196
262
  def settings_map
197
263
  super.concat([
198
264
  [:basemodulepath, :basemodulepath],
199
265
  [:vendormoduledir, :vendormoduledir],
200
266
  ])
201
267
  end
268
+
269
+ def catalog(node, _exported)
270
+ super.tap do |c|
271
+ Puppet::Pops::Evaluator::DeferredResolver.resolve_and_replace(node.facts, c)
272
+ end
273
+ end
202
274
  end
203
275
 
204
276
  class Adapter30 < Base
@@ -11,12 +11,17 @@ module RSpec::Puppet
11
11
  end
12
12
 
13
13
  def get(*args, &blk)
14
- # decouple the hash key from whatever the blk might do to it
15
14
  key = Marshal.load(Marshal.dump(args))
16
- if !@cache.has_key? key
17
- @cache[key] = (blk || @default_proc).call(*args)
18
- @lra << key
15
+ if @cache.has_key?(key)
16
+ # Cache hit
17
+ # move that entry last to make it "most recenty used"
18
+ @lra.insert(-1, @lra.delete_at(@lra.index(args)))
19
+ else
20
+ # Cache miss
21
+ # Ensure room by evicting least recently used if no space left
19
22
  expire!
23
+ @cache[args] = (blk || @default_proc).call(*args)
24
+ @lra << args
20
25
  end
21
26
 
22
27
  @cache[key]
@@ -25,8 +30,8 @@ module RSpec::Puppet
25
30
  private
26
31
 
27
32
  def expire!
28
- expired = @lra.slice!(0, @lra.size - MAX_ENTRIES)
29
- expired.each { |key| @cache.delete(key) } if expired
33
+ # delete one entry (the oldest) when there is no room in cache
34
+ @cache.delete(@lra.shift) if @cache.size == MAX_ENTRIES
30
35
  end
31
36
  end
32
37
  end
@@ -17,13 +17,25 @@ end
17
17
  module RSpec::Puppet
18
18
  class Coverage
19
19
 
20
- attr_accessor :filters
20
+ attr_accessor :filters, :filters_regex
21
21
 
22
22
  class << self
23
23
  extend Forwardable
24
- def_delegators(:instance, :add, :cover!, :report!,
25
- :filters, :add_filter, :add_from_catalog,
26
- :results)
24
+
25
+ delegated_methods = [
26
+ :instance,
27
+ :add,
28
+ :cover!,
29
+ :report!,
30
+ :filters,
31
+ :filters_regex,
32
+ :add_filter,
33
+ :add_filter_regex,
34
+ :add_from_catalog,
35
+ :results,
36
+ ]
37
+
38
+ def_delegators(*delegated_methods)
27
39
 
28
40
  attr_writer :instance
29
41
 
@@ -35,10 +47,17 @@ module RSpec::Puppet
35
47
  def initialize
36
48
  @collection = {}
37
49
  @filters = ['Stage[main]', 'Class[Settings]', 'Class[main]', 'Node[default]']
50
+ @filters_regex = []
38
51
  end
39
52
 
40
53
  def save_results
41
54
  slug = "#{Digest::MD5.hexdigest(Dir.pwd)}-#{Process.pid}"
55
+ File.open(File.join(Dir.tmpdir, "rspec-puppet-filter-#{slug}"), 'w+') do |f|
56
+ f.puts @filters.to_json
57
+ end
58
+ File.open(File.join(Dir.tmpdir, "rspec-puppet-filter_regex-#{slug}"), 'w+') do |f|
59
+ f.puts @filters_regex.to_json
60
+ end
42
61
  File.open(File.join(Dir.tmpdir, "rspec-puppet-coverage-#{slug}"), 'w+') do |f|
43
62
  f.puts @collection.to_json
44
63
  end
@@ -52,6 +71,21 @@ module RSpec::Puppet
52
71
  end
53
72
  end
54
73
 
74
+ def merge_filters
75
+ pattern = File.join(Dir.tmpdir, "rspec-puppet-filter-#{Digest::MD5.hexdigest(Dir.pwd)}-*")
76
+ regex_filter_pattern = File.join(Dir.tmpdir, "rspec-puppet-filter_regex-#{Digest::MD5.hexdigest(Dir.pwd)}-*")
77
+
78
+ Dir[pattern].each do |result_file|
79
+ load_filters(result_file)
80
+ FileUtils.rm(result_file)
81
+ end
82
+
83
+ Dir[regex_filter_pattern].each do |result_file|
84
+ load_filters_regex(result_file)
85
+ FileUtils.rm(result_file)
86
+ end
87
+ end
88
+
55
89
  def load_results(path)
56
90
  saved_results = JSON.parse(File.read(path))
57
91
  saved_results.each do |resource, data|
@@ -60,6 +94,23 @@ module RSpec::Puppet
60
94
  end
61
95
  end
62
96
 
97
+ def load_filters(path)
98
+ saved_filters = JSON.parse(File.read(path))
99
+ saved_filters.each do |resource|
100
+ @filters << resource
101
+ @collection.delete(resource) if @collection.key?(resource)
102
+ end
103
+ end
104
+
105
+ def load_filters_regex(path)
106
+ saved_regex_filters = JSON.parse(File.read(path))
107
+ saved_regex_filters.each do |pattern|
108
+ regex = Regexp.new(pattern)
109
+ @filters_regex << regex
110
+ @collection.delete_if { |resource, _| resource =~ regex }
111
+ end
112
+ end
113
+
63
114
  def add(resource)
64
115
  if !exists?(resource) && !filtered?(resource)
65
116
  @collection[resource.to_s] = ResourceWrapper.new(resource)
@@ -67,11 +118,8 @@ module RSpec::Puppet
67
118
  end
68
119
 
69
120
  def add_filter(type, title)
70
- def capitalize_name(name)
71
- name.split('::').map { |subtitle| subtitle.capitalize }.join('::')
72
- end
73
-
74
121
  type = capitalize_name(type)
122
+
75
123
  if type == 'Class'
76
124
  title = capitalize_name(title)
77
125
  end
@@ -79,6 +127,34 @@ module RSpec::Puppet
79
127
  @filters << "#{type}[#{title}]"
80
128
  end
81
129
 
130
+ def add_filter_regex(type, pattern)
131
+ raise ArgumentError.new('pattern argument must be a Regexp') unless pattern.is_a?(Regexp)
132
+
133
+ type = capitalize_name(type)
134
+
135
+ # avoid recompiling the regular expression during processing
136
+ src = pattern.source
137
+
138
+ # switch from anchors to wildcards since it is embedded into a larger pattern
139
+ src = if src.start_with?('\\A', '^')
140
+ src.gsub(/\A(?:\\A|\^)/, '')
141
+ else
142
+ # no anchor at the start
143
+ ".*#{src}"
144
+ end
145
+
146
+ # match an even number of backslashes before the anchor - this indicates that the anchor was not escaped
147
+ # note the necessity for the negative lookbehind `(?<!)` to assert that there is no backslash before this
148
+ src = if src.match(/(?<!\\)(\\\\)*(?:\\[zZ]|\$)\z/)
149
+ src.gsub(/(?:\\[zZ]|\$)\z/, '')
150
+ else
151
+ # no anchor at the end
152
+ "#{src}.*"
153
+ end
154
+
155
+ @filters_regex << /\A#{Regexp.escape(type)}\[#{src}\]\z/
156
+ end
157
+
82
158
  # add all resources from catalog declared in module test_module
83
159
  def add_from_catalog(catalog, test_module)
84
160
  coverable_resources = catalog.to_a.reject { |resource| !test_module.nil? && filter_resource?(resource, test_module) }
@@ -88,7 +164,10 @@ module RSpec::Puppet
88
164
  end
89
165
 
90
166
  def filtered?(resource)
91
- filters.include?(resource.to_s)
167
+ return true if filters.include?(resource.to_s)
168
+ return true if filters_regex.any? { |f| resource.to_s =~ f }
169
+
170
+ false
92
171
  end
93
172
 
94
173
  def cover!(resource)
@@ -117,13 +196,16 @@ module RSpec::Puppet
117
196
  end
118
197
 
119
198
  def run_report(coverage_desired = nil)
120
- merge_results if ENV['TEST_ENV_NUMBER']
199
+ if parallel_tests?
200
+ merge_filters
201
+ merge_results
202
+ end
121
203
 
122
204
  report = results
123
205
 
124
206
  coverage_test(coverage_desired, report)
125
207
 
126
- puts report[:text]
208
+ puts "\n\nCoverage Report:\n\n#{report[:text]}"
127
209
  end
128
210
 
129
211
  def coverage_test(coverage_desired, report)
@@ -137,6 +219,17 @@ module RSpec::Puppet
137
219
  end
138
220
  coverage_test.run(RSpec.configuration.reporter)
139
221
 
222
+ status = if coverage_results.execution_result.respond_to?(:status)
223
+ coverage_results.execution_result.status
224
+ else
225
+ coverage_results.execution_result[:status]
226
+ end
227
+
228
+ if status == :failed
229
+ RSpec.world.non_example_failure = true
230
+ RSpec.world.wants_to_quit = true
231
+ end
232
+
140
233
  # This is not available on RSpec 2.x
141
234
  if coverage_results.execution_result.respond_to?(:pending_message)
142
235
  coverage_results.execution_result.pending_message = report[:text]
@@ -154,7 +247,9 @@ module RSpec::Puppet
154
247
  report[:total] = @collection.size
155
248
  report[:touched] = @collection.count { |_, resource| resource.touched? }
156
249
  report[:untouched] = report[:total] - report[:touched]
157
- report[:coverage] = "%5.2f" % ((report[:touched].to_f / report[:total].to_f) * 100)
250
+
251
+ coverage = report[:total].to_f > 0 ? ((report[:touched].to_f / report[:total].to_f) * 100) : 100.0
252
+ report[:coverage] = "%5.2f" % coverage
158
253
 
159
254
  report[:resources] = Hash[*@collection.map do |name, wrapper|
160
255
  [name, wrapper.to_hash]
@@ -193,7 +288,7 @@ module RSpec::Puppet
193
288
  # @param test_module [String] The name of the module under test
194
289
  # @return [true, false]
195
290
  def filter_resource?(resource, test_module)
196
- if @filters.include?(resource.to_s)
291
+ if filtered?(resource)
197
292
  return true
198
293
  end
199
294
 
@@ -234,6 +329,10 @@ module RSpec::Puppet
234
329
  !find(resource).nil?
235
330
  end
236
331
 
332
+ def capitalize_name(name)
333
+ name.split('::').map { |subtitle| subtitle.capitalize }.join('::')
334
+ end
335
+
237
336
  class ResourceWrapper
238
337
  attr_reader :resource
239
338