Class: Inspec::GemProvider

Inherits:
FileProvider show all
Defined in:
lib/inspec/file_provider.rb

Overview

class DirProvider

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FileProvider

for_path, #relative_provider

Constructor Details

#initialize(gem_info) ⇒ GemProvider

Returns a new instance of GemProvider.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/inspec/file_provider.rb', line 109

def initialize(gem_info)
  # Determine a path to the gem installation directory
  gem_name = gem_info[:gem]
  bootstrap_file = gem_info[:path]
  gem_version = gem_info[:version]
  if bootstrap_file
    # We were given an explicit path - go two directories up
    parts = Pathname(gem_info[:path]).each_filename.to_a
    @gem_lib_root = File.join(parts.slice(0, parts.length - 2))
    @files = Dir[File.join(Shellwords.shellescape(@gem_lib_root), "**", "*")]
  else
    # Look up where the gem is installed, respecting version
    loader = Inspec::Plugin::V2::Loader.new
    gem_path = loader.find_gem_directory(gem_name, gem_version)
    if gem_path && File.exist?(gem_path)
      gem = DirProvider.new(gem_path)
      @files = gem.files
      gem
    else
      raise Inspec::Exceptions::GemDependencyNotFound, "Dependency does not exist #{gem_name}"
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



108
109
110
# File 'lib/inspec/file_provider.rb', line 108

def files
  @files
end

Instance Method Details

#binread(file) ⇒ Object



140
141
142
143
144
145
# File 'lib/inspec/file_provider.rb', line 140

def binread(file)
  return nil unless files.include?(file)
  return nil unless File.file?(file)

  File.binread(file)
end

#read(file) ⇒ Object



133
134
135
136
137
138
# File 'lib/inspec/file_provider.rb', line 133

def read(file)
  return nil unless files.include?(file)
  return nil unless File.file?(file)

  File.read(file)
end