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.



50
51
52
53
54
55
56
57
58
# File 'lib/inspec/dependencies/requirement.rb', line 50

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

#cacheObject (readonly)

Returns the value of attribute cache.



49
50
51
# File 'lib/inspec/dependencies/requirement.rb', line 49

def cache
  @cache
end

#cwdObject (readonly)

Returns the value of attribute cwd.



49
50
51
# File 'lib/inspec/dependencies/requirement.rb', line 49

def cwd
  @cwd
end

#optsObject (readonly)

Returns the value of attribute opts.



49
50
51
# File 'lib/inspec/dependencies/requirement.rb', line 49

def opts
  @opts
end

#version_constraintsObject (readonly)

Returns the value of attribute version_constraints.



49
50
51
# File 'lib/inspec/dependencies/requirement.rb', line 49

def version_constraints
  @version_constraints
end

Class Method Details

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



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

def self.from_lock_entry(entry, config, opts = {})
  resolved_source = entry[:resolved_source]
    .merge(backend: config[:backend])
    .merge(opts)

  req = new(entry[:name],
            entry[:version_constraints],
            config,
            resolved_source)

  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



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

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



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

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

#fetcherObject



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

def fetcher
  @runner_options ||= (Inspec::Config.cached || {})
  @fetcher ||= Inspec::CachedFetcher.new(opts, @cache, @runner_options)
end

#lock_deps(dep_array) ⇒ Object



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

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.



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

def name
  @name || profile.name
end

#profileObject

load the profile for the requirement



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/inspec/dependencies/requirement.rb', line 121

def profile
  return @profile unless @profile.nil?

  opts = @opts.dup
  opts[:backend] = @backend
  opts[:runner_conf] = Inspec::Config.cached
  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



82
83
84
# File 'lib/inspec/dependencies/requirement.rb', line 82

def resolved_source
  @resolved_source ||= fetcher.resolved_source
end

#source_satisfies_spec?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/inspec/dependencies/requirement.rb', line 72

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



68
69
70
# File 'lib/inspec/dependencies/requirement.rb', line 68

def source_version
  profile.version
end

#to_hashObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/inspec/dependencies/requirement.rb', line 86

def to_hash
  h = {
    "name" => name,
    "resolved_source" => resolved_source,
    "version_constraints" => version_constraints,
  }

  unless dependencies.empty?
    h["dependencies"] = dependencies.map(&:to_hash)
  end

  h
end

#to_sObject



116
117
118
# File 'lib/inspec/dependencies/requirement.rb', line 116

def to_s
  name
end