Update default cli options to be per command. (#2378)

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2017-12-07 07:19:36 -05:00 committed by Dominik Richter
parent 628d778b05
commit 72af4a96f1
3 changed files with 29 additions and 16 deletions

View file

@ -73,9 +73,11 @@ module Inspec
def self.default_options
{
color: true,
create_lockfile: true,
backend_cache: false,
exec: {
'color' => true,
'create_lockfile' => true,
'backend_cache' => false,
},
}
end
@ -109,8 +111,8 @@ module Inspec
puts
end
def opts
o = merged_opts
def opts(type = nil)
o = merged_opts(type)
# Due to limitations in Thor it is not possible to set an argument to be
# both optional and its value to be mandatory. E.g. the user supplying
@ -126,9 +128,11 @@ module Inspec
o
end
def merged_opts
# start with default options
opts = BaseCLI.default_options
def merged_opts(type = nil)
opts = {}
# start with default options if we have any
opts = BaseCLI.default_options[type] unless type.nil?
# merge in any options from json-config
opts.merge!(options_json)

View file

@ -152,8 +152,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
exec_options
def exec(*targets)
diagnose
configure_logger(opts)
o = opts.dup
o = opts(:exec).dup
configure_logger(o)
# run tests
run_tests(targets, o)

View file

@ -9,28 +9,28 @@ describe 'BaseCLI' do
describe 'merge_options' do
it 'cli defaults populate correctly' do
default_options = { format: 'json', backend_cache: false }
default_options = { exec: { format: 'json', backend_cache: false }}
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
opts = cli.send(:merged_opts)
opts = cli.send(:merged_opts, :exec)
expected = { 'format' => 'json', 'backend_cache' => false }
opts.must_equal expected
end
it 'json-config options override cli defaults' do
default_options = { format: 'json', backend_cache: false }
default_options = { exec: { format: 'json', backend_cache: false }}
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
parsed_json = { 'backend_cache' => true }
cli.expects(:options_json).returns(parsed_json)
opts = cli.send(:merged_opts)
opts = cli.send(:merged_opts, :exec)
expected = { 'format' => 'json', 'backend_cache' => true }
opts.must_equal expected
end
it 'cli options override json-config and default' do
default_options = { format: 'json', backend_cache: false }
default_options = { exec: { format: 'json', backend_cache: false }}
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
parsed_json = { 'backend_cache' => false }
@ -39,9 +39,18 @@ describe 'BaseCLI' do
cli_options = { 'backend_cache' => true }
cli.instance_variable_set(:@options, cli_options)
opts = cli.send(:merged_opts)
opts = cli.send(:merged_opts, :exec)
expected = { 'format' => 'json', 'backend_cache' => true }
opts.must_equal expected
end
it 'make sure shell does not get exec defaults' do
default_options = { exec: { format: 'json', backend_cache: false }}
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
opts = cli.send(:merged_opts)
expected = {}
opts.must_equal expected
end
end
end