rubocop-rspec
rubocop-rspec copied to clipboard
Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only
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.mdif 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: pendinginconfig/default.yml. - [ ] The cop is configured as
Enabled: truein.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>>"indefault/config.yml.
If you have modified an existing cop's configuration options:
- [x] Set
VersionChanged: "<<next>>"inconfig/default.yml.
@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.
I rebased and prepped this so it can be ready for the Monthly release
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.
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.
So what happens if you add the following to .rubocop.yml?
RSpec/StringAsInstanceDoubleConstant:
Enabled: false
RSpec/VerifiedDoubleReference:
EnforcedStyle: string
and run rubocop?
So what happens if you add the following to .rubocop.yml?
RSpec/StringAsInstanceDoubleConstant: Enabled: false RSpec/VerifiedDoubleReference: EnforcedStyle: stringand 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
If we used the changed_parameters, this would also result in an error, not a warning, right?
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
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?
Thanks @bquorning -- what about the EnforcedStyle parameter no longer being needed, regardless of value?
Ah, that is true. Perhaps the changed_parameters obsoletion config is better then.
Happy to help, @bquorning -- thanks for the guidance. How does it look now?
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).
I did the same steps as you @pirj, but also had
RSpec/VerifiedDoubleReference:
Enabled: true
EnforcedStyle: constant
And the deprecation warnings work perfectly :+1: