Test for piped config, and fix to make it work

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-02-01 13:00:46 -05:00
parent cbd0dacd39
commit cb3e0aca60
2 changed files with 30 additions and 15 deletions

View file

@ -143,6 +143,7 @@ module Inspec
# Regardless of our situation, end up with a readable IO object
def resolve_cfg_io(cli_opts, cfg_io)
raise(ArgumentError, 'Inspec::Config must use an IO to read from') if cfg_io && !cfg_io.respond_to?(:read)
cfg_io ||= check_for_piped_config(cli_opts)
return cfg_io if cfg_io
path = determine_cfg_path(cli_opts)
@ -151,13 +152,20 @@ module Inspec
cfg_io || StringIO.new('{ "version": "1.1" }')
end
def check_for_piped_config(cli_opts)
cli_opt = cli_opts[:config] || cli_opts[:json_config]
return nil unless cli_opt
return nil unless cli_opt == '-'
# This warning is here so that if a user invokes inspec with --config=-,
# they will have an explanation for why it appears to hang.
Inspec::Log.warn 'Reading JSON config from standard input' if STDIN.tty?
STDIN
end
def determine_cfg_path(cli_opts)
path = cli_opts[:config] || cli_opts[:json_config] # TODO: deprecate --json-config see #3661
if path == '-'
Inspec::Log.warn 'Reading JSON config from standard input' if STDIN.tty?
path = STDIN
elsif path.nil?
if path.nil?
default_path = File.join(Inspec.config_dir, 'config.json')
path = default_path if File.exist?(default_path)
elsif !File.exist?(path)

View file

@ -580,17 +580,24 @@ Test Summary: \e[38;5;41m2 successful\e[0m, 0 failures, 0 skipped\n"
end
end
# TODO - Use bash redirection to populate STDIN
# unless windows?
# describe 'when using the --config option to read from STDIN' do
# let(:cli_args) { '--config ' + File.join(config_dir_path, 'json-config', 'good.json') }
# it 'should see the custom target ID value' do
# stderr.must_be_empty
# seen_target_id.must_equal 'from-config-file'
# # TODO
# end
# end
# end
unless windows?
describe 'when using the --config option to read from STDIN' do
let(:json_path) { File.join(config_dir_path, 'json-config', 'good.json') }
let(:cli_args) { '--config -' }
let(:run_result) do
prefix = 'cat ' + json_path + ' | '
simple_profile = File.join(profile_path, 'simple-metadata')
run_inspec_process(
'exec ' + simple_profile + ' ' + cli_args + ' ',
{ prefix: prefix, json: true, env: env }
)
end
it 'should see the custom target ID value' do
stderr.must_be_empty
seen_target_id.must_equal 'from-config-file'
end
end
end
describe 'when reading from the default location' do
# Should read from File.join(config_dir_path, 'fakehome-2', '.inspec', 'config.json')