From 578577f79aa816dc6b7fd98e6d1b8026477c5426 Mon Sep 17 00:00:00 2001 From: Jared Quick Date: Tue, 5 Dec 2017 08:21:31 -0500 Subject: [PATCH] Update command resource to check for mock backend. (#2353) Signed-off-by: Jared Quick --- lib/resources/command.rb | 6 +++--- test/unit/resources/command_test.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/resources/command.rb b/lib/resources/command.rb index cecb4acc5..482f6cd9a 100644 --- a/lib/resources/command.rb +++ b/lib/resources/command.rb @@ -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 diff --git a/test/unit/resources/command_test.rb b/test/unit/resources/command_test.rb index 3299a4639..19470e864 100644 --- a/test/unit/resources/command_test.rb +++ b/test/unit/resources/command_test.rb @@ -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