rspec-puppet-facts 1.7.0 → 1.8.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
2
  SHA1:
3
- metadata.gz: 13500ef636b561fc94ded0be8be865f94f004b07
4
- data.tar.gz: 515732b3aa600a4f4b6de18598d064972160944a
3
+ metadata.gz: 785242ffa008d8289d585447477c09447bda5d8b
4
+ data.tar.gz: 0f112311ebf40c1b3272e6276d1341eb1aa51eec
5
5
  SHA512:
6
- metadata.gz: 1495b667b0e488ac1c9bcf8ee6bfd89a01ec5e747ff31f97f0eca476325e4efb57141e8eef4341db9596ebcf2d9f5569292ed6baa12417643740466992b7336d
7
- data.tar.gz: 2494c062b9d5e89c6fd94e0dd5b62c0e873c820e5bd14468114ce9ce4577afedf5c4c3674b1c9874bbc2d354176cafba10751b761727d89d1e75db56850be4b4
6
+ metadata.gz: 017a8282b68844be3988ec6b514bff9311c0fe953474ba8db434a5f83481f6adcf981c1a15b2a87f9bb2855655de9f0eaed03f5cac839dd10808c990682bd644
7
+ data.tar.gz: e4f609545cfefa58d7b8779c1741f4db8ce8294a08bff965fc0a767895fa715d57f09b61ccb664bd5b78e2697aadc126772350b79a70202e3ac6ed1ab42a72eb
data/.gitignore CHANGED
@@ -7,3 +7,4 @@ coverage/
7
7
  .idea
8
8
  doc
9
9
  .yardoc
10
+ vendor/
data/.travis.yml CHANGED
@@ -22,5 +22,6 @@ matrix:
22
22
  fast_finish: true
23
23
  allow_failures:
24
24
  - rvm: 1.8.7
25
+ - rvm: 1.9.3
25
26
  notifications:
26
27
  email: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ##2017-06-23 - Release 1.8.0
2
+ - Support specifying facter version
3
+
4
+ ##2017-01-04 - Release 1.7.1
5
+ - Ignore case when choosing H/W models
6
+
1
7
  ##2016-09-16 - Release 1.7.0
2
8
  - Support custom facts defined by spec_helper
3
9
 
