File: upgrade_to_version_2.adoc

package info (click to toggle)
ruby-rubocop-rspec 2.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,892 kB
  • sloc: ruby: 22,283; makefile: 4
file content (273 lines) | stat: -rw-r--r-- 7,951 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
= Upgrade to Version 2.x
:doctype: book

== Configuration File Update

In version 2.x:

 - `RSpec/InvalidPredicateMatcher` cop is removed
 - `CustomIncludeMethods` configuration option for `RSpec/EmptyExampleGroup` is removed
 - cop departments are nested for cops with a department that doesn’t match the extension name (`Capybara`, `FactoryBot`, `Rails`)
 - `AllCops/RSpec/Patterns`/`AllCops/FactoryBot/Patterns` options are removed
 - Calling `super` from `#on_new_investigation` defined in a cop is mandatory now
 - In specs, do not define `cop`

[discrete]
=== Adjust the configuration of `RSpec/EmptyExampleGroup`

[source,yaml]
----
# .rubocop.yml

# Before
RSpec/EmptyExampleGroup:
  CustomIncludeMethods:
    - include_tests

# After
RSpec:
  Language:
    Includes:
      Examples:
        - include_tests
----

=== Add a top-level `RSpec` department

RuboCop extensions had cops with clashing names and departments, e.g. both `rspec-rails` and `rubocop-rspec` had `Rails::HttpStatus` cops.
To avoid issues, e.g. inability to disable just one of the cops, each extension now has its own uber-department.
Expectedly, RuboCop RSpec’s uber-department name is `RSpec`.
Changes are only applied to cops that don’t already have the department set to `RSpec`, i.e. `Capybara`, `FactoryBot` and `Rails`.

[source,yaml]
----
# .rubocop.yml

# Before
Capybara/CurrentPathExpectation:
  Enabled: false

FactoryBot/AttributeDefinedStatically:
  Enabled: false

# remains the same
RSpec/EmptyExampleGroup:
  Enabled: false

# After
RSpec/Capybara/CurrentPathExpectation:
  Enabled: false

RSpec/FactoryBot/AttributeDefinedStatically:
  Enabled: false

# remains the same
RSpec/EmptyExampleGroup:
  Enabled: false
----

https://github.com/rubocop/rubocop/pull/8490[Learn more about this change].


=== Use the RuboCop standard `Include` option to filter inspected files

`Patterns` was a RuboCop RSpec-specific option, and RuboCop has a standard replacement.

[source,yaml]
----
# .rubocop.yml

# Before
AllCops:
  RSpec/FactoryBot:
    Patterns:
      - spec/factories/**/*.rb
      - property/factories/**/*.rb

# After
RSpec/FactoryBot:
  Include:
    - spec/factories/**/*.rb
    - property/factories/**/*.rb
----

NOTE: Please keep in mind that merge mode for `Include` is set to override the default settings, so if you intend to add a path while keeping the default paths, you should include the default `Include` paths in your configuration.

https://github.com/rubocop/rubocop-rspec/pull/1063[Learn more about this change].

== Custom Cop Update Guide

Due to significant API changes, custom cops may break.
Here is the summary of the changes:

1. The base class for cops is now `RuboCop::Cop::RSpec::Base` instead of `RuboCop::Cop::RSpec::Cop`.

2. The module `RuboCop::Cop::RSpec::TopLevelDescribe` is replaced with a more generic `RuboCop::Cop::RSpec::TopLevelGroup`.

3. `RuboCop::RSpec::Language` has been completely rewritten to support dynamic RSpec DSL aliases and negated matchers to fully support third-party libraries such as RSpec Rails, Pundit, Action Policy and many others.

4. RuboCop RSpec updated the dependency of RuboCop to 1.0+.

Below are the necessary steps to update custom cops to work with `rubocop-rspec` version 2.x.


=== Change the Parent Class

Change the parent class of the custom cops from `RuboCop::Cop::RSpec::Cop` to `RuboCop::Cop::RSpec::Base`.

[source,ruby]
----
# Before
module RuboCop
  module Cop
    module RSpec
      class FightPowerty < Cop

# After
module RuboCop
  module Cop
    module RSpec
      class FightPowerty < Base
----

https://github.com/rubocop/rubocop-rspec/pull/962[Example pull request].


