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: Fix false negative for RSpec/Dialect when specified Capybara-specific methods #1952

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sanfrecce-osaka
Copy link

fixed #1951

RuboCop::Cop::RSpec::Dialect#rspec_method? detects if node's second child is an RSpec block using RuboCop::RSpec::Language::ALL.all. However, RuboCop::Cop::RSpec::Dialect#rspec_method? returned nil because the following Capybara-specific methods were not set in config/default.yml.

  • given
  • given!
  • background

I remove the line that checks if it’s a rspec_method? and add a inside_example_group? check.

cf. #1951 (comment)

Due to the above, it wouldn't filter out are calls with an explicit receiver where the receiver is not RSpec, eg foo.describe. so the following examples were removed.

  • allows calling methods named xxx in examples

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.

…pecific methods

fixed rubocop#1951

`RuboCop::Cop::RSpec::Dialect#rspec_method?` detects if node's second child is an RSpec block using `RuboCop::RSpec::Language::ALL.all`. However, `RuboCop::Cop::RSpec::Dialect#rspec_method?` returned nil because the following Capybara-specific methods were not set in config/default.yml.

- given
- given!
- background

I remove the line that checks if it’s a `rspec_method?` and add a `inside_example_group?` check.

cf. rubocop#1951 (comment)

Due to the above, it wouldn't filter out are calls with an explicit receiver where the receiver is not RSpec, eg `foo.describe`. so the following examples were removed.

- allows calling methods named xxx in examples
@sanfrecce-osaka sanfrecce-osaka requested a review from a team as a code owner August 26, 2024 12:18
@sanfrecce-osaka
Copy link
Author

Should I also change the following document as it no longer checks for RuboCop::RSpec::Language::ALL.all?

# A dialect can be based on the following RSpec methods:
#
# - describe, context, feature, example_group
# - xdescribe, xcontext, xfeature
# - fdescribe, fcontext, ffeature
# - shared_examples, shared_examples_for, shared_context
# - it, specify, example, scenario, its
# - fit, fspecify, fexample, fscenario, focus
# - xit, xspecify, xexample, xscenario, skip
# - pending
# - prepend_before, before, append_before,
# - around
# - prepend_after, after, append_after
# - let, let!
# - subject, subject!
# - expect, is_expected, expect_any_instance_of

@pirj
Copy link
Member

pirj commented Aug 26, 2024

change the following document

Maybe. I find it hard to understand what the “based” means in “dialect can be based on the following RSpec methods”, though.

Can you think of a simpler description?

RSpec.context 'context' do
^^^^^^^^^^^^^^^^^^^^^^^ Prefer `describe` over `context`.
it 'tests common context invocations' do
expect(request.context).to be_empty?
Copy link
Member

Choose a reason for hiding this comment

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

Those ones were here to ensure that context with an explicit receiver is not detected. Can you keep it in some way?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for the late reply 🙏

Those ones were here to ensure that context with an explicit receiver is not detected. Can you keep it in some way?

In fa5da43 the check using rspec_method? has been removed, so explicit receivers are also detected. To avoid detection, I can't think of any ideas other than to do the following.

          def on_send(node)
            return unless inside_example_group?(node)
+           return unless rspec?(node.children.first)
            return unless preferred_methods[node.method_name]

Do you have any good ideas?

@ydah
Copy link
Member

ydah commented Oct 8, 2024

@sanfrecce-osaka ping ;)

@sanfrecce-osaka
Copy link
Author

#1952 (comment)

change the following document

Maybe. I find it hard to understand what the “based” means in “dialect can be based on the following RSpec methods”, though.

Can you think of a simpler description?

How about the following?

-       # Enforces custom RSpec dialects.
+       # Enforces custom RSpec dialects based on your config.
        #
-       # A dialect can be based on the following RSpec methods:
+       # `Dialects` are the following examples:
        #
        # - describe, context, feature, example_group
        # - xdescribe, xcontext, xfeature
        # - fdescribe, fcontext, ffeature
        # - shared_examples, shared_examples_for, shared_context
        # - it, specify, example, scenario, its
        # - fit, fspecify, fexample, fscenario, focus
        # - xit, xspecify, xexample, xscenario, skip
        # - pending
        # - prepend_before, before, append_before,
        # - around
        # - prepend_after, after, append_after
        # - let, let!
        # - subject, subject!
        # - expect, is_expected, expect_any_instance_of
        #
        # By default all of the RSpec methods and aliases are allowed. By setting
        # a config like:
        #
        #   RSpec/Dialect:
        #     PreferredMethods:
        #       context: describe

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.

RSpec/Dialect causes a false negative when using below setting mentioned in the documentation
3 participants