diff --git a/Library/Homebrew/rubocops/lines.rb b/Library/Homebrew/rubocops/lines.rb index e7d70da60e..ef4eaf0352 100644 --- a/Library/Homebrew/rubocops/lines.rb +++ b/Library/Homebrew/rubocops/lines.rb @@ -335,7 +335,7 @@ module RuboCop find_strings(body_node).each do |str| string_content = string_content(str) - next unless match = string_content.match(/^python(@)?(\d\.\d)$/) + next unless match = string_content.match(/^python(@)?(\d\.\d+)$/) next if python_version == match[2] @fix = if match[1] diff --git a/Library/Homebrew/test/rubocops/lines_spec.rb b/Library/Homebrew/test/rubocops/lines_spec.rb index aa4f0069af..d9c117deb8 100644 --- a/Library/Homebrew/test/rubocops/lines_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_spec.rb @@ -793,6 +793,30 @@ describe RuboCop::Cop::FormulaAudit::PythonVersions do RUBY end + it "allow matching versions with two digits" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on "python@3.10" + + def install + puts "python@3.10" + end + end + RUBY + end + + it "allow matching versions without `@` with two digits" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on "python@3.10" + + def install + puts "python3.10" + end + end + RUBY + end + it "do not allow mismatching versions" do expect_offense(<<~RUBY) class Foo < Formula @@ -819,6 +843,32 @@ describe RuboCop::Cop::FormulaAudit::PythonVersions do RUBY end + it "do not allow mismatching versions with two digits" do + expect_offense(<<~RUBY) + class Foo < Formula + depends_on "python@3.11" + + def install + puts "python@3.10" + ^^^^^^^^^^^^^ References to `python@3.10` should match the specified python dependency (`python@3.11`) + end + end + RUBY + end + + it "do not allow mismatching versions without `@` with two digits" do + expect_offense(<<~RUBY) + class Foo < Formula + depends_on "python@3.11" + + def install + puts "python3.10" + ^^^^^^^^^^^^ References to `python3.10` should match the specified python dependency (`python3.11`) + end + end + RUBY + end + it "autocorrects mismatching versions" do source = <<~RUBY class Foo < Formula @@ -868,6 +918,56 @@ describe RuboCop::Cop::FormulaAudit::PythonVersions do new_source = autocorrect_source(source) expect(new_source).to eq(corrected_source) end + + it "autocorrects mismatching versions with two digits" do + source = <<~RUBY + class Foo < Formula + depends_on "python@3.10" + + def install + puts "python@3.9" + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + depends_on "python@3.10" + + def install + puts "python@3.10" + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end + + it "autocorrects mismatching versions without `@` with two digits" do + source = <<~RUBY + class Foo < Formula + depends_on "python@3.11" + + def install + puts "python3.10" + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + depends_on "python@3.11" + + def install + puts "python3.11" + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end end end