diff --git a/Library/Homebrew/rubocops/class.rb b/Library/Homebrew/rubocops/class.rb index 3c9aad32965f9..d7b2469536f79 100644 --- a/Library/Homebrew/rubocops/class.rb +++ b/Library/Homebrew/rubocops/class.rb @@ -29,14 +29,21 @@ def audit_formula(formula_nodes) 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 @@ def audit_formula(formula_nodes) 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 diff --git a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/test_present.rbi b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/test_present.rbi deleted file mode 100644 index dcb027f2d07cb..0000000000000 --- a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/test_present.rbi +++ /dev/null @@ -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 diff --git a/Library/Homebrew/sorbet/rbi/shims/portable_formula.rbi b/Library/Homebrew/sorbet/rbi/shims/portable_formula.rbi index baba382fc21c6..90cfc58f00ab4 100644 --- a/Library/Homebrew/sorbet/rbi/shims/portable_formula.rbi +++ b/Library/Homebrew/sorbet/rbi/shims/portable_formula.rbi @@ -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 diff --git a/Library/Homebrew/test/rubocops/class/test_present.rb b/Library/Homebrew/test/rubocops/class/test_present.rb deleted file mode 100644 index c19991f18795e..0000000000000 --- a/Library/Homebrew/test/rubocops/class/test_present.rb +++ /dev/null @@ -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 diff --git a/Library/Homebrew/test/rubocops/class/test_spec.rb b/Library/Homebrew/test/rubocops/class/test_spec.rb index f8ee743af477c..a4522c47d7bb1 100644 --- a/Library/Homebrew/test/rubocops/class/test_spec.rb +++ b/Library/Homebrew/test/rubocops/class/test_spec.rb @@ -6,6 +6,25 @@ 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 diff --git a/docs/.rubocop.yml b/docs/.rubocop.yml index a6631bf137f75..962137340c4dc 100644 --- a/docs/.rubocop.yml +++ b/docs/.rubocop.yml @@ -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.