forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rubocop#254 from backus/feature/SeparatedHooks
Add ScatteredSetup cop
- Loading branch information
Showing
10 changed files
with
309 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for setup scattered across multiple hooks in an example group. | ||
# | ||
# Unify `before`, `after`, and `around` hooks when possible. | ||
# | ||
# @example | ||
# # bad | ||
# describe Foo do | ||
# before { setup1 } | ||
# before { setup2 } | ||
# end | ||
# | ||
# # good | ||
# describe Foo do | ||
# before do | ||
# setup1 | ||
# setup2 | ||
# end | ||
# end | ||
# | ||
class ScatteredSetup < Cop | ||
MSG = 'Do not define multiple hooks in the same example group.'.freeze | ||
|
||
def on_block(node) | ||
return unless example_group?(node) | ||
|
||
analyzable_hooks(node).each do |repeated_hook| | ||
add_offense(repeated_hook, :expression) | ||
end | ||
end | ||
|
||
def analyzable_hooks(node) | ||
RuboCop::RSpec::ExampleGroup.new(node) | ||
.hooks | ||
.select { |hook| hook.knowable_scope? && hook.valid_scope? } | ||
.group_by { |hook| [hook.name, hook.scope] } | ||
.values | ||
.reject(&:one?) | ||
.flatten | ||
.map(&:to_node) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
# Wrapper for RSpec hook | ||
class Hook < Concept | ||
STANDARDIZED_SCOPES = %i(each context suite).freeze | ||
private_constant(:STANDARDIZED_SCOPES) | ||
|
||
def name | ||
node.method_name | ||
end | ||
|
||
def knowable_scope? | ||
return true unless scope_argument | ||
|
||
scope_argument.sym_type? | ||
end | ||
|
||
def valid_scope? | ||
STANDARDIZED_SCOPES.include?(scope) | ||
end | ||
|
||
def scope | ||
case scope_name | ||
when nil, :each, :example then :each | ||
when :context, :all then :context | ||
when :suite then :suite | ||
else | ||
scope_name | ||
end | ||
end | ||
|
||
private | ||
|
||
def scope_name | ||
scope_argument.to_a.first | ||
end | ||
|
||
def scope_argument | ||
node.method_args.first | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
# Wrapper for RSpec DSL methods | ||
class Concept | ||
include Language | ||
extend NodePattern::Macros | ||
|
||
def initialize(node) | ||
@node = node | ||
end | ||
|
||
def eql?(other) | ||
node.eql?(other.node) | ||
end | ||
|
||
alias == eql? | ||
|
||
def hash | ||
[self.class, node].hash | ||
end | ||
|
||
def to_node | ||
node | ||
end | ||
|
||
protected | ||
|
||
attr_reader :node | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
describe RuboCop::Cop::RSpec::ScatteredSetup do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'flags multiple hooks in the same example group' do | ||
expect_violation(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
before { baz } | ||
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'flags multiple hooks of the same scope with different symbols' do | ||
expect_violation(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
before(:each) { baz } | ||
^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
before(:example) { baz } | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'flags multiple before(:all) hooks in the same example group' do | ||
expect_violation(<<-RUBY) | ||
describe Foo do | ||
before(:all) { bar } | ||
^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
before(:all) { baz } | ||
^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not flag different hooks' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
after { baz } | ||
around { qux } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not flag different hook types' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
before(:all) { baz } | ||
before(:suite) { baz } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not flag hooks in different example groups' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
describe '.baz' do | ||
before { baz } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not flag hooks in different shared contexts' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
shared_context 'one' do | ||
before { bar } | ||
end | ||
shared_context 'two' do | ||
before { baz } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not flag similar method names inside of examples' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
before { bar } | ||
it 'uses an instance method called before' do | ||
expect(before { tricky }).to_not confuse_rubocop_rspec | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
Oops, something went wrong.