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

Commit

Permalink
(FACT-2525) Fix for tests/options/color.rb (#512)
Browse files Browse the repository at this point in the history
* (FACT-2525) Add color to option store.

* (FACT-2525) Add color to log message if --color flag is enabled.

* (FACT-2525) Fix color in bash.

* (FACT-2525) Add rspec tests.

* (FACT-2525) Add dhcp client.

* (FACT-2525) Fix test message.
  • Loading branch information
Bogdan Irimie authored May 14, 2020
1 parent 0962fc1 commit a86d0d8
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/acceptance_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ jobs:
sudo chmod a-w /home/runner /usr/share &&
sudo chmod -R a-w /usr/share/rust /home/runner/.config /home/linuxbrew
- name: Instal dhclient
if: runner.os == 'Linux'
run: |
sudo apt install dhcpcd5
sudo dhclient
- name: Run acceptance tests
run: sudo -E "PATH=$PATH" ruby facter_4/.github/actions/presuite.rb
3 changes: 2 additions & 1 deletion lib/framework/core/options/option_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class OptionStore
@user_query = []
@block_list = {}
@fact_groups = {}
@color = false

class << self
attr_reader :debug, :verbose, :log_level, :show_legacy, :trace, :ruby,
Expand All @@ -31,7 +32,7 @@ class << self
attr_accessor :config, :user_query, :strict, :json, :haml, :external_facts,
:cache, :yaml, :puppet, :ttls, :block, :cli, :config_file_custom_dir,
:config_file_external_dir, :default_external_dir, :fact_groups,
:block_list
:block_list, :color

attr_writer :external_dir

Expand Down
20 changes: 17 additions & 3 deletions lib/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

module Facter
RED = 31
GREEN = 32
YELLOW = 33
CYAN = 36

DEFAULT_LOG_LEVEL = :warn

class Log
Expand Down Expand Up @@ -69,30 +73,36 @@ def debug(msg)

if msg.nil? || msg.empty?
invoker = caller(1..1).first.slice(/.*:\d+/)
self.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
empty_message_error(msg, invoker)
elsif @@message_callback
@@message_callback.call(:debug, msg)
else
msg = colorize(msg, CYAN) if Options[:color]
@@logger.debug(@class_name + ' - ' + msg)
end
end

def info(msg)
msg = colorize(msg, GREEN) if Options[:color]
@@logger.info(@class_name + ' - ' + msg)
end

def warn(msg)
msg = colorize(msg, YELLOW) if Options[:color]

@@logger.warn(@class_name + ' - ' + msg)
end

def error(msg, colorize = false)
@@has_errors = true
msg = colorize(msg, RED) if colorize && !OsDetector.instance.identifier.eql?(:windows)
msg = colorize(msg, RED) if colorize || Options[:color]
@@logger.error(@class_name + ' - ' + msg)
end

def colorize(msg, color)
"\e[#{color}m#{msg}\e[0m"
return msg if OsDetector.instance.identifier.eql?(:windows)

"\e[0;#{color}m#{msg}\e[0m"
end

private
Expand All @@ -102,5 +112,9 @@ def debugging_active?

Facter.debugging?
end

def empty_message_error(msg, invoker)
self.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
end
end
end
3 changes: 2 additions & 1 deletion spec/framework/core/options/option_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
user_query: [],
verbose: false,
config: nil,
cache: true
cache: true,
color: false
)
end
end
Expand Down
111 changes: 110 additions & 1 deletion spec/framework/logging/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

before do
Facter::Log.class_variable_set(:@@logger, multi_logger_double)
Facter::Options[:color] = false
end

after do
Expand Down Expand Up @@ -80,6 +81,42 @@
expect(Facter).not_to have_received(:debugging?)
end
end

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

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print CYAN (36) debug message' do
log.debug('debug_message')

expect(multi_logger_double).to have_received(:debug).with("Class - \e[0;36mdebug_message\e[0m")
end
end
end

context 'when Windows OS' do
before do
allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
end

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print debug message' do
log.debug('debug_message')

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

describe '#info' do
Expand All @@ -88,6 +125,42 @@

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

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

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print Green (32) info message' do
log.info('info_message')

expect(multi_logger_double).to have_received(:info).with("Class - \e[0;32minfo_message\e[0m")
end
end
end

context 'when Windows OS' do
before do
allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
end

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print info message' do
log.info('info_message')

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

describe '#warn' do
Expand All @@ -96,6 +169,42 @@

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

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

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print Yellow (33) info message' do
log.warn('warn_message')

expect(multi_logger_double).to have_received(:warn).with("Class - \e[0;33mwarn_message\e[0m")
end
end
end

context 'when Windows OS' do
before do
allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
end

context 'when --colorize option is enabled' do
before do
Facter::Options[:color] = true
end

it 'print warn message' do
log.warn('warn_message')

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

describe '#error' do
Expand All @@ -104,7 +213,7 @@

log.error('error_message', true)

expect(multi_logger_double).to have_received(:error).with("Class - \e[31merror_message\e[0m")
expect(multi_logger_double).to have_received(:error).with("Class - \e[0;31merror_message\e[0m")
end

it 'writes error message not colorized on Windows' do
Expand Down

0 comments on commit a86d0d8

Please sign in to comment.