Skip to content

Commit

Permalink
Merge pull request #179 from rwstauner/rwstauner/assert-raises-regexp…
Browse files Browse the repository at this point in the history
…-literal

Add cop to prevent regexp literals as arguments to assert_matches
  • Loading branch information
koic committed Sep 3, 2022
2 parents babc4c0 + 788f4ce commit bfeac82
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_cop_to_prevent_regexp_literals_as.md
@@ -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][])
5 changes: 5 additions & 0 deletions config/default.yml
Expand Up @@ -82,6 +82,11 @@ Minitest/AssertRaisesCompoundBody:
Enabled: pending
VersionAdded: '0.21'

Minitest/AssertRaisesWithRegexpArgument:
Description: 'This cop enforces checks for regular expression literals passed to `assert_raises`.'
Enabled: pending
VersionAdded: '<<next>>'

Minitest/AssertRespondTo:
Description: 'This cop enforces the test to use `assert_respond_to(object, :do_something)` over `assert(object.respond_to?(:do_something))`.'
StyleGuide: 'https://minitest.rubystyle.guide#assert-responds-to-method'
Expand Down
36 changes: 36 additions & 0 deletions lib/rubocop/cop/minitest/assert_raises_with_regexp_argument.rb
@@ -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
1 change: 1 addition & 0 deletions lib/rubocop/cop/minitest_cops.rb
Expand Up @@ -12,6 +12,7 @@
require_relative 'minitest/assert_in_delta'
require_relative 'minitest/assert_predicate'
require_relative 'minitest/assert_raises_compound_body'
require_relative 'minitest/assert_raises_with_regexp_argument'
require_relative 'minitest/assert_with_expected_argument'
require_relative 'minitest/assertion_in_lifecycle_hook'
require_relative 'minitest/assert_kind_of'
Expand Down
@@ -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

0 comments on commit bfeac82

Please sign in to comment.