Class: Dependabot::Python::FileUpdater::RequirementReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/python/file_updater/requirement_replacer.rb

Instance Method Summary collapse

Constructor Details

#initialize(content:, dependency_name:, old_requirement:, new_requirement:, new_hash_version: nil) ⇒ RequirementReplacer

Returns a new instance of RequirementReplacer.


14
15
16
17
18
19
20
21
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 14

def initialize(content:, dependency_name:, old_requirement:,
               new_requirement:, new_hash_version: nil)
  @content          = content
  @dependency_name  = normalise(dependency_name)
  @old_requirement  = old_requirement
  @new_requirement  = new_requirement
  @new_hash_version = new_hash_version
end

Instance Method Details

#updated_contentObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 23

def updated_content
  updated_content =
    content.gsub(original_declaration_replacement_regex) do |mtch|
      # If the "declaration" is setting an option (e.g., no-binary)
      # ignore it, since it isn't actually a declaration
      next mtch if Regexp.last_match.pre_match.match?(/--.*\z/)

      updated_dependency_declaration_string
    end

  unless old_requirement == new_requirement
    raise "Expected content to change!" if content == updated_content
  end

  updated_content
end