class Inspec::Requirement
appropriate we delegate to Inspec::Profile directly.
Inspec::Requirement represents a given profile dependency, where
def self.from_lock_entry(entry, config, opts = {})
def self.from_lock_entry(entry, config, opts = {}) req = new(entry[:name], entry[:version_constraints], config, entry[:resolved_source].merge(backend: config[:backend]).merge(opts)) locked_deps = [] Array(entry[:dependencies]).each do |dep_entry| dep_config = config.dup dep_config[:parent_profile] = entry[:name] locked_deps << Inspec::Requirement.from_lock_entry(dep_entry, dep_config, opts) end req.lock_deps(locked_deps) req end
def self.from_metadata(dep, cache, opts)
def self.from_metadata(dep, cache, opts) raise 'Cannot load empty dependency.' if dep.nil? || dep.empty? req_path = opts[:cwd] if dep[:path] req_path = File.expand_path(dep[:path], req_path) end config = { cache: cache, cwd: req_path, } new(dep[:name], dep[:version], config, opts.merge(dep)) end
def dependencies
def dependencies @dependencies ||= profile.metadata.dependencies.map do |r| Inspec::Requirement.from_metadata(r, @cache, cwd: @cwd, backend: @backend) end end
def fetcher
def fetcher @fetcher ||= Inspec::CachedFetcher.new(opts, @cache) end
def initialize(name, version_constraints, config, opts)
def initialize(name, version_constraints, config, opts) @name = name @version_constraints = Array(version_constraints) @cache = config[:cache] @backend = opts[:backend] @opts = opts @cwd = config[:cwd] @parent_profile = config[:parent_profile] end
def lock_deps(dep_array)
def lock_deps(dep_array) @dependencies = dep_array end
def name
the name the user gave this dependency over the profile name.
A dependency can be renamed in inspec.yml/inspec.lock. Prefer
def name @name || profile.name end
def profile
def profile return @profile unless @profile.nil? opts = @opts.dup opts[:backend] = @backend if !@dependencies.nil? && !@dependencies.empty? opts[:dependencies] = Inspec::DependencySet.from_array(@dependencies, @cwd, @cache, @backend) end @profile = Inspec::Profile.for_fetcher(fetcher, opts) @profile.parent_profile = @parent_profile @profile end
def resolved_source
def resolved_source @resolved_source ||= fetcher.resolved_source end
def source_satisfies_spec?
def source_satisfies_spec? return true if version_constraints.empty? # Semverse::Constraint.satisfy_all returns a list of versions that match all of the # supplied constraints. Since we're only matching against a single version, the return # of satisfy_all will be non-empty if the profile version we have satisfies the constraints. constraints = @version_constraints.map { |x| Semverse::Constraint.new(x) } !Semverse::Constraint.satisfy_all(constraints, Semverse::Version.new(profile.version)).empty? end
def source_version
def source_version profile.version end
def to_hash
def to_hash h = { 'name' => name, 'resolved_source' => resolved_source, 'version_constraints' => version_constraints, } if !dependencies.empty? h['dependencies'] = dependencies.map(&:to_hash) end h end
def to_s
def to_s name end