Fix installed? check for gem resource

The gem resource used to determine if a gem is installed based on the exit
status of the `gem` command, however that command will return zero
if the package was found or not. This patch checks to ensure that the
`gem list` command actually includes the gem name or is empty to
determine if the gem is in fact installed.

If the gem command returns something other than a `0` exit code, then
it'll skip the resource.

Signed-off-by: Keith Walters <keith.walters@cattywamp.us>
This commit is contained in:
Keith Walters 2017-04-04 15:35:40 -04:00
parent 9e71c94b83
commit 215ef38ee9
4 changed files with 12 additions and 6 deletions

View file

@ -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]

View file

@ -174,6 +174,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'),

View file

@ -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 = {