Skip to content

Commit

Permalink
(FACT-2315) added warn method
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-miclea authored and Filipovici-Andrei committed Sep 14, 2020
1 parent fe98e99 commit 85daa91
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
22 changes: 15 additions & 7 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def on_message(&block)
Facter::Log.on_message(&block)
end

# Check whether debuging is enabled
# Check whether debugging is enabled
#
# @return [bool]
#
Expand All @@ -108,7 +108,7 @@ def debugging(debug_bool)
# call {Facter::Util::Fact#value `value`} on it to retrieve the actual
# value.
#
# @param name [String] the name of the fact
# @param user_query [String] the name of the fact
#
# @return [Facter::Util::Fact, nil] The fact object, or nil if no fact
# is found.
Expand Down Expand Up @@ -199,7 +199,7 @@ def trace?
end

# Enable or disable trace
# @param debug_bool [bool] Set trace on debug state
# @param bool [bool] Set trace on debug state
#
# @return [type] [description]
#
Expand All @@ -210,7 +210,7 @@ def trace(bool)

# Gets the value for a fact. Returns `nil` if no such fact exists.
#
# @param name [String] the fact name
# @param user_query [String] the fact name
# @return [String] the value of the fact, or nil if no fact is found
#
# @api public
Expand Down Expand Up @@ -262,6 +262,15 @@ def log_exception(exception, message = :default)
logger.error(arr.flatten.join("\n"))
end

# Logs the message parameter as a warning.
#
# @param message [Object] the warning object to be displayed
#
# @api public
def warn(message)
logger.warn(message)
end

private

def logger
Expand Down Expand Up @@ -303,10 +312,9 @@ def resolve_fact(user_query)
# Returns exit status when user query contains facts that do
# not exist
#
# @param dirs [Array] Arguments sent to CLI
# @param dirs [Array] List of resolved facts
# @param resolved_facts [Array] List of resolved facts
#
# @return [Integer, nil] Will return status 1 if user query contains
# @return [1/nil] Will return status 1 if user query contains
# facts that are not found or resolved, otherwise it will return nil
#
# @api private
Expand Down
6 changes: 2 additions & 4 deletions lib/facter/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ def info(msg)
end

def warn(msg)
if msg.nil? || msg.empty?
empty_message_error(msg)
elsif @@message_callback
if @@message_callback
@@message_callback.call(:warn, msg)
else
msg = colorize(msg, YELLOW) if Options[:color]
@@logger.warn(@class_name + ' - ' + msg)
@@logger.warn(@class_name + ' - ' + msg.to_s)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/facter/facter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,18 @@ def mock_collection(method, os_name = nil, error = nil)
end
end
end

describe '#warn' do
let(:message) { 'Some error message' }

before do
allow(logger).to receive(:warn).with(message)
end

it 'calls logger with an unmodified warning message object' do
Facter.warn(message)

expect(logger).to have_received(:warn).with(message)
end
end
end
24 changes: 24 additions & 0 deletions spec/framework/logging/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@
expect(multi_logger_double).to have_received(:warn).with('Class - warn_message')
end

it 'writes empty message when message is nil' do
log.warn(nil)

expect(multi_logger_double).to have_received(:warn).with('Class - ')
end

it 'writes empty message when message is empty string' do
log.warn('')

expect(multi_logger_double).to have_received(:warn).with('Class - ')
end

it 'writes warn message when message is a hash' do
log.warn({ warn: 'message' })

expect(multi_logger_double).to have_received(:warn).with('Class - {:warn=>"message"}')
end

it 'writes warn message when message is an array' do
log.warn([1, 2, 3])

expect(multi_logger_double).to have_received(:warn).with('Class - [1, 2, 3]')
end

context 'when non Windows OS' do
before do
allow(OsDetector.instance).to receive(:identifier).and_return(:macosx)
Expand Down

0 comments on commit 85daa91

Please sign in to comment.