Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from rwstauner/rwstauner/assert-raises-regexp…
…-literal Add cop to prevent regexp literals as arguments to assert_matches
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#179](https://github.com/rubocop/rubocop-minitest/pull/179): New `Minitest/AssertRaisesWithRegexpArgument` cop checks for regular expression literals passed to `assert_raises`. ([@rwstauner][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
lib/rubocop/cop/minitest/assert_raises_with_regexp_argument.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Checks for `assert_raises` with arguments of regular expression literals. | ||
# Arguments should be exception classes. | ||
# Optionally the last argument can be a custom message string to help explain failures. | ||
# Either way, it's not the argument that `exception.message` is compared to. | ||
# The raised exception is returned and can be used | ||
# to match against a regular expression. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# assert_raises FooError, /some message/ do | ||
# obj.occur_error | ||
# end | ||
# | ||
# # good | ||
# exception = assert_raises FooError do | ||
# obj.occur_error | ||
# end | ||
# assert_match(/some message/, exception.message) | ||
# | ||
class AssertRaisesWithRegexpArgument < Base | ||
MSG = 'Do not pass regular expression literals to `assert_raises`. Test the resulting exception.' | ||
RESTRICT_ON_SEND = %i[assert_raises].freeze | ||
|
||
def on_send(node) | ||
add_offense(node) if node.last_argument&.regexp_type? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/rubocop/cop/minitest/assert_raises_with_regexp_argument_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertRaisesWithRegexpArgumentTest < Minitest::Test | ||
def test_registers_offense_with_regexp | ||
assert_offense(<<~RUBY) | ||
assert_raises(MyError, /some message/) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not pass regular expression literals to `assert_raises`. Test the resulting exception. | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_with_other_args | ||
assert_no_offenses(<<~RUBY) | ||
assert_raises(MyError, SomeOtherError, some_local_var) do | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_with_single_arg | ||
assert_no_offenses(<<~RUBY) | ||
assert_raises(MyError) do | ||
foo | ||
end | ||
RUBY | ||
end | ||
end |