Skip to content

Commit

Permalink
(FACT-2699) Detect augeas from gem if augparse is not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie committed Jul 30, 2020
1 parent 1ec10ee commit 458f69e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
14 changes: 6 additions & 8 deletions lib/facter/resolvers/augeas_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ def read_augeas_from_cli
def read_augeas_from_gem
require 'augeas'

return ::Augeas.create { |aug| aug.get('/augeas/version') } if ::Augeas.respond_to?(:create)

# it is used for legacy augeas <= 0.5.0
return ::Augeas.open { |aug| aug.get('/augeas/version') } if ::Augeas.respond_to?(:open)
rescue StandardError => e
log.debug('ruby-augeas not available')
log.log_exception(e)
nil
if Gem.loaded_specs['augeas']
::Augeas.create { |aug| aug.get('/augeas/version') }
else
# it is used for legacy augeas <= 0.5.0 (ruby-augeas gem)
::Augeas.open { |aug| aug.get('/augeas/version') }
end
end
end
end
Expand Down
27 changes: 5 additions & 22 deletions spec/facter/resolvers/augeas_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@
end

context 'when augeas gem > 0.5.0 is installed' do
let(:augeas_mock) { instance_spy(Augeas) }

before do
allow(Augeas).to receive(:respond_to?).with(:create).and_return(true)
allow(Augeas).to receive(:respond_to?).with(:create, true).and_return(true)
allow(Gem).to receive(:loaded_specs).and_return({ 'augeas' => true })
allow(Augeas).to receive(:create).and_return('1.12.0')
end

Expand All @@ -47,12 +44,8 @@
end

context 'when augeas gem <= 0.5.0 is installed' do
let(:augeas_mock) { instance_spy(Augeas) }

before do
allow(Augeas).to receive(:respond_to?).with(:create).and_return(false)
allow(Augeas).to receive(:respond_to?).with(:open).and_return(true)
allow(Augeas).to receive(:respond_to?).with(:open, true).and_return(true)
allow(Gem).to receive(:loaded_specs).and_return({})
allow(Augeas).to receive(:open).and_return('1.12.0')
end

Expand All @@ -62,26 +55,16 @@
end

context 'when augeas gem is not installed' do
let(:exception) { StandardError.new('error_message') }
let(:exception) { LoadError.new('load_error_message') }

before do
allow(Facter::Resolvers::Augeas).to receive(:require).with('augeas').and_raise(exception)
end

it 'returns nil' do
expect(augeas.resolve(:augeas_version)).to be_nil
end

it 'logs a debug message' do
augeas.resolve(:augeas_version)

expect(log_spy).to have_received(:debug).with('ruby-augeas not available')
end

it 'logs an exception' do
it 'raises a LoadError error' do
augeas.resolve(:augeas_version)

expect(log_spy).to have_received(:log_exception).with(exception)
expect(log_spy).to have_received(:debug).with('resolving fact augeas_version, but load_error_message')
end
end
end
Expand Down

0 comments on commit 458f69e

Please sign in to comment.