Skip to content

Commit

Permalink
Merge pull request #16364 from cho-m/pybuild-rubocopy
Browse files Browse the repository at this point in the history
rubocops/lines: consistency with single non-runtime Python
  • Loading branch information
MikeMcQuaid committed Dec 20, 2023
2 parents f61ef4b + 61b512c commit 4d93a50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Library/Homebrew/rubocops/lines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,17 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
string_content(parameters(dep).first).start_with? "python@"
end

return if python_formula_node.blank?
python_version = if python_formula_node.blank?
other_python_nodes = find_every_method_call_by_name(body_node, :depends_on).select do |dep|
parameters(dep).first.instance_of?(RuboCop::AST::HashNode) &&
string_content(parameters(dep).first.keys.first).start_with?("python@")
end
return if other_python_nodes.size != 1

python_version = string_content(parameters(python_formula_node).first).split("@").last
string_content(parameters(other_python_nodes.first).first.keys.first).split("@").last
else
string_content(parameters(python_formula_node).first).split("@").last
end

find_strings(body_node).each do |str|
content = string_content(str)
Expand Down
40 changes: 40 additions & 0 deletions Library/Homebrew/test/rubocops/text/python_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,45 @@ def install
end
RUBY
end

it "reports no offenses for multiple non-runtime Python dependencies" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
depends_on "python@3.9" => :build
depends_on "python@3.10" => :test
def install
puts "python3.9"
end
test do
puts "python3.10"
end
end
RUBY
end

it "reports and corrects Python references that mismatch single non-runtime Python dependency" do
expect_offense(<<~RUBY)
class Foo < Formula
depends_on "python@3.9" => :build
def install
puts "python@3.8"
^^^^^^^^^^^^ FormulaAudit/PythonVersions: References to `python@3.8` should match the specified python dependency (`python@3.9`)
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
depends_on "python@3.9" => :build
def install
puts "python@3.9"
end
end
RUBY
end
end
end

0 comments on commit 4d93a50

Please sign in to comment.