=== Replace `TopLevelDescribe`

`TopLevelDescribe` was incomplete, had poor performance and did not distinguish between example groups and shared example groups.

`TopLevelGroup` provides a similar interface, but instead of a single `on_top_level_describe` hook there are two, `on_top_level_example_group` and `on_top_level_group`.
There’s no need yet for `on_top_level_shared_group` for RuboCop core cops, but if your custom cop needs such a hook, please feel free to send a pull request.

Additionally, `single_top_level_describe?` is removed with no direct replacement.
You may use `top_level_groups` query method instead, e.g. `top_level_groups.one?`.

Example pull requests to replace `TopLevelDescribe` with `TopLevelGroup` [https://github.com/rubocop/rubocop-rspec/pull/978[1], https://github.com/rubocop/rubocop-rspec/pull/932[2], https://github.com/rubocop/rubocop-rspec/pull/977[3]].


=== Change the `Language` Module Usages

To allow for lazy initialization, and for loading of the language configuration after the class are loaded, a https://docs.rubocop.org/rubocop-ast/node_pattern.html#to-call-functions[function call feature of RuboCop AST] is used.

The `RuboCop::RSpec::Language` is completely different now.

`Hooks::ALL` and alike, and their accompanying helpers work differently.

[source,ruby]
----
# Before
def_node_matcher :shared_context,
                 SharedGroups::CONTEXT.block_pattern

# After
def_node_matcher :shared_context,
                 block_pattern('#SharedGroups.context')
----

[source,ruby]
----
# Before
def_node_search :examples?,
                (Includes::EXAMPLES + Examples::ALL).send_pattern

# After
def_node_search :examples?,
                send_pattern('{#Includes.examples #Examples.all}')
----

[source,ruby]
----
# Before
def_node_search :find_rspec_blocks,
                ExampleGroups::ALL.block_pattern

# After
def_node_search :find_rspec_blocks,
                block_pattern('#ExampleGroups.all')
----

If you were calling Language elements directly, you have to make the same adjustments:

[source,ruby]
----
# Before
node&.sym_type? && Hooks::Scopes::ALL.include?(node.value)

# After
node&.sym_type? && Language::HookScopes.all(node.value)
----

You may see a common pattern in the change.
There is a small exception, though:

[source,ruby]
----
# Before
ExampleGroups::GROUPS

# After
ExampleGroups.regular

# Before
Examples::EXAMPLES

# After
Examples.regular
----

https://github.com/rubocop/rubocop-rspec/pull/956[Pull request with more examples].

=== Always call `super` from `on_new_investigation` in your cops

`on_new_investigation` is now used for internal purposes, and not calling `super` from your cop involves a risk of configuration not being properly loaded, and dynamic RSpec DSL matchers won't work.

NOTE: You don't have to define `on_new_investigation` in your cops unless you need to.

[source,ruby]
----
module RuboCop
  module Cop
    module RSpec
      class MultipleMemoizedHelpers < Base
        def on_new_investigation
          super # Always call `super`
          @example_group_memoized_helpers = {}
        end
      end
    end
  end
end
----

https://github.com/rubocop/rubocop-rspec/pull/956[Pull request with more examples].

=== Use `:config` RSpec metadata in cop specs

`:config` metadata should be added to the top-level example group of your cop spec.
Doing otherwise will not pass configuration to the cop, and dynamic RSpec DSL matchers might not work.

[source,ruby]
----
# Before
RSpec.describe 'MyMightyCop' do
  let(:cop) { described_class.new }
  # ...
end

# After
RSpec.describe 'MyMightyCop', :config do
  # `cop` is defined for you by RuboCop's shared context that is included
  # to example groups with :config metadata

  # ...
end
----

https://github.com/rubocop/rubocop/blob/51ff1d7e29c985732fe129082c98d66c531a2611/lib/rubocop/rspec/shared_contexts.rb#L56[RuboCop takes care of defining everything for your cop specs].

=== Conform with RuboCop API Changes

The parent project, RuboCop, has API changes.
While they won’t result in cop breakages, it is recommended to update cops to use new API’s.
Follow the https://docs.rubocop.org/rubocop/v1_upgrade_notes[RuboCop v1 update guide] to adjust custom cops’ use of RuboCop’s auto-correction API.