Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #897 from tejasbubane/empty-hooks-cop
Add new `RSpec/EmptyHooks` cop
- Loading branch information
Showing
7 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for empty before and after hooks. | ||
# | ||
# @example | ||
# # bad | ||
# before {} | ||
# after do; end | ||
# before(:all) do | ||
# end | ||
# after(:all) { } | ||
# | ||
# # good | ||
# before { create_users } | ||
# after do | ||
# cleanup_users | ||
# end | ||
# before(:all) do | ||
# create_feed | ||
# end | ||
# after(:all) { cleanup_feed } | ||
class EmptyHook < Cop | ||
include RuboCop::Cop::RangeHelp | ||
|
||
MSG = 'Empty hook detected.' | ||
|
||
def_node_matcher :empty_hook?, <<~PATTERN | ||
(block $#{Hooks::ALL.send_pattern} _ nil?) | ||
PATTERN | ||
|
||
def on_block(node) | ||
empty_hook?(node) do |hook| | ||
add_offense(hook) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
block = node.parent | ||
range = range_with_surrounding_space(range: block.loc.expression) | ||
corrector.remove(range) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::EmptyHook do | ||
subject(:cop) { described_class.new } | ||
|
||
context 'with `before` hook' do | ||
it 'detects offense for empty `before`' do | ||
expect_offense(<<~RUBY) | ||
before {} | ||
^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'detects offense for empty `before` with :each scope' do | ||
expect_offense(<<~RUBY) | ||
before(:each) {} | ||
^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'detects offense for empty `before` with :example scope' do | ||
expect_offense(<<~RUBY) | ||
before(:example) {} | ||
^^^^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'detects offense for empty `before` with :context scope' do | ||
expect_offense(<<~RUBY) | ||
before(:context) {} | ||
^^^^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'detects offense for empty `before` with :all scope' do | ||
expect_offense(<<~RUBY) | ||
before(:all) {} | ||
^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'detects offense for empty `before` with :suite scope' do | ||
expect_offense(<<~RUBY) | ||
before(:suite) {} | ||
^^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
before { create_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
before(:all) do | ||
create_users | ||
create_products | ||
end | ||
RUBY | ||
end | ||
|
||
it 'autocorrects `before` with semicolon' do | ||
expect_offense(<<~RUBY) | ||
before {}; after { clean_up(:foo) } | ||
^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
; after { clean_up(:foo) } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `after` hook' do | ||
it 'detects offense for empty `after`' do | ||
expect_offense(<<~RUBY) | ||
after {} | ||
^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
after { cleanup_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
after(:suite) do | ||
cleanup_users | ||
cleanup_products | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `around` hook' do | ||
it 'detects offense for empty `around`' do | ||
expect_offense(<<~RUBY) | ||
around {} | ||
^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `around` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
around { yield } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `around` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
around(:suite) do | ||
setup_users | ||
yield | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `prepend_before` hook' do | ||
it 'detects offense for empty `prepend_before`' do | ||
expect_offense(<<~RUBY) | ||
prepend_before {} | ||
^^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `prepend_before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
prepend_before { create_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `prepend_before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
prepend_before(:all) do | ||
create_users | ||
create_products | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `append_before` hook' do | ||
it 'detects offense for empty `append_before`' do | ||
expect_offense(<<~RUBY) | ||
append_before {} | ||
^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `append_before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
append_before { create_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `append_before` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
append_before(:each) do | ||
create_users | ||
create_products | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `prepend_after` hook' do | ||
it 'detects offense for empty `prepend_after`' do | ||
expect_offense(<<~RUBY) | ||
prepend_after {} | ||
^^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `prepend_after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
prepend_after { cleanup_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `prepend_after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
prepend_after(:all) do | ||
cleanup_users | ||
cleanup_products | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `append_after` hook' do | ||
it 'detects offense for empty `append_after`' do | ||
expect_offense(<<~RUBY) | ||
append_after {} | ||
^^^^^^^^^^^^ Empty hook detected. | ||
RUBY | ||
|
||
expect_correction('') | ||
end | ||
|
||
it 'accepts non-empty `append_after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
append_after { cleanup_users } | ||
RUBY | ||
end | ||
|
||
it 'accepts multiline `append_after` hook' do | ||
expect_no_offenses(<<~RUBY) | ||
append_after(:all) do | ||
cleanup_users | ||
cleanup_products | ||
end | ||
RUBY | ||
end | ||
end | ||
end |