Merge pull request #6916 from jonchang/new-audits

* check for build.with? in core
* unnecessary comments from external taps
* avoid build-time checks in core
This commit is contained in:
Jonathan Chang 2020-01-11 13:18:13 -05:00 committed by GitHub
commit 798e0a651d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 0 deletions

View File

@ -64,6 +64,15 @@ module RuboCop
problem "Commented-out dependency #{Regexp.last_match(1)}" problem "Commented-out dependency #{Regexp.last_match(1)}"
end end
return if formula_tap != "homebrew-core"
# Citation and tag comments from third-party taps
audit_comments do |comment|
next if comment !~ /#\s*(cite(?=\s*\w+:)|doi(?=\s*['"])|tag(?=\s*['"]))/
problem "Formulae in homebrew/core should not use `#{Regexp.last_match(1)}` comments"
end
end end
end end
@ -148,6 +157,16 @@ module RuboCop
problem "Reference '#{match[1]}' without dashes" problem "Reference '#{match[1]}' without dashes"
end end
return if formula_tap != "homebrew-core"
# Use of build.with? implies options, which are forbidden in homebrew/core
find_instance_method_call(body_node, :build, :without?) do
problem "Formulae in homebrew/core should not use `build.without?`."
end
find_instance_method_call(body_node, :build, :with?) do
problem "Formulae in homebrew/core should not use `build.with?`."
end
end end
def unless_modifier?(node) def unless_modifier?(node)
@ -400,6 +419,21 @@ module RuboCop
problem "Use the `#{match}` Ruby method instead of `#{method.source}`" problem "Use the `#{match}` Ruby method instead of `#{method.source}`"
end end
return if formula_tap != "homebrew-core"
# Avoid build-time checks in homebrew/core
find_every_method_call_by_name(body_node, :system).each do |method|
params = parameters(method)
next unless node_equals?(params[0], "make")
params[1..].each do |arg|
next unless regex_match_group(arg, /^(checks?|tests?)$/)
offending_node(method)
problem "Formulae in homebrew/core should not run build-time checks"
end
end
end end
def modifier?(node) def modifier?(node)

View File

@ -106,6 +106,21 @@ describe RuboCop::Cop::FormulaAudit::Comments do
end end
RUBY RUBY
end end
it "citation tags" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
# cite Howell_2009:
^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not use `cite` comments
# doi "10.111/222.x"
^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not use `doi` comments
# tag "software"
^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not use `tag` comments
end
RUBY
end
end end
end end
@ -160,6 +175,32 @@ end
describe RuboCop::Cop::FormulaAudit::OptionDeclarations do describe RuboCop::Cop::FormulaAudit::OptionDeclarations do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it "build.without? in homebrew/core" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def install
build.without? "bar"
^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not use `build.without?`.
end
end
RUBY
end
it "build.with? in homebrew/core" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def install
build.with? "bar"
^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not use `build.with?`.
end
end
RUBY
end
it "unless build.without? conditional" do it "unless build.without? conditional" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
@ -308,6 +349,17 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "When auditing formula" do context "When auditing formula" do
it "build-time checks in homebrew/core" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
system "make", "-j1", "test"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should not run build-time checks
end
RUBY
end
it "FileUtils usage" do it "FileUtils usage" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula