Sha256: 0c70d6db758b02e22d936ceb828f2c287cfb4361c81f8ae3dba829f2dedcacb6

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

# Usage:
# describe cpan('DBD::Pg') do
#   it { should be_installed }
# end
#

module Inspec::Resources
  class CpanPackage < Inspec.resource(1)
    name 'cpan'
    supports platform: 'unix'
    desc 'Use the `cpan` InSpec audit resource to test Perl modules that are installed by system packages or the CPAN installer.'
    example <<~EXAMPLE
      describe cpan('DBD::Pg') do
        it { should be_installed }
      end
    EXAMPLE

    def initialize(package_name, perl_lib_path = nil)
      @package_name = package_name
      @perl_lib_path = perl_lib_path
      @perl_cmd = 'perl'

      # this resource is not supported on Windows
      return skip_resource 'The `cpan` resource is not supported on your OS yet.' if inspec.os.windows?
      return skip_resource 'perl not found' unless inspec.command(@perl_cmd).exist?
    end

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

      @info = {}
      @info[:type] = 'cpan'
      @info[:name] = @package_name
      # set PERL5LIB environment variable if a custom lib path is given
      lib_path = @perl_lib_path.nil? ? '' : "PERL5LIB=#{@perl_lib_path} "
      cmd = inspec.command("#{lib_path+@perl_cmd} -le 'eval \"require $ARGV[0]\" and print $ARGV[0]->VERSION or exit 1' #{@package_name}")
      @info[:installed] = cmd.exit_status.zero?
      return @info unless cmd.exit_status.zero?

      @info[:version] = cmd.stdout.strip
      @info
    end

    def installed?
      info[:installed] == true
    end

    def version
      info[:version]
    end

    def to_s
      "Perl Module #{@package_name}"
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
inspec-core-4.3.2 lib/resources/cpan.rb
inspec-4.3.2 lib/resources/cpan.rb
inspec-core-4.2.0.preview lib/resources/cpan.rb
inspec-4.2.0.preview lib/resources/cpan.rb
inspec-core-4.1.4.preview lib/resources/cpan.rb
inspec-4.1.4.preview lib/resources/cpan.rb
inspec-core-3.9.3 lib/resources/cpan.rb
inspec-3.9.3 lib/resources/cpan.rb
inspec-core-3.9.0 lib/resources/cpan.rb
inspec-3.9.0 lib/resources/cpan.rb
inspec-core-3.7.11 lib/resources/cpan.rb
inspec-3.7.11 lib/resources/cpan.rb