rubocop-rspec 3.0.5 → 3.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ce22630f88fd7a9b996711a4af330a186471fdc8a6dd99b05830ad695a09118
4
- data.tar.gz: 44a91f15e435ec623b2a25ae6e7b890772d7cf3066206f97b0ca1abf42c62c7a
3
+ metadata.gz: 47c427b063cba22b4601d8244b18db288081f95889e287be82473e9148748c2e
4
+ data.tar.gz: 9d9543614cb00ee6cf4c94c750ce2368a4e34dae73f1a778e3fc9a7470698526
5
5
  SHA512:
6
- metadata.gz: 0d6b6f4113c4fc5253dcd59525e348879ae5763d09bb416de752d037b3ee20c38774df54322a8c7abd4c21652b22e6c4ac708bce51a2f1df61fecb4aab9dc4ac
7
- data.tar.gz: ec0278b64e0dc0ca9cf0e9558ae02224ea5a3360ee86380336b4bc07e134801e46011f5ef673586eac6b85557884b3d3986d05935b3255ba48c227ecd760227c
6
+ metadata.gz: 04fb1fe03b1fb818c7ee43d937fbb394e93a91e70c0cc00c06d259c53210ebb6b5243b584342838de566e99d0263d60b975113362b3a6306acf31f46ea2ed0ff
7
+ data.tar.gz: 66daeeaff0d894825dfcf0dc836b0fc614490b358b508ddaacc98652c37f0dc6e9c29e38222493e21a662f848a20efce503fe25a349b7a16fcc3693b9ecac4a1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 3.2.0 (2024-10-26)
6
+
7
+ - Fix `RSpec/VoidExpect` to only operate inside an example block. ([@corsonknowles])
8
+ - Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty. ([@ydah])
9
+ - Add support for `and` and `or` compound matchers to `RSpec/ChangeByZero` cop. ([@ydah])
10
+
11
+ ## 3.1.0 (2024-10-01)
12
+
13
+ - Add `RSpec/StringAsInstanceDoubleConstant` to check for and correct strings used as instance_doubles. ([@corsonknowles])
14
+ - Fix false-positive for `RSpec/UnspecifiedException` when a method is literally named `raise_exception`. ([@aarestad])
15
+ - Fix false-positive for `RSpec/UnspecifiedException` when `not_to raise_error` is used within a block. ([@aarestad], [@G-Rath])
16
+
5
17
  ## 3.0.5 (2024-09-07)
6
18
 
7
19
  - Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
@@ -899,6 +911,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
899
911
 
900
912
  <!-- Contributors (alphabetically) -->
901
913
 
914
+ [@aarestad]: https://github.com/aarestad
902
915
  [@abrom]: https://github.com/abrom
903
916
  [@ahukkanen]: https://github.com/ahukkanen
904
917
  [@akiomik]: https://github.com/akiomik
@@ -920,6 +933,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
920
933
  [@cfabianski]: https://github.com/cfabianski
921
934
  [@clupprich]: https://github.com/clupprich
922
935
  [@composerinteralia]: https://github.com/composerinteralia
936
+ [@corsonknowles]: https://github.com/corsonknowles
923
937
  [@corydiamand]: https://github.com/corydiamand
924
938
  [@darhazer]: https://github.com/Darhazer
925
939
  [@daveworth]: https://github.com/daveworth
data/config/default.yml CHANGED
@@ -403,6 +403,7 @@ RSpec/ExampleWithoutDescription:
403
403
  - single_line_only
404
404
  - disallow
405
405
  VersionAdded: '1.22'
406
+ StyleGuide: https://rspec.rubystyle.guide/#it-and-specify
406
407
  Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWithoutDescription
407
408
 
408
409
  RSpec/ExampleWording:
@@ -929,6 +930,13 @@ RSpec/SpecFilePathSuffix:
929
930
  - "**/spec/**/*"
930
931
  Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix
931
932
 
