From 3038ff872619e14c73ed91abb3deed74b02104b8 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 13 Aug 2020 07:39:54 +0200 Subject: [PATCH] Improve detection of descriptions starting with cask/formula name. --- .../Homebrew/rubocops/shared/desc_helper.rb | 3 +- .../Homebrew/test/rubocops/cask/desc_spec.rb | 88 +++++++++++-------- .../test/rubocops/formula_desc_spec.rb | 2 +- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Library/Homebrew/rubocops/shared/desc_helper.rb b/Library/Homebrew/rubocops/shared/desc_helper.rb index 80092a8df7..63cc95537c 100644 --- a/Library/Homebrew/rubocops/shared/desc_helper.rb +++ b/Library/Homebrew/rubocops/shared/desc_helper.rb @@ -61,7 +61,8 @@ module RuboCop end # Check if the desc starts with the formula's or cask's name. - problem "Description shouldn't start with the #{type} name." if regex_match_group(desc, /^#{name} /i) + name_regex = name.delete("-").split("").join('[\s\-]?') + problem "Description shouldn't start with the #{type} name." if regex_match_group(desc, /^#{name_regex}\b/i) # Check if a full stop is used at the end of a desc (apart from in the case of "etc."). if regex_match_group(desc, /\.$/) && !string_content(desc).end_with?("etc.") diff --git a/Library/Homebrew/test/rubocops/cask/desc_spec.rb b/Library/Homebrew/test/rubocops/cask/desc_spec.rb index 28a2b6d3f7..38793ccf99 100644 --- a/Library/Homebrew/test/rubocops/cask/desc_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/desc_spec.rb @@ -4,49 +4,63 @@ require "rubocops/rubocop-cask" require "test/rubocops/cask/shared_examples/cask_cop" describe RuboCop::Cop::Cask::Desc do - include CaskCop - subject(:cop) { described_class.new } - context "with incorrect `desc` stanza" do - let(:source) { - <<~RUBY - cask "foo" do - desc "A bar program" - end - RUBY - } - let(:correct_source) { - <<~RUBY - cask "foo" do - desc "Bar program" - end - RUBY - } - let(:expected_offenses) do - [{ - message: "Description shouldn't start with an indefinite article, i.e. \"A\".", - severity: :convention, - line: 2, - column: 8, - source: "A", - }] - end + it "does not start with an indefinite article" do + expect_no_offenses <<~RUBY + cask "foo" do + desc "Bar program" + end + RUBY - include_examples "reports offenses" + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foo' do + desc 'A bar program' + ^ Description shouldn\'t start with an indefinite article, i.e. \"A\". + end + RUBY - include_examples "autocorrects source" + expect_correction <<~RUBY + cask 'foo' do + desc 'Bar program' + end + RUBY end - context "with correct `desc` stanza" do - let(:source) { - <<~RUBY - cask "foo" do - desc "Bar program" - end - RUBY - } + it "does not start with the cask name" do + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foobar' do + desc 'Foo bar program' + ^^^^^^^ Description shouldn't start with the cask name. + end + RUBY - include_examples "does not report any offenses" + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foobar' do + desc 'Foo-Bar program' + ^^^^^^^ Description shouldn\'t start with the cask name. + end + RUBY + + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foo-bar' do + desc 'Foo bar program' + ^^^^^^^ Description shouldn\'t start with the cask name. + end + RUBY + + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foo-bar' do + desc 'Foo-Bar program' + ^^^^^^^ Description shouldn\'t start with the cask name. + end + RUBY + + expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" + cask 'foo-bar' do + desc 'Foo Bar' + ^^^^^^^ Description shouldn\'t start with the cask name. + end + RUBY end end diff --git a/Library/Homebrew/test/rubocops/formula_desc_spec.rb b/Library/Homebrew/test/rubocops/formula_desc_spec.rb index e7c7509e41..e3bd25f62a 100644 --- a/Library/Homebrew/test/rubocops/formula_desc_spec.rb +++ b/Library/Homebrew/test/rubocops/formula_desc_spec.rb @@ -104,7 +104,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' desc 'Foo is a foobar' - ^^^^ Description shouldn\'t start with the formula name. + ^^^ Description shouldn\'t start with the formula name. end RUBY end