rspec-puppet-facts 1.6.1 → 1.7.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: 187629e1d3599c172d3b0e23135f5d92ae04a8e2
4
- data.tar.gz: 1205b765d9578ded7591cd1822943212404f9634
3
+ metadata.gz: 13500ef636b561fc94ded0be8be865f94f004b07
4
+ data.tar.gz: 515732b3aa600a4f4b6de18598d064972160944a
5
5
  SHA512:
6
- metadata.gz: 3d28c8fc4e11155a4c96e1f4f22816913eee8220d824305022b5bb084ec7a95a4bc4961b436b56959975beab6ee9375a01ed732302fa7c4f07ddd71e53afde9c
7
- data.tar.gz: 7fd003eb3d0808311230355ff22dd791089d6878d7841ec8b58814c94a1ce17c09e4965c9ae4f4fbc9a275e47795739bb663f9ae74034973c93e1f9ce3d27f8d
6
+ metadata.gz: 1495b667b0e488ac1c9bcf8ee6bfd89a01ec5e747ff31f97f0eca476325e4efb57141e8eef4341db9596ebcf2d9f5569292ed6baa12417643740466992b7336d
7
+ data.tar.gz: 2494c062b9d5e89c6fd94e0dd5b62c0e873c820e5bd14468114ce9ce4577afedf5c4c3674b1c9874bbc2d354176cafba10751b761727d89d1e75db56850be4b4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ##2016-09-16 - Release 1.7.0
2
+ - Support custom facts defined by spec_helper
3
+
1
4
  ##2016-05-19 - Release 1.6.1
2
5
  - Fix a bug where not all specified Ubuntu or OpenBSD were captured
3
6
 
data/Gemfile CHANGED
@@ -7,3 +7,9 @@ if facterversion = ENV['FACTER_GEM_VERSION']
7
7
  else
8
8
  gem 'facter', :require => false
9
9
  end
10
+
11
+ platforms :ruby_18, :ruby_19 do
12
+ gem 'json', '~> 1.0'
13
+ gem 'json_pure', '~> 1.0'
14
+ gem 'tins', '~> 1.6.0'
15
+ end
data/README.md CHANGED
@@ -29,7 +29,7 @@ describe 'myclass' do
29
29
  :operatingsystemmajrelease => '7',
30
30
  ...
31
31
  }
32
-
32
+
33
33
  it { is_expected.to compile.with_all_deps }
34
34
  ...
35
35
  end
@@ -43,12 +43,12 @@ describe 'myclass' do
43
43
  :operatingsystemmajrelease => '6',
44
44
  ...
45
45
  }
46
-
46
+
47
47
  it { is_expected.to compile.with_all_deps }
48
48
  ...
49
49
  end
50
50
  end
51
-
51
+
52
52
  ...
53
53
  end
54
54
  ```
@@ -65,7 +65,7 @@ describe 'myclass' do
65
65
  let(:facts) do
66
66
  facts
67
67
  end
68
-
68
+
69
69
  it { is_expected.to compile.with_all_deps }
70
70
  ...
71
71
  case facts[:osfamily]
@@ -193,9 +193,9 @@ describe Puppet::Parser::Functions.function(:myfunction) do
193
193
  end
194
194
  end
195
195
  end
196
-
196
+
197
197
  ...
198
-
198
+
199
199
  end
200
200
  end
201
201
  ```
@@ -230,7 +230,7 @@ describe 'myclass' do
230
230
  let(:facts) do
231
231
  facts
232
232
  end
233
-
233
+
234
234
  it { is_expected.to compile.with_all_deps }
235
235
  ...
236
236
  end
@@ -241,6 +241,8 @@ end
241
241
  Append some facts:
242
242
  ------------------
243
243
 
244
+ 1. In your spec
245
+
244
246
  ```ruby
245
247
  require 'spec_helper'
246
248
 
@@ -253,7 +255,7 @@ describe 'myclass' do
253
255
  :foo => 'bar',
254
256
  })
255
257
  end
256
-
258
+
257
259
  it { is_expected.to compile.with_all_deps }
258
260
  ...
259
261
  end
@@ -261,6 +263,30 @@ describe 'myclass' do
261
263
  end
262
264
  ```
263
265
 
266
+ 2. In your `spec_helper.rb`
267
+ * Simple:
268
+
269
+ ```ruby
270
+ add_custom_fact 'foo', 'bar'
271
+ ```
272
+
273
+ * Confine to an OS:
274
+
275
+ ```ruby
276
+ add_custom_fact :root_home, '/root', :confine => 'redhat-7-x86_64'
277
+ ```
278
+
279
+ * Exclude an OS:
280
+
281
+ ```ruby
282
+ add_custom_fact :root_home, '/root', :exclude => 'redhat-7-x86_64'
283
+ ```
284
+ * Call a proc to get a value:
285
+
286
+ ```ruby
287
+ add_custom_fact :root_home, ->(_os,facts) { "/tmp/#{facts['hostname']}"
288
+ ```
289
+
264
290
  Usage
265
291
  -----
266
292
 
