Skip to content
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

Fix a false positive for RSpec/ExpectActual with rspec-rails #1764

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Support correcting `assert_nil` and `refute_nil` to `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])

## 2.26.1 (2024-01-05)

Expand Down Expand Up @@ -894,6 +895,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@mockdeep]: https://github.com/mockdeep
[@mothonmars]: https://github.com/MothOnMars
[@mvz]: https://github.com/mvz
[@naveg]: https://github.com/naveg
[@nc-holodakg]: https://github.com/nc-holodakg
[@nevir]: https://github.com/nevir
[@ngouy]: https://github.com/ngouy
Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop/cop/rspec/expect_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class ExpectActual < Base
regexp
].freeze

SUPPORTED_MATCHERS = %i[eq eql equal be].freeze
SKIPPED_MATCHERS = %i[route_to be_routable].freeze
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are planning to extract the rspec-rails specific parts from rubocop-rspec, I don’t think we should hardcode these matchers names. Can we either have (the non-existing) rubocop-rspec-rails inject them, or maybe just add a SkippedMatchers to the configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we wait to see how the rails extraction takes shape before doing that? It's true that this is rails-specific, but it's also highly unlikely to matter, since we'd only miss adding offenses for non-rails matchers that happened to be named "route_to" or "be_routable".

CORRECTABLE_MATCHERS = %i[eq eql equal be].freeze

# @!method expect_literal(node)
def_node_matcher :expect_literal, <<~PATTERN
Expand All @@ -66,8 +67,10 @@ class ExpectActual < Base

def on_send(node)
expect_literal(node) do |actual, matcher, expected|
next if SKIPPED_MATCHERS.include?(matcher)

add_offense(actual.source_range) do |corrector|
next unless SUPPORTED_MATCHERS.include?(matcher)
next unless CORRECTABLE_MATCHERS.include?(matcher)
next if literal?(expected)

swap(corrector, actual, expected)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rspec/expect_actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@
expect_no_corrections
end

it 'does not flag when the matcher is a rails routing matcher' do
expect_no_offenses(<<~RUBY)
describe Foo do
it 'routes correctly' do
expect({:get => "foo"}).to be_routable
expect({:get => "foo"}).to route_to("bar#baz")
end
end
RUBY
end

context 'when inspecting rspec-rails routing specs' do
let(:cop_config) { {} }

Expand Down