Class: Dependabot::Terraform::Requirement

Inherits:
Gem::Requirement
  • Object
show all
Defined in:
lib/dependabot/terraform/requirement.rb

Constant Summary collapse

OPERATORS =

Override regex PATTERN from Gem::Requirement to add support for the optional 'v' prefix to release tag names, which Terraform supports. www.terraform.io/docs/registry/modules/publish.html#requirements

OPS.keys.map { |key| Regexp.quote(key) }.join("|").freeze
PATTERN_RAW =
"\\s*(#{OPERATORS})?\\s*v?(#{Gem::Version::VERSION_PATTERN})\\s*"
PATTERN =
/\A#{PATTERN_RAW}\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Patches Gem::Requirement to make it accept requirement strings like “~> 4.2.5, >= 4.2.5.1” without first needing to split them.


39
40
41
42
43
44
45
# File 'lib/dependabot/terraform/requirement.rb', line 39

def initialize(*requirements)
  requirements = requirements.flatten.flat_map do |req_string|
    req_string.split(",").map(&:strip)
  end

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dependabot/terraform/requirement.rb', line 17

def self.parse(obj)
  return ["=", Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

  unless (matches = PATTERN.match(obj.to_s))
    msg = "Illformed requirement [#{obj.inspect}]"
    raise BadRequirementError, msg
  end

  return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"

  [matches[1] || "=", Terraform::Version.new(matches[2])]
end

.requirements_array(requirement_string) ⇒ Object

For consistency with other langauges, we define a requirements array. Terraform doesn't have an `OR` separator for requirements, so it always contains a single element.


33
34
35
# File 'lib/dependabot/terraform/requirement.rb', line 33

def self.requirements_array(requirement_string)
  [new(requirement_string)]
end