rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only

Open corsonknowles opened this issue 1 year ago • 1 comments

This PR obsoletes our newly introduced StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference.

This changes VerifiedDoubleReference to no longer support string style, as string references are not verifying when the class has not yet been loaded.

From the RSpec docs:

No checking will happen if the underlying object or class is not defined, but when run with it present (either as a full spec run or by explicitly preloading collaborators) a failure will be triggered if an invalid method is being stubbed or a method is called with an invalid number of arguments.

  • https://rspec.info/features/3-12/rspec-mocks/verifying-doubles/

This PR essentially reverts nearly all of

  • https://github.com/rubocop/rubocop-rspec/pull/1957

This uses the Obsoletion feature to point leading edge branch users to a revised VerifiedDoubleReference instead.


Before submitting the PR make sure the following are checked:

  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] Updated documentation.
  • [x] Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • [x] 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:

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

corsonknowles avatar Oct 13 '24 10:10 corsonknowles

@pirj @bquorning Does this look like the right approach to you as outlined in the comments here?

  • https://github.com/rubocop/rubocop-rspec/pull/1957

@lucthev also tagging you as a courtesy since you identified the duplication here.

corsonknowles avatar Oct 13 '24 10:10 corsonknowles

I rebased and prepped this so it can be ready for the Monthly release

corsonknowles avatar Nov 16 '24 14:11 corsonknowles

The code looks good, but I am not sure if removing string from SupportedStyles in RSpec/VerifiedDoubleReference is enough of a breaking change that we should wait until v4? @pirj I think (/hope) you know the major/minor release rules better than I.

bquorning avatar Nov 16 '24 22:11 bquorning

I’d check if we can avoid errors by using the obsoletion config. If removal of the option errors out, it should go to v4. But even if the above would error out, we can soft-deprecate the option by ignoring it, or printing a warning if it’s detected to use the deprecated style. It’s a hack, but worth it to push this out earlier.

pirj avatar Nov 17 '24 09:11 pirj

So what happens if you add the following to .rubocop.yml?

RSpec/StringAsInstanceDoubleConstant:
  Enabled: false

RSpec/VerifiedDoubleReference:
  EnforcedStyle: string

and run rubocop?

pirj avatar Nov 17 '24 09:11 pirj

So what happens if you add the following to .rubocop.yml?

RSpec/StringAsInstanceDoubleConstant:
  Enabled: false

RSpec/VerifiedDoubleReference:
  EnforcedStyle: string

and run rubocop?

I tried that and first got this error:

Error: The `RSpec/StringAsInstanceDoubleConstant` cop has been renamed to `RSpec/VerifiedDoubleReference`.
(obsolete configuration found in .rubocop.yml, please update it)

I removed the obsolete configuration and ran rubocop again, this time getting a successful output, but got a warning:

Warning: RSpec/VerifiedDoubleReference does not support EnforcedStyle parameter.

Supported parameters are:

  - Enabled

bquorning avatar Nov 17 '24 17:11 bquorning

If we used the changed_parameters, this would also result in an error, not a warning, right?

pirj avatar Nov 17 '24 21:11 pirj

Both changed_parameters and changed_enforced_styles can be configures with a severity, so we could do e.g.

changed_enforced_styles:
  - cops: RSpec/VerifiedDoubleReference
    parameters: EnforcedStyle
    value: string
    reason: String references are not verifying unless the class is loaded.
    severity: warning

Of course, users will now get two warnings instead of one:

Warning: obsolete `EnforcedStyle: string` (for `RSpec/VerifiedDoubleReference`) found in .rubocop.yml
String references are not verifying unless the class is loaded.
Warning: RSpec/VerifiedDoubleReference does not support EnforcedStyle parameter.

Supported parameters are:

  - Enabled

bquorning avatar Nov 17 '24 21:11 bquorning

I think the correct thing to do is to add a changed_enforced_styles entry into config/obsoletion.yml. That RuboCop chooses to emit two different warnings is perhaps a bug, but I can live with that.

@corsonknowles Do you want to add this last change?

bquorning avatar Nov 20 '24 10:11 bquorning

Thanks @bquorning -- what about the EnforcedStyle parameter no longer being needed, regardless of value?

corsonknowles avatar Nov 20 '24 17:11 corsonknowles

Ah, that is true. Perhaps the changed_parameters obsoletion config is better then.

bquorning avatar Nov 20 '24 18:11 bquorning

Happy to help, @bquorning -- thanks for the guidance. How does it look now?

corsonknowles avatar Jan 10 '25 00:01 corsonknowles

Side observation: this wasn't trivial to check out locally, or is it just me.

git checkout corsonknowles-master
git pull [email protected]:corsonknowles/rubocop-rspec.git master

I wouldn't be certain how to push amendments to the PR (I wanted to fix the CHANGELOG.md).

pirj avatar Jan 10 '25 10:01 pirj

I did the same steps as you @pirj, but also had

RSpec/VerifiedDoubleReference:
  Enabled: true
  EnforcedStyle: constant

And the deprecation warnings work perfectly :+1:

bquorning avatar Jan 10 '25 10:01 bquorning