rubocop-rspec-guide 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/rubocop/cop/rspec_guide/happy_path_first.rb +43 -0
- data/lib/rubocop/rspec/guide/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0c6e4154c7d3d386bd161fa1405ac9e36eb2152fbf38f568a3fa15698a53e7ab
|
|
4
|
+
data.tar.gz: 86b32810a2ad0c401d6757297ca6880d454966d9355aa7de45f69026cf4426d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 59676351d5629e7947ce2123819409a90657976b064b0386f1488f5593f7760b65f81d9a1911468e5ed4ebf85ce529f15dbaf3ce4249d51fb0f4ab4cbd122770
|
|
7
|
+
data.tar.gz: 43841a5b040e380c13f8210c55ac1297b7b7b6570a4438d0d4f30951b7a29313ae4af55295fe8d4069e8434a3f33b0b542d15de76b6d80524a40fd976dd9f820
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.2.1] - 2025-10-29
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
- **RSpecGuide/HappyPathFirst**: Allow corner case contexts when examples (it/specify) appear before first context
|
|
7
|
+
- Examples before contexts are considered happy path
|
|
8
|
+
- No offense if at least one example exists before the first context
|
|
9
|
+
|
|
3
10
|
## [0.2.0] - 2025-10-28
|
|
4
11
|
|
|
5
12
|
### Changed
|
|
@@ -6,6 +6,10 @@ module RuboCop
|
|
|
6
6
|
# Checks that corner cases are not the first context in a describe block.
|
|
7
7
|
# Happy path should come first for better readability.
|
|
8
8
|
#
|
|
9
|
+
# The cop allows corner case contexts to appear first if there are
|
|
10
|
+
# example blocks (it/specify) before the first context, as those examples
|
|
11
|
+
# represent the happy path.
|
|
12
|
+
#
|
|
9
13
|
# @example
|
|
10
14
|
# # bad
|
|
11
15
|
# describe '#process' do
|
|
@@ -37,6 +41,17 @@ module RuboCop
|
|
|
37
41
|
# end
|
|
38
42
|
# end
|
|
39
43
|
#
|
|
44
|
+
# # good - examples before first context represent happy path
|
|
45
|
+
# describe '#add_child' do
|
|
46
|
+
# it 'adds child to children collection' do
|
|
47
|
+
# # ...
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# context 'but child is already in collection' do
|
|
51
|
+
# # ...
|
|
52
|
+
# end
|
|
53
|
+
# end
|
|
54
|
+
#
|
|
40
55
|
class HappyPathFirst < Base
|
|
41
56
|
MSG = "Place happy path contexts before corner cases. " \
|
|
42
57
|
"First context appears to be a corner case: %<description>s"
|
|
@@ -67,6 +82,10 @@ module RuboCop
|
|
|
67
82
|
contexts = collect_direct_child_contexts(node)
|
|
68
83
|
return if contexts.size < 2
|
|
69
84
|
|
|
85
|
+
# If there are any examples (it/specify) before the first context,
|
|
86
|
+
# this is a happy path, so no offense
|
|
87
|
+
return if has_examples_before_first_context?(node, contexts.first)
|
|
88
|
+
|
|
70
89
|
# Check first context
|
|
71
90
|
context_with_description?(contexts.first) do |description|
|
|
72
91
|
if corner_case_context?(description)
|
|
@@ -80,6 +99,30 @@ module RuboCop
|
|
|
80
99
|
|
|
81
100
|
private
|
|
82
101
|
|
|
102
|
+
def has_examples_before_first_context?(node, first_context)
|
|
103
|
+
body = node.body
|
|
104
|
+
return false unless body
|
|
105
|
+
|
|
106
|
+
children = body.begin_type? ? body.children : [body]
|
|
107
|
+
|
|
108
|
+
children.each do |child|
|
|
109
|
+
# Stop when we reach the first context
|
|
110
|
+
break if child == first_context
|
|
111
|
+
|
|
112
|
+
# Check if this is an example (it/specify)
|
|
113
|
+
return true if example?(child)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
false
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def example?(node)
|
|
120
|
+
return false unless node.block_type?
|
|
121
|
+
|
|
122
|
+
send_node = node.send_node
|
|
123
|
+
send_node.method?(:it) || send_node.method?(:specify)
|
|
124
|
+
end
|
|
125
|
+
|
|
83
126
|
def collect_direct_child_contexts(node)
|
|
84
127
|
body = node.body
|
|
85
128
|
return [] unless body
|