Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a describe_on_supported_os DSL #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the example here just so you could easily compare it to the block above it.

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]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current setup only symbolizes part of the data used in blocks like this. How would one expanded to use non-legacy facts look? It looks like this today, for context:

case os_facts[:os]['family']
when 'RedHat'
  ...
when 'Debian'
  ...
else
  ...
end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metadata[:os_facts] is os_facts so metadata[:os_facts][:os]['family'].

Again, really unrelated but yes, it's really confusing that it's not fully using symbols. The root cause is here. Ideally speaking it would use something like HashWithIndifferentAccess but pulling in ActiveSupport just for that feels very heavy. However, facts from FacterDB should really be read-only besides overrides. We can probably easily subclass Hash and override [](key) to call .to_s(key). While we're at it, implement override in it as well.

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