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, config, opts) ⇒ Requirement

Returns a new instance of Requirement.



47
48
49
50
51
52
53
54
55
# File 'lib/inspec/dependencies/requirement.rb', line 47

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

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



46
47
48
# File 'lib/inspec/dependencies/requirement.rb', line 46

def cwd
  @cwd
end

#optsObject (readonly)

Returns the value of attribute opts.



46
47
48
# File 'lib/inspec/dependencies/requirement.rb', line 46

def opts
  @opts
end

#version_constraintsObject (readonly)

Returns the value of attribute version_constraints.



46
47
48
# File 'lib/inspec/dependencies/requirement.rb', line 46

def version_constraints
  @version_constraints
end

Class Method Details

.from_lock_entry(entry, config, opts = {}) ⇒ Object



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

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

.from_metadata(dep, cache, opts) ⇒ Object



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

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
  config = {
    cache: cache,
    cwd: req_path,
  }

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

Instance Method Details

#dependenciesObject

load dependencies of the dependency



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

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

#fetcherObject



101
102
103
# File 'lib/inspec/dependencies/requirement.rb', line 101

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

#lock_deps(dep_array) ⇒ Object



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

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.



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

def name
  @name || profile.name
end

#profileObject

load the profile for the requirement



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/inspec/dependencies/requirement.rb', line 117

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
  opts[:profile_name] = @name
  opts[:parent_profile] = @parent_profile
  @profile = Inspec::Profile.for_fetcher(fetcher, opts)
  @profile
end

#resolved_sourceObject



79
80
81
# File 'lib/inspec/dependencies/requirement.rb', line 79

def resolved_source
  @resolved_source ||= fetcher.resolved_source
end

#source_satisfies_spec?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
# File 'lib/inspec/dependencies/requirement.rb', line 69

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



65
66
67
# File 'lib/inspec/dependencies/requirement.rb', line 65

def source_version
  profile.version
end

#to_hashObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/inspec/dependencies/requirement.rb', line 83

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



112
113
114
# File 'lib/inspec/dependencies/requirement.rb', line 112

def to_s
  name
end