Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AllCops:
Patterns:
- _spec.rb
- "(?:^|/)spec/"
CustomHooks:
- custom_hook
RSpec/FactoryBot:
Patterns:
- spec/factories.rb
Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/rspec/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Examples
end

module Hooks
ALL = SelectorSet.new(
BUILTIN = SelectorSet.new(
%i[
prepend_before
before
Expand All @@ -92,9 +92,15 @@ module Hooks
prepend_after
after
append_after
setup
]
)

custom_hooks = CONFIG.dig('AllCops', 'RSpec', 'CustomHooks') || []
CUSTOM = SelectorSet.new(custom_hooks.map(&:to_sym))

ALL = BUILTIN + CUSTOM

module Scopes
ALL = SelectorSet.new(
%i[
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rspec/expect_in_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,22 @@
end
RUBY
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be used somehow to stub AllCops. I believe we'll have to patch it in the long run to accept arbitrary keypairs (that would also allow moving Rails-related code to rspec-rails).
It's ok to use a copy of it now.

This doesn't solve the issue of ALL being pre-calculated though.
What if we take a non-performant way and define ALL as a method that would return BUILD plus fetch from config?

I vote for postponing the decision on how to cache config and be flexible in the specs at the same time.

it 'adds an offense for `expect` with block in `setup` hook' do
expect_offense(<<-RUBY)
setup do
expect { something }.to eq('foo')
^^^^^^ Do not use `expect` in `setup` hook
end
RUBY
end

it 'adds an offense for `expect` with block in custom `custom_hook` hook' do
expect_offense(<<-RUBY)
custom_hook do
expect { something }.to eq('foo')
^^^^^^ Do not use `expect` in `custom_hook` hook
end
RUBY
end
end