rubocop-rspec 1.17.1 → 1.18.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
  SHA1:
3
- metadata.gz: 173b96ec8e65798ba9231c7d57413056381b680a
4
- data.tar.gz: 5b8b0010efd6c40c21d9febaf0017de0b46da612
3
+ metadata.gz: fef3eb92e763b31bdcf1b4a1adc0557e603b63d6
4
+ data.tar.gz: 48a039a782f517efb9cd9b7ff84f30acce3b9644
5
5
  SHA512:
6
- metadata.gz: d2c56f9cb57a365fb7dabcd9fd5d1f59d0f687947bc9a79ac85eae78beaff37d649e4bdcb980ef3771b21c083ebef7192de09510a8c5cf5f24f60f1288a8fcca
7
- data.tar.gz: 0e0b35c09f62bf77e0d9777a9ae87ab15b43c7c380b34956ae799fb4a91169352c0a932c8f548bb7f15064238fd9c562ad1b41ecd9d995b33f17d205562451f3
6
+ metadata.gz: 98638e435ef551ee81ee340767d6adf1de37bf36f3b848514cd244fbb6b7d8a43c2a30c049eb6454376e35d98f86f17e3bd2afbcc985ef424a73499c3b182ba6
7
+ data.tar.gz: a4024e740e9a5fc79189dfae61b2ba62e5b2ea649f78be9c3aa1b733c47b7bb9bed4a73744e4773f2cd402f8d99caae2e6c1d5e509d07cda2d789730d8783a18
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.18.0 (2017-09-29)
6
+
7
+ * Fix false positive in `Capybara/FeatureMethods`. ([@Darhazer][])
8
+ * Add `RSpec/Capybara/CurrentPathExpectation` cop for feature specs, disallowing setting expectations on `current_path`. ([@timrogers][])
9
+ * Fix false positive in `RSpec/LetBeforeExamples` cop when example group contains single let. ([@Darhazer][])
10
+
5
11
  ## 1.17.1 (2017-09-20)
6
12
 
7
13
  * Improved `RSpec/ReturnFromStub` to handle string interpolation, hashes and do..end blocks. ([@Darhazer][])
@@ -248,3 +254,4 @@
248
254
  [@pocke]: https://github.com/pocke
249
255
  [@bmorrall]: https:/github.com/bmorrall
250
256
  [@zverok]: https:/github.com/zverok
257
+ [@timrogers]: https://github.com/timrogers
data/config/default.yml CHANGED
@@ -325,6 +325,11 @@ RSpec/VoidExpect:
325
325
  Enabled: true
326
326
  StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VoidExpect
327
327
 
328
+ Capybara/CurrentPathExpectation:
329
+ Description: Checks that no expectations are set on Capybara's `current_path`.
330
+ Enabled: true
331
+ StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Capybara/CurrentPathExpectation
332
+
328
333
  Capybara/FeatureMethods:
329
334
  Description: Checks for consistent method usage in feature specs.
330
335
  Enabled: true
data/lib/rubocop-rspec.rb CHANGED
@@ -29,6 +29,7 @@ require 'rubocop/cop/rspec/any_instance'
29
29
  require 'rubocop/cop/rspec/around_block'
30
30
  require 'rubocop/cop/rspec/be_eql'
31
31
  require 'rubocop/cop/rspec/before_after_all'
32
+ require 'rubocop/cop/rspec/capybara/current_path_expectation'
32
33
  require 'rubocop/cop/rspec/capybara/feature_methods'
33
34
  require 'rubocop/cop/rspec/describe_class'
34
35
  require 'rubocop/cop/rspec/describe_method'
@@ -0,0 +1,42 @@
1
+ module RuboCop
2
+ module Cop
3
+ module RSpec
4
+ module Capybara
5
+ # Checks that no expectations are set on Capybara's `current_path`.
6
+ #
7
+ # The `have_current_path` matcher (http://www.rubydoc.info/github/
8
+ # teamcapybara/capybara/master/Capybara/RSpecMatchers#have_current_path-
9
+ # instance_method) should be used on `page` to set expectations on
10
+ # Capybara's current path, since it uses Capybara's waiting
11
+ # functionality (https://github.com/teamcapybara/capybara/blob/master/
12
+ # README.md#asynchronous-javascript-ajax-and-friends) which ensures that
13
+ # preceding actions (like `click_link`) have completed.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # expect(current_path).to eq('/callback')
18
+ # expect(page.current_path).to match(/widgets/)
19
+ #
20
+ # # good
21
+ # expect(page).to have_current_path("/callback")
22
+ # expect(page).to have_current_path(/widgets/)
23
+ #
24
+ class CurrentPathExpectation < Cop
25
+ MSG = 'Do not set an RSpec expectation on `current_path` in ' \
26
+ 'Capybara feature specs - instead, use the ' \
27
+ '`have_current_path` matcher on `page`'.freeze
28
+
29
+ def_node_matcher :expectation_set_on_current_path, <<-PATTERN
30
+ (send nil :expect (send {(send nil :page) nil} :current_path))
31
+ PATTERN
32
+
33
+ def on_send(node)
34
+ expectation_set_on_current_path(node) do
35
+ add_offense(node, :selector)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -45,13 +45,15 @@ module RuboCop
45
45
  feature: :describe
46
46
  }.freeze
47
47
 
