From 72af4a96f17f14ad11a17560c1658a065a1a419d Mon Sep 17 00:00:00 2001 From: Jared Quick Date: Thu, 7 Dec 2017 07:19:36 -0500 Subject: [PATCH] Update default cli options to be per command. (#2378) Signed-off-by: Jared Quick --- lib/inspec/base_cli.rb | 20 ++++++++++++-------- lib/inspec/cli.rb | 4 ++-- test/unit/base_cli_test.rb | 21 +++++++++++++++------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/inspec/base_cli.rb b/lib/inspec/base_cli.rb index 36b06a913..ddb81e48a 100644 --- a/lib/inspec/base_cli.rb +++ b/lib/inspec/base_cli.rb @@ -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) diff --git a/lib/inspec/cli.rb b/lib/inspec/cli.rb index 345b0d904..2cb240a32 100644 --- a/lib/inspec/cli.rb +++ b/lib/inspec/cli.rb @@ -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) diff --git a/test/unit/base_cli_test.rb b/test/unit/base_cli_test.rb index dcccc7096..18f791fd8 100644 --- a/test/unit/base_cli_test.rb +++ b/test/unit/base_cli_test.rb @@ -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