data/README.md CHANGED
@@ -7,14 +7,79 @@ rspec-puppet-facts
7
7
  [![Gem Downloads](https://img.shields.io/gem/dt/rspec-puppet-facts.svg)](https://rubygems.org/gems/rspec-puppet-facts)
8
8
  [![Coverage Status](https://img.shields.io/coveralls/mcanevet/rspec-puppet-facts.svg)](https://coveralls.io/r/mcanevet/rspec-puppet-facts?branch=master)
9
9
 
10
- Based on an original idea from [apenney](https://github.com/apenney/puppet_facts/).
10
+ Based on an original idea from [apenney](https://github.com/apenney/puppet_facts/), this gem provides a method of running your [rspec-puppet](https://github.com/rodjek/rspec-puppet) tests against the facts for all your supported operating systems (provided by [facterdb](https://github.com/camptocamp/facterdb). This simplifies unit testing because you don't need to specify the facts yourself.
11
11
 
12
- Simplify your unit tests by looping on every supported Operating System and populating facts.
12
+ ## Installation
13
13
 
14
- Testing a class or define
15
- -------------------------
14
+ If you're using Bundler to manage gems in your module repository, install `rspec-puppet-facts` by adding it to the Gemfile.
16
15
 
17
- ### Before
16
+ 1. Add the following line to your `Gemfile`:
17
+
18
+ ```ruby
19
+ gem 'rspec-puppet-facts', '~> 1.7', :require => false
20
+ ```
21
+
22
+ 2. Run `bundle install`.
23
+
24
+ If you're not using Bundler, install the `rspec-puppet-facts` manually.
25
+
26
+ 1. On the command line, run:
27
+
28
+ ```bash
29
+ $ gem install rspec-puppet-facts
30
+ ```
31
+
32
+ After the gem is installed (using either method), make the gem available to rspec by adding the following lines in your `spec/spec_helper.rb` file. Place the lines after `require 'rspec-puppet'` and before the `RSpec.configure` block, if one exists.
33
+
34
+ ```ruby
35
+ require 'rspec-puppet-facts'
36
+ include RspecPuppetFacts
37
+ ```
38
+
39
+ ## Specifying the supported operating systems
40
+
41
+ To determine which facts to run your tests against, `rspec-puppet-facts` checks your module's `metadata.json` to find out what operating systems your module supports. The `metadata.json` file is located in the root of your module. To learn more about this file, see Puppet's [metatdata](https://docs.puppet.com/puppet/latest/modules_metadata.html) documentation.
42
+
43
+ By default, `rspec-puppet-facts` provides the facts only for `x86_64` architecture. However, you can override this default and the supported operating system list by passing a hash to `on_supported_os` in your tests. This hash must contain either or both of the following keys:
44
+
45
+ * `:hardwaremodels` - An array of hardware architecture names, as strings.
46
+ * `:supported_os` - An array of hashes representing the operating systems.
47
+ **Note: the keys of these hashes must be strings**
48
+ * `'operatingsystem'` - The name of the operating system, as a string.
49
+ * `'operatingsystemrelease'` - An array of version numbers, as strings.
50
+
51
+ This is particularly useful if your module is split into operating system subclasses. For example, if you had a class called `myclass::debian` that you wanted to test against Debian 6 and Debian 7 on both `x86_64` _and_ `i386` architectures, you could write the following test:
52
+
53
+ ```ruby
54
+ require 'spec_helper'
55
+
56
+ describe 'myclass::debian' do
57
+ test_on = {
58
+ :hardwaremodels => ['x86_64', 'i386'],
59
+ :supported_os => [
60
+ {
61
+ 'operatingsystem' => 'Debian',
62
+ 'operatingsystemrelease' => ['6', '7'],
63
+ },
64
+ ],
65
+ }
66
+
67
+ on_supported_os(test_on).each do |os, facts|
68
+ it { is_expected.to compile.with_all_deps }
69
+ end
70
+ end
71
+ ```
72
+
73
+ ## Usage
74
+
75
+ Use the `on_supported_os` iterator to loop through all of your module's supported operating systems. This allows you to simplify your tests and remove a lot of duplicate code.
76
+
77
+ Each iteration of `on_supported_os` provides two variables to your tests. (In the code examples below, these variables are specified by the values between the pipe (`|`) characters.)
78
+
79
+ * The first value is the name of the fact set. This is made from the values of the operatingsystem, operatingsystemmajrelease, and architecture facts separated by dashes (for example, 'debian-7-x86_64').
80
+ * The second value is the facts for that combination of operating system, release, and architecture.
81
+
82
+ For example, previously, you might have written a test that specified Debian 7 and Red Hat 6 as the supported modules:
18
83
 
19
84
  ```ruby
20
85
  require 'spec_helper'
@@ -29,10 +94,10 @@ describe 'myclass' do
29
94
  :operatingsystemmajrelease => '7',
30
95
  ...
31
96
  }
32
-
33
- it { is_expected.to compile.with_all_deps }
34
- ...
35
97
  end
98
+
99
+ it { is_expected.to compile.with_all_deps }
100
+ ...
36
101
  end
37
102
 
38
103
  context "on redhat-6-x86_64" do
@@ -43,17 +108,17 @@ describe 'myclass' do
43
108
  :operatingsystemmajrelease => '6',
44
109
  ...
45
110
  }
46
-
47
- it { is_expected.to compile.with_all_deps }
48
- ...
49
111
  end
112
+
113
+ it { is_expected.to compile.with_all_deps }
114
+ ...
50
115
  end
51
116
 
52
117
  ...
53
118
  end
54
119
  ```
55
120
 
56
- ### After
121
+ With `on_supported_os` iteration, you can rewrite this test to loop over each of the supported operating systems without explicitly specifying them:
57
122
 
58
123
  ```ruby
59
124
  require 'spec_helper'
@@ -68,6 +133,8 @@ describe 'myclass' do
68
133
 
69
134
  it { is_expected.to compile.with_all_deps }
70
135
  ...
136
+
137
+ # If you need any to specify any operating system specific tests
71
138
  case facts[:osfamily]
72
139
  when 'Debian'
73
140
  ...
@@ -79,55 +146,63 @@ describe 'myclass' do
79
146
  end
80
147
  ```
81
148
 
82
- Testing a type or provider
83
- --------------------------
149
+ ### Testing a type or provider
150
+
151
+ Use `on_supported_os` in the same way for your type and provider unit tests.
84
152
 
85
- ### Before
153
+ **Specifying each operating system**:
86
154
 
87
155
  ```ruby
88
156
  require 'spec_helper'
89
157
 
90
- describe Puppet::Type.type(:mytype) do
158
+ describe 'mytype' do
91
159
 
92
160
  context "on debian-7-x86_64" do
93
- before :each do
94
- Facter.clear
95
- Facter.stubs(:fact).with(:osfamily).returns Facter.add(:osfamily) { setcode { 'Debian' } }
96
- Facter.stubs(:fact).with(:operatingsystem).returns Facter.add(:operatingsystem) { setcode { 'Debian' } }
97
- Facter.stubs(:fact).with(:operatingsystemmajrelease).returns Facter.add(:operatingsystemmajrelease) { setcode { '7' } }
161
+ let(:facts) do
162
+ {
163
+ :osfamily => 'Debian',
164
+ :operatingsystem => 'Debian',
165
+ :operatingsystemmajrelease => '7',
166
+ }
98
167
  end
168
+
169
+ it { should be_valid_type }
99
170
  ...
100
171
  end
101
172
 
102
173
  context "on redhat-7-x86_64" do
103
- before :each do
104
- Facter.clear
105
- Facter.stubs(:fact).with(:osfamily).returns Facter.add(:osfamily) { setcode { 'RedHat' } }
106
- Facter.stubs(:fact).with(:operatingsystem).returns Facter.add(:operatingsystem) { setcode { 'RedHat' } }
107
- Facter.stubs(:fact).with(:operatingsystemmajrelease).returns Facter.add(:operatingsystemmajrelease) { setcode { '7' } }
174
+ let(:facts) do
175
+ {
176
+ :osfamily => 'RedHat',
177
+ :operatingsystem => 'RedHat',
178
+ :operatingsystemmajrelease => '7',
179
+ }
108
180
  end
181
+
182
+ it { should be_valid_type }
109
183
  ...
110
184
  end
111
185
  end
112
186
 
113
187
  ```
114
188
 
115
- ### After
189
+ **Looping with `on_supported_os` iterator**:
116
190
 
117
191
  ```ruby
118
192
  require 'spec_helper'
119
193
 
120
- describe Puppet::Type.type(:mytype) do
194
+ describe 'mytype' do
121
195
 
122
196
  on_supported_os.each do |os, facts|
123
197
  context "on #{os}" do
124
- before :each do
125
- Facter.clear
126
- facts.each do |k, v|
127
- Facter.stubs(:fact).with(k).returns Facter.add(k) { setcode { v } }
128
- end
198
+ let(:facts) do
199
+ facts
129
200
  end
201
+
202
+ it { should be_valid_type }
130
203
  ...
204
+
205
+ # If you need to specify any operating system specific tests
131
206
  case facts[:osfamily]
132
207
  when 'Debian'
133
208
  ...
@@ -141,115 +216,87 @@ end
141
216
 
142
217
  ```
143
218
 
144
- Testing a function
145
- ------------------
219
+ ### Testing a function
220
+
221
+ As with testing manifests, types, or providers, `on_supported_os` iteration simplifies your function unit tests.
146
222
 
147
- ### Before
223
+ **Specifying each operating system**:
148
224
 
149
225
  ```ruby
150
226
  require 'spec_helper'
151
227
 
152
- describe Puppet::Parser::Functions.function(:myfunction) do
153
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
154
-
228
+ describe 'myfunction' do
155
229
  context "on debian-7-x86_64" do
156
- before :each do
157
- scope.stubs(:lookupvar).with('::osfamily').returns('Debian')
158
- scope.stubs(:lookupvar).with('osfamily').returns('Debian')
159
- scope.stubs(:lookupvar).with('::operatingsystem').returns('Debian')
160
- scope.stubs(:lookupvar).with('operatingsystem').returns('Debian')
161
- ...
230
+ let(:facts) do
231
+ {
232
+ :osfamily => 'Debian',
233
+ :operatingsystem => 'Debian',
234
+ ...
235
+ }
162
236
  end
163
- ...
164
- end
165
237
 
166
- context "on redhat-7-x86_64" do
167
- before :each do
168
- scope.stubs(:lookupvar).with('::osfamily').returns('RedHat')
169
- scope.stubs(:lookupvar).with('osfamily').returns('RedHat')
170
- scope.stubs(:lookupvar).with('::operatingsystem').returns('RedHat')
171
- scope.stubs(:lookupvar).with('operatingsystem').returns('RedHat')
172
- ...
173
- end
238
+ it { should run.with_params('something').and_return('a value') }
174
239
  ...
175
240
  end
176
- end
177
- ```
178
-
179
- ### After
180
-
181
- ```ruby
182
- require 'spec_helper'
183
-
184
- describe Puppet::Parser::Functions.function(:myfunction) do
185
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
186
241
 
187
- on_supported_os.each do |os, facts|
188
- context "on #{os}" do
189
- before :each do
190
- facts.each do |k, v|
191
- scope.stubs(:lookupvar).with("::#{k}").returns(v)
192
- scope.stubs(:lookupvar).with(k).returns(v)
193
- end
194
- end
242
+ context "on redhat-7-x86_64" do
243
+ let(:facts) do
244
+ {
245
+ :osfamily => 'RedHat',
246
+ :operatingsystem => 'RedHat',
247
+ ...
248
+ }
195
249
  end
196
250
 
251
+ it { should run.with_params('something').and_return('a value') }
197
252
  ...
198
-
199
253
  end
200
254
  end
201
255
  ```
202
256
 
203
- By default rspec-puppet-facts looks at your `metadata.json` to find supported operating systems and tests only with `x86_64`, but you can specify for each context which ones you want to use:
257
+ **Looping with `on_supported_os` iterator**:
204
258
 
205
259
  ```ruby
206
260
  require 'spec_helper'
207
261
 
208
- describe 'myclass' do
262
+ describe 'myfunction' do
209
263
 
210
- on_supported_os({
211
- :hardwaremodels => ['i386', 'x86_64'],
212
- :supported_os => [
213
- {
214
- "operatingsystem" => "Debian",
215
- "operatingsystemrelease" => [
216
- "6",
217
- "7"
218
- ]
219
- },
220
- {
221
- "operatingsystem" => "RedHat",
222
- "operatingsystemrelease" => [
223
- "5",
224
- "6"
225
- ]
226
- }
227
- ],
228
- }).each do |os, facts|
264
+ on_supported_os.each do |os, facts|
229
265
  context "on #{os}" do
230
266
  let(:facts) do
231
267
  facts
232
268
  end
233
269
 
234
- it { is_expected.to compile.with_all_deps }
270
+ it { should run.with_params('something').and_return('a value') }
235
271
  ...
236
272
  end
237
273
  end
238
274
  end
239
275
  ```
240
276
 
241
- Append some facts:
242
- ------------------
277
+ ### Adding custom fact values
278
+
279
+ By adding custom fact values, you can:
280
+
281
+ * Override fact values
282
+ * Include additional facts in your tests.
283
+ * Add global custom facts for all of your unit tests
284
+ * Add custom facts to only certain operating systems
285
+ * Add custom facts to all operating systems _except_ specific operating systems
286
+ * Create dynamic values for custom facts by setting a lambda as the value.
287
+
288
+ #### Override and add facts
243
289
 
244
- 1. In your spec
290
+ To override fact values and include additional facts in your tests, merge values with the facts hash provided by each iteration of `on_supported_os`.
245
291
 
246
292
  ```ruby
247
293
  require 'spec_helper'
248
294
 
249
295
  describe 'myclass' do
250
-
251
296
  on_supported_os.each do |os, facts|
252
297
  context "on #{os}" do
298
+
299
+ # Add the 'foo' fact with the value 'bar' to the tests
253
300
  let(:facts) do
254
301
  facts.merge({
255
302
  :foo => 'bar',
@@ -263,77 +310,57 @@ describe 'myclass' do
263
310
  end
264
311
  ```
265
312
 
266
- 2. In your `spec_helper.rb`
267
- * Simple:
313
+ #### Set global custom facts
268
314
 
269
- ```ruby
270
- add_custom_fact 'foo', 'bar'
271
- ```
315
+ Set global custom fact values in your `spec/spec_helper.rb` file so that they are automatically available to all of your unit tests using `on_supported_os`.
272
316
 
273
- * Confine to an OS:
317
+ Pass the fact name and value to the `add_custom_fact` function:
274
318
 
275
- ```ruby
276
- add_custom_fact :root_home, '/root', :confine => 'redhat-7-x86_64'
277
- ```
278
-
279
- * Exclude an OS:
319
+ ```ruby
320
+ require 'rspec-puppet'
321
+ require 'rspec-puppet-facts'
322
+ include RspecPuppetFacts
280
323
 
281
- ```ruby
282
- add_custom_fact :root_home, '/root', :exclude => 'redhat-7-x86_64'
283
- ```
284
- * Call a proc to get a value:
324
+ # Add the 'concat_basedir' fact to all tests
325
+ add_custom_fact :concat_basedir, '/doesnotexist'
285
326
 
286
- ```ruby
287
- add_custom_fact :root_home, ->(_os,facts) { "/tmp/#{facts['hostname']}"
288
- ```
327
+ RSpec.configure do |config|
328
+ # normal rspec-puppet configuration
329
+ ...
330
+ end
331
+ ```
289
332
 
290
- Usage
291
- -----
333
+ #### Confine custom facts
292
334
 
293
- Add this in your Gemfile:
335
+ To add custom facts for only certain operating systems, set `confine` with the operating system as a string value:
294
336
 
295
337
  ```ruby
296
- gem 'rspec-puppet-facts', :require => false
338
+ add_custom_fact :root_home, '/root', :confine => 'redhat-7-x86_64'
297
339
  ```
298
340
 
299
- Add this is your spec/spec_helper.rb:
341
+ To add custom facts for all operating systems _except_ specific ones, set `exclude` with the operating system as a string value:
300
342
 
301
343
  ```ruby
302
- require 'rspec-puppet-facts'
303
- include RspecPuppetFacts
344
+ add_custom_fact :root_home, '/root', :exclude => 'redhat-7-x86_64'
304
345
  ```
305
346
 
306
- Run the tests:
347
+ #### Create dynamic facts
307
348
 
308
- ```bash
309
- rake spec
349
+ In addition to the static fact values shown in the previous examples, you can create dynamic values.
350
+
351
+ To do this, pass a lambda as the value for the custom fact. The lambda is passed the same values for operating system name and fact values that your tests are provided by `on_supported_os`.
352
+
353
+ ```ruby
354
+ add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']" }
310
355
  ```
311
356
 
312
- Run the tests only on some of the facts sets:
357
+ Running your tests
358
+ ------------------
359
+
360
+ For most cases, there is no change to how you run your tests. Running `rake spec` will run all the tests against the facts for all the supported operating systems.
361
+
362
+ If you want to run the tests against the facts for specific operating systems, you can provide a filter in the `SPEC_FACTS_OS` environment variable and only the supported operating systems whose name starts with the specified filter will be used.
313
363
 
314
364
  ```bash
315
365
  SPEC_FACTS_OS='ubuntu-14' rake spec
316
366
  ```
317
-
318
- Finaly, Add some `facter` version to test in your .travis.yml
319
-
320
- ```yaml
321
- ...
322
- matrix:
323
- fast_finish: true
324
- include:
325
- - rvm: 1.8.7
326
- env: PUPPET_GEM_VERSION="~> 2.7.0" FACTER_GEM_VERSION="~> 1.6.0"
327
- - rvm: 1.8.7
328
- env: PUPPET_GEM_VERSION="~> 2.7.0" FACTER_GEM_VERSION="~> 1.7.0"
329
- - rvm: 1.9.3
330
- env: PUPPET_GEM_VERSION="~> 3.0" FACTER_GEM_VERSION="~> 2.1.0"
331
- - rvm: 1.9.3
332
- env: PUPPET_GEM_VERSION="~> 3.0" FACTER_GEM_VERSION="~> 2.2.0"
333
- - rvm: 2.0.0
334
- env: PUPPET_GEM_VERSION="~> 3.0"
335
- allow_failures:
336
- - rvm: 1.8.7
337
- env: PUPPET_GEM_VERSION="~> 2.7.0" FACTER_GEM_VERSION="~> 1.6.0"
338
- ...
339
- ```
@@ -2,6 +2,6 @@ module RspecPuppetFacts
2
2
  # This module contains the current version constant
3
3
  module Version
4
4
  # The current version of this gem
5
- STRING = '1.7.0'
5
+ STRING = '1.8.0'
6
6
  end
7
7
  end
@@ -23,12 +23,22 @@ module RspecPuppetFacts
23
23
  # @param [Hash] opts
24
24
  # @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. x86_64
25
25
  # @option opts [Array<Hash>] :supported_os If this options is provided the data
26
+ # @option opts [String] :facterversion the facter version of which to
27
+ # select facts from, e.g.: '3.6'
26
28
  # will be used instead of the "operatingsystem_support" section if the metadata file
27
29
  # even if the file is missing.
28
30
  def on_supported_os(opts = {})
29
31
  opts[:hardwaremodels] ||= ['x86_64']
30
32
  opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
31
33
  opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os
34
+ opts[:facterversion] ||= Facter.version
35
+
36
+ unless (facterversion = opts[:facterversion]) =~ /\A\d+\.\d+(?:\.\d+)*\z/
37
+ raise ArgumentError, ":facterversion must be in the format 'n.n' or " \
38
+ "'n.n.n' (n is numeric), not '#{facterversion}'"
39
+ end
40
+
41
+ facter_version_filter = RspecPuppetFacts.facter_version_to_filter(facterversion)
32
42
 
33
43
  filter = []
34
44
  opts[:supported_os].map do |os_sup|
@@ -36,16 +46,16 @@ module RspecPuppetFacts
36
46
  os_sup['operatingsystemrelease'].map do |operatingsystemmajrelease|
37
47
  opts[:hardwaremodels].each do |hardwaremodel|
38
48
 
39
- if os_sup['operatingsystem'] =~ /BSD/
49
+ if os_sup['operatingsystem'] =~ /BSD/i
40
50
  hardwaremodel = 'amd64'
41
- elsif os_sup['operatingsystem'] =~ /Solaris/
51
+ elsif os_sup['operatingsystem'] =~ /Solaris/i
42
52
  hardwaremodel = 'i86pc'
43
- elsif os_sup['operatingsystem'] =~ /windows/
53
+ elsif os_sup['operatingsystem'] =~ /Windows/i
44
54
  hardwaremodel = 'x64'
45
55
  end
46
56
 
47
57
  filter << {
48
- :facterversion => "/^#{Facter.version[0..2]}/",
58
+ :facterversion => facter_version_filter,
49
59
  :operatingsystem => os_sup['operatingsystem'],
50
60
  :operatingsystemrelease => "/^#{operatingsystemmajrelease.split(' ')[0]}/",
51
61
  :hardwaremodel => hardwaremodel,
@@ -55,7 +65,7 @@ module RspecPuppetFacts
55
65
  else
56
66
  opts[:hardwaremodels].each do |hardwaremodel|
57
67
  filter << {
58
- :facterversion => "/^#{Facter.version[0..2]}/",
68
+ :facterversion => facter_version_filter,
59
69
  :operatingsystem => os_sup['operatingsystem'],
60
70
  :hardwaremodel => hardwaremodel,
61
71
  }
@@ -220,4 +230,12 @@ module RspecPuppetFacts
220
230
  @metadata = nil
221
231
  end
222
232
 
233
+ # Generates a JGrep statement expression for a specific facter version
234
+ # @return [String] JGrep statement expression
235
+ # @param version [String] the Facter version
236
+ # @api private
237
+ def self.facter_version_to_filter(version)
238
+ major, minor = version.split('.')
239
+ "/\\A#{major}\\.#{minor}\\./"
240
+ end
223
241
  end
@@ -370,6 +370,109 @@ describe RspecPuppetFacts do
370
370
  )
371
371
  end
372
372
  end
373
+
374
+ context 'Without a custom facterversion in the options hash' do
375
+ subject do
376
+ on_supported_os(
377
+ supported_os: [
378
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
379
+ ]
380
+ )
381
+ end
382
+
383
+ it 'returns facts from the loaded facter version' do
384
+ major, minor = Facter.version.split('.')
385
+ is_expected.to match(
386
+ 'centos-7-x86_64' => include(
387
+ facterversion: /\A#{major}\.#{minor}\./
388
+ )
389
+ )
390
+ end
391
+ end
392
+
393
+ context 'With a custom facterversion (3.1) in the options hash' do
394
+ subject do
395
+ on_supported_os(
396
+ supported_os: [
397
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
398
+ ],
399
+ facterversion: '3.1'
400
+ )
401
+ end
402
+
403
+ it 'returns facts from a facter version matching 3.1' do
404
+ is_expected.to match(
405
+ 'centos-7-x86_64' => include(facterversion: '3.1.6')
406
+ )
407
+ end
408
+ end
409
+
410
+ context 'With a custom facterversion (3.1.2) in the options hash' do
411
+ subject do
412
+ on_supported_os(
413
+ supported_os: [
414
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
415
+ ],
416
+ facterversion: '3.1.2'
417
+ )
418
+ end
419
+
420
+ it 'returns facts from a facter version matching 3.1' do
421
+ is_expected.to match(
422
+ 'centos-7-x86_64' => include(facterversion: '3.1.6')
423
+ )
424
+ end
425
+ end
426
+
427
+ context 'With a custom facterversion (3.3) in the options hash' do
428
+ subject do
429
+ on_supported_os(
430
+ supported_os: [
431
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
432
+ ],
433
+ facterversion: '3.3'
434
+ )
435
+ end
436
+
437
+ it 'returns facts from a facter version matching 3.3' do
438
+ is_expected.to match(
439
+ 'centos-7-x86_64' => include(facterversion: '3.3.0')
440
+ )
441
+ end
442
+ end
443
+
444
+ context 'With a custom facterversion (3.3.2) in the options hash' do
445
+ subject do
446
+ on_supported_os(
447
+ supported_os: [
448
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
449
+ ],
450
+ facterversion: '3.3.2'
451
+ )
452
+ end
453
+
454
+ it 'returns facts from a facter version matching 3.3' do
455
+ is_expected.to match(
456
+ 'centos-7-x86_64' => include(facterversion: '3.3.0')
457
+ )
458
+ end
459
+ end
460
+
461
+ context 'With an invalid facterversion in the options hash' do
462
+ let(:method_call) do
463
+ on_supported_os(
464
+ supported_os: [
465
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
466
+ ],
467
+ facterversion: '3'
468
+ )
469
+ end
470
+
471
+ it 'raises an error' do
472
+ expect { method_call }.to raise_error(ArgumentError,
473
+ /:facterversion must be in the /)
474
+ end
475
+ end
373
476
  end
374
477
 
375
478
  context '#add_custom_fact' do
@@ -451,4 +554,69 @@ describe RspecPuppetFacts do
451
554
  end
452
555
  end
453
556
 
557
+ describe '.facter_version_to_filter' do
558
+ context 'when passed a version that is major.minor (1)' do
559
+ subject { RspecPuppetFacts.facter_version_to_filter('1.2') }
560
+
561
+ it 'returns the correct JGrep statement expression' do
562
+ is_expected.to eq('/\A1\.2\./')
563
+ end
564
+ end
565
+
566
+ context 'when passed a version that is major.minor (2)' do
567
+ subject { RspecPuppetFacts.facter_version_to_filter('10.2') }
568
+
569
+ it 'returns the correct JGrep statement expression' do
570
+ is_expected.to eq('/\A10\.2\./')
571
+ end
572
+ end
573
+
574
+ context 'when passed a version that is major.minor (3)' do
575
+ subject { RspecPuppetFacts.facter_version_to_filter('1.20') }
576
+
577
+ it 'returns the correct JGrep statement expression' do
578
+ is_expected.to eq('/\A1\.20\./')
579
+ end
580
+ end
581
+
582
+ context 'when passed a version that is major.minor (4)' do
583
+ subject { RspecPuppetFacts.facter_version_to_filter('10.20') }
584
+
585
+ it 'returns the correct JGrep statement expression' do
586
+ is_expected.to eq('/\A10\.20\./')
587
+ end
588
+ end
589
+
590
+ context 'when passed a version that is major.minor.patch (1)' do
591
+ subject { RspecPuppetFacts.facter_version_to_filter('1.2.3') }
592
+
593
+ it 'returns the correct JGrep statement expression' do
594
+ is_expected.to eq('/\A1\.2\./')
595
+ end
596
+ end
597
+
598
+ context 'when passed a version that is major.minor.patch (2)' do
599
+ subject { RspecPuppetFacts.facter_version_to_filter('10.2.3') }
600
+
601
+ it 'returns the correct JGrep statement expression' do
602
+ is_expected.to eq('/\A10\.2\./')
603
+ end
604
+ end
605
+
606
+ context 'when passed a version that is major.minor.patch (3)' do
607
+ subject { RspecPuppetFacts.facter_version_to_filter('1.20.3') }
608
+
609
+ it 'returns the correct JGrep statement expression' do
610
+ is_expected.to eq('/\A1\.20\./')
611
+ end
612
+ end
613
+
614
+ context 'when passed a version that is major.minor.patch (4)' do
615
+ subject { RspecPuppetFacts.facter_version_to_filter('10.20.3') }
616
+
617
+ it 'returns the correct JGrep statement expression' do
618
+ is_expected.to eq('/\A10\.20\./')
619
+ end
620
+ end
621
+ end
454
622
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-puppet-facts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickaël Canévet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-16 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  requirements: []
194
194
  rubyforge_project:
195
- rubygems_version: 2.5.1
195
+ rubygems_version: 2.5.2
196
196
  signing_key:
197
197
  specification_version: 4
198
198
  summary: Standard facts fixtures for Puppet