Skip to content

Commit

Permalink
Convert from mocha to rspec-mocks (voxpupuli#158)
Browse files Browse the repository at this point in the history
This is recommended by puppetlabs_spec_helper and avoids a deprecation
warning. bbed0b8 already started the
conversion, but didn't update the existing fact tests. Because the
mock_with call was incorrect (needs to be done before require), this
didn't show.
  • Loading branch information
ekohl authored and simondeziel committed Jun 16, 2021
1 parent ec45772 commit 356b9d0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Gemfile:
- gem: 'github_changelog_generator'
Rakefile:
changelog_version_tag_pattern: '%s'
spec/spec_helper.rb:
mock_with: :rspec
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

RSpec.configure do |c|
c.mock_with :rspec
end

require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'

Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
RSpec::Mocks::Syntax.enable_expect(RSpec::Puppet::ManifestMatchers)

RSpec.configure do |config|
config.mock_with :rspec

config.before :each do
# Ensure that we don't accidentally cache facts and environment between
# test cases. This requires each example group to explicitly load the
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/facter/systemd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe 'systemd' do
context 'returns true when systemd present' do
before(:each) do
Facter.fact(:kernel).stubs(:value).returns(:linux)
allow(Facter.fact(:kernel)).to receive(:value).and_return(:linux)
Facter.add(:service_provider) { setcode { 'systemd' } }
end

Expand All @@ -17,7 +17,7 @@

context 'returns false when systemd not present' do
before(:each) do
Facter.fact(:kernel).stubs(:value).returns(:linux)
allow(Facter.fact(:kernel)).to receive(:value).and_return(:linux)
Facter.add(:service_provider) { setcode { 'redhat' } }
end

Expand All @@ -27,7 +27,7 @@

context 'returns nil when kernel is not linux' do
before(:each) do
Facter.fact(:kernel).stubs(:value).returns(:windows)
allow(Facter.fact(:kernel)).to receive(:value).and_return(:windows)
end

it { expect(Facter.value(:systemd)).to be_nil }
Expand Down
9 changes: 4 additions & 5 deletions spec/unit/facter/systemd_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
describe 'systemd_version' do
context 'returns version when systemd fact present' do
before(:each) do
Facter.fact(:systemd).stubs(:value).returns(true)
allow(Facter.fact(:systemd)).to receive(:value).and_return(true)
end
let(:facts) { { systemd: true } }

it do
Facter::Util::Resolution.expects(:exec).with("systemctl --version | awk '/systemd/{ print $2 }'").returns('229')
expect(Facter::Util::Resolution).to receive(:exec).with("systemctl --version | awk '/systemd/{ print $2 }'").and_return('229')
expect(Facter.value(:systemd_version)).to eq('229')
end
end
context 'returns nil when systemd fact not present' do
before(:each) do
Facter.fact(:systemd).stubs(:value).returns(false)
allow(Facter.fact(:systemd)).to receive(:value).and_return(false)
end
let(:facts) { { systemd: false } }

it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:exec).with("systemctl --version | awk '/systemd/{ print $2 }'").never
expect(Facter::Util::Resolution).not_to receive(:exec).with("systemctl --version | awk '/systemd/{ print $2 }'")
expect(Facter.value(:systemd_version)).to eq(nil)
end
end
Expand Down

0 comments on commit 356b9d0

Please sign in to comment.