Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
126 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,41 @@ | ||
# 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 | ||
# all(:all) { nil } | ||
# | ||
# # good | ||
# before { create_users } | ||
# after do; cleanup_users; end | ||
# before(:all) do | ||
# create_feed | ||
# end | ||
# all(:all) { cleanup_feed } | ||
class EmptyHook < Cop | ||
MSG = 'Empty hook detected.' | ||
|
||
def_node_matcher :empty_hook?, <<~PATTERN | ||
(block | ||
$(send nil? {:before :after} | ||
(sym {:each :all :suite})?) | ||
_ {(:nil) nil?}) | ||
PATTERN | ||
|
||
def on_block(node) | ||
empty_hook?(node) do |hook| | ||
add_offense(hook) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rspec::EmptyHook do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
shared_examples 'empty hook' do |hook_type| | ||
it 'registers offense with empty block' do | ||
expect_offense(<<~RUBY) | ||
#{hook_type} {} | ||
#{'^' * hook_type.length} Empty hook detected. | ||
RUBY | ||
end | ||
|
||
it 'registers offense with empty single-line do-end' do | ||
expect_offense(<<~RUBY) | ||
#{hook_type} do; end | ||
#{'^' * hook_type.length} Empty hook detected. | ||
RUBY | ||
end | ||
|
||
it 'registers offense with empty multi-line do-end' do | ||
expect_offense(<<~RUBY) | ||
#{hook_type} do | ||
#{'^' * hook_type.length} Empty hook detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense with empty single-line nil' do | ||
expect_offense(<<~RUBY) | ||
#{hook_type} { nil } | ||
#{'^' * hook_type.length} Empty hook detected. | ||
RUBY | ||
end | ||
|
||
it 'registers offense with empty multi-line nil' do | ||
expect_offense(<<~RUBY) | ||
#{hook_type} do | ||
#{'^' * hook_type.length} Empty hook detected. | ||
nil | ||
end | ||
RUBY | ||
end | ||
|
||
it 'allows single-line non-empty blocks' do | ||
expect_no_offenses(<<~RUBY) | ||
#{hook_type} { create_users } | ||
RUBY | ||
end | ||
|
||
it 'allows multi-line non-empty blocks' do | ||
expect_no_offenses(<<~RUBY) | ||
#{hook_type} do | ||
create_users | ||
create_businesses | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with before hooks' do | ||
it_behaves_like 'empty hook', 'before' | ||
it_behaves_like 'empty hook', 'before(:each)' | ||
it_behaves_like 'empty hook', 'before(:suite)' | ||
it_behaves_like 'empty hook', 'before(:all)' | ||
end | ||
|
||
context 'with after hooks' do | ||
it_behaves_like 'empty hook', 'after' | ||
it_behaves_like 'empty hook', 'after(:each)' | ||
it_behaves_like 'empty hook', 'after(:suite)' | ||
it_behaves_like 'empty hook', 'after(:all)' | ||
end | ||
end |