Skip to content

Commit

Permalink
Handling markers with multiple condition joined with and/or logic. (#…
Browse files Browse the repository at this point in the history
…11244)

* Handling markers with multiple condition joined with and/or

* Explicitly define the type of result as T::Boolean

* remove version changes .ruby-version

---------

Co-authored-by: “Thavachelvam <“[email protected]”>
  • Loading branch information
thavaahariharangit and “Thavachelvam authored Jan 7, 2025
1 parent 9ee4434 commit 6948d5f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
23 changes: 22 additions & 1 deletion python/lib/dependabot/python/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,28 @@ def blocking_marker?(dep)
end

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

# Explicitly define the type of result as T::Boolean
result = T.let(evaluate_condition(conditions.shift, python_version), T::Boolean)

until conditions.empty?
operator = conditions.shift
next_condition = conditions.shift
next_result = evaluate_condition(next_condition, python_version)

result = if operator == "and"
result && next_result
else
result || next_result
end
end

result
end

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

case operator
when "<"
Expand Down
27 changes: 27 additions & 0 deletions python/spec/dependabot/python/file_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@
end
end

context "when there is a combination of multiple conditions with 'and' in a marker" do
before do
allow(parser).to receive(:python_raw_version).and_return("3.13.1")
end

# python_version >= '3.0' and python_version <= '3.7'
let(:requirements_fixture_name) { "markers_with_combination_of_conditions.txt" }

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

dependency = dependencies.first

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

context "when including a < in the requirement" do
let(:requirements_fixture_name) { "markers_2.txt" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
arrow==0.14.7; python_version < '3.0'
arrow==1.2.3; python_version >= '3.0' and python_version <= '3.7'
arrow==1.3.0; python_version >= '3.8'

0 comments on commit 6948d5f

Please sign in to comment.