lib/inspec/dependencies/requirement.rb in inspec-0.34.1 vs lib/inspec/dependencies/requirement.rb in inspec-0.35.0
- old
+ new
@@ -7,68 +7,69 @@
#
# Inspec::Requirement represents a given profile dependency, where
# appropriate we delegate to Inspec::Profile directly.
#
class Requirement
- attr_reader :name, :dep, :cwd, :opts
- attr_writer :dependencies
-
def self.from_metadata(dep, vendor_index, opts)
fail 'Cannot load empty dependency.' if dep.nil? || dep.empty?
- name = dep[:name] || fail('You must provide a name for all dependencies')
- version = dep[:version]
- new(name, version, vendor_index, opts[:cwd], opts.merge(dep))
+ new(dep[:name], dep[:version], vendor_index, opts[:cwd], opts.merge(dep))
end
def self.from_lock_entry(entry, cwd, vendor_index, backend)
req = new(entry[:name],
entry[:version_constraints],
vendor_index,
cwd,
entry[:resolved_source].merge(backend: backend))
locked_deps = []
- Array(entry['dependencies']).each do |dep_entry|
+ Array(entry[:dependencies]).each do |dep_entry|
locked_deps << Inspec::Requirement.from_lock_entry(dep_entry, cwd, vendor_index, backend)
end
req.lock_deps(locked_deps)
req
end
+ attr_reader :cwd, :opts, :required_version
def initialize(name, version_constraints, vendor_index, cwd, opts)
@name = name
- @version_requirement = Gem::Requirement.new(Array(version_constraints))
- @dep = Gem::Dependency.new(name, @version_requirement, :runtime)
+ @required_version = Gem::Requirement.new(Array(version_constraints))
@vendor_index = vendor_index
@backend = opts[:backend]
@opts = opts
@cwd = cwd
end
- def required_version
- @version_requirement
+ #
+ # A dependency can be renamed in inspec.yml/inspec.lock. Prefer
+ # the name the user gave this dependency over the profile name.
+ #
+ def name
+ @name || profile.name
end
def source_version
- profile.metadata.params[:version]
+ profile.version
end
def source_satisfies_spec?
- name = profile.metadata.params[:name]
- version = profile.metadata.params[:version]
- @dep.match?(name, version)
+ gem_dep.match?(profile.name, profile.version)
end
+ def gem_dep
+ @gem_dep ||= Gem::Dependency.new(profile.name, required_version, :runtime)
+ end
+
def resolved_source
@resolved_source ||= fetcher.resolved_source
end
def to_hash
h = {
'name' => name,
'resolved_source' => resolved_source,
- 'version_constraints' => @version_requirement.to_s,
+ 'version_constraints' => required_version.to_s,
}
if !dependencies.empty?
h['dependencies'] = dependencies.map(&:to_hash)
end
@@ -101,10 +102,10 @@
Inspec::Requirement.from_metadata(r, @vendor_index, cwd: @cwd, backend: @backend)
end
end
def to_s
- "#{dep} (#{resolved_source})"
+ "#{name ? name : '<unfetched>'} (#{resolved_source})"
end
def profile
opts = @opts.dup
opts[:cache] = @vendor_index