forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip_without_reason.rb
66 lines (55 loc) · 1.52 KB
/
skip_without_reason.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Checks for skipped tests missing the skipping reason.
#
# @example
# # bad
# skip
# skip('')
#
# # bad
# if condition?
# skip
# else
# skip
# end
#
# # good
# skip("Reason why the test was skipped")
#
# # good
# skip if condition?
#
class SkipWithoutReason < Base
MSG = 'Add a reason explaining why the test is skipped.'
RESTRICT_ON_SEND = %i[skip].freeze
def on_send(node)
return if node.receiver || !blank_argument?(node)
conditional_node = conditional_parent(node)
return if conditional_node && !only_skip_branches?(conditional_node)
return if node.parent&.resbody_type?
add_offense(node)
end
private
def blank_argument?(node)
message = node.first_argument
message.nil? || (message.str_type? && message.value == '')
end
def conditional_parent(node)
return unless (parent = node.parent)
if parent.if_type? || parent.case_type?
parent
elsif parent.when_type?
parent.parent
end
end
def only_skip_branches?(node)
branches = node.branches.compact
branches.size > 1 && branches.all? { |branch| branch.send_type? && branch.method?(:skip) }
end
end
end
end
end