rubocop-rspec 1.17.0 → 1.17.1

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
  SHA1:
3
- metadata.gz: 920ff6aef932dcc189cf4a9b36a4da113414e514
4
- data.tar.gz: 924b473a982bea7b74862f9d38c7adebd54b0d15
3
+ metadata.gz: 173b96ec8e65798ba9231c7d57413056381b680a
4
+ data.tar.gz: 5b8b0010efd6c40c21d9febaf0017de0b46da612
5
5
  SHA512:
6
- metadata.gz: 95f9ed3893655f17e5d78fbe03decd86deb93fab86db2599f2848795c035eeea9e4347df6b3700f3cc6e31599bd76e37ab4ab57477a9c17b9cf462c479bafb9a
7
- data.tar.gz: ad2e034857001d8fd654008c96446fb48641dde3787a2ace21709005c2c185f8fa2a370a11e7dca03d42ccb428f1d110de574eaf286bf17447f92b8bc08dd2c5
6
+ metadata.gz: d2c56f9cb57a365fb7dabcd9fd5d1f59d0f687947bc9a79ac85eae78beaff37d649e4bdcb980ef3771b21c083ebef7192de09510a8c5cf5f24f60f1288a8fcca
7
+ data.tar.gz: 0e0b35c09f62bf77e0d9777a9ae87ab15b43c7c380b34956ae799fb4a91169352c0a932c8f548bb7f15064238fd9c562ad1b41ecd9d995b33f17d205562451f3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.17.1 (2017-09-20)
6
+
7
+ * Improved `RSpec/ReturnFromStub` to handle string interpolation, hashes and do..end blocks. ([@Darhazer][])
8
+ * Fixed compatibility with JRuby. ([@zverok][])
9
+
5
10
  ## 1.17.0 (2017-09-14)
6
11
 
7
12
  * Add `RSpec/Capybara` namespace including the first cop for feature specs: `Capybara/FeatureMethods`. ([@rspeicher][])
@@ -242,3 +247,4 @@
242
247
  [@jonatas]: https://github.com/jonatas
243
248
  [@pocke]: https://github.com/pocke
244
249
  [@bmorrall]: https:/github.com/bmorrall
250
+ [@zverok]: https:/github.com/zverok
@@ -5,10 +5,11 @@ module RuboCop
5
5
  # Clone of the the normal RuboCop::Cop::Cop class so we can rewrite
6
6
  # the inherited method without breaking functionality
7
7
  class WorkaroundCop
8
- # Remove the cop inherited method to be a noop. Our RSpec::Cop
8
+ # Remove the Cop.inherited method to be a noop. Our RSpec::Cop
9
9
  # class will invoke the inherited hook instead
10
10
  class << self
11
- remove_method :inherited
11
+ undef inherited
12
+ def inherited(*) end
12
13
  end
13
14
 
14
15
  # Special case `Module#<` so that the rspec support rubocop exports
@@ -39,43 +39,38 @@ module RuboCop
39
39
  MSG_AND_RETURN = 'Use `and_return` for static values.'.freeze
40
40
  MSG_BLOCK = 'Use block for static values.'.freeze
41
41
 
42
- def_node_matcher :receive_with_block, <<-PATTERN
43
- (block
44
- (send nil :receive ...)
45
- (args)
46
- $(...)
47
- )
48
- PATTERN
49
-
50
42
  def_node_matcher :and_return_value, <<-PATTERN
51
43
  (send
52
44
  (send nil :receive (...)) :and_return $(...)
53
45
  )
54
46
  PATTERN
55
47
 
56
- def on_block(node)
57
- return unless style == :and_return
58
- receive_with_block(node) do |args|
59
- add_offense(node, :expression, MSG_AND_RETURN) unless dynamic?(args)
48
+ def on_send(node)
49
+ if style == :block
50
+ check_and_return_call(node)
51
+ elsif node.method_name == :receive
52
+ check_block_body(node)
60
53
  end
61
54
  end
62
55
 
63
- def on_send(node)
64
- return unless style == :block
56
+ private
65
57
 
58
+ def check_and_return_call(node)
66
59
  and_return_value(node) do |args|
67
60
  add_offense(node, :expression, MSG_BLOCK) unless dynamic?(args)
