Sha256: 5520357d1828715ea26db21307171cb1ba13d38df3d0579c69f7e996af359ab5

Contents?: true

Size: 1.15 KB

Versions: 26

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter

module Inspec::Resources
  class GemPackage < Inspec.resource(1)
    name 'gem'
    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 }
        its('version') { should eq '0.33.0' }
      end
    "

    def initialize(package_name)
      @package_name = package_name
    end

    def info
      return @info if defined?(@info)

      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
      "gem package #{@package_name}"
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
inspec-1.17.0 lib/resources/gem.rb
inspec-1.16.1 lib/resources/gem.rb
inspec-1.16.0 lib/resources/gem.rb
inspec-1.15.0 lib/resources/gem.rb
inspec-1.14.1 lib/resources/gem.rb
inspec-1.14.0 lib/resources/gem.rb
inspec-1.13.0 lib/resources/gem.rb
inspec-1.12.0 lib/resources/gem.rb
inspec-1.11.0 lib/resources/gem.rb
inspec-1.10.0 lib/resources/gem.rb
inspec-1.9.0 lib/resources/gem.rb
inspec-1.8.0 lib/resources/gem.rb
inspec-1.7.2 lib/resources/gem.rb
inspec-1.7.1 lib/resources/gem.rb
inspec-1.7.0 lib/resources/gem.rb
inspec-1.6.0 lib/resources/gem.rb
inspec-1.5.0 lib/resources/gem.rb
inspec-1.4.1 lib/resources/gem.rb
inspec-1.4.0 lib/resources/gem.rb
inspec-1.3.0 lib/resources/gem.rb