Including the AssertOffense module currently fails with a cryptic message if the cop can't be found. This PR introduces an explicit failure with a useful message, replacing the NoMethodError that is currently raised in this situation.
Before
Error:
RuboCop::Cop::MyCops::MyNewCopTest#test_works:
NoMethodError: undefined method `[]=' for nil
/Users/brandoncc/.gem/ruby/3.3.1/gems/rubocop-minitest-0.35.1/lib/rubocop/minitest/assert_offense.rb:109:in `assert_offense'
test/rubocop/cop/my_cops/my_new_cop_test.rb:12:in `block in <class:MyNewCopTest>'
After
Error:
RuboCop::Cop::MyCops::MyNewCopTest#test_works:
RuntimeError: Cop not defined: RuboCop::Cop::MyCops::MyNewCop
/Users/brandoncc/dev/rubocop-minitest/lib/rubocop/minitest/assert_offense.rb:81:in `cop'
/Users/brandoncc/dev/rubocop-minitest/lib/rubocop/minitest/assert_offense.rb:111:in `assert_offense'
test/rubocop/cop/my_cops/my_new_cop_test.rb:12:in `block in <class:MyNewCopTest>'
The second implementation refactors the initialization of a Cop to be lazy, so that the error is only raised when the cop is attempted to be accessed. This has the downside of @cop being nil until #cop is called, but these failures will only happen in tests where @cop is accessed directly. I believe these failures, if experienced by consumers of the gem, will be easier to resolve than the errors likely to have been introduced by my first implementation.
Before submitting the PR make sure the following are checked:
[x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
[ ] Commit message starts with [Fix #issue-number] (if the related issue exists).
[x] Feature branch is up-to-date with master (if not - rebase it).
[x] Squashed related commits together.
[x] Added tests.
[x] Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
[x] Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
Including the
AssertOffense
module currently fails with a cryptic message if the cop can't be found. This PR introduces an explicit failure with a useful message, replacing theNoMethodError
that is currently raised in this situation.Before
After
My first implementation simply changed
AssertOffense#setup
to fail if the cop wasn't found, but that broke four tests in the test suite. Given that the advice given is to requiresupport.rb
, which includesAssertOffense
on every instance ofMinitest::Test
, I realized that implementation was likely to break many gem-consumer repositories in this same way.The second implementation refactors the initialization of a Cop to be lazy, so that the error is only raised when the cop is attempted to be accessed. This has the downside of
@cop
beingnil
until#cop
is called, but these failures will only happen in tests where@cop
is accessed directly. I believe these failures, if experienced by consumers of the gem, will be easier to resolve than the errors likely to have been introduced by my first implementation.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.