@@ -86,15 +86,65 @@ module RspecPuppetFacts
86
86
  os = "#{facts[:operatingsystem].downcase}-#{operatingsystemmajrelease}-#{facts[:hardwaremodel]}"
87
87
  next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
88
88
  facts.merge! RspecPuppetFacts.common_facts
89
- os_facts_hash[os] = facts
89
+ os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
90
90
  end
91
91
  os_facts_hash
92
92
  end
93
93
 
94
+ # Register a custom fact that will be included in the facts hash.
95
+ # If it should be limited to a particular OS, pass a :confine option
96
+ # that contains the operating system(s) to confine to. If it should
97
+ # be excluded on a particular OS, use :exclude.
98
+ #
99
+ # @param [String] name Fact name
100
+ # @param [String,Proc] value Fact value. If proc, takes 2 params: os and facts hash
101
+ # @param [Hash] opts
102
+ # @option opts [String,Array<String>] :confine The applicable OS's
103
+ # @option opts [String,Array<String>] :exclude OS's to exclude
104
+ #
105
+ def add_custom_fact(name, value, options = {})
106
+ options[:confine] = [options[:confine]] if options[:confine].is_a?(String)
107
+ options[:exclude] = [options[:exclude]] if options[:exclude].is_a?(String)
108
+
109
+ RspecPuppetFacts.register_custom_fact(name, value, options)
110
+ end
111
+
112
+ # Adds a custom fact to the @custom_facts variable.
113
+ #
114
+ # @param [String] name Fact name
115
+ # @param [String,Proc] value Fact value. If proc, takes 2 params: os and facts hash
116
+ # @param [Hash] opts
117
+ # @option opts [String,Array<String>] :confine The applicable OS's
118
+ # @option opts [String,Array<String>] :exclude OS's to exclude
119
+ # @api private
120
+ def self.register_custom_fact(name, value, options)
121
+ @custom_facts ||= {}
122
+ @custom_facts[name.to_s] = {:options => options, :value => value}
123
+ end
124
+
125
+ # Adds any custom facts according to the rules defined for the operating
126
+ # system with the given facts.
127
+ # @param [String] os Name of the operating system
128
+ # @param [Hash] facts Facts hash
129
+ # @return [Hash] facts Facts hash with custom facts added
130
+ # @api private
131
+ def self.with_custom_facts(os, facts)
132
+ return facts unless @custom_facts
133
+
134
+ @custom_facts.each do |name, fact|
135
+ next if fact[:options][:confine] && !fact[:options][:confine].include?(os)
136
+ next if fact[:options][:exclude] && fact[:options][:exclude].include?(os)
137
+
138
+ facts[name] = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
139
+ end
140
+
141
+ facts
142
+ end
143
+
94
144
  # If provided this filter can be used to limit the set
95
145
  # of retrieved facts only to the matched OS names.
96
146
  # The value is being taken from the SPEC_FACTS_OS environment
97
- # variable and
147
+ # variable and
98
148
  # @return [nil,String]
99
149
  # @api private
100
150
  def self.spec_facts_os_filter
@@ -165,6 +215,7 @@ module RspecPuppetFacts
165
215
  # be generated again
166
216
  # @api private
167
217
  def self.reset
218
+ @custom_facts = nil
168
219
  @common_facts = nil
169
220
  @metadata = nil
170
221
  end
@@ -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.6.1'
5
+ STRING = '1.7.0'
6
6
  end
7
7
  end
@@ -372,6 +372,50 @@ describe RspecPuppetFacts do
372
372
  end
373
373
  end
374
374
 
375
+ context '#add_custom_fact' do
376
+ subject {
377
+ on_supported_os(
378
+ {
379
+ :supported_os => [
380
+ {
381
+ "operatingsystem" => "RedHat",
382
+ "operatingsystemrelease" => [
383
+ "6",
384
+ "7"
385
+ ]
386
+ }
387
+ ]
388
+ }
389
+ )
390
+ }
391
+
392
+ before(:each) do
393
+ RspecPuppetFacts.reset
394
+ end
395
+
396
+ it 'adds a simple fact and value' do
397
+ add_custom_fact 'root_home', '/root'
398
+ expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
399
+ end
400
+
401
+ it 'confines a fact to a particular operating system' do
402
+ add_custom_fact 'root_home', '/root', :confine => 'redhat-7-x86_64'
403
+ expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
404
+ expect(subject['redhat-6-x86_64']['root_home']).to be_nil
405
+ end
406
+
407
+ it 'excludes a fact from a particular operating system' do
408
+ add_custom_fact 'root_home', '/root', :exclude => 'redhat-7-x86_64'
409
+ expect(subject['redhat-7-x86_64']['root_home']).to be_nil
410
+ expect(subject['redhat-6-x86_64']['root_home']).to eq '/root'
411
+ end
412
+
413
+ it 'takes a proc as a value' do
414
+ add_custom_fact 'root_home', ->(_os, _facts) { '/root' }
415
+ expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
416
+ end
417
+ end
418
+
375
419
  context '#misc' do
376
420
  it 'should have a common facts structure' do
377
421
  RspecPuppetFacts.reset
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.6.1
4
+ version: 1.7.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-05-19 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types