rubocop-rspec 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/config/default.yml +9 -1
- data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +2 -23
- data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +4 -3
- data/lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb +107 -0
- data/lib/rubocop/cop/rspec/mixin/inside_example_group.rb +29 -0
- data/lib/rubocop/cop/rspec_cops.rb +1 -0
- data/lib/rubocop/rspec/factory_bot/language.rb +17 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- data/lib/rubocop-rspec.rb +5 -2
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f73bb98579faf4e173aa834c1a3aa5fc1c9b6aa7c8790d3fcef2451594fadc78
|
4
|
+
data.tar.gz: e66399f76cea55c1eed9b348f3015ab726fb5df49b2f1a391bbf043b45b18507
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44ba849ca8fd9ad7cb65355c78046bd5702f9d5b1f5df305fda876c6d7feb5947ea15e6fdb3749e32c1ae18b089703abefc074991fc780d6147b2295f3202a05
|
7
|
+
data.tar.gz: 5281f8fa3c1cb64028dd54544fc2f26fe25f382a5f842fa0dfe5040dbb6ea3627e74a386a596806cd6a0f56d8353d749fbec713c48c19d45f55840f9983cf755
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
|
3
3
|
## Master (Unreleased)
|
4
4
|
|
5
|
+
## 2.7.0 (2021-12-26)
|
6
|
+
|
7
|
+
* Add new `RSpec/FactoryBot/SyntaxMethods` cop. ([@leoarnold][])
|
8
|
+
* Exclude `task` type specs from `RSpec/DescribeClass` cop. ([@harry-graham][])
|
9
|
+
|
5
10
|
## 2.6.0 (2021-11-08)
|
6
11
|
|
7
12
|
* Fix merging RSpec DSL configuration from third-party gems. ([@pirj][])
|
@@ -651,3 +656,5 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
|
|
651
656
|
[@dswij]: https://github.com/dswij
|
652
657
|
[@francois-ferrandis]: https://github.com/francois-ferrandis
|
653
658
|
[@r7kamura]: https://github.com/r7kamura
|
659
|
+
[@leoarnold]: https://github.com/leoarnold
|
660
|
+
[@harry-graham]: https://github.com/harry-graham
|
data/config/default.yml
CHANGED
@@ -196,8 +196,9 @@ RSpec/DescribeClass:
|
|
196
196
|
- system
|
197
197
|
- mailbox
|
198
198
|
- aruba
|
199
|
+
- task
|
199
200
|
VersionAdded: '1.0'
|
200
|
-
VersionChanged: '2.
|
201
|
+
VersionChanged: '2.7'
|
201
202
|
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeClass
|
202
203
|
|
203
204
|
RSpec/DescribeMethod:
|
@@ -816,6 +817,13 @@ RSpec/FactoryBot/FactoryClassName:
|
|
816
817
|
VersionChanged: '2.0'
|
817
818
|
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName
|
818
819
|
|
820
|
+
RSpec/FactoryBot/SyntaxMethods:
|
821
|
+
Description: Use shorthands from `FactoryBot::Syntax::Methods` in your specs.
|
822
|
+
Enabled: pending
|
823
|
+
SafeAutoCorrect: false
|
824
|
+
VersionAdded: '2.7'
|
825
|
+
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/SyntaxMethods
|
826
|
+
|
819
827
|
RSpec/Rails:
|
820
828
|
Enabled: true
|
821
829
|
Include: *1
|
@@ -42,6 +42,7 @@ module RuboCop
|
|
42
42
|
# end
|
43
43
|
class FeatureMethods < Base
|
44
44
|
extend AutoCorrector
|
45
|
+
include InsideExampleGroup
|
45
46
|
|
46
47
|
MSG = 'Use `%<replacement>s` instead of `%<method>s`.'
|
47
48
|
|
@@ -60,13 +61,6 @@ module RuboCop
|
|
60
61
|
{#{MAP.keys.map(&:inspect).join(' ')}}
|
61
62
|
PATTERN
|
62
63
|
|
63
|
-
# @!method spec?(node)
|
64
|
-
def_node_matcher :spec?, <<-PATTERN
|
65
|
-
(block
|
66
|
-
(send #rspec? {:describe :feature} ...)
|
67
|
-
...)
|
68
|
-
PATTERN
|
69
|
-
|
70
64
|
# @!method feature_method(node)
|
71
65
|
def_node_matcher :feature_method, <<-PATTERN
|
72
66
|
(block
|
@@ -75,7 +69,7 @@ module RuboCop
|
|
75
69
|
PATTERN
|
76
70
|
|
77
71
|
def on_block(node)
|
78
|
-
return unless
|
72
|
+
return unless inside_example_group?(node)
|
79
73
|
|
80
74
|
feature_method(node) do |send_node, match|
|
81
75
|
next if enabled?(match)
|
@@ -93,21 +87,6 @@ module RuboCop
|
|
93
87
|
|
94
88
|
private
|
95
89
|
|
96
|
-
def inside_spec?(node)
|
97
|
-
return spec?(node) if root_node?(node)
|
98
|
-
|
99
|
-
root = node.ancestors.find { |parent| root_node?(parent) }
|
100
|
-
spec?(root)
|
101
|
-
end
|
102
|
-
|
103
|
-
def root_node?(node)
|
104
|
-
node.parent.nil? || root_with_siblings?(node.parent)
|
105
|
-
end
|
106
|
-
|
107
|
-
def root_with_siblings?(node)
|
108
|
-
node.begin_type? && node.parent.nil?
|
109
|
-
end
|
110
|
-
|
111
90
|
def enabled?(method_name)
|
112
91
|
enabled_methods.include?(method_name)
|
113
92
|
end
|
@@ -27,6 +27,7 @@ module RuboCop
|
|
27
27
|
class CreateList < Base
|
28
28
|
extend AutoCorrector
|
29
29
|
include ConfigurableEnforcedStyle
|
30
|
+
include RuboCop::RSpec::FactoryBot::Language
|
30
31
|
|
31
32
|
MSG_CREATE_LIST = 'Prefer create_list.'
|
32
33
|
MSG_N_TIMES = 'Prefer %<number>s.times.'
|
@@ -43,12 +44,12 @@ module RuboCop
|
|
43
44
|
|
44
45
|
# @!method factory_call(node)
|
45
46
|
def_node_matcher :factory_call, <<-PATTERN
|
46
|
-
(send ${
|
47
|
+
(send ${nil? #factory_bot?} :create (sym $_) $...)
|
47
48
|
PATTERN
|
48
49
|
|
49
50
|
# @!method factory_list_call(node)
|
50
51
|
def_node_matcher :factory_list_call, <<-PATTERN
|
51
|
-
(send {
|
52
|
+
(send {nil? #factory_bot?} :create_list (sym _) (int $_) ...)
|
52
53
|
PATTERN
|
53
54
|
|
54
55
|
def on_block(node)
|
@@ -160,7 +161,7 @@ module RuboCop
|
|
160
161
|
def call_with_block_replacement(node)
|
161
162
|
block = node.body
|
162
163
|
arguments = build_arguments(block, node.receiver.source)
|
163
|
-
replacement = format_receiver(block.
|
164
|
+
replacement = format_receiver(block.receiver)
|
164
165
|
replacement += format_method_call(block, 'create_list', arguments)
|
165
166
|
replacement += format_block(block)
|
166
167
|
replacement
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
module FactoryBot
|
7
|
+
# Use shorthands from `FactoryBot::Syntax::Methods` in your specs.
|
8
|
+
#
|
9
|
+
# @safety
|
10
|
+
# The auto-correction is marked as unsafe because the cop
|
11
|
+
# cannot verify whether you already include
|
12
|
+
# `FactoryBot::Syntax::Methods` in your test suite.
|
13
|
+
#
|
14
|
+
# If you're using Rails, add the following configuration to
|
15
|
+
# `spec/support/factory_bot.rb` and be sure to require that file in
|
16
|
+
# `rails_helper.rb`:
|
17
|
+
#
|
18
|
+
# [source,ruby]
|
19
|
+
# ----
|
20
|
+
# RSpec.configure do |config|
|
21
|
+
# config.include FactoryBot::Syntax::Methods
|
22
|
+
# end
|
23
|
+
# ----
|
24
|
+
#
|
25
|
+
# If you're not using Rails:
|
26
|
+
#
|
27
|
+
# [source,ruby]
|
28
|
+
# ----
|
29
|
+
# RSpec.configure do |config|
|
30
|
+
# config.include FactoryBot::Syntax::Methods
|
31
|
+
#
|
32
|
+
# config.before(:suite) do
|
33
|
+
# FactoryBot.find_definitions
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# ----
|
37
|
+
#
|
38
|
+
# @example
|
39
|
+
# # bad
|
40
|
+
# FactoryBot.create(:bar)
|
41
|
+
# FactoryBot.build(:bar)
|
42
|
+
# FactoryBot.attributes_for(:bar)
|
43
|
+
#
|
44
|
+
# # good
|
45
|
+
# create(:bar)
|
46
|
+
# build(:bar)
|
47
|
+
# attributes_for(:bar)
|
48
|
+
#
|
49
|
+
class SyntaxMethods < Base
|
50
|
+
extend AutoCorrector
|
51
|
+
include InsideExampleGroup
|
52
|
+
include RangeHelp
|
53
|
+
include RuboCop::RSpec::FactoryBot::Language
|
54
|
+
|
55
|
+
MSG = 'Use `%<method>s` from `FactoryBot::Syntax::Methods`.'
|
56
|
+
|
57
|
+
RESTRICT_ON_SEND = %i[
|
58
|
+
attributes_for
|
59
|
+
attributes_for_list
|
60
|
+
attributes_for_pair
|
61
|
+
build
|
62
|
+
build_list
|
63
|
+
build_pair
|
64
|
+
build_stubbed
|
65
|
+
build_stubbed_list
|
66
|
+
build_stubbed_pair
|
67
|
+
create
|
68
|
+
create_list
|
69
|
+
create_pair
|
70
|
+
generate
|
71
|
+
generate_list
|
72
|
+
null
|
73
|
+
null_list
|
74
|
+
null_pair
|
75
|
+
].to_set.freeze
|
76
|
+
|
77
|
+
def on_send(node)
|
78
|
+
return unless factory_bot?(node.receiver)
|
79
|
+
return unless inside_example_group?(node)
|
80
|
+
|
81
|
+
message = format(MSG, method: node.method_name)
|
82
|
+
|
83
|
+
add_offense(crime_scene(node), message: message) do |corrector|
|
84
|
+
corrector.remove(offense(node))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def crime_scene(node)
|
91
|
+
range_between(
|
92
|
+
node.loc.expression.begin_pos,
|
93
|
+
node.loc.selector.end_pos
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
def offense(node)
|
98
|
+
range_between(
|
99
|
+
node.loc.expression.begin_pos,
|
100
|
+
node.loc.selector.begin_pos
|
101
|
+
)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Helps you identify whether a given node
|
7
|
+
# is within an example group or not.
|
8
|
+
module InsideExampleGroup
|
9
|
+
private
|
10
|
+
|
11
|
+
def inside_example_group?(node)
|
12
|
+
return example_group?(node) if example_group_root?(node)
|
13
|
+
|
14
|
+
root = node.ancestors.find { |parent| example_group_root?(parent) }
|
15
|
+
|
16
|
+
example_group?(root)
|
17
|
+
end
|
18
|
+
|
19
|
+
def example_group_root?(node)
|
20
|
+
node.parent.nil? || example_group_root_with_siblings?(node.parent)
|
21
|
+
end
|
22
|
+
|
23
|
+
def example_group_root_with_siblings?(node)
|
24
|
+
node.begin_type? && node.parent.nil?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -7,6 +7,7 @@ require_relative 'rspec/capybara/visibility_matcher'
|
|
7
7
|
require_relative 'rspec/factory_bot/attribute_defined_statically'
|
8
8
|
require_relative 'rspec/factory_bot/create_list'
|
9
9
|
require_relative 'rspec/factory_bot/factory_class_name'
|
10
|
+
require_relative 'rspec/factory_bot/syntax_methods'
|
10
11
|
|
11
12
|
require_relative 'rspec/rails/avoid_setup_hook'
|
12
13
|
begin
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module RSpec
|
5
|
+
module FactoryBot
|
6
|
+
# Contains node matchers for common FactoryBot DSL.
|
7
|
+
module Language
|
8
|
+
extend RuboCop::NodePattern::Macros
|
9
|
+
|
10
|
+
# @!method factory_bot?(node)
|
11
|
+
def_node_matcher :factory_bot?, <<~PATTERN
|
12
|
+
(const {nil? cbase} {:FactoryGirl :FactoryBot})
|
13
|
+
PATTERN
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rubocop-rspec.rb
CHANGED
@@ -12,11 +12,14 @@ require_relative 'rubocop/rspec/wording'
|
|
12
12
|
require_relative 'rubocop/rspec/language/node_pattern'
|
13
13
|
require_relative 'rubocop/rspec/language'
|
14
14
|
|
15
|
+
require_relative 'rubocop/rspec/factory_bot/language'
|
16
|
+
|
15
17
|
require_relative 'rubocop/cop/rspec/mixin/top_level_group'
|
16
18
|
require_relative 'rubocop/cop/rspec/mixin/variable'
|
17
19
|
require_relative 'rubocop/cop/rspec/mixin/final_end_location'
|
18
20
|
require_relative 'rubocop/cop/rspec/mixin/comments_help'
|
19
21
|
require_relative 'rubocop/cop/rspec/mixin/empty_line_separation'
|
22
|
+
require_relative 'rubocop/cop/rspec/mixin/inside_example_group'
|
20
23
|
|
21
24
|
require_relative 'rubocop/rspec/concept'
|
22
25
|
require_relative 'rubocop/rspec/example_group'
|
@@ -31,8 +34,8 @@ RuboCop::RSpec::Inject.defaults!
|
|
31
34
|
|
32
35
|
require_relative 'rubocop/cop/rspec_cops'
|
33
36
|
|
34
|
-
# We have to register our autocorrect
|
35
|
-
# so we do not hit infinite loops
|
37
|
+
# We have to register our autocorrect incompatibilities in RuboCop's cops
|
38
|
+
# as well so we do not hit infinite loops
|
36
39
|
|
37
40
|
module RuboCop
|
38
41
|
module Cop
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Backus
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2021-
|
13
|
+
date: 2021-12-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb
|
167
167
|
- lib/rubocop/cop/rspec/factory_bot/create_list.rb
|
168
168
|
- lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb
|
169
|
+
- lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb
|
169
170
|
- lib/rubocop/cop/rspec/file_path.rb
|
170
171
|
- lib/rubocop/cop/rspec/focus.rb
|
171
172
|
- lib/rubocop/cop/rspec/hook_argument.rb
|
@@ -189,6 +190,7 @@ files:
|
|
189
190
|
- lib/rubocop/cop/rspec/mixin/comments_help.rb
|
190
191
|
- lib/rubocop/cop/rspec/mixin/empty_line_separation.rb
|
191
192
|
- lib/rubocop/cop/rspec/mixin/final_end_location.rb
|
193
|
+
- lib/rubocop/cop/rspec/mixin/inside_example_group.rb
|
192
194
|
- lib/rubocop/cop/rspec/mixin/top_level_group.rb
|
193
195
|
- lib/rubocop/cop/rspec/mixin/variable.rb
|
194
196
|
- lib/rubocop/cop/rspec/multiple_describes.rb
|
@@ -234,6 +236,7 @@ files:
|
|
234
236
|
- lib/rubocop/rspec/example.rb
|
235
237
|
- lib/rubocop/rspec/example_group.rb
|
236
238
|
- lib/rubocop/rspec/factory_bot.rb
|
239
|
+
- lib/rubocop/rspec/factory_bot/language.rb
|
237
240
|
- lib/rubocop/rspec/hook.rb
|
238
241
|
- lib/rubocop/rspec/inject.rb
|
239
242
|
- lib/rubocop/rspec/language.rb
|
@@ -247,6 +250,7 @@ licenses:
|
|
247
250
|
metadata:
|
248
251
|
changelog_uri: https://github.com/rubocop/rubocop-rspec/blob/master/CHANGELOG.md
|
249
252
|
documentation_uri: https://docs.rubocop.org/rubocop-rspec/
|
253
|
+
rubygems_mfa_required: 'true'
|
250
254
|
post_install_message:
|
251
255
|
rdoc_options: []
|
252
256
|
require_paths:
|
@@ -262,7 +266,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
262
266
|
- !ruby/object:Gem::Version
|
263
267
|
version: '0'
|
264
268
|
requirements: []
|
265
|
-
rubygems_version: 3.
|
269
|
+
rubygems_version: 3.3.1
|
266
270
|
signing_key:
|
267
271
|
specification_version: 4
|
268
272
|
summary: Code style checking for RSpec files
|