Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #133] Add new Minitest/UnreachableAssertion cop #137

Merged

Conversation

koic
Copy link
Member

@koic koic commented Jun 23, 2021

Fixes #133.

This PR adds new Minitest/UnreachableAssertion cop.

This cop checks for assert_raises has an assertion method at the bottom of block because the assertion will be never reached.

# bad
assert_raises FooError do
  obj.occur_error
  assert_equal('foo', obj.bar) # Never reached.
end

# good
assert_raises FooError do
  obj.occur_error
end
assert_equal('foo', obj.bar)

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the add_new_minitest_assertion_in_assert_raises_cop branch 3 times, most recently from a3f0387 to b2bd8c6 Compare June 23, 2021 08:31
config/default.yml Outdated Show resolved Hide resolved
@koic koic force-pushed the add_new_minitest_assertion_in_assert_raises_cop branch 2 times, most recently from c14cb6e to ce6d72e Compare June 24, 2021 01:54
config/default.yml Outdated Show resolved Hide resolved
@koic koic force-pushed the add_new_minitest_assertion_in_assert_raises_cop branch from ce6d72e to 2c6446e Compare June 24, 2021 02:46
Fixes rubocop#133.

This PR adds new `Minitest/UnreachableAssertion` cop.

This cop checks for `assert_raises` has an assertion method at
the bottom of block because the assertion will be never reached.

```ruby
# bad
assert_raises FooError do
  obj.occur_error
  assert_equal('foo', obj.bar) # Never reached.
end

# good
assert_raises FooError do
  obj.occur_error
end
assert_equal('foo', obj.bar)
```
@koic koic force-pushed the add_new_minitest_assertion_in_assert_raises_cop branch from 2c6446e to 73d2969 Compare June 24, 2021 03:13
Copy link
Contributor

@andyw8 andyw8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@koic koic merged commit 1be3e3c into rubocop:master Jun 26, 2021
@koic koic deleted the add_new_minitest_assertion_in_assert_raises_cop branch June 26, 2021 18:51
koic added a commit to koic/rails that referenced this pull request Aug 17, 2021
I found an unexpected use of assertion in the block of `assert_raise`
when I implemented rubocop/rubocop-minitest#137.
It is expected to be asserted after an exception is raised in
`assert_raise` block, but in actually it is not asserted after an
exception is raised. Therefore, this PR removes or updates assertions
that have not been asserted after an exception has raised.

This PR will add `rubocop-minitest` and enable
`Minitest/UnreachableAssertion` cop to able similar auto-detection,
but will remove `rubocop-minitest` from this PR if you don't like it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New cop suggestion: Detect invalid assertions within an assert_raises block
2 participants