-
-
Notifications
You must be signed in to change notification settings - Fork 277
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 #1133 from paydaylight/add-prefer-before-over-setu…
…p-cop Add new RSpec/AvoidSetupHook cop
- Loading branch information
Showing
7 changed files
with
121 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
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Rails | ||
# Checks that tests use RSpec `before` hook over Rails `setup` method. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# setup do | ||
# allow(foo).to receive(:bar) | ||
# end | ||
# | ||
# # good | ||
# before do | ||
# allow(foo).to receive(:bar) | ||
# end | ||
# | ||
class AvoidSetupHook < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `before` instead of `setup`.' | ||
|
||
# @!method setup_call(node) | ||
def_node_matcher :setup_call, <<-PATTERN | ||
(block | ||
$(send nil? :setup) | ||
(args) _) | ||
PATTERN | ||
|
||
def on_block(node) | ||
setup_call(node) do |setup| | ||
add_offense(node) do |corrector| | ||
corrector.replace setup, 'before' | ||
end | ||
end | ||
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Rails::AvoidSetupHook do | ||
it 'registers an offense for `setup`' do | ||
expect_offense(<<~RUBY) | ||
setup do | ||
^^^^^^^^ Use `before` instead of `setup`. | ||
allow(foo).to receive(:bar) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before do | ||
allow(foo).to receive(:bar) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `before`' do | ||
expect_no_offenses(<<~RUBY) | ||
before do | ||
allow(foo).to receive(:bar) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for an unrelated `setup` call' do | ||
expect_no_offenses(<<~RUBY) | ||
navigation.setup do | ||
direction 'to infinity!' | ||
end | ||
RUBY | ||
end | ||
end |