Class: Inspec::Requirement

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/dependencies/requirement.rb

Overview

Inspec::Requirement represents a given profile dependency, where appropriate we delegate to Inspec::Profile directly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version_constraints, cache, cwd, opts) ⇒ Requirement

Returns a new instance of Requirement.



44
45
46
47
48
49
50
51
# File 'lib/inspec/dependencies/requirement.rb', line 44

def initialize(name, version_constraints, cache, cwd, opts)
  @name = name
  @version_constraints = Array(version_constraints)
  @cache = cache
  @backend = opts[:backend]
  @opts = opts
  @cwd = cwd
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



43
44
45
# File 'lib/inspec/dependencies/requirement.rb', line 43

def cwd
  @cwd
end

#optsObject (readonly)

Returns the value of attribute opts.



43
44
45
# File 'lib/inspec/dependencies/requirement.rb', line 43

def opts
  @opts
end

#version_constraintsObject (readonly)

Returns the value of attribute version_constraints.



43
44
45
# File 'lib/inspec/dependencies/requirement.rb', line 43

def version_constraints
  @version_constraints
end

Class Method Details

.from_lock_entry(entry, cwd, cache, backend, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inspec/dependencies/requirement.rb', line 28

def self.from_lock_entry(entry, cwd, cache, backend, opts = {})
  req = new(entry[:name],
            entry[:version_constraints],
            cache,
            cwd,
            entry[:resolved_source].merge(backend: backend).merge(opts))

  locked_deps = []
  Array(entry[:dependencies]).each do |dep_entry|
    locked_deps << Inspec::Requirement.from_lock_entry(dep_entry, cwd, cache, backend, opts)
  end
  req.lock_deps(locked_deps)
  req
end

.from_metadata(dep, cache, opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/inspec/dependencies/requirement.rb', line 12

def self.(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

  new(dep[:name],
      dep[:version],
      cache,
      req_path,
      opts.merge(dep))
end

Instance Method Details

#dependenciesObject

load dependencies of the dependency



102
103
104
105
106
# File 'lib/inspec/dependencies/requirement.rb', line 102

def dependencies
  @dependencies ||= profile..dependencies.map do |r|
    Inspec::Requirement.(r, @cache, cwd: @cwd, backend: @backend)
  end
end

#fetcherObject



97
98
99
# File 'lib/inspec/dependencies/requirement.rb', line 97

def fetcher
  @fetcher ||= Inspec::CachedFetcher.new(opts, @cache)
end

#lock_deps(dep_array) ⇒ Object



93
94
95
# File 'lib/inspec/dependencies/requirement.rb', line 93

def lock_deps(dep_array)
  @dependencies = dep_array
end

#nameObject

A dependency can be renamed in inspec.yml/inspec.lock. Prefer the name the user gave this dependency over the profile name.



57
58
59
# File 'lib/inspec/dependencies/requirement.rb', line 57

def name
  @name || profile.name
end

#profileObject

load the profile for the requirement



113
114
115
116
117
118
119
120
121
# File 'lib/inspec/dependencies/requirement.rb', line 113

def profile
  return @profile unless @profile.nil?
  opts = @opts.dup
  opts[:backend] = @backend
  if !@dependencies.nil?
    opts[:dependencies] = Inspec::DependencySet.from_array(@dependencies, @cwd, @cache, @backend)
  end
  @profile = Inspec::Profile.for_fetcher(fetcher, opts)
end

#resolved_sourceObject



75
76
77
# File 'lib/inspec/dependencies/requirement.rb', line 75

def resolved_source
  @resolved_source ||= fetcher.resolved_source
end

#source_satisfies_spec?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
# File 'lib/inspec/dependencies/requirement.rb', line 65

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

#source_versionObject



61
62
63
# File 'lib/inspec/dependencies/requirement.rb', line 61

def source_version
  profile.version
end

#to_hashObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inspec/dependencies/requirement.rb', line 79

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

#to_sObject



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

def to_s
  name
end