Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve merge issue with json-config vs thor defaults #2377

Merged
merged 2 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/inspec/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def inspect
#
# @param [Hash] config for the transport backend
# @return [TransportBackend] enriched transport instance
def self.create(config)
def self.create(config) # rubocop:disable Metrics/AbcSize
conf = Train.target_config(config)
name = Train.validate_backend(conf)
transport = Train.create(name, conf)
Expand All @@ -56,12 +56,15 @@ def self.create(config)
# Set caching settings. We always want to enable caching for
# the Mock transport for testing.
if config[:backend_cache] || config[:backend] == :mock
Inspec::Log.debug 'Option backend_cache is enabled'
connection.enable_cache(:file)
connection.enable_cache(:command)
elsif config[:debug_shell]
Inspec::Log.debug 'Option backend_cache is disabled'
connection.disable_cache(:file)
connection.disable_cache(:command)
else
Inspec::Log.debug 'Option backend_cache is disabled'
connection.disable_cache(:command)
end

Expand Down
25 changes: 20 additions & 5 deletions lib/inspec/base_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,26 @@ def self.exec_options
desc: 'A list of controls to run. Ignore all other tests.'
option :format, type: :string,
desc: 'Which formatter to use: cli, progress, documentation, json, json-min, junit'
option :color, type: :boolean, default: true,
option :color, type: :boolean,
desc: 'Use colors in output.'
option :attrs, type: :array,
desc: 'Load attributes file (experimental)'
option :cache, type: :string,
desc: 'Use the given path for caching dependencies. (default: ~/.inspec/cache)'
option :create_lockfile, type: :boolean, default: true,
option :create_lockfile, type: :boolean,
desc: 'Write out a lockfile based on this execution (unless one already exists)'
option :backend_cache, type: :boolean, default: false,
option :backend_cache, type: :boolean,
desc: 'Allow caching for backend command output.'
end

def self.default_options
{
color: true,
create_lockfile: true,
backend_cache: false,
}
end

private

# helper method to run tests
Expand Down Expand Up @@ -119,8 +127,15 @@ def opts
end

def merged_opts
# argv overrides json
Thor::CoreExt::HashWithIndifferentAccess.new(options_json.merge(options))
# start with default options
opts = BaseCLI.default_options

# merge in any options from json-config
opts.merge!(options_json)

# merge in any options defined via thor
opts.merge!(options)
Thor::CoreExt::HashWithIndifferentAccess.new(opts)
end

def options_json
Expand Down
47 changes: 47 additions & 0 deletions test/unit/base_cli_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# encoding: utf-8
# copyright: 2017, Chef Software Inc.

require 'helper'
require 'thor'

describe 'BaseCLI' do
let(:cli) { Inspec::BaseCLI.new }

describe 'merge_options' do
it 'cli defaults populate correctly' do
default_options = { format: 'json', backend_cache: false }
Inspec::BaseCLI.stubs(:default_options).returns(default_options)

opts = cli.send(:merged_opts)
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 }
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)
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 }
Inspec::BaseCLI.stubs(:default_options).returns(default_options)

parsed_json = { 'backend_cache' => false }
cli.expects(:options_json).returns(parsed_json)

cli_options = { 'backend_cache' => true }
cli.instance_variable_set(:@options, cli_options)

opts = cli.send(:merged_opts)
expected = { 'format' => 'json', 'backend_cache' => true }
opts.must_equal expected
end
end
end