Skip to content

Commit

Permalink
Selecting the correct requirement based on the given marker condition. (
Browse files Browse the repository at this point in the history
#11204)

* Selecting the correct requirement based on the given marker condition.

* Lint error fixes

* Updated as per the review comment and added a test.

* Removing the null check as per the comment.

---------

Co-authored-by: “Thavachelvam <“thavaahariharangit@git.com”>
  • Loading branch information
thavaahariharangit and “Thavachelvam authored Jan 6, 2025
1 parent eb6d6fa commit 81a7934
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
33 changes: 30 additions & 3 deletions python/lib/dependabot/python/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,37 @@ def group_from_filename(filename)

def blocking_marker?(dep)
return false if dep["markers"] == "None"
return true if dep["markers"].include?("<")
return false if dep["markers"].include?(">")

dep["requirement"]&.include?("<")
marker = dep["markers"]
version = python_raw_version

if marker.include?("python_version")
!marker_satisfied?(marker, version)
else
return true if dep["markers"].include?("<")
return false if dep["markers"].include?(">")

dep["requirement"]&.include?("<")
end
end

def marker_satisfied?(marker, python_version)
operator, version = marker.match(/([<>=!]=?)\s*"?([\d.]+)"?/).captures

case operator
when "<"
Dependabot::Python::Version.new(python_version) < Dependabot::Python::Version.new(version)
when "<="
Dependabot::Python::Version.new(python_version) <= Dependabot::Python::Version.new(version)
when ">"
Dependabot::Python::Version.new(python_version) > Dependabot::Python::Version.new(version)
when ">="
Dependabot::Python::Version.new(python_version) >= Dependabot::Python::Version.new(version)
when "=="
Dependabot::Python::Version.new(python_version) == Dependabot::Python::Version.new(version)
else
false
end
end

def setup_file_dependencies
Expand Down
34 changes: 32 additions & 2 deletions python/spec/dependabot/python/file_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,40 @@
end

context "with markers" do
context "when including a < in the marker" do
context "when the marker <= 2.6" do
before do
allow(parser).to receive(:python_raw_version).and_return("2.6")
end

let(:requirements_fixture_name) { "markers.txt" }

it "parses only the >= marker" do
it "then the dependency version should be 1.0.4" do
expect(dependencies.length).to eq(1)

dependency = dependencies.first

expect(dependency).to be_a(Dependabot::Dependency)
expect(dependency.name).to eq("distro")
expect(dependency.version).to eq("1.0.4")
expect(dependency.requirements).to eq(
[{
requirement: "==1.0.4",
file: "requirements.txt",
groups: ["dependencies"],
source: nil
}]
)
end
end

context "when the marker => 2.7" do
before do
allow(parser).to receive(:python_raw_version).and_return("2.7")
end

let(:requirements_fixture_name) { "markers.txt" }

it "then the dependency version should be 1.3.0" do
expect(dependencies.length).to eq(1)

dependency = dependencies.first
Expand Down

0 comments on commit 81a7934

Please sign in to comment.