Use IO globals instead of IO constants.

Helps testing and lots of other things if we only use $stdout/$stderr.

STDOUT/STDERR should only be used to restore the globals.

Signed-off-by: Ryan Davis <zenspider@chef.io>
This commit is contained in:
Ryan Davis 2019-05-17 17:43:58 -07:00
parent 40d74cb5be
commit 9b7c292d4c
28 changed files with 49 additions and 48 deletions

View file

@ -259,9 +259,9 @@ module Inspec
loc = if o['log_location']
o['log_location']
elsif suppress_log_output?(o)
STDERR
$stderr
else
STDOUT
$stdout
end
Inspec::Log.init(loc)

View file

@ -22,7 +22,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
desc: 'Set the log level: info (default), debug, warn, error'
class_option :log_location, type: :string,
desc: 'Location to send diagnostic log messages to. (default: STDOUT or Inspec::Log.error)'
desc: 'Location to send diagnostic log messages to. (default: $stdout or Inspec::Log.error)'
class_option :diagnose, type: :boolean,
desc: 'Show diagnostics (versions, configurations)'
@ -54,7 +54,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
def json(target)
o = config
diagnose(o)
o['log_location'] = STDERR
o['log_location'] = $stderr
configure_logger(o)
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
@ -142,7 +142,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
def vendor(path = nil)
o = config
configure_logger(o)
o[:logger] = Logger.new(STDOUT)
o[:logger] = Logger.new($stdout)
o[:logger].level = get_log_level(o[:log_level])
vendor_deps(path, o)
@ -164,7 +164,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
o = config
diagnose(o)
o[:logger] = Logger.new(STDOUT)
o[:logger] = Logger.new($stdout)
o[:logger].level = get_log_level(o[:log_level])
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
@ -313,7 +313,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
diagnose(o)
o[:debug_shell] = true
log_device = suppress_log_output?(o) ? nil : STDOUT
log_device = suppress_log_output?(o) ? nil : $stdout
o[:logger] = Logger.new(log_device)
o[:logger].level = get_log_level(o[:log_level])

View file

@ -44,6 +44,6 @@ module Inspec::Plugin::V2::PluginType
desc: 'Set the log level: info (default), debug, warn, error'
class_option :log_location, type: :string,
desc: 'Location to send diagnostic log messages to. (default: STDOUT or Inspec::Log.error)'
desc: 'Location to send diagnostic log messages to. (default: $stdout or Inspec::Log.error)'
end
end

View file

@ -47,7 +47,7 @@ module Inspec::Reporters
File.write(config['file'], output)
elsif config['stdout'] == true
print output
STDOUT.flush
$stdout.flush
end
end

View file

@ -62,7 +62,7 @@ module InspecPlugins
exit 1
end
rescue InspecPlugins::Compliance::ServerConfigurationMissing
STDERR.puts "\nServer configuration information is missing. Please login using `inspec compliance login`"
$stderr.puts "\nServer configuration information is missing. Please login using `inspec compliance login`"
exit 1
end

View file

@ -44,7 +44,7 @@ describe 'inspec exec automate' do
end
end
describe 'when outputting to STDOUT' do
describe 'when outputting to $stdout' do
let(:config_data) do
data = <<~EOF
{

View file

@ -7,7 +7,7 @@ describe 'Deprecation Facility Behavior' do
let(:profile) { File.join(profile_path, 'deprecation', profile_name) }
let(:invocation) { "exec #{profile} #{control_flag}" }
# Running in JSON mode has the side-effect of sending log messages to STDERR
# Running in JSON mode has the side-effect of sending log messages to $stderr
let(:run_result) { run_inspec_process(invocation, json: true) }
# Expect one control, 3 results
@ -145,7 +145,7 @@ describe 'Deprecation Facility Behavior' do
json_result[1]['status'].must_equal 'passed'
json_result[2]['status'].must_equal 'passed'
# JSON mode will send debug messages to STDERR
# JSON mode will send debug messages to $stderr
# [2019-01-15T23:41:41-05:00] DEBUG: DEPRECATION: This should be ignored (used at test/unit/mock/profiles/deprecation/typical/controls/typical.rb:79)
deprecation_line = run_result.stderr.split("\n").detect { |line| line.include?('This should be ignored') }
deprecation_line.wont_be_nil

View file

@ -1,8 +1,8 @@
STDERR.puts "-----------------------------------"
STDERR.puts " TEST ENVIRONMENT "
STDERR.puts "-----------------------------------"
STDERR.puts " Docker: #{!ENV['DOCKER'].nil?}"
STDERR.puts " OS name: #{os[:name] || 'unknown' }"
STDERR.puts "OS release: #{os[:release] || 'unknown'}"
STDERR.puts " OS family: #{os[:family] || 'unknown'}"
STDERR.puts "-----------------------------------"
$stderr.puts "-----------------------------------"
$stderr.puts " TEST ENVIRONMENT "
$stderr.puts "-----------------------------------"
$stderr.puts " Docker: #{!ENV['DOCKER'].nil?}"
$stderr.puts " OS name: #{os[:name] || 'unknown' }"
$stderr.puts "OS release: #{os[:release] || 'unknown'}"
$stderr.puts " OS family: #{os[:family] || 'unknown'}"
$stderr.puts "-----------------------------------"

View file

@ -1,11 +1,11 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end
supported = %w{ubuntu centos amazon fedora}
unless supported.include?(os[:name])
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on #{supported.join(', ')}.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on #{supported.join(', ')}.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
if os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Linux.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Linux.\033[0m"
return
end

View file

@ -1,10 +1,10 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end
if !os.linux?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,10 +1,10 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end
if !os.linux?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,10 +1,10 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end
if !os.linux?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on linux.\033[0m"
return
end

View file

@ -1,5 +1,5 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,6 +1,6 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,5 +1,5 @@
if ENV['DOCKER']
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__.split("/").last} because we are running in docker\033[0m"
return
end

View file

@ -1,6 +1,6 @@
unless os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Windows.\033[0m"
return
end

View file

@ -1,6 +1,6 @@
if os.windows?
STDERR.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Linux.\033[0m"
$stderr.puts "\033[1;33mTODO: Not running #{__FILE__} because we are not on Linux.\033[0m"
return
end

View file

@ -26,7 +26,7 @@ EOF
let(:options) do
o = {
'log_location' => STDERR,
'log_location' => $stderr,
'log_level' => 'debug',
'reporter' => {
'json' => {
@ -36,6 +36,7 @@ EOF
}
Thor::CoreExt::HashWithIndifferentAccess.new(o)
end
let(:format) do
device = options[:logger].instance_variable_get(:"@logdev")
device.instance_variable_get(:"@dev")
@ -43,14 +44,14 @@ EOF
it 'sets to stderr for log_location' do
cli.send(:configure_logger, options)
format.must_equal STDERR
format.must_equal $stderr
end
it 'sets to stderr for json' do
options.delete('log_location')
options.delete('log_level')
cli.send(:configure_logger, options)
format.must_equal STDERR
format.must_equal $stderr
end
it 'sets defaults to stdout for everything else' do
@ -59,7 +60,7 @@ EOF
options.delete('reporter')
cli.send(:configure_logger, options)
format.must_equal STDOUT
format.must_equal $stdout
end
end