933
+ RSpec/StringAsInstanceDoubleConstant:
934
+ Description: Do not use a string as `instance_double` constant.
935
+ Enabled: pending
936
+ Safe: false
937
+ VersionAdded: '3.1'
938
+ Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant
939
+
932
940
  RSpec/StubbedMock:
933
941
  Description: Checks that message expectations do not have a configured response.
934
942
  Enabled: true
@@ -104,12 +104,12 @@ module RuboCop
104
104
  # rubocop:disable Metrics/MethodLength
105
105
  def register_offense(node, change_node)
106
106
  if compound_expectations?(node)
107
- add_offense(node.source_range,
107
+ add_offense(node,
108
108
  message: message_compound(change_node)) do |corrector|
109
109
  autocorrect_compound(corrector, node)
110
110
  end
111
111
  else
112
- add_offense(node.source_range,
112
+ add_offense(node,
113
113
  message: message(change_node)) do |corrector|
114
114
  autocorrect(corrector, node, change_node)
115
115
  end
@@ -118,7 +118,8 @@ module RuboCop
118
118
  # rubocop:enable Metrics/MethodLength
119
119
 
120
120
  def compound_expectations?(node)
121
- %i[and or & |].include?(node.parent.method_name)
121
+ node.parent.send_type? &&
122
+ %i[and or & |].include?(node.parent.method_name)
122
123
  end
123
124
 
124
125
  def message(change_node)
@@ -12,6 +12,9 @@ module RuboCop
12
12
  #
13
13
  # @see http://www.betterspecs.org/#contexts
14
14
  #
15
+ # If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
16
+ # report an offense. So you need to set at least one of them.
17
+ #
15
18
  # @example `Prefixes` configuration
16
19
  # # .rubocop.yml
17
20
  # # RSpec/ContextWording:
@@ -58,7 +61,9 @@ module RuboCop
58
61
  class ContextWording < Base
59
62
  include AllowedPattern
60
63
 
61
- MSG = 'Context description should match %<patterns>s.'
64
+ MSG_MATCH = 'Context description should match %<patterns>s.'
65
+ MSG_ALWAYS = 'Current settings will always report an offense. Please ' \
66
+ 'add allowed words to `Prefixes` or `AllowedPatterns`.'
62
67
 
63
68
  # @!method context_wording(node)
64
69
  def_node_matcher :context_wording, <<~PATTERN
@@ -67,8 +72,7 @@ module RuboCop
67
72
 
68
73
  def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
69
74
  context_wording(node) do |context|
70
- if bad_pattern?(context)
71
- message = format(MSG, patterns: expect_patterns)
75
+ unless matches_allowed_pattern?(description(context))
72
76
  add_offense(context, message: message)
73
77
  end
74
78
  end
@@ -84,12 +88,6 @@ module RuboCop
84
88
  @prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ }
85
89
  end
86
90
 
87
- def bad_pattern?(node)
88
- return false if allowed_patterns.empty?
89
-
90
- !matches_allowed_pattern?(description(node))
91
- end
92
-
93
91
  def description(context)
94
92
  if context.xstr_type?
95
93
  context.value.value
@@ -98,6 +96,14 @@ module RuboCop
98
96
  end
99
97
  end
100
98
 
99
+ def message
100
+ if allowed_patterns.empty?
101
+ MSG_ALWAYS
102
+ else
103
+ format(MSG_MATCH, patterns: expect_patterns)
104
+ end
105
+ end
106
+
101
107
  def expect_patterns
102
108
  inspected = allowed_patterns.map do |pattern|
103
109
  pattern.inspect.gsub(/\A"|"\z/, '/')
@@ -69,7 +69,7 @@ module RuboCop
69
69
  expect_literal(node) do |actual, send_node, matcher, expected|
70
70
  next if SKIPPED_MATCHERS.include?(matcher)
71
71
 
72
- add_offense(actual.source_range) do |corrector|
72
+ add_offense(actual) do |corrector|
73
73
  next unless CORRECTABLE_MATCHERS.include?(matcher)
74
74
  next if literal?(expected)
75
75
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Do not use a string as `instance_double` constant.
7
+ #
8
+ # @safety
9
+ # This cop is unsafe because the correction requires loading the class.
10
+ # Loading before stubbing causes RSpec to only allow instance methods
11
+ # to be stubbed.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # instance_double('User', name: 'John')
16
+ #
17
+ # # good
18
+ # instance_double(User, name: 'John')
19
+ #
20
+ class StringAsInstanceDoubleConstant < Base
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Do not use a string as `instance_double` constant.'
24
+ RESTRICT_ON_SEND = %i[instance_double].freeze
25
+
26
+ # @!method stringified_instance_double_const?(node)
27
+ def_node_matcher :stringified_instance_double_const?, <<~PATTERN
28
+ (send nil? :instance_double $str ...)
29
+ PATTERN
30
+
31
+ def on_send(node)
32
+ stringified_instance_double_const?(node) do |args_node|
33
+ add_offense(args_node) do |corrector|
34
+ autocorrect(corrector, args_node)
35
+ end
36
+ end
37
+ end
38
+
39
+ def autocorrect(corrector, node)
40
+ corrector.replace(node, node.value)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -62,7 +62,10 @@ module RuboCop
62
62
  end
63
63
 
64
64
  def find_expect_to(node)
65
- node.each_ancestor(:send).find do |ancestor|
65
+ node.each_ancestor.find do |ancestor|
66
+ break if ancestor.block_type?
67
+ next unless ancestor.send_type?
68
+
66
69
  expect_to?(ancestor)
67
70
  end
68
71
  end
@@ -29,12 +29,14 @@ module RuboCop
29
29
 
30
30
  def on_send(node)
31
31
  return unless expect?(node)
32
+ return unless inside_example?(node)
32
33
 
33
34
  check_expect(node)
34
35
  end
35
36
 
36
37
  def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
37
38
  return unless expect_block?(node)
39
+ return unless inside_example?(node)
38
40
 
39
41
  check_expect(node)
40
42
  end
@@ -49,11 +51,14 @@ module RuboCop
49
51
 
50
52
  def void?(expect)
51
53
  parent = expect.parent
52
- return true unless parent
53
54
  return true if parent.begin_type?
54
55
 
55
56
  parent.block_type? && parent.body == expect
56
57
  end
58
+
59
+ def inside_example?(node)
60
+ node.each_ancestor(:block).any? { |ancestor| example?(ancestor) }
61
+ end
57
62
  end
58
63
  end
59
64
  end
@@ -99,6 +99,7 @@ require_relative 'rspec/skip_block_inside_example'
99
99
  require_relative 'rspec/sort_metadata'
100
100
  require_relative 'rspec/spec_file_path_format'
101
101
  require_relative 'rspec/spec_file_path_suffix'
102
+ require_relative 'rspec/string_as_instance_double_constant'
102
103
  require_relative 'rspec/stubbed_mock'
103
104
  require_relative 'rspec/subject_declaration'
104
105
  require_relative 'rspec/subject_stub'
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module RSpec
5
5
  # Version information for the RSpec RuboCop plugin.
6
6
  module Version
7
- STRING = '3.0.5'
7
+ STRING = '3.2.0'
8
8
  end
9
9
  end
10
10
  end
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: 3.0.5
4
+ version: 3.2.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: 2024-09-07 00:00:00.000000000 Z
13
+ date: 2024-10-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -157,6 +157,7 @@ files:
157
157
  - lib/rubocop/cop/rspec/sort_metadata.rb
158
158
  - lib/rubocop/cop/rspec/spec_file_path_format.rb
159
159
  - lib/rubocop/cop/rspec/spec_file_path_suffix.rb
160
+ - lib/rubocop/cop/rspec/string_as_instance_double_constant.rb
160
161
  - lib/rubocop/cop/rspec/stubbed_mock.rb
161
162
  - lib/rubocop/cop/rspec/subject_declaration.rb
162
163
  - lib/rubocop/cop/rspec/subject_stub.rb