forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_path_exists.rb
56 lines (49 loc) · 1.57 KB
/
assert_path_exists.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
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Enforces the test to use `assert_path_exists` instead of using `assert(File.exist?(path))`.
#
# @example
# # bad
# assert(File.exist?(path))
# assert(File.exist?(path), 'message')
#
# # good
# assert_path_exists(path)
# assert_path_exists(path, 'message')
#
class AssertPathExists < Base
extend AutoCorrector
MSG = 'Prefer using `%<good_method>s`.'
RESTRICT_ON_SEND = %i[assert].freeze
def_node_matcher :assert_file_exists, <<~PATTERN
(send nil? :assert
(send
(const _ :File) {:exist? :exists?} $_)
$...)
PATTERN
def on_send(node)
assert_file_exists(node) do |path, failure_message|
failure_message = failure_message.first
good_method = build_good_method(node, path, failure_message)
message = format(MSG, good_method: good_method)
add_offense(node, message: message) do |corrector|
corrector.replace(node.loc.selector, 'assert_path_exists')
corrector.replace(node.first_argument, path.source)
end
end
end
private
def build_good_method(node, path, message)
args = [path.source, message&.source].compact.join(', ')
if node.parenthesized?
"assert_path_exists(#{args})"
else
"assert_path_exists #{args}"
end
end
end
end
end
end