Skip to content

Commit

Permalink
Add a describe_on_supported_os DSL
Browse files Browse the repository at this point in the history
It is quite tedious to write the on_supported_os loop in every file.
This defines a short hand in the RSpec DSL. To keep access to the OS
facts, it is added to the metadata.
  • Loading branch information
ekohl committed Aug 6, 2021
1 parent 563ce3d commit d586583
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ describe 'myclass' do
end
```

```ruby
require 'spec_helper'

describe_on_supported_os 'myclass' do
it { is_expected.to compile.with_all_deps }
...

# If you need any to specify any operating system specific tests
case metadata[:os_facts][:osfamily]
when 'Debian'
...
else
...
end
end
```

When using roles and profiles to manage a heterogeneous IT estate, you can test a profile that supports several OSes with many `let(:facts)` as long as each is in its own context:
```ruby
require 'spec_helper'
Expand Down
18 changes: 18 additions & 0 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,26 @@ def self.facter_version_for_puppet_version(puppet_version)
ensure
fd.close if fd
end

module DSL
def describe_on_supported_os(*args, &block)
describe(*args) do
# TODO: assumes RspecPuppetFacts was included
on_supported_os.each do |os, os_facts|
context("on #{os}") do
let(:facts) { os_facts }

class_exec(&block)
end
end
end
end
end
end

RSpec::Core::ExampleGroup.extend(RspecPuppetFacts::DSL)
RSpec::Core::DSL.expose_example_group_alias(:describe_on_supported_os)

RSpec.configure do |c|
c.add_setting :default_facter_version,
:default => RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
Expand Down

0 comments on commit d586583

Please sign in to comment.