Skip to content

Commit

Permalink
Merge pull request #66 from rubocop/fix/50
Browse files Browse the repository at this point in the history
Fix an incorrect autocorrect for `Capybara/CurrentPathExpectation`
  • Loading branch information
ydah authored Sep 19, 2023
2 parents d020309 + 292c1b8 commit 8696fdd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fix a false positive for `Capybara/SpecificFinders` when `find` with kind option. ([@ydah])
- Add new `Capybara/RSpec/HaveSelector` cop. ([@ydah])
- Add new `Capybara/ClickLinkOrButtonStyle` cop. ([@ydah])
- Fix an incorrect autocorrect for `Capybara/CurrentPathExpectation`. ([@ydah])

## 2.18.0 (2023-04-21)

Expand Down
10 changes: 4 additions & 6 deletions docs/modules/ROOT/pages/cops_capybara.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ This cop does not support autocorrection in some cases.
----
# bad
expect(current_path).to eq('/callback')
expect(page.current_path).to eq('/callback')
# good
expect(page).to have_current_path('/callback')
expect(page).to have_current_path('/callback', ignore_query: true)
# bad (does not support autocorrection)
expect(page.current_path).to match(variable)
# good
expect(page).to have_current_path('/callback')
# bad (does not support autocorrection when `match` with a variable)
expect(page).to match(variable)
----

=== References
Expand Down
27 changes: 10 additions & 17 deletions lib/rubocop/cop/capybara/current_path_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ module Capybara
# @example
# # bad
# expect(current_path).to eq('/callback')
# expect(page.current_path).to eq('/callback')
#
# # good
# expect(page).to have_current_path('/callback')
# expect(page).to have_current_path('/callback', ignore_query: true)
#
# # bad (does not support autocorrection)
# expect(page.current_path).to match(variable)
#
# # good
# expect(page).to have_current_path('/callback')
# # bad (does not support autocorrection when `match` with a variable)
# expect(page).to match(variable)
#
class CurrentPathExpectation < ::RuboCop::Cop::Base
extend AutoCorrector
Expand Down Expand Up @@ -95,7 +93,7 @@ def rewrite_expectation(corrector, node, to_symbol, matcher_node)
end
corrector.replace(matcher_node.loc.selector, matcher_method)
add_argument_parentheses(corrector, matcher_node.first_argument)
add_ignore_query_options(corrector, node)
add_ignore_query_options(corrector, node, matcher_node)
end

def convert_regexp_node_to_literal(corrector, matcher_node, regexp_node)
Expand Down Expand Up @@ -129,18 +127,13 @@ def method_call_with_no_parentheses?(arg_node)
# `have_current_path` with no options will include the querystring
# while `page.current_path` does not.
# This ensures the option `ignore_query: true` is added
# except when the expectation is a regexp or string
def add_ignore_query_options(corrector, node)
# except when `match` matcher.
def add_ignore_query_options(corrector, node, matcher_node)
return if matcher_node.method?(:match)

expectation_node = node.parent.last_argument
expectation_last_child = expectation_node.children.last
return if %i[
regexp str dstr xstr
].include?(expectation_last_child.type)

corrector.insert_after(
expectation_last_child,
', ignore_query: true'
)
corrector.insert_after(expectation_last_child, ', ignore_query: true')
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions spec/rubocop/cop/capybara/current_path_expectation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
RUBY

expect_correction(<<~RUBY)
expect(page).to have_current_path "/callback"
expect(page).to have_current_path "/callback", ignore_query: true
RUBY
end

Expand All @@ -22,7 +22,7 @@

expect_correction(<<~'RUBY')
expect(page).to have_current_path "/callback" \
"/foo"
"/foo", ignore_query: true
RUBY
end

Expand All @@ -33,7 +33,7 @@
RUBY

expect_correction(<<~RUBY)
expect(page).to have_current_path `pwd`
expect(page).to have_current_path `pwd`, ignore_query: true
RUBY
end

Expand All @@ -42,6 +42,10 @@
expect(page.current_path).to eq("/callback")
^^^^^^ Do not set an RSpec expectation on `current_path` in Capybara feature specs - instead, use the `have_current_path` matcher on `page`
RUBY

expect_correction(<<-RUBY)
expect(page).to have_current_path("/callback", ignore_query: true)
RUBY
end

it 'registers an offense when a variable is used' do
Expand Down

0 comments on commit 8696fdd

Please sign in to comment.