rubocop-rspec 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a953b5fa50cd545314cbe25e453b476b670cd2a4eb2d60c75109354ed7c333c
4
- data.tar.gz: be5782eb1c0f8e4ca86b28ed39fba908719afe73dd544a161885719485853a89
3
+ metadata.gz: 35d3cf723ee0c50db00cbc5e63f5d9bf142186843fd56a86248dfb284c65180c
4
+ data.tar.gz: 8d0c0e01c405332eac2075b60d518a71e82a7d54f4b41ef111f5ef2b869e1f88
5
5
  SHA512:
6
- metadata.gz: e859b8c055f4502d3156595c8293decfe10883a6bea0f61a21558b9acc771b1231699439611a3da3ccb0841495ad7c1a5af3ec1c6d3e948fbb29b70a0b77289d
7
- data.tar.gz: a817dcdbcd1850d4888f4d5fc58837a1c8c59fa754f10dff0aa3eebf24013e88230d84719592115b3339551ac2c932240d01bac1b8a5094982943f1a9e960908
6
+ metadata.gz: 2e28be09b6cc11154b903ef574ef44487c44ade441f79b1172569d66cecd2c027d525dbcefbe755d7a0f2c872491a018fb24cd026453dc13ac764bcae7312abc
7
+ data.tar.gz: 3062cea8205acc5e020c404034c8c2590c6c5c089649c62280887b0bc20ca3b4b38048a983b2947f9ed10bc1824992955c6a58736db53f071c72a3fdde9363f8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 2.4.0 (2021-06-09)
6
+
7
+ * Update `RSpec/FilePath` to check suffix when given a non-constant top-level node (e.g. features). ([@topalovic][])
8
+ * Add missing documentation for `single_statement_only` style of `RSpec/ImplicitSubject` cop. ([@tejasbubane][])
9
+ * Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
10
+ * Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
11
+ * Add `RSpec/Rails/AvoidSetupHook` cop. ([@paydaylight][])
12
+ * Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call. ([@tejasbubane][])
13
+
5
14
  ## 2.3.0 (2021-04-28)
6
15
 
7
16
  * Allow `RSpec/ContextWording` to accept multi-word prefixes. ([@hosamaly][])
@@ -613,3 +622,6 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
613
622
  [@hosamaly]: https://github.com/hosamaly
614
623
  [@stephannv]: https://github.com/stephannv
615
624
  [@Tietew]: https://github.com/Tietew
625
+ [@rrosenblum]: https://github.com/rrosenblum
626
+ [@paydaylight]: https://github.com/paydaylight
627
+ [@topalovic]: https://github.com/topalovic
data/config/default.yml CHANGED
@@ -351,6 +351,12 @@ RSpec/HooksBeforeExamples:
351
351
  VersionAdded: '1.29'
352
352
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples
353
353
 
354
+ RSpec/IdenticalEqualityAssertion:
355
+ Description: Checks for equality assertions with identical expressions on both sides.
356
+ Enabled: pending
357
+ VersionAdded: '2.4'
358
+ StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion
359
+
354
360
  RSpec/ImplicitBlockExpectation:
355
361
  Description: Check that implicit block expectation syntax is not used.
356
362
  Enabled: true
@@ -753,6 +759,12 @@ RSpec/FactoryBot/FactoryClassName:
753
759
  VersionChanged: '2.0'
754
760
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName
755
761
 
762
+ RSpec/Rails/AvoidSetupHook:
763
+ Description: Checks that tests use RSpec `before` hook over Rails `setup` method.
764
+ Enabled: pending
765
+ VersionAdded: '2.4'
766
+ StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/AvoidSetupHook
767
+
756
768
  RSpec/Rails/HttpStatus:
757
769
  Description: Enforces use of symbolic or numeric value to describe HTTP status.
758
770
  Enabled: true
@@ -203,7 +203,7 @@ module RuboCop
203
203
  [name]
204
204
  elsif namespace.const_type?
205
205
  [*const_name(namespace), name]
206
- elsif namespace.lvar_type? || namespace.cbase_type?
206
+ elsif %i[lvar cbase send].include?(namespace.type)
207
207
  [nil, name]
