diff --git a/lib/resources/gem.rb b/lib/resources/gem.rb index 281f3036f..e3c169134 100644 --- a/lib/resources/gem.rb +++ b/lib/resources/gem.rb @@ -39,16 +39,16 @@ module Inspec::Resources return @info if defined?(@info) cmd = inspec.command("#{@gem_binary} list --local -a -q \^#{@package_name}\$") - @info = { - installed: cmd.exit_status.zero?, - type: 'gem', - } - return @info unless @info[:installed] + return {} unless cmd.exit_status.zero? # extract package name and version # parses data like winrm (1.3.4, 1.3.3) params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp) - return {} if params.nil? + @info = { + installed: !params.nil?, + type: 'gem', + } + return @info unless @info[:installed] versions = params[2].split(',') @info[:name] = params[1] diff --git a/test/helper.rb b/test/helper.rb index 9fa9d627a..b200e1a33 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -175,6 +175,7 @@ class MockLoader 'rpm -qia curl' => cmd.call('rpm-qia-curl'), 'pacman -Qi curl' => cmd.call('pacman-qi-curl'), 'brew info --json=v1 curl' => cmd.call('brew-info--json-v1-curl'), + 'gem list --local -a -q ^not-installed$' => cmd.call('gem-list-local-a-q-not-installed'), 'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'), '/opt/ruby-2.3.1/embedded/bin/gem list --local -a -q ^pry$' => cmd.call('gem-list-local-a-q-pry'), '/opt/chef/embedded/bin/gem list --local -a -q ^chef-sugar$' => cmd.call('gem-list-local-a-q-chef-sugar'), diff --git a/test/unit/mock/cmd/gem-list-local-a-q-not-installed b/test/unit/mock/cmd/gem-list-local-a-q-not-installed new file mode 100644 index 000000000..e69de29bb diff --git a/test/unit/resources/gem_test.rb b/test/unit/resources/gem_test.rb index 32f5f58bf..f7b3124ed 100644 --- a/test/unit/resources/gem_test.rb +++ b/test/unit/resources/gem_test.rb @@ -7,6 +7,11 @@ require 'helper' require 'inspec/resource' describe 'Inspec::Resources::Gem' do + it 'verify gem is not installed' do + resource = load_resource('gem', 'not-installed') + _(resource.installed?).must_equal false + end + it 'verify gem package detail parsing' do resource = load_resource('gem', 'rubocop') pkg = {