Skip to content

Commit 1f2503b

Browse files
authored
Merge pull request #1253 from ydah/fix-capybara-current-path-expectation-autocorrect
[Fix #1237] Fix `Capybara/CurrentPathExpectation` autocorrect incompatible with `Style/TrailingCommaInArguments` autocorrect
2 parents d0de0b7 + eaeadbb commit 1f2503b

File tree

6 files changed

+116
-9
lines changed

6 files changed

+116
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix a false positive for `RSpec/EmptyExampleGroup` when expectations in case statement. ([@ydah][])
66
* Add `RSpec/VerifiedDoubleReference` cop. ([@t3h2mas][])
77
* Make `RSpec/BeNil` cop configurable with a `be_nil` style and a `be` style. ([@bquorning][])
8+
* Fix `Capybara/CurrentPathExpectation` autocorrect incompatible with `Style/TrailingCommaInArguments` autocorrect. ([@ydah][])
89

910
## 2.9.0 (2022-02-28)
1011

lib/rubocop-rspec.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@
3737
# We have to register our autocorrect incompatibilities in RuboCop's cops
3838
# as well so we do not hit infinite loops
3939

40-
module RuboCop
41-
module Cop
42-
module Layout
43-
class ExtraSpacing # rubocop:disable Style/Documentation
44-
def self.autocorrect_incompatible_with
45-
[RSpec::AlignLeftLetBrace, RSpec::AlignRightLetBrace]
46-
end
47-
end
40+
RuboCop::Cop::Layout::ExtraSpacing.singleton_class.prepend(
41+
Module.new do
42+
def autocorrect_incompatible_with
43+
super.push(RuboCop::Cop::RSpec::AlignLeftLetBrace)
44+
.push(RuboCop::Cop::RSpec::AlignRightLetBrace)
4845
end
4946
end
50-
end
47+
)
48+
49+
RuboCop::Cop::Style::TrailingCommaInArguments.singleton_class.prepend(
50+
Module.new do
51+
def autocorrect_incompatible_with
52+
super.push(RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation)
53+
end
54+
end
55+
)
5156

5257
RuboCop::AST::Node.include(RuboCop::RSpec::Node)

lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class CurrentPathExpectation < Base
5252
$(send nil? :match (str $_)))
5353
PATTERN
5454

55+
def self.autocorrect_incompatible_with
56+
[Style::TrailingCommaInArguments]
57+
end
58+
5559
def on_send(node)
5660
expectation_set_on_current_path(node) do
5761
add_offense(node.loc.selector) do |corrector|

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'RuboCop::CLI --autocorrect', :isolated_environment do # rubocop:disable RSpec/DescribeClass
4+
subject(:cli) { RuboCop::CLI.new }
5+
6+
include_context 'when cli spec behavior'
7+
8+
context 'when corrects `RSpec/Capybara/CurrentPathExpectation` with ' \
9+
'`Style/TrailingCommaInArguments`' do
10+
before do
11+
RuboCop::ConfigLoader
12+
.default_configuration
13+
.for_all_cops['SuggestExtensions'] = false
14+
15+
create_file('.rubocop.yml', <<~YAML)
16+
Style/TrailingCommaInArguments:
17+
EnforcedStyleForMultiline: 'comma'
18+
YAML
19+
20+
create_file('spec/example.rb', <<-RUBY)
21+
expect(page.current_path).to eq(
22+
some_path(
23+
id: id
24+
)
25+
)
26+
RUBY
27+
end
28+
29+
it 'rubocop terminates with a succsess' do
30+
expect(cli.run(['-A', '--only',
31+
'RSpec/Capybara/CurrentPathExpectation,' \
32+
'Style/TrailingCommaInArguments'])).to eq(0)
33+
end
34+
35+
it 'autocorrects be compatible with each other' do
36+
cli.run(['-A', '--only',
37+
'RSpec/Capybara/CurrentPathExpectation,' \
38+
'Style/TrailingCommaInArguments'])
39+
40+
expect(File.read('spec/example.rb')).to eq(<<-RUBY)
41+
expect(page).to have_current_path(
42+
some_path(
43+
id: id,
44+
), ignore_query: true
45+
)
46+
RUBY
47+
end
48+
end
49+
end

spec/support/cli_spec_behavior.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_context 'when cli spec behavior' do
4+
include_context 'mock console output'
5+
6+
include FileHelper
7+
8+
before do
9+
RuboCop::ConfigLoader.debug = false
10+
11+
# OPTIMIZE: Makes these specs faster. Work directory (the parent of
12+
# .rubocop_cache) is removed afterwards anyway.
13+
RuboCop::ResultCache.inhibit_cleanup = true
14+
end
15+
16+
# Wrap all cli specs in `aggregate_failures` so that the expected and
17+
# actual results of every expectation per example are shown. This is
18+
# helpful because it shows information like expected and actual
19+
# $stdout messages while not using `aggregate_failures` will only
20+
# show information about expected and actual exit code
21+
around { |example| aggregate_failures(&example) }
22+
23+
after { RuboCop::ResultCache.inhibit_cleanup = false }
24+
end

spec/support/file_helper.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require 'fileutils'
4+
5+
module FileHelper
6+
def create_file(file_path, content)
7+
file_path = File.expand_path(file_path)
8+
create_dir file_path
9+
10+
File.open(file_path, 'w') do |file|
11+
case content
12+
when String
13+
file.puts content
14+
when Array
15+
file.puts content.join("\n")
16+
end
17+
end
18+
end
19+
20+
def create_dir(file_path)
21+
dir_path = File.dirname(file_path)
22+
FileUtils.makedirs dir_path unless File.exist?(dir_path)
23+
end
24+
end

0 commit comments

Comments
 (0)