48
- def_node_matcher :feature_method?, <<-PATTERN
49
- (send {(const nil :RSpec) nil} ${:#{MAP.keys.join(' :')}} ...)
48
+ def_node_matcher :feature_method, <<-PATTERN
49
+ (block
50
+ $(send {(const nil :RSpec) nil} ${#{MAP.keys.map(&:inspect).join(' ')}} ...)
51
+ ...)
50
52
  PATTERN
51
53
 
52
- def on_send(node)
53
- feature_method?(node) do |match|
54
- add_offense(node, :selector, format(MSG, MAP[match], match))
54
+ def on_block(node)
55
+ feature_method(node) do |send_node, match|
56
+ add_offense(send_node, :selector, format(MSG, MAP[match], match))
55
57
  end
56
58
  end
57
59
 
@@ -44,9 +44,13 @@ module RuboCop
44
44
  def on_block(node)
45
45
  return unless example_group_with_body?(node)
46
46
 
47
- _describe, _args, body = *node
47
+ check_let_declarations(node.body) if multiline_block?(node.body)
48
+ end
49
+
50
+ private
48
51
 
49
- check_let_declarations(body)
52
+ def multiline_block?(block)
53
+ block.begin_type?
50
54
  end
51
55
 
52
56
  def check_let_declarations(node)
@@ -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.1'.freeze
7
+ STRING = '1.18.0'.freeze
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,29 @@
1
+ RSpec.describe RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation do
2
+ subject(:cop) { described_class.new }
3
+
4
+ it 'flags violations for `expect(current_path)`' do
5
+ expect_offense(<<-RUBY)
6
+ expect(current_path).to eq("/callback")
7
+ ^^^^^^ Do not set an RSpec expectation on `current_path` in Capybara feature specs - instead, use the `have_current_path` matcher on `page`
8
+ RUBY
9
+ end
10
+
11
+ it 'flags violations for `expect(page.current_path)`' do
12
+ expect_offense(<<-RUBY)
13
+ expect(page.current_path).to eq("/callback")
14
+ ^^^^^^ Do not set an RSpec expectation on `current_path` in Capybara feature specs - instead, use the `have_current_path` matcher on `page`
15
+ RUBY
16
+ end
17
+
18
+ it "doesn't flag a violation for other expectations" do
19
+ expect_no_offenses(<<-RUBY)
20
+ expect(current_user).to eq(user)
21
+ RUBY
22
+ end
23
+
24
+ it "doesn't flag a violation for other references to `current_path`" do
25
+ expect_no_offenses(<<-RUBY)
26
+ current_path = WalkingRoute.last.path
27
+ RUBY
28
+ end
29
+ end
@@ -43,6 +43,16 @@ RSpec.describe RuboCop::Cop::RSpec::Capybara::FeatureMethods do
43
43
  RUBY
44
44
  end
45
45
 
46
+ it 'ignores variables inside examples' do
47
+ expect_no_offenses(<<-RUBY)
48
+ it 'is valid code' do
49
+ given(feature)
50
+ assign(background)
51
+ run scenario
52
+ end
53
+ RUBY
54
+ end
55
+
46
56
  include_examples 'autocorrect', 'background { }', 'before { }'
47
57
  include_examples 'autocorrect', 'scenario { }', 'it { }'
48
58
  include_examples 'autocorrect', 'xscenario { }', 'xit { }'
@@ -76,6 +76,16 @@ RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
76
76
  RUBY
77
77
  end
78
78
 
79
+ it 'ignores single-line example blocks' do
80
+ expect_no_offenses(<<-RUBY)
81
+ RSpec.describe User do
82
+ include_examples 'special user' do
83
+ let(:foo) { bar }
84
+ end
85
+ end
86
+ RUBY
87
+ end
88
+
79
89
  it 'does not encounter an error when handling an empty describe' do
80
90
  expect { inspect_source('RSpec.describe(User) do end', 'a_spec.rb') }
81
91
  .not_to raise_error
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.1
4
+ version: 1.18.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: 2017-09-20 00:00:00.000000000 Z
13
+ date: 2017-09-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -108,6 +108,7 @@ files:
108
108
  - lib/rubocop/cop/rspec/around_block.rb
109
109
  - lib/rubocop/cop/rspec/be_eql.rb
110
110
  - lib/rubocop/cop/rspec/before_after_all.rb
111
+ - lib/rubocop/cop/rspec/capybara/current_path_expectation.rb
111
112
  - lib/rubocop/cop/rspec/capybara/feature_methods.rb
112
113
  - lib/rubocop/cop/rspec/cop.rb
113
114
  - lib/rubocop/cop/rspec/describe_class.rb
@@ -183,6 +184,7 @@ files:
183
184
  - spec/rubocop/cop/rspec/around_block_spec.rb
184
185
  - spec/rubocop/cop/rspec/be_eql_spec.rb
185
186
  - spec/rubocop/cop/rspec/before_after_all_spec.rb
187
+ - spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb
186
188
  - spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb
187
189
  - spec/rubocop/cop/rspec/cop_spec.rb
188
190
  - spec/rubocop/cop/rspec/describe_class_spec.rb
@@ -277,6 +279,7 @@ test_files:
277
279
  - spec/rubocop/cop/rspec/around_block_spec.rb
278
280
  - spec/rubocop/cop/rspec/be_eql_spec.rb
279
281
  - spec/rubocop/cop/rspec/before_after_all_spec.rb
282
+ - spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb
280
283
  - spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb
281
284
  - spec/rubocop/cop/rspec/cop_spec.rb
282
285
  - spec/rubocop/cop/rspec/describe_class_spec.rb