From 929e481dce5366dcf7314cb92479511605615be2 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Tue, 12 Jan 2021 17:02:48 +1100 Subject: [PATCH] rubocops/keg_only: use rubocop v1 API --- Library/Homebrew/rubocops/keg_only.rb | 11 ++- .../Homebrew/test/rubocops/keg_only_spec.rb | 72 ++++++------------- 2 files changed, 31 insertions(+), 52 deletions(-) diff --git a/Library/Homebrew/rubocops/keg_only.rb b/Library/Homebrew/rubocops/keg_only.rb index 3b811f07b8..ea2bd9131d 100644 --- a/Library/Homebrew/rubocops/keg_only.rb +++ b/Library/Homebrew/rubocops/keg_only.rb @@ -10,6 +10,8 @@ module RuboCop # # @api private class KegOnly < FormulaCop + extend AutoCorrector + def audit_formula(_node, _class_node, _parent_class_node, body_node) keg_only_node = find_node_method_by_name(body_node, :keg_only) return unless keg_only_node @@ -33,12 +35,17 @@ module RuboCop first_word = reason.split.first if reason =~ /\A[A-Z]/ && !reason.start_with?(*allowlist) - problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'." + problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'." do |corrector| + reason[0] = reason[0].downcase + corrector.replace(@offensive_node.source_range, "\"#{reason}\"") + end end return unless reason.end_with?(".") - problem "`keg_only` reason should not end with a period." + problem "`keg_only` reason should not end with a period." do |corrector| + corrector.replace(@offensive_node.source_range, "\"#{reason.chop}\"") + end end def autocorrect(node) diff --git a/Library/Homebrew/test/rubocops/keg_only_spec.rb b/Library/Homebrew/test/rubocops/keg_only_spec.rb index 16bb4c6234..c98d1ef599 100644 --- a/Library/Homebrew/test/rubocops/keg_only_spec.rb +++ b/Library/Homebrew/test/rubocops/keg_only_spec.rb @@ -6,7 +6,7 @@ require "rubocops/keg_only" describe RuboCop::Cop::FormulaAudit::KegOnly do subject(:cop) { described_class.new } - specify "keg_only_needs_downcasing" do + it "reports and corrects an offense when the `keg_only` reason is capitalized" do expect_offense(<<~RUBY) class Foo < Formula @@ -17,9 +17,19 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do ^^^^^^^^^^^^^^^^^ 'Because' from the `keg_only` reason should be 'because'. end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + + url "https://brew.sh/foo-1.0.tgz" + homepage "https://brew.sh" + + keg_only "because why not" + end + RUBY end - specify "keg_only_redundant_period" do + it "reports and corrects an offense when the `keg_only` reason ends with a period" do expect_offense(<<~RUBY) class Foo < Formula url "https://brew.sh/foo-1.0.tgz" @@ -29,51 +39,18 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do ^^^^^^^^^^^^^^^^^^^^^^^ `keg_only` reason should not end with a period. end RUBY - end - specify "keg_only_autocorrects_downcasing" do - source = <<~RUBY + expect_correction(<<~RUBY) class Foo < Formula url "https://brew.sh/foo-1.0.tgz" homepage "https://brew.sh" - keg_only "Because why not" - end - RUBY - corrected_source = <<~RUBY - class Foo < Formula - url "https://brew.sh/foo-1.0.tgz" - homepage "https://brew.sh" - keg_only "because why not" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end - - specify "keg_only_autocorrects_redundant_period" do - source = <<~RUBY - class Foo < Formula - url "https://brew.sh/foo-1.0.tgz" - homepage "https://brew.sh" - keg_only "ending with a period." - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url "https://brew.sh/foo-1.0.tgz" - homepage "https://brew.sh" keg_only "ending with a period" end RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) end - specify "keg_only_handles_block_correctly" do + it "reports no offenses when a `keg_only` reason is a block" do expect_no_offenses(<<~RUBY) class Foo < Formula url "https://brew.sh/foo-1.0.tgz" @@ -89,7 +66,7 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do RUBY end - specify "keg_only_handles_allowlist_correctly" do + it "reports no offenses if a capitalized `keg-only` reason is an exempt proper noun" do expect_no_offenses(<<~RUBY) class Foo < Formula url "https://brew.sh/foo-1.0.tgz" @@ -100,18 +77,13 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do RUBY end - specify "keg_only does not need downcasing of formula name in reason" do - filename = Formulary.core_path("foo") - File.open(filename, "w") do |file| - FileUtils.chmod "-rwx", filename + it "reports no offenses if a capitalized `keg_only` reason is the formula's name" do + expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb") + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" - expect_no_offenses(<<~RUBY, file) - class Foo < Formula - url "https://brew.sh/foo-1.0.tgz" - - keg_only "Foo is the formula name hence downcasing is not required" - end - RUBY - end + keg_only "Foo is the formula name hence downcasing is not required" + end + RUBY end end