Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Aug 19, 2023
1 parent 10494b0 commit fceb811
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
38 changes: 19 additions & 19 deletions lib/rspec-puppet/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,7 @@ class Base
# @param example_group [RSpec::Core::ExampleGroup] The RSpec context to use for local settings
# @return [void]
def setup_puppet(example_group)

Check failure on line 38 in lib/rspec-puppet/adapters.rb

View workflow job for this annotation

GitHub Actions / spec (windows-latest ruby 2.7 | puppet ~> 7.0) / spec

Metrics/MethodLength: Method has too many lines. [41/37]

Check failure on line 38 in lib/rspec-puppet/adapters.rb

View workflow job for this annotation

GitHub Actions / spec (ubuntu-latest ruby 2.7 | puppet ~> 7.0) / spec

Metrics/MethodLength: Method has too many lines. [41/37]

Check failure on line 38 in lib/rspec-puppet/adapters.rb

View workflow job for this annotation

GitHub Actions / spec (ubuntu-latest ruby 3.2 | puppet https://github.com/puppetlabs/puppet) / spec

Metrics/MethodLength: Method has too many lines. [41/37]

Check failure on line 38 in lib/rspec-puppet/adapters.rb

View workflow job for this annotation

GitHub Actions / spec (windows-latest ruby 3.2 | puppet https://github.com/puppetlabs/puppet) / spec

Metrics/MethodLength: Method has too many lines. [41/37]
case RSpec.configuration.facter_implementation.to_sym
when :rspec
# Lazily instantiate FacterTestImpl here to optimize memory
# allocation, as the proc will only be called if FacterImpl is unset
set_facter_impl(proc { RSpec::Puppet::FacterTestImpl.new })
when :facter
set_facter_impl(Facter)
else
raise "Unsupported facter_implementation '#{RSpec.configuration.facter_implementation}'"
end

Puppet.runtime[:facter] = FacterImpl
Puppet.runtime[:facter] = cached_facter_impl

settings = settings_map.map do |puppet_setting, rspec_setting|
[puppet_setting, get_setting(example_group, rspec_setting)]
Expand Down Expand Up @@ -169,15 +158,26 @@ def manifest
# @api private
#
# Set the FacterImpl constant to the given Facter implementation or noop
# if the constant is already set. If a proc is given, it will only be
# called if FacterImpl is not defined.
#
# @param impl [Object, Proc] An object or a proc that implements the Facter API
def set_facter_impl(impl)
return if defined?(FacterImpl)
# if the constant is already set.
def cached_facter_impl
return FacterImpl if defined?(FacterImpl)

impl = impl.call if impl.is_a?(Proc)
impl = facter_impl
Object.send(:const_set, :FacterImpl, impl)
impl
end

# @summary Return the Facter implementation based on the configuration
# @api private
def facter_impl
case RSpec.configuration.facter_implementation.to_sym
when :rspec
RSpec::Puppet::FacterTestImpl.new
when :facter
set_facter_impl(Facter)
else
raise "Unsupported facter_implementation '#{RSpec.configuration.facter_implementation}'"
end
end
end

Expand Down
74 changes: 38 additions & 36 deletions spec/unit/adapters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,53 @@ def context_double(options = {})
expect(Puppet[:confdir]).to match(%r{(C:)?/etc/sockpuppet})
end
end
end

describe 'when the setting is not available on the given version of Puppet' do
it 'logs a warning about the setting' do
end
describe '#cached_facter_impl' do
subject { described_class.send(:cached_facter_impl) }

before do
Object.send(:remove_const, :FacterImpl) if defined? FacterImpl
end
end

describe '#setup_puppet' do
describe 'when managing the facter_implementation' do
after do
Object.send(:remove_const, :FacterImpl) if defined? FacterImpl
end
after do
Object.send(:remove_const, :FacterImpl) if defined? FacterImpl
end

it 'uses facter as default implementation' do
context = context_double
subject.setup_puppet(context)
expect(FacterImpl).to be(Facter)
end
it 'uses facter as default implementation' do
is_expected.to be(Facter)

Check failure on line 105 in spec/unit/adapters_spec.rb

View workflow job for this annotation

GitHub Actions / spec (windows-latest ruby 2.7 | puppet ~> 7.0) / spec

RSpec/ImplicitSubject: Don't use implicit subject.

Check failure on line 105 in spec/unit/adapters_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ubuntu-latest ruby 2.7 | puppet ~> 7.0) / spec

RSpec/ImplicitSubject: Don't use implicit subject.

Check failure on line 105 in spec/unit/adapters_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ubuntu-latest ruby 3.2 | puppet https://github.com/puppetlabs/puppet) / spec

RSpec/ImplicitSubject: Don't use implicit subject.

Check failure on line 105 in spec/unit/adapters_spec.rb

View workflow job for this annotation

GitHub Actions / spec (windows-latest ruby 3.2 | puppet https://github.com/puppetlabs/puppet) / spec

RSpec/ImplicitSubject: Don't use implicit subject.
end

it 'uses the hash implementation if set and if puppet supports runtimes' do
context = context_double
Puppet.runtime[:facter] = 'something'
allow(RSpec.configuration).to receive(:facter_implementation).and_return('rspec')
subject.setup_puppet(context)
expect(FacterImpl).to be_a(RSpec::Puppet::FacterTestImpl)
end
it 'uses the rspec implementation' do
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:rspec)
is_expected.to be_a(RSpec::Puppet::FacterTestImpl)
end

it 'ensures consistency of FacterImpl in subsequent example groups' do
context = context_double
it 'ensures consistency of FacterImpl in subsequent example groups' do
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:facter)
is_expected.to be(Facter)

# Pretend that FacterImpl is already initialized from a previous example group
Puppet.runtime[:facter] = RSpec::Puppet::FacterTestImpl.new
Object.send(:const_set, :FacterImpl, Puppet.runtime[:facter])
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:rspec)
is_expected.to be(Facter)
end
end

allow(RSpec.configuration).to receive(:facter_implementation).and_return('rspec')
subject.setup_puppet(context)
expect(FacterImpl).to eq(Puppet.runtime[:facter])
end
describe '#facter_impl' do
subject { described_class.send(:facter_impl) }

it 'raises if given an unsupported option' do
context = context_double
allow(RSpec.configuration).to receive(:facter_implementation).and_return('salam')
expect { subject.setup_puppet(context) }
.to raise_error(RuntimeError, "Unsupported facter_implementation 'salam'")
end
it 'supports facter' do
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:facter)
is_expected.to be(Facter)
end

it 'supports rspec' do
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:rspec)
is_expected.to be_a(RSpec::Puppet::FacterTestImpl)
end

it 'raises if given an unsupported option' do
allow(RSpec.configuration).to receive(:facter_implementation).and_return(:salam)
expect { subject }.to raise_error(RuntimeError, "Unsupported facter_implementation 'salam'")
end
end
end

0 comments on commit fceb811

Please sign in to comment.