/
before_after_all.rb
47 lines (44 loc) · 1.3 KB
/
before_after_all.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Check that before/after(:all) isn't being used.
#
# @example
# # bad
# #
# # Faster but risk of state leaking between examples
# #
# describe MyClass do
# before(:all) { Widget.create }
# after(:all) { Widget.delete_all }
# end
#
# # good
# #
# # Slower but examples are properly isolated
# #
# describe MyClass do
# before(:each) { Widget.create }
# after(:each) { Widget.delete_all }
# end
class BeforeAfterAll < Base
MSG = 'Beware of using `%<hook>s` as it may cause state to leak ' \
'between tests. If you are using `rspec-rails`, and ' \
'`use_transactional_fixtures` is enabled, then records created ' \
'in `%<hook>s` are not automatically rolled back.'
def_node_matcher :before_or_after_all, <<-PATTERN
$(send _ {:before :after} (sym {:all :context}))
PATTERN
def on_send(node)
before_or_after_all(node) do |hook|
add_offense(
node,
message: format(MSG, hook: hook.source)
)
end
end
end
end
end
end