208
208
  end
209
209
  end
@@ -39,7 +39,7 @@ module RuboCop
39
39
 
40
40
  # @!method expect_change_with_arguments(node)
41
41
  def_node_matcher :expect_change_with_arguments, <<-PATTERN
42
- (send nil? :change ({const send} nil? $_) (sym $_))
42
+ (send nil? :change $_ (sym $_))
43
43
  PATTERN
44
44
 
45
45
  # @!method expect_change_with_block(node)
@@ -55,9 +55,9 @@ module RuboCop
55
55
  return unless style == :block
56
56
 
57
57
  expect_change_with_arguments(node) do |receiver, message|
58
- msg = format(MSG_CALL, obj: receiver, attr: message)
58
+ msg = format(MSG_CALL, obj: receiver.source, attr: message)
59
59
  add_offense(node, message: msg) do |corrector|
60
- replacement = "change { #{receiver}.#{message} }"
60
+ replacement = "change { #{receiver.source}.#{message} }"
61
61
  corrector.replace(node, replacement)
62
62
  end
63
63
  end
@@ -61,10 +61,10 @@ module RuboCop
61
61
 
62
62
  MSG = 'Spec path should end with `%<suffix>s`.'
63
63
 
64
- # @!method const_described(node)
65
- def_node_matcher :const_described, <<~PATTERN
64
+ # @!method example_group(node)
65
+ def_node_matcher :example_group, <<~PATTERN
66
66
  (block
67
- $(send #rspec? _example_group $(const ...) $...) ...
67
+ $(send #rspec? _example_group $_ $...) ...
68
68
  )
69
69
  PATTERN
70
70
 
@@ -74,17 +74,17 @@ module RuboCop
74
74
  def on_top_level_example_group(node)
75
75
  return unless top_level_groups.one?
76
76
 
77
- const_described(node) do |send_node, described_class, arguments|
77
+ example_group(node) do |send_node, example_group, arguments|
78
78
  next if routing_spec?(arguments)
79
79
 
80
- ensure_correct_file_path(send_node, described_class, arguments)
80
+ ensure_correct_file_path(send_node, example_group, arguments)
81
81
  end
82
82
  end
83
83
 
84
84
  private
85
85
 
86
- def ensure_correct_file_path(send_node, described_class, arguments)
87
- pattern = pattern_for(described_class, arguments.first)
86
+ def ensure_correct_file_path(send_node, example_group, arguments)
87
+ pattern = pattern_for(example_group, arguments.first)
88
88
  return if filename_ends_with?(pattern)
89
89
 
90
90
  # For the suffix shown in the offense message, modify the regular
@@ -99,11 +99,13 @@ module RuboCop
99
99
  args.any?(&method(:routing_metadata?))
100
100
  end
101
101
 
102
- def pattern_for(described_class, method_name)
103
- return pattern_for_spec_suffix_only? if spec_suffix_only?
102
+ def pattern_for(example_group, method_name)
103
+ if spec_suffix_only? || !example_group.const_type?
104
+ return pattern_for_spec_suffix_only?
105
+ end
104
106
 
105
107
  [
106
- expected_path(described_class),
108
+ expected_path(example_group),
107
109
  name_pattern(method_name),
108
110
  '[^/]*_spec\.rb'
109
111
  ].join
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Checks for equality assertions with identical expressions on both sides.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # expect(foo.bar).to eq(foo.bar)
12
+ # expect(foo.bar).to eql(foo.bar)
13
+ #
14
+ # # good
15
+ # expect(foo.bar).to eq(2)
16
+ # expect(foo.bar).to eql(2)
17
+ #
18
+ class IdenticalEqualityAssertion < Base
19
+ MSG = 'Identical expressions on both sides of the equality ' \
20
+ 'may indicate a flawed test.'
21
+ RESTRICT_ON_SEND = %i[to].freeze
22
+
23
+ # @!method equality_check?(node)
24
+ def_node_matcher :equality_check?, <<~PATTERN
25
+ (send (send nil? :expect $_) :to
26
+ {(send nil? {:eql :eq :be} $_)
27
+ (send (send nil? :be) :== $_)})
28
+ PATTERN
29
+
30
+ def on_send(node)
31
+ equality_check?(node) do |left, right|
32
+ add_offense(node) if left == right
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -7,7 +7,7 @@ module RuboCop
7
7
  #
8
8
  # This cop can be configured using the `EnforcedStyle` option
9
9
  #
10
- # @example `EnforcedStyle: single_line_only`
10
+ # @example `EnforcedStyle: single_line_only` (default)
11
11
  # # bad
12
12
  # it do
13
13
  # is_expected.to be_truthy
@@ -19,6 +19,22 @@ module RuboCop
19
19
  # expect(subject).to be_truthy
20
20
  # end
21
21
  #
22
+ # @example `EnforcedStyle: single_statement_only`
23
+ # # bad
24
+ # it do
25
+ # foo = 1
26
+ # is_expected.to be_truthy
27
+ # end
28
+ #
29
+ # # good
30
+ # it do
31
+ # foo = 1
32
+ # expect(subject).to be_truthy
33
+ # end
34
+ # it do
35
+ # is_expected.to be_truthy
36
+ # end
37
+ #
22
38
  # @example `EnforcedStyle: disallow`
23
39
  # # bad
24
40
  # it { is_expected.to be_truthy }
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ module Rails
7
+ # Checks that tests use RSpec `before` hook over Rails `setup` method.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # setup do
13
+ # allow(foo).to receive(:bar)
14
+ # end
15
+ #
16
+ # # good
17
+ # before do
18
+ # allow(foo).to receive(:bar)
19
+ # end
20
+ #
21
+ class AvoidSetupHook < Base
22
+ extend AutoCorrector
23
+
24
+ MSG = 'Use `before` instead of `setup`.'
25
+
26
+ # @!method setup_call(node)
27
+ def_node_matcher :setup_call, <<-PATTERN
28
+ (block
29
+ $(send nil? :setup)
30
+ (args) _)
31
+ PATTERN
32
+
33
+ def on_block(node)
34
+ setup_call(node) do |setup|
35
+ add_offense(node) do |corrector|
36
+ corrector.replace setup, 'before'
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -8,6 +8,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
10
 
11
+ require_relative 'rspec/rails/avoid_setup_hook'
11
12
  begin
12
13
  require_relative 'rspec/rails/http_status'
13
14
  rescue LoadError
@@ -47,6 +48,7 @@ require_relative 'rspec/file_path'
47
48
  require_relative 'rspec/focus'
48
49
  require_relative 'rspec/hook_argument'
49
50
  require_relative 'rspec/hooks_before_examples'
51
+ require_relative 'rspec/identical_equality_assertion'
50
52
  require_relative 'rspec/implicit_block_expectation'
51
53
  require_relative 'rspec/implicit_expect'
52
54
  require_relative 'rspec/implicit_subject'
@@ -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 = '2.3.0'
7
+ STRING = '2.4.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: 2.3.0
4
+ version: 2.4.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-04-28 00:00:00.000000000 Z
13
+ date: 2021-06-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -183,6 +183,7 @@ files:
183
183
  - lib/rubocop/cop/rspec/focus.rb
184
184
  - lib/rubocop/cop/rspec/hook_argument.rb
185
185
  - lib/rubocop/cop/rspec/hooks_before_examples.rb
186
+ - lib/rubocop/cop/rspec/identical_equality_assertion.rb
186
187
  - lib/rubocop/cop/rspec/implicit_block_expectation.rb
187
188
  - lib/rubocop/cop/rspec/implicit_expect.rb
188
189
  - lib/rubocop/cop/rspec/implicit_subject.rb
@@ -213,6 +214,7 @@ files:
213
214
  - lib/rubocop/cop/rspec/overwriting_setup.rb
214
215
  - lib/rubocop/cop/rspec/pending.rb
215
216
  - lib/rubocop/cop/rspec/predicate_matcher.rb
217
+ - lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
216
218
  - lib/rubocop/cop/rspec/rails/http_status.rb
217
219
  - lib/rubocop/cop/rspec/receive_counts.rb
218
220
  - lib/rubocop/cop/rspec/receive_never.rb