Skip to content

Commit 14f3dd2

Browse files
authored
Merge pull request #2073 from rubocop/1943
Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered
2 parents 0f0fb8b + bfe68bc commit 14f3dd2

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
66
- Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro])
77
- Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis])
8+
- Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered. ([@ydah])
89

910
## 3.5.0 (2025-02-16)
1011

docs/modules/ROOT/pages/cops_rspec.adoc

+9-1
Original file line numberDiff line numberDiff line change
@@ -5524,7 +5524,9 @@ end
55245524
55255525
Checks for setup scattered across multiple hooks in an example group.
55265526
5527-
Unify `before`, `after`, and `around` hooks when possible.
5527+
Unify `before` and `after` hooks when possible.
5528+
However, `around` hooks are allowed to be defined multiple times,
5529+
as unifying them would typically make the code harder to read.
55285530
55295531
[#examples-rspecscatteredsetup]
55305532
=== Examples
@@ -5544,6 +5546,12 @@ describe Foo do
55445546
setup2
55455547
end
55465548
end
5549+
5550+
# good
5551+
describe Foo do
5552+
around { |example| before1; example.call; after1 }
5553+
around { |example| before2; example.call; after2 }
5554+
end
55475555
----
55485556
55495557
[#references-rspecscatteredsetup]

lib/rubocop/cop/rspec/scattered_setup.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ module Cop
55
module RSpec
66
# Checks for setup scattered across multiple hooks in an example group.
77
#
8-
# Unify `before`, `after`, and `around` hooks when possible.
8+
# Unify `before` and `after` hooks when possible.
9+
# However, `around` hooks are allowed to be defined multiple times,
10+
# as unifying them would typically make the code harder to read.
911
#
1012
# @example
1113
# # bad
@@ -22,6 +24,12 @@ module RSpec
2224
# end
2325
# end
2426
#
27+
# # good
28+
# describe Foo do
29+
# around { |example| before1; example.call; after1 }
30+
# around { |example| before2; example.call; after2 }
31+
# end
32+
#
2533
class ScatteredSetup < Base
2634
include FinalEndLocation
2735
include RangeHelp
@@ -48,7 +56,7 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
4856
def repeated_hooks(node)
4957
hooks = RuboCop::RSpec::ExampleGroup.new(node)
5058
.hooks
51-
.select(&:knowable_scope?)
59+
.select { |hook| hook.knowable_scope? && hook.name != :around }
5260
.group_by { |hook| [hook.name, hook.scope, hook.metadata] }
5361
.values
5462
.reject(&:one?)

spec/rubocop/cop/rspec/scattered_setup_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
RUBY
5959
end
6060

61+
it 'ignores around hooks' do
62+
expect_no_offenses(<<~RUBY)
63+
describe Foo do
64+
around { bar }
65+
around { baz }
66+
end
67+
RUBY
68+
end
69+
6170
it 'ignores different hooks' do
6271
expect_no_offenses(<<~RUBY)
6372
describe Foo do

0 commit comments

Comments
 (0)