Update command resource to check for mock backend. (#2353)

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2017-12-05 08:21:31 -05:00 committed by Dominik Richter
parent 0248d80557
commit 578577f79a
2 changed files with 13 additions and 3 deletions

View file

@ -45,9 +45,9 @@ module Inspec::Resources
result.exit_status.to_i
end
def exist?
def exist? # rubocop:disable Metrics/AbcSize
# silent for mock resources
return false if inspec.os.name.nil?
return false if inspec.os.name.nil? || inspec.os.name == 'mock'
if inspec.os.linux?
res = inspec.backend.run_command("bash -c 'type \"#{@command}\"'")
@ -56,7 +56,7 @@ module Inspec::Resources
elsif inspec.os.unix?
res = inspec.backend.run_command("type \"#{@command}\"")
else
warn "`command(#{@command}).exist?` is not suported on your OS: #{inspec.os[:name]}"
warn "`command(#{@command}).exist?` is not supported on your OS: #{inspec.os[:name]}"
return false
end
res.exit_status.to_i == 0

View file

@ -22,6 +22,16 @@ describe Inspec::Resources::Cmd do
resource('env').exit_status.must_equal 0
end
it 'exist? returns false on nil os name' do
Inspec::Resources::OSResource.any_instance.stubs(:name).returns(nil)
resource('test').exist?.must_equal false
end
it 'exist? returns false on mock os name' do
Inspec::Resources::OSResource.any_instance.stubs(:name).returns('mock')
resource('test').exist?.must_equal false
end
it 'raises when called with nil as a command' do
proc { resource(nil).result }.must_raise StandardError
end