Skip to content

Commit b2215f6

Browse files
authored
Merge pull request #193 from bastelfreak/deprecate
Deprecate symbolized facts
2 parents 0f8009f + db6cff0 commit b2215f6

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

README.md

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ If you're using Bundler to manage gems in your module repository, install `rspec
2222
1. Add the following line to your `Gemfile`:
2323

2424
```ruby
25-
gem 'rspec-puppet-facts', :require => false
25+
gem 'rspec-puppet-facts', require: false
2626
```
2727

2828
2. Run `bundle install`.
@@ -54,38 +54,18 @@ By default, `rspec-puppet-facts` provides the facts only for `x86_64` architectu
5454
* `'operatingsystem'` - The name of the operating system, as a string.
5555
* `'operatingsystemrelease'` - An array of version numbers, as strings.
5656

57-
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:
57+
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 12 and Debian 11 on both `x86_64` _and_ `i386` architectures, you could write the following test:
5858

5959
```ruby
6060
require 'spec_helper'
6161

62-
describe 'myclass::debian' do
63-
test_on = {
64-
:hardwaremodels => ['x86_64', 'i386'],
65-
:supported_os => [
66-
{
67-
'operatingsystem' => 'Debian',
68-
'operatingsystemrelease' => ['6', '7'],
69-
},
70-
],
71-
}
72-
73-
on_supported_os(test_on).each do |os, os_facts|
74-
let (:facts) { os_facts }
75-
it { is_expected.to compile.with_all_deps }
76-
end
77-
end
78-
```
79-
Ruby 1.9 and later:
80-
```ruby
81-
require 'spec_helper'
82-
8362
describe 'myclass::raspbian' do
8463
test_on = {
64+
hardwaremodels: ['x86_64', 'i386'],
8565
supported_os: [
8666
{
8767
'operatingsystem' => 'Debian',
88-
'operatingsystemrelease' => ['10', '9', '8'],
68+
'operatingsystemrelease' => ['12', '11'],
8969
},
9070
],
9171
}
@@ -98,17 +78,64 @@ end
9878

9979
## Specifying a default Facter version
10080

101-
By default, `on_supported_os` will return the facts for the version of Facter
102-
that it has loaded (usually this is Facter 2.5.1). This behaviour can be
103-
overridden by setting the `default_facter_version` RSpec setting in your
104-
`spec/spec_helper.rb` file.
81+
By default, `on_supported_os` will return the facts for the version of Facter that it has loaded. It will check for the loaded Puppet version and maps that to the Facter version that Perforce released in the matching AIO.
82+
The mapping is stored in `ext/puppet_agent_components.json` (check the [maintenance](#maintenance) section for details) and computated in the `facter_version_for_puppet_version` method.
83+
This behaviour can be overridden by setting the `default_facter_version` RSpec setting in your `spec/spec_helper.rb` file.
10584

10685
```ruby
10786
RSpec.configure do |c|
10887
c.default_facter_version = '3.14.0'
10988
end
11089
```
11190

91+
## Symbolized vs stringified facts
92+
93+
For a long long time, the first level of keys in a factsets were symbols.
94+
This was fine before there were structured facts, but structured facts ended up as nested hashes that always had strings.
95+
This doesn't make sense and easy to get wrong.
96+
The original data contains only strings so conversion actually costs performance.
97+
98+
The option to switch between symbolized vs stringified facts was introduced in [25634f4481f20f2fc7444e867928ca607234e33e](https://github.com/voxpupuli/rspec-puppet-facts/commit/25634f4481f20f2fc7444e867928ca607234e33e) (Release [1.9.5](https://github.com/voxpupuli/rspec-puppet-facts/blob/master/CHANGELOG.md#2019-07-29---release-195)), but version 5.0.0 has resolved various implementation bugs.
99+
100+
This can be configured like this:
101+
102+
```ruby
103+
RSpec.configure do |c|
104+
c.facterdb_string_keys = false
105+
end
106+
```
107+
108+
In the 5.x release series of rspec-puppet-facts the default `false` is deprecated.
109+
With the 6.0.0 release we will flip it to `true` (https://github.com/voxpupuli/rspec-puppet-facts/pull/189).
110+
111+
You may have the following rspec-puppet test using deprecated symbolized facts:
112+
113+
```ruby
114+
on_supported_os.each do |os, os_facts|
115+
case os_facts[:os]['name']
116+
when 'Archlinux'
117+
context 'on Archlinux' do
118+
it { is_expected.to contain_package('borg') }
119+
end
120+
when 'Ubuntu'
121+
end
122+
end
123+
```
124+
125+
With the string facts, the same test looks like:
126+
127+
```ruby
128+
on_supported_os.each do |os, os_facts|
129+
case os_facts['os']['name']
130+
when 'Archlinux'
131+
context 'on Archlinux' do
132+
it { is_expected.to contain_package('borg') }
133+
end
134+
when 'Ubuntu'
135+
end
136+
end
137+
```
138+
112139
## Usage
113140

114141
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.
@@ -475,7 +502,7 @@ fact sets that only make sense in your environment or might contain sensitive in
475502
To supply external facts to facterdb just set the `FACTERDB_SEARCH_PATHS` environment variable with one or more
476503
paths to your facts.
477504

478-
When separating paths please use the default path separator character supported by your OS.
505+
When separating paths please use the default path separator character supported by your OS.
479506
* Unix/Linux/OSX = `:`
480507
* Windows = `;`
481508

0 commit comments

Comments
 (0)