Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
(FACT-2559) Fixed some tests leakage in logger_spec.rb
Browse files Browse the repository at this point in the history
Added tests for when Facter is not fully loaded
  • Loading branch information
Andrei Filipovici committed Apr 16, 2020
1 parent ffb48d7 commit 35015af
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
14 changes: 11 additions & 3 deletions lib/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def self.set_format_for_legacy_logger
end

def debug(msg)
if Facter.respond_to?(:debugging?)
return unless Facter.debugging?
end
return unless debugging_active?

if msg.nil? || msg.empty?
invoker = caller(1..1).first.slice(/.*:\d+/)
Expand Down Expand Up @@ -96,5 +94,15 @@ def error(msg, colorize = false)
def colorize(msg, color)
"\e[#{color}m#{msg}\e[0m"
end

private

def debugging_active?
if Facter.respond_to?(:debugging?)
Facter.debugging?
else
true
end
end
end
end
20 changes: 10 additions & 10 deletions spec/facter/util/file_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@
expect(File).not_to have_received(:read)
end

# it 'logs a debug message' do
# file_helper.safe_read(path)
#
# expect(logger).to have_received(:debug).with(error_message)
# end
it 'logs a debug message' do
file_helper.safe_read(path)

expect(logger).to have_received(:debug).with(error_message)
end
end
end

Expand Down Expand Up @@ -142,11 +142,11 @@
expect(File).not_to have_received(:readlines)
end

# it 'logs a debug message' do
# file_helper.safe_read(path)
#
# expect(logger).to have_received(:debug).with(error_message)
# end
it 'logs a debug message' do
file_helper.safe_read(path)

expect(logger).to have_received(:debug).with(error_message)
end
end
end
end
76 changes: 37 additions & 39 deletions spec/framework/logging/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,75 @@
describe Logger do
subject(:log) { Facter::Log.new(Class) }

let(:file_logger_double) { instance_spy(Logger) }
let(:multi_logger_double) { instance_spy(Facter::MultiLogger, level: :warn) }

before do
Facter::Log.class_variable_set(:@@file_logger, file_logger_double)
Facter::Log.class_variable_set(:@@logger, multi_logger_double)

allow(file_logger_double).to receive(:formatter=)
end

describe '#debug' do
context 'when Facter.debugging? is accessible' do
before do
allow(Facter).to receive(:debugging?).and_return(true)
end
before do
allow(Facter).to receive(:debugging?).and_return(true)
end

it 'no debug messages are sent if debugging is set to false' do
allow(Facter).to receive(:debugging?).and_return(false)

it 'noops of debugging is not set' do
allow(Facter).to receive(:debugging?).and_return(false)
log.debug('info_message')

log.debug('info_message')
expect(multi_logger_double).not_to have_received(:debug)
end

expect(multi_logger_double).not_to have_received(:debug)
end
it 'logs a warn if message is nil' do
log.debug(nil)

expect(multi_logger_double).to have_received(:warn).with(/debug invoked with invalid message/)
end

it 'logs a warn if message is nil' do
log.debug(nil)
it 'logs a warn if message is empty' do
log.debug('')

expect(multi_logger_double).to have_received(:warn).with(/debug invoked with invalid message/)
end
expect(multi_logger_double).to have_received(:warn).with(/debug invoked with invalid message/)
end

it 'logs a warn if message is empty' do
log.debug('')
shared_examples 'writes debug message' do
it 'calls debug on multi_logger' do
log.debug('debug_message')

expect(multi_logger_double).to have_received(:warn).with(/debug invoked with invalid message/)
expect(multi_logger_double).to have_received(:debug).with('Class - debug_message')
end
end

shared_examples 'writes debug message' do
it 'calls multi_logger with :debug' do
log.debug('debug_message')
it_behaves_like 'writes debug message'

expect(multi_logger_double).to have_received(:debug).with('Class - debug_message')
end
context 'when message callback is provided' do
after do
Facter::Log.class_variable_set(:@@message_callback, nil)
end

it_behaves_like 'writes debug message'

context 'when strange things happen' do
let(:handler) { instance_spy(Logger) }
it 'provides on_message hook' do
Facter.on_message do |level, message|
handler.debug("on_message called with level: #{level}, message: #{message}")
end
it 'provides on_message hook' do
logger_double = instance_spy(Logger)
Facter.on_message do |level, message|
logger_double.debug("on_message called with level: #{level}, message: #{message}")
end

log.debug('test')
log.debug('test')

expect(handler).to have_received(:debug).with('on_message called with level: debug, message: test')
end
expect(logger_double).to have_received(:debug).with('on_message called with level: debug, message: test')
end
end

context 'when #debug is called during os detection in os_detector.rb' do
context 'when call is made during os detection in os_detector.rb and facter.rb is not fully loaded' do
before do
allow(Facter).to receive(:respond_to?).with(:debugging?).and_return(false)
end

it 'calls multi_logger with :debug' do
it_behaves_like 'writes debug message'

it 'does not call Facter.debugging?' do
log.debug('debug_message')

expect(multi_logger_double).to have_received(:debug).with('Class - debug_message')
expect(Facter).not_to have_received(:debugging?)
end
end
end
Expand Down

0 comments on commit 35015af

Please sign in to comment.