style: require java dependency for JAVA_HOME

This commit is contained in:
Rylan Polster 2020-07-05 14:32:27 -04:00
parent 9ad342eba0
commit b4a9565b8b
3 changed files with 70 additions and 7 deletions

View File

@ -852,13 +852,6 @@ module Homebrew
) )
end 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 return unless @strict
# TODO: check could be in RuboCop # TODO: check could be in RuboCop

View File

@ -78,6 +78,21 @@ module RuboCop
problem "Do not concatenate paths in string interpolation" problem "Do not concatenate paths in string interpolation"
end end
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 end
end end

View File

@ -237,6 +237,61 @@ describe RuboCop::Cop::FormulaAudit::Text do
end end
RUBY RUBY
end 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
end end