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

feat: support correcting *_in_delta assertions in RSpec/Rails/MinitestAssertions #1791

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Jan 26, 2024

This is branched off #1790, showing how easy it is to now add a new assertion matcher - until the prior PR is merged, the last commit of this PR should be looked at for review.

related to rubocop/rubocop-rspec_rails#7


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

].freeze

NODE_MATCHER_PATTERN = <<~PATTERN
(send nil? {:assert_in_delta :assert_not_in_delta :refute_in_delta} $_ $_ $!{sym str}? $_?)
Copy link
Member

Choose a reason for hiding this comment

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

Message can be a proc:

# File lib/minitest/unit.rb, line 196
    def assert test, msg = nil
      msg ||= "Failed assertion, no message given."
      self._assertions += 1
      unless test then
        msg = msg.call if Proc === msg
        raise MiniTest::Assertion, msg
      end
      true
    end
    ```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not unique to this though, and none of our code depends on that - we're just copying the source code as is (you might be mentioning this because we're doing !failure_message.empty? for this assertion, but that is checking against an array which contains the matched nodes).

We should have tests for this but since it applies for every assertion we currently have I'll do it in a dedicated PR

Copy link
Member

Choose a reason for hiding this comment

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

I’m most concerned (see my earlier comments below), is that we assume that the message (str/sym) can be passed as the third argument, and in this case, skip matching. But this would be incorrect for Minitest.
My point is to disregard the type, and always consider the third argument to be the delta (optional), and the fourth - the message (optional, no type constraints.

I agree that we’d better investigate proc messages in a separate pr. Wondering if RSpec provides a lazily evaluated failure message and we could autocorrect those.
If it can’t - we need to skip autocorrection.

Copy link
Member

Choose a reason for hiding this comment

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

Good news that RSpec does seem to support proc failure messages.. I would still double check this.

@G-Rath G-Rath marked this pull request as ready for review January 29, 2024 18:18
@G-Rath G-Rath requested a review from a team as a code owner January 29, 2024 18:18
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you! What a streak!

@pirj pirj requested a review from a team February 2, 2024 20:59
@pirj pirj merged commit 31c6344 into rubocop:master Feb 2, 2024
24 checks passed
@G-Rath G-Rath deleted the support-delta branch February 2, 2024 21:32
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.

3 participants