Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling markers with multiple condition joined with and/or logic. #11244

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Loading