Skip to content

Commit

Permalink
Fix a false positive for Minitest/EmptyLineBeforeAssertionMethods
Browse files Browse the repository at this point in the history
Follow up #189 (comment).

This PR fixes a false positive for `Minitest/EmptyLineBeforeAssertionMethods`
when using `rescue` before assertion method.
  • Loading branch information
koic committed Nov 3, 2022
1 parent 62abedc commit e3b4936
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#189](https://github.com/rubocop/rubocop-minitest/issues/189): Fix a false positive for `Minitest/EmptyLineBeforeAssertionMethods` when using `rescue` before assertion method. ([@koic][])
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EmptyLineBeforeAssertionMethods < Base
def on_send(node)
return unless (assertion_method = assertion_method(node))
return unless (previous_line_node = assertion_method.left_sibling)
return unless previous_line_node.is_a?(RuboCop::AST::Node)
return if node.parent.resbody_type?
return if accept_previous_line?(previous_line_node, assertion_method)

previous_line_node = previous_line_node.arguments.last if use_heredoc_argument?(previous_line_node)
Expand All @@ -45,7 +45,8 @@ def assertion_method(node)
end

def accept_previous_line?(previous_line_node, node)
return true if previous_line_node.args_type? || node.parent.basic_conditional?
return true if !previous_line_node.is_a?(RuboCop::AST::Node) ||
previous_line_node.args_type? || node.parent.basic_conditional?

previous_line_node.send_type? && assertion_method?(previous_line_node)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_statement_before_assertion_method_used_in_rescue
assert_offense(<<~'RUBY')
def test_do_something
yaml_load_paths.each do |path|
YAML.load_file(path)
rescue Psych::Exception => e
do_something
flunk("Error loading #{path}: #{e.inspect}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion.
end
end
RUBY

assert_correction(<<~'RUBY')
def test_do_something
yaml_load_paths.each do |path|
YAML.load_file(path)
rescue Psych::Exception => e
do_something
flunk("Error loading #{path}: #{e.inspect}")
end
end
RUBY
end

def test_does_not_register_offense_when_using_empty_line_before_assertion_methods
assert_no_offenses(<<~RUBY)
def test_do_something
Expand Down Expand Up @@ -238,6 +264,18 @@ def test_do_something
RUBY
end

def test_does_not_register_offense_when_using_rescue_before_assertion_method
assert_no_offenses(<<~'RUBY')
def test_do_something
yaml_load_paths.each do |path|
YAML.load_file(path)
rescue Psych::Exception => e
flunk("Error loading #{path}: #{e.inspect}")
end
end
RUBY
end

def test_does_not_register_offense_when_using_only_non_assertion_method
assert_no_offenses(<<~RUBY)
do_something(thing)
Expand Down

0 comments on commit e3b4936

Please sign in to comment.