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 #169] Add new Minitest/SkipEnsure cop #171

Merged
merged 1 commit into from
May 27, 2022

Conversation

koic
Copy link
Member

@koic koic commented May 20, 2022

Closes #169.

This cop checks that ensure call even if skip. It is unexpected that ensure will be called when skipping test.
If conditional skip is used, it checks that ensure is also called conditionally.

On the other hand, it accepts skip used in rescue because ensure may be teardown process to begin setup process.

# bad
def test_skip
  skip 'This test is skipped.'

  assert 'foo'.present?
ensure
  do_something
end

# bad
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  do_teardown
end

# good
def test_skip
  skip 'This test is skipped.'

  begin
    assert 'foo'.present?
  ensure
    do_something
  end
end

# good
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  if condition
    do_teardown
  end
end

# good
def test_skip_is_used_in_rescue
  do_setup
  assert do_something
rescue
  skip 'This test is skipped.'
ensure
  do_teardown
end

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • 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.

@koic koic force-pushed the add_new_ensure_call_even_if_skip_cop branch 4 times, most recently from 8e4e7a2 to 5ddc634 Compare May 20, 2022 08:19
@koic koic force-pushed the add_new_ensure_call_even_if_skip_cop branch 2 times, most recently from 36b2ae5 to 1843925 Compare May 22, 2022 07:15
@koic
Copy link
Member Author

koic commented May 24, 2022

@yahonda I'm worried about the cop name. It may be abstract, but the cop name Minitest/SkipEnsure cop is simple name.
I would like to hear because you are the author of the issue.
Which of Minitest/EnsureCallEvenIfSkip cop or Minitest/SkipEnsure cop is easier to understand as a user? Of course there may be even better names. If you have any ideas, please share it.

@yahonda
Copy link
Contributor

yahonda commented May 24, 2022

Let me have time to think about the cop's name.

@yahonda
Copy link
Contributor

yahonda commented May 24, 2022

I like Minitest/SkipEnsure than Minitest/EnsureCallEvenIfSkip because it is short.
and how about Minitest/EnsureNotSkipped ?

@koic
Copy link
Member Author

koic commented May 25, 2022

Thank you for your feedback and suggestion. I'll take the simplest Minitest/SkipEnsure, including the suggested name.

Closes rubocop#169.

This cop checks that `ensure` call even if `skip`. It is unexpected
that `ensure` will be called when skipping test.
If conditional `skip` is used, it checks that `ensure` is also called
conditionally.

On the other hand, it accepts `skip` used in `rescue` because `ensure`
may be teardown process to `begin` setup process.

```ruby
# bad
def test_skip
  skip 'This test is skipped.'

  assert 'foo'.present?
ensure
  do_something
end

# bad
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  do_teardown
end

# good
def test_skip
  skip 'This test is skipped.'

  begin
    assert 'foo'.present?
  ensure
    do_something
  end
end

# good
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  if condition
    do_teardown
  end
end

# good
def test_skip_is_used_in_rescue
  do_setup
  assert do_something
rescue
  skip 'This test is skipped.'
ensure
  do_teardown
end
```
@koic koic force-pushed the add_new_ensure_call_even_if_skip_cop branch from 1843925 to 01feafd Compare May 25, 2022 06:11
@koic koic changed the title [Fix #169] Add new Minitest/EnsureCallEvenIfSkip cop [Fix #169] Add new Minitest/SkipEnsure cop May 25, 2022
@koic koic merged commit b6e4b5d into rubocop:master May 27, 2022
@koic koic deleted the add_new_ensure_call_even_if_skip_cop branch May 27, 2022 15:50
@koic
Copy link
Member Author

koic commented May 28, 2022

@yahonda RuboCop Minitest 0.20 has been released.

@yahonda
Copy link
Contributor

yahonda commented May 29, 2022

Thanks for implementing Minitest/SkipEnsure cop and releasing RuboCop Minitest 0.20. Let me take a look at this and open a pull request to rails/rails.

@yahonda
Copy link
Contributor

yahonda commented May 30, 2022

Opened rails/rails#45216

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.

Cop which detects ensure is called even if it is skipped
2 participants