Skip to content

Commit

Permalink
Strict type Dependabot::Terraform::Requirement. (#10001)
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-meka authored Jun 14, 2024
1 parent ba31c4f commit fff2779
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions terraform/lib/dependabot/terraform/requirement.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "sorbet-runtime"
Expand All @@ -11,13 +11,15 @@
module Dependabot
module Terraform
class Requirement < Dependabot::Requirement
extend T::Sig
# Override regex PATTERN from Gem::Requirement to add support for the
# optional 'v' prefix to release tag names, which Terraform supports.
# https://www.terraform.io/docs/registry/modules/publish.html#requirements
OPERATORS = OPS.keys.map { |key| Regexp.quote(key) }.join("|").freeze
PATTERN_RAW = "\\s*(#{OPERATORS})?\\s*v?(#{Gem::Version::VERSION_PATTERN})\\s*".freeze
OPERATORS = T.let(OPS.keys.map { |key| Regexp.quote(key) }.join("|").freeze, String)
PATTERN_RAW = T.let("\\s*(#{OPERATORS})?\\s*v?(#{Gem::Version::VERSION_PATTERN})\\s*".freeze, String)
PATTERN = /\A#{PATTERN_RAW}\z/

sig { params(obj: T.any(String, Gem::Version)).returns(T::Array[T.any(String, Version)]) }
def self.parse(obj)
return ["=", Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

Expand All @@ -28,19 +30,20 @@ def self.parse(obj)

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

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

# For consistency with other languages, we define a requirements array.
# Terraform doesn't have an `OR` separator for requirements, so it
# always contains a single element.
sig { override.params(requirement_string: T.nilable(String)).returns(T::Array[Requirement]) }
def self.requirements_array(requirement_string)
[new(requirement_string)]
[new(requirement_string.to_s)]
end

# Patches Gem::Requirement to make it accept requirement strings like
# "~> 4.2.5, >= 4.2.5.1" without first needing to split them.
sig { params(requirements: T.any(String, T::Array[String])).void }
def initialize(*requirements)
requirements = requirements.flatten.flat_map do |req_string|
req_string.split(",").map(&:strip)
Expand Down

0 comments on commit fff2779

Please sign in to comment.