-
Notifications
You must be signed in to change notification settings - Fork 166
Enable additional Rubocop rules for Capybara tests #12413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module IdentityIdp | ||
| # This lint is similar to | ||
| # https://docs.rubocop.org/rubocop-capybara/cops_capybara.html#capybaracurrentpathexpectation | ||
| # and our `IdentityIdp::CapybaraCurrentUrlExpectLinter` in that it is intended to help prevent | ||
| # race conditions. A comparison of the `current_path` or `current_url` relies on page | ||
| # navigation timing which can be asynchronous and lead to inconsistent results. | ||
| # | ||
| # These cases typically come up when trying to support multiple action paths in a feature | ||
| # test. We should prefer being explicit about what actions are expected rather than relying | ||
| # on changing a shared helper, even if it results in a more verbose test. | ||
| # Using method parameters to control the flow is another option. The critical part is the | ||
| # conditional should be stable regardless of the timing of browser activity. | ||
| # | ||
| # @example | ||
| # #bad | ||
| # return if page.current_path == idv_document_capture_path | ||
| # | ||
| class CapybaraCurrentPathEqualityLinter < RuboCop::Cop::Base | ||
| include RangeHelp | ||
|
|
||
| MSG = 'Do not compare equality of `current_path` in Capybara feature specs - instead,' \ | ||
| ' use the `have_current_path` matcher on `page` or avoid it entirely' | ||
|
|
||
| RESTRICT_ON_SEND = %i[== !=].freeze | ||
|
|
||
| def_node_matcher :current_path_equality_lhs, <<~PATTERN | ||
| (send (send {(send nil? :page) nil?} :current_path) ${:== :!=} (...)) | ||
| PATTERN | ||
|
|
||
| def_node_matcher :current_path_equality_rhs, <<~PATTERN | ||
| (send (...) ${:== :!=} (send {(send nil? :page) nil?} :current_path)) | ||
| PATTERN | ||
|
|
||
| def on_send(node) | ||
| if current_path_equality_lhs(node) || current_path_equality_rhs(node) | ||
| add_offense(node) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module IdentityIdp | ||
| # This lint is a very similar to | ||
| # https://docs.rubocop.org/rubocop-capybara/cops_capybara.html#capybaracurrentpathexpectation | ||
| # but for `current_url` instead of `current_path`. The reasoning is the same in that it | ||
| # ensures usage of Capybara's waiting functionality. This linter is not autocorrectable. | ||
| # | ||
| # @example | ||
| # #bad | ||
| # expect(current_url).to eq authentication_methods_setup_url | ||
| # expect(current_url).to eq 'http://localhost:3001/auth/result' | ||
| # | ||
| # #good | ||
| # expect(page).to have_current_path(authentication_methods_setup_path) | ||
| # expect(page).to have_current_path('http://localhost:3001/auth/result', url: true) | ||
| # | ||
| class CapybaraCurrentUrlExpectLinter < RuboCop::Cop::Base | ||
| include RangeHelp | ||
|
|
||
| MSG = 'Do not set an RSpec expectation on `current_url` in ' \ | ||
| 'Capybara feature specs - instead, use the ' \ | ||
| '`have_current_path` matcher on `page`' | ||
|
|
||
| RESTRICT_ON_SEND = %i[expect].freeze | ||
|
|
||
| # @!method expectation_set_on_current_url(node) | ||
| def_node_matcher :expectation_set_on_current_url, <<~PATTERN | ||
| (send nil? :expect (send {(send nil? :page) nil?} :current_url)) | ||
| PATTERN | ||
|
|
||
| # Supported matchers: eq(...) / match(/regexp/) / match('regexp') | ||
| # @!method as_is_matcher(node) | ||
| def_node_matcher :as_is_matcher, <<~PATTERN | ||
| (send | ||
| #expectation_set_on_current_url ${:to :to_not :not_to} | ||
| ${(send nil? :eq ...) (send nil? :match (...)) (send nil? :include ...) (send nil? :start_with ...)}) | ||
| PATTERN | ||
|
|
||
| # @!method regexp_node_matcher(node) | ||
| def_node_matcher :regexp_node_matcher, <<~PATTERN | ||
| (send | ||
| #expectation_set_on_current_url ${:to :to_not :not_to} | ||
| $(send nil? :match ${str dstr xstr})) | ||
| PATTERN | ||
|
|
||
| def on_send(node) | ||
| expectation_set_on_current_url(node) do | ||
| as_is_matcher(node.parent) do | ||
| add_offense(node.parent.loc.selector) | ||
| end | ||
|
|
||
| regexp_node_matcher(node.parent) do | ||
| add_offense(node.parent.loc.selector) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| url: true, | ||
| ignore_query: true, | ||
| ) | ||
| expect(current_url).to start_with('http://localhost:7654/auth/result?error=access_denied') | ||
| params = UriService.params(current_url) | ||
| expect(params['error']).to eq('access_denied') | ||
|
||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.