Skip to content

Commit

Permalink
Merge branch 'main' into FACT-2315
Browse files Browse the repository at this point in the history
# Conflicts:
#	spec/facter/facter_spec.rb
  • Loading branch information
Filipovici-Andrei committed Sep 15, 2020
2 parents f1effaa + 202f8d2 commit 737ff92
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 22 deletions.
27 changes: 23 additions & 4 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ResolveCustomFactError < StandardError; end
Options.init
Log.output(STDOUT)
@already_searched = {}
@debug_once = []

class << self
def clear_messages
Expand Down Expand Up @@ -54,6 +55,7 @@ def add(name, options = {}, &block)
# @api public
def clear
@already_searched = {}
@debug_once = []
LegacyFacter.clear
Options[:custom_dir] = []
LegacyFacter.collection.invalidate_custom_facts
Expand All @@ -68,16 +70,33 @@ def core_value(user_query)
fact_collection.dig(*splitted_user_query)
end

# Prints out a debug message when debug option is set to true
# @param msg [String] Message to be printed out
# Logs debug message when debug option is set to true
# @param message [Object] Message object to be logged
#
# @return [nil]
#
# @api public
def debug(msg)
def debug(message)
return unless debugging?

logger.debug(msg)
logger.debug(message.to_s)
nil
end

# Logs the same debug message only once when debug option is set to true
# @param message [Object] Message object to be logged
#
# @return [nil]
#
# @api public
def debugonce(message)
return unless debugging?

message_string = message.to_s
return if @debug_once.include? message_string

@debug_once << message_string
logger.debug(message_string)
nil
end

Expand Down
4 changes: 1 addition & 3 deletions lib/facter/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def initialize(logged_class)
def debug(msg)
return unless debugging_active?

if msg.nil? || msg.empty?
empty_message_error(msg)
elsif @@message_callback
if @@message_callback
@@message_callback.call(:debug, msg)
else
msg = colorize(msg, CYAN) if Options[:color]
Expand Down
60 changes: 60 additions & 0 deletions spec/facter/facter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,66 @@ def mock_collection(method, os_name = nil, error = nil)
end
end

describe '#debugonce' do
context 'when debugging is active' do
before do
allow(logger).to receive(:debug)
Facter.debugging(true)
end

after do
Facter.debugging(false)
end

it 'calls logger with the debug message' do
message = 'Some error message'

Facter.debugonce(message)

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

it 'writes the same debug message only once' do
message = 'Some error message'

Facter.debugonce(message)
Facter.debugonce(message)

expect(logger).to have_received(:debug).once.with(message)
end

it 'writes empty message when message is nil' do
Facter.debugonce(nil)

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

it 'when message is a hash' do
Facter.debugonce({ warn: 'message' })

expect(logger).to have_received(:debug).with('{:warn=>"message"}')
end

it 'returns nil' do
result = Facter.debugonce({ warn: 'message' })

expect(result).to be_nil
end
end
end

context 'when debugging is inactive' do
before do
allow(logger).to receive(:debug)
end

it 'does not call the logger' do
Facter.debugonce('message')

expect(logger).not_to have_received(:debug)
end
end

describe '#warn' do
before do
allow(logger).to receive(:warn)
Expand Down
2 changes: 1 addition & 1 deletion spec/facter/resolvers/windows/netkvm_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Facter::Resolvers::NetKVM do
describe '#resolve' do
let(:reg) { instance_double('Win32::Registry::HKEY_LOCAL_MACHINE') }
let(:reg) { instance_spy('Win32::Registry') }

before do
allow(reg).to receive(:keys).and_return(reg_value)
Expand Down
12 changes: 0 additions & 12 deletions spec/framework/logging/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@
end
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 empty' do
log.debug('')

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

shared_examples 'writes debug message' do
it 'calls debug on multi_logger' do
log.debug('debug_message')
Expand Down
6 changes: 5 additions & 1 deletion spec/mocks/win32_mock.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

module Win32
module Registry
class Registry
def keys; end

def close; end

class HKEY_LOCAL_MACHINE
def self.open(*); end

Expand Down
2 changes: 1 addition & 1 deletion spec/mocks/win32_registry_error_mock.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Win32
module Registry
class Registry
class Error < RuntimeError
end
end
Expand Down

0 comments on commit 737ff92

Please sign in to comment.