diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index b6507d56f6..4e3b29dfbd 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -852,13 +852,6 @@ module Homebrew ) end - if line =~ /JAVA_HOME/i && - [formula.name, *formula.deps.map(&:name)].none? { |name| name.match?(/^openjdk(@|$)/) } && - formula.requirements.none? { |req| req.is_a?(JavaRequirement) } - # TODO: check could be in RuboCop - problem "Use `depends_on :java` to set JAVA_HOME" - end - return unless @strict # TODO: check could be in RuboCop diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 8aa2b9ab2a..3559d12ce1 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -78,6 +78,21 @@ module RuboCop problem "Do not concatenate paths in string interpolation" end end + + find_strings(body_node).each do |n| + next unless regex_match_group(n, /JAVA_HOME/i) + + next if @formula_name.match?(/^openjdk(@|$)/) + + next if find_every_method_call_by_name(body_node, :depends_on).any? do |dependency| + dependency.each_descendant(:str).count.zero? || + regex_match_group(dependency.each_descendant(:str).first, /^openjdk(@|$)/) || + depends_on?(:java) + end + + offending_node(n) + problem "Use `depends_on :java` to set JAVA_HOME" + end end end end diff --git a/Library/Homebrew/test/rubocops/text_spec.rb b/Library/Homebrew/test/rubocops/text_spec.rb index f915daafcd..7ab156c74d 100644 --- a/Library/Homebrew/test/rubocops/text_spec.rb +++ b/Library/Homebrew/test/rubocops/text_spec.rb @@ -237,6 +237,61 @@ describe RuboCop::Cop::FormulaAudit::Text do end RUBY end + + it "When using JAVA_HOME without a java dependency" do + expect_offense(<<~RUBY) + class Foo < Formula + def install + ohai "JAVA_HOME" + ^^^^^^^^^^^ Use `depends_on :java` to set JAVA_HOME + end + end + RUBY + end + + it "When using JAVA_HOME with an openjdk dependency" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on "openjdk" + def install + ohai "JAVA_HOME" + end + end + RUBY + end + + it "When using JAVA_HOME with an openjdk build dependency" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on "openjdk" => :build + def install + ohai "JAVA_HOME" + end + end + RUBY + end + + it "When using JAVA_HOME with a java dependency" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on :java + def install + ohai "JAVA_HOME" + end + end + RUBY + end + + it "When using JAVA_HOME with a java build dependency" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + depends_on :java => :build + def install + ohai "JAVA_HOME" + end + end + RUBY + end end end