inspec/lib/resources/gem.rb

49 lines
1 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
2015-10-26 03:04:18 +00:00
class GemPackage < Inspec.resource(1)
name 'gem'
2015-11-27 13:02:38 +00:00
desc 'Use the gem InSpec audit resource to test if a global gem package is installed.'
example "
describe gem('rubocop') do
it { should be_installed }
end
"
def initialize(package_name)
@package_name = package_name
end
def info
2015-10-01 13:25:08 +00:00
return @info if defined?(@info)
2015-10-26 03:04:18 +00:00
cmd = inspec.command("gem list --local -a -q \^#{@package_name}\$")
@info = {
installed: cmd.exit_status == 0,
type: 'gem',
}
return @info unless @info[:installed]
# extract package name and version
# parses data like winrm (1.3.4, 1.3.3)
params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp)
versions = params[2].split(',')
@info[:name] = params[1]
@info[:version] = versions[0]
@info
end
def installed?
info[:installed] == true
end
def version
info[:version]
end
def to_s
2015-10-01 20:45:27 +00:00
"gem package #{@package_name}"
end
end