Skip to content

Commit

Permalink
(FACT-2315) added warn method
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipovici-Andrei committed Sep 15, 2020
1 parent a14eab4 commit 04235fe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
25 changes: 18 additions & 7 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 @@ -127,7 +127,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 @@ -218,7 +218,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 @@ -229,7 +229,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 @@ -290,6 +290,18 @@ def list
to_hash.keys.sort
end

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

private

def logger
Expand Down Expand Up @@ -331,10 +343,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
4 changes: 1 addition & 3 deletions lib/facter/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ 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]
Expand Down
44 changes: 44 additions & 0 deletions spec/facter/facter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,48 @@ def mock_collection(method, os_name = nil, error = nil)
expect(result).to eq(%w[timezone up_time virtual])
end
end

describe '#warn' do
before do
allow(logger).to receive(:warn)
end

it 'calls logger' do
message = 'Some error message'

Facter.warn(message)

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

it 'when message is nil' do
Facter.warn(nil)

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

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

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

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

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

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

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

it 'returns nil' do
result = Facter.warn('message')

expect(result).to be_nil
end
end
end

0 comments on commit 04235fe

Please sign in to comment.