From 680a8e40900413aac0aec8add34d9a8da5a65bed Mon Sep 17 00:00:00 2001 From: Ulugbek Tuychiev Date: Wed, 8 Apr 2020 17:51:08 +0500 Subject: [PATCH] temporary edit default config file --- config/default.yml | 2 ++ lib/rubocop/rspec/language.rb | 8 +++++++- spec/rubocop/cop/rspec/expect_in_hook_spec.rb | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index 2b1e87b16..7442d77af 100644 --- a/config/default.yml +++ b/config/default.yml @@ -4,6 +4,8 @@ AllCops: Patterns: - _spec.rb - "(?:^|/)spec/" + CustomHooks: + - custom_hook RSpec/FactoryBot: Patterns: - spec/factories.rb diff --git a/lib/rubocop/rspec/language.rb b/lib/rubocop/rspec/language.rb index 60e182a82..2feb5d8b7 100644 --- a/lib/rubocop/rspec/language.rb +++ b/lib/rubocop/rspec/language.rb @@ -83,7 +83,7 @@ module Examples end module Hooks - ALL = SelectorSet.new( + BUILTIN = SelectorSet.new( %i[ prepend_before before @@ -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[ diff --git a/spec/rubocop/cop/rspec/expect_in_hook_spec.rb b/spec/rubocop/cop/rspec/expect_in_hook_spec.rb index 47593c54a..6ff61e013 100644 --- a/spec/rubocop/cop/rspec/expect_in_hook_spec.rb +++ b/spec/rubocop/cop/rspec/expect_in_hook_spec.rb @@ -76,4 +76,22 @@ end RUBY end + + 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