diff --git a/python/lib/dependabot/python/file_parser/subdependency_type_parser.rb b/python/lib/dependabot/python/file_parser/subdependency_type_parser.rb new file mode 100644 index 00000000000..20dcf2b9010 --- /dev/null +++ b/python/lib/dependabot/python/file_parser/subdependency_type_parser.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "toml-rb" +require "dependabot/python/file_parser" +require "dependabot/python/name_normaliser" + +module Dependabot + module Python + class FileParser + class PoetrySubdependencyTypeParser + def initialize(lockfile:) + @lockfile = lockfile + end + + def subdep_type(dep) + category = + TomlRB.parse(lockfile.content).fetch("package", []). + find { |dets| normalise(dets.fetch("name")) == dep.name }. + fetch("category") + + category == "dev" ? "dev-dependencies" : "dependencies" + end + + private + + attr_reader :lockfile + + def normalise(name) + NameNormaliser.normalise(name) + end + end + end + end +end diff --git a/python/lib/dependabot/python/file_updater/poetry_file_updater.rb b/python/lib/dependabot/python/file_updater/poetry_file_updater.rb index f88ddd3adcd..e674273961a 100644 --- a/python/lib/dependabot/python/file_updater/poetry_file_updater.rb +++ b/python/lib/dependabot/python/file_updater/poetry_file_updater.rb @@ -8,6 +8,7 @@ require "dependabot/python/version" require "dependabot/python/requirement" require "dependabot/python/file_parser/python_requirement_parser" +require "dependabot/python/file_parser/subdependency_type_parser" require "dependabot/python/file_updater" require "dependabot/python/native_helpers" require "dependabot/python/name_normaliser" @@ -156,17 +157,10 @@ def lock_declaration_to_new_version!(poetry_object, dep) end def create_declaration_at_new_version!(poetry_object, dep) - poetry_object[subdep_type] ||= {} - poetry_object[subdep_type][dependency.name] = dep.version - end + subdep_type = subdependency_type_parser.subdep_type(dep) - def subdep_type - category = - TomlRB.parse(lockfile.content).fetch("package", []). - find { |dets| normalise(dets.fetch("name")) == dependency.name }. - fetch("category") - - category == "dev" ? "dev-dependencies" : "dependencies" + poetry_object[subdep_type] ||= {} + poetry_object[subdep_type][dep.name] = dep.version end def sanitize(pyproject_content) @@ -299,6 +293,13 @@ def python_requirement_parser ) end + def subdependency_type_parser + @subdependency_type_parser ||= + FileParser::PoetrySubdependencyTypeParser.new( + lockfile: lockfile + ) + end + def language_version_manager @language_version_manager ||= LanguageVersionManager.new( diff --git a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb index 43d28c8090b..e80562e7bdb 100644 --- a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb +++ b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb @@ -9,6 +9,7 @@ require "dependabot/shared_helpers" require "dependabot/python/file_parser" require "dependabot/python/file_parser/python_requirement_parser" +require "dependabot/python/file_parser/subdependency_type_parser" require "dependabot/python/file_updater/pyproject_preparer" require "dependabot/python/update_checker" require "dependabot/python/version" @@ -275,6 +276,8 @@ def set_target_dependency_req(pyproject_content, updated_requirement) # If this is a sub-dependency, add the new requirement unless dependency.requirements.find { |r| r[:file] == pyproject.name } + subdep_type = subdependency_type_parser.subdep_type(dependency) + poetry_object[subdep_type] ||= {} poetry_object[subdep_type][dependency.name] = updated_requirement end @@ -294,15 +297,6 @@ def update_dependency_requirement(toml_node, requirement) end end - def subdep_type - category = - TomlRB.parse(lockfile.content).fetch("package", []). - find { |dets| normalise(dets.fetch("name")) == dependency.name }. - fetch("category") - - category == "dev" ? "dev-dependencies" : "dependencies" - end - def python_requirement_parser @python_requirement_parser ||= FileParser::PythonRequirementParser.new( @@ -310,6 +304,13 @@ def python_requirement_parser ) end + def subdependency_type_parser + @subdependency_type_parser ||= + FileParser::PoetrySubdependencyTypeParser.new( + lockfile: lockfile + ) + end + def language_version_manager @language_version_manager ||= LanguageVersionManager.new(