68
61
  end
69
62
  end
70
63
 
71
- private
64
+ def check_block_body(node)
65
+ block = node.each_ancestor(:block).first
66
+ return unless block
72
67
 
73
- def dynamic?(node)
74
- if node.array_type?
75
- return node.each_child_node.any? { |child| dynamic?(child) }
76
- end
68
+ _receiver, _args, body = *block
69
+ add_offense(node, :expression, MSG_AND_RETURN) unless dynamic?(body)
70
+ end
77
71
 
78
- !node.literal?
72
+ def dynamic?(node)
73
+ !node.recursive_literal?
79
74
  end
80
75
  end
81
76
  end
@@ -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 = '1.17.0'.freeze
7
+ STRING = '1.17.1'.freeze
8
8
  end
9
9
  end
10
10
  end
@@ -12,7 +12,7 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
12
12
  expect_offense(<<-RUBY)
13
13
  it do
14
14
  allow(Foo).to receive(:bar) { 42 }
15
- ^^^^^^^^^^^^^^^^^^^^ Use `and_return` for static values.
15
+ ^^^^^^^^^^^^^ Use `and_return` for static values.
16
16
  end
17
17
  RUBY
18
18
  end
@@ -21,7 +21,16 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
21
21
  expect_offense(<<-RUBY)
22
22
  it do
23
23
  allow(Foo).to receive(:bar) { [42, 43] }
24
- ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `and_return` for static values.
24
+ ^^^^^^^^^^^^^ Use `and_return` for static values.
25
+ end
26
+ RUBY
27
+ end
28
+
29
+ it 'finds hash with only static values returned from block' do
30
+ expect_offense(<<-RUBY)
31
+ it do
32
+ allow(Foo).to receive(:bar) { {a: 42, b: 43} }
33
+ ^^^^^^^^^^^^^ Use `and_return` for static values.
25
34
  end
26
35
  RUBY
27
36
  end
@@ -52,6 +61,51 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
52
61
  end
53
62
  RUBY
54
63
  end
64
+
65
+ it 'ignores hash with dynamic values returned from block' do
66
+ expect_no_offenses(<<-RUBY)
67
+ it do
68
+ allow(Foo).to receive(:bar) { {a: 42, b: baz} }
69
+ end
70
+ RUBY
71
+ end
72
+
73
+ it 'ignores block returning string with interpolation' do
74
+ expect_no_offenses(<<-RUBY)
75
+ it do
76
+ bar = 42
77
+ allow(Foo).to receive(:bar) { "You called \#{bar}" }
78
+ end
79
+ RUBY
80
+ end
81
+
82
+ it 'finds concatenated strings with no variables' do
83
+ expect_offense(<<-RUBY)
84
+ it do
85
+ allow(Foo).to receive(:bar) do
86
+ ^^^^^^^^^^^^^ Use `and_return` for static values.
87
+ "You called" \
88
+ "me"
89
+ end
90
+ end
91
+ RUBY
92
+ end
93
+
94
+ it 'ignores stubs without return value' do
95
+ expect_no_offenses(<<-RUBY)
96
+ it do
97
+ allow(Foo).to receive(:bar)
98
+ end
99
+ RUBY
100
+ end
101
+
102
+ it 'handles stubs in a method' do
103
+ expect_no_offenses(<<-RUBY)
104
+ def stub_foo
105
+ allow(Foo).to receive(:bar)
106
+ end
107
+ RUBY
108
+ end
55
109
  end
56
110
 
57
111
  context 'with EnforcedStyle `block`' do
@@ -74,6 +128,15 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
74
128
  RUBY
75
129
  end
76
130
 
131
+ it 'ignores string with interpolation returned from method' do
132
+ expect_no_offenses(<<-RUBY)
133
+ it do
134
+ bar = 42
135
+ allow(Foo).to receive(:bar).and_return("You called \#{bar}")
136
+ end
137
+ RUBY
138
+ end
139
+
77
140
  it 'ignores multiple values being returned from method' do
78
141
  expect_no_offenses(<<-RUBY)
79
142
  it do
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: 1.17.0
4
+ version: 1.17.1
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: 2017-09-14 00:00:00.000000000 Z
13
+ date: 2017-09-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop