Infer --sudo when --sudo-password is used (#3313)

This does the following:
  - Adds `--sudo` if using `--sudo-password`
  - Warns the user if using `--sudo-password` without `--sudo`
  - Adds unit tests for `Inspec::BaseCLI#opts`

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
Jerry Aldrich 2018-08-23 10:47:26 -07:00 committed by Jared Quick
parent 741ad8a2fb
commit 7098631d3e
2 changed files with 36 additions and 0 deletions

View file

@ -277,6 +277,12 @@ module Inspec
raise ArgumentError, "Please provide a value for --#{v}. For example: --#{v}=hello."
end
# Infer `--sudo` if using `--sudo-password` without `--sudo`
if o[:sudo_password] && !o[:sudo]
o[:sudo] = true
warn 'WARN: `--sudo-password` used without `--sudo`. Adding `--sudo`.'
end
# check for compliance settings
Compliance::API.login(o['compliance']) if o['compliance']

View file

@ -7,6 +7,36 @@ require 'thor'
describe 'BaseCLI' do
let(:cli) { Inspec::BaseCLI.new }
describe 'opts' do
it 'raises if `--password/--sudo-password` are used without value' do
default_options = { mock: { sudo_password: -1 } }
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
e = proc { cli.send(:opts, :mock) }.must_raise(ArgumentError)
e.message.must_match /Please provide a value for --sudo-password/
end
it 'assumes `--sudo` if `--sudo-password` is used without it' do
default_options = { mock: { sudo_password: 'p@ssw0rd' } }
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
opts = {}
out, err = capture_io do
cli.send(:opts, :mock)[:sudo].must_equal true
end
err.must_match(/WARN: `--sudo-password` used without `--sudo`/)
end
it 'calls `Compliance::API.login` if `opts[:compliance] is passed`' do
default_options = { mock: { compliance: 'mock' } }
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
Compliance::API.expects(:login).with('mock')
cli.send(:opts, :mock)
end
end
describe 'merge_options' do
let(:default_options) do
{ exec: { 'reporter' => ['json'], 'backend_cache' => false }}