rubocop: Always run the "every formula should have a test block" check

- So that every formula has a test block, this should be non-strict.
- Fold it into the existing "test should have real things in it" cop for
  tidiness.
This commit is contained in:
Issy Long committed 2026-10-10 18:19:08 +01:00
1 parent c7c0518a63
commit f7aede5a94
6 files changed
+35 -55

No files matched your search

+10 -18
View File
@@ -29,14 +29,21 @@ module RuboCop
end
end
# This cop makes sure that a `test` block contains a proper test.
# This cop makes sure that a `test` block exists and contains a real test.
class Test < FormulaCop
extend AutoCorrector
sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
test = find_block(formula_nodes.body_node, :test)
return unless test
body_node = formula_nodes.body_node
test = find_block(body_node, :test)
if test.nil?
return if find_node_method_by_name(body_node, :disable!)
offending_node(formula_nodes.class_node) if body_node.nil?
problem "A `test do` test block should be added"
return
end
if test.body.nil?
problem "`test do` should not be empty"
@@ -130,20 +137,5 @@ module RuboCop
end
end
end
module FormulaAuditStrict
# This cop makes sure that a `test` block exists.
class TestPresent < FormulaCop
sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
body_node = formula_nodes.body_node
return if find_block(body_node, :test)
return if find_node_method_by_name(body_node, :disable!)
offending_node(formula_nodes.class_node) if body_node.nil?
problem "A `test do` test block should be added"
end
end
end
end
end
@@ -1,8 +0,0 @@
# typed: true
# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `RuboCop::Cop::FormulaAuditStrict::TestPresent`.
# Please instead update this file by running `bin/tapioca dsl RuboCop::Cop::FormulaAuditStrict::TestPresent`.
class RuboCop::Cop::FormulaAuditStrict::TestPresent; end
@@ -1,7 +1,7 @@
# typed: strict
# Not a real formula, just a Sorbet shim for external taps.
# rubocop:disable FormulaAudit/Desc,FormulaAudit/Homepage
# rubocop:disable FormulaAudit/Desc,FormulaAudit/Homepage,FormulaAudit/Test
class PortableFormula < Formula
extend T::Generic
@@ -9,4 +9,4 @@ class PortableFormula < Formula
# This could also be added to homebrew-core/Abstract/portable-formula.rb and subsequently removed here
Cache = type_template { { fixed: T::Hash[Symbol, T.untyped] } }
end
# rubocop:enable FormulaAudit/Desc,FormulaAudit/Homepage
# rubocop:enable FormulaAudit/Desc,FormulaAudit/Homepage,FormulaAudit/Test
@@ -1,27 +0,0 @@
# typed: true
# frozen_string_literal: true
require "rubocops/class"
RSpec.describe RuboCop::Cop::FormulaAuditStrict::TestPresent do
subject(:cop) { RuboCop::Cop::FormulaAuditStrict::TestPresent.new }
it "reports an offense when there is no test block" do
expect_offense(<<~RUBY)
class Foo < Formula
^^^^^^^^^^^^^^^^^^^ A `test do` test block should be added
url 'https://brew.sh/foo-1.0.tgz'
end
RUBY
end
it "reports no offenses when there is no test block and formula is disabled" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
disable! date: "2024-07-03", because: :unsupported
end
RUBY
end
end
@@ -6,6 +6,25 @@ require "rubocops/class"
RSpec.describe RuboCop::Cop::FormulaAudit::Test do
subject(:cop) { described_class.new }
it "reports an offense when there is no test block" do
expect_offense(<<~RUBY)
class Foo < Formula
^^^^^^^^^^^^^^^^^^^ FormulaAudit/Test: A `test do` test block should be added
url 'https://brew.sh/foo-1.0.tgz'
end
RUBY
end
it "reports no offenses when there is no test block and formula is disabled" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
disable! date: "2024-07-03", because: :unsupported
end
RUBY
end
it "allows existing resources inside test blocks without a strict audit" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
+4
View File
@@ -17,6 +17,10 @@ FormulaAudit/Desc:
FormulaAudit/Homepage:
Enabled: false
# The formula blocks in docs are deliberately truncated to only include bits relevant to the doc's purpose.
FormulaAudit/Test:
Enabled: false
Layout/LineLength:
Exclude:
- "**/Bottles.md" # The bottle block line length is long in its full form.