From d0dc22161e5d6da10581855b9a61fa277f291801 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Wed, 5 Jun 2019 19:46:59 -0700 Subject: Use $stdout instead of STDOUT for the default logdev. This makes it testable using minitest/rspec IO assertions/expectations. STDOUT/STDERR should only be used to restore $stdout/$stderr if something goes haywire. The globals should be used for everyday use. Also bolstered an IO test to prevent the output from going to the rspec result output. Signed-off-by: Ryan Davis --- lib/mixlib/log.rb | 2 +- lib/mixlib/log/logger.rb | 2 +- spec/mixlib/log_spec.rb | 13 ++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb index 18cdfd3..599ac57 100644 --- a/lib/mixlib/log.rb +++ b/lib/mixlib/log.rb @@ -170,7 +170,7 @@ module Mixlib def logger_for(*opts) if opts.empty? - Mixlib::Log::Logger.new(STDOUT) + Mixlib::Log::Logger.new($stdout) elsif LEVELS.keys.inject(true) { |quacks, level| quacks && opts.first.respond_to?(level) } opts.first else diff --git a/lib/mixlib/log/logger.rb b/lib/mixlib/log/logger.rb index f92d4a2..f227f23 100644 --- a/lib/mixlib/log/logger.rb +++ b/lib/mixlib/log/logger.rb @@ -21,7 +21,7 @@ module Mixlib # # +logdev+:: # The log device. This is a filename (String) or IO object (typically - # +STDOUT+, +STDERR+, or an open file). + # +$stdout+, +$stderr+, or an open file). # +shift_age+:: # Number of old log files to keep, *or* frequency of rotation (+daily+, # +weekly+ or +monthly+). diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb index bbf290b..b58c4e2 100644 --- a/spec/mixlib/log_spec.rb +++ b/spec/mixlib/log_spec.rb @@ -142,9 +142,14 @@ RSpec.describe Mixlib::Log do end it "should pass other method calls directly to logger" do - Logit.level = :debug - expect(Logit).to be_debug - expect { Logit.debug("Gimme some sugar!") }.to_not raise_error + expect do + # this needs to be inside of the block because the level setting + # is causing the init, which grabs $stderr before rspec replaces + # it for output testing. + Logit.level = :debug + expect(Logit).to be_debug + Logit.debug("Gimme some sugar!") + end.to output(/DEBUG: Gimme some sugar!/).to_stdout end it "should pass add method calls directly to logger" do @@ -158,6 +163,7 @@ RSpec.describe Mixlib::Log do it "should default to STDOUT if init is called with no arguments" do logger_mock = Struct.new(:formatter, :level).new + # intentionally STDOUT to avoid unfailable test expect(Logger).to receive(:new).with(STDOUT).and_return(logger_mock) Logit.init end @@ -203,6 +209,7 @@ RSpec.describe Mixlib::Log do end it "should return nil from its logging methods" do + # intentionally STDOUT to avoid unfailable test expect(Logger).to receive(:new).with(STDOUT) { double("a-quiet-logger").as_null_object } Logit.init -- cgit v1.2.1