-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Add new Rails/AssertChangesSecondArgument
cop
#1401
Open
joseph-carino
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
joseph-carino:add_assert_changes_second_argument_cop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−0
Open
Changes from all commits
Commits
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 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 @@ | ||
* [#1396](https://github.com/rubocop/rubocop-rails/issues/1396): Add cop to prevent use of assert_changes with non-string second arguments. ([@joseph-carino][]) |
This file contains 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 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 Rails | ||
# Checks misuse of the second argument to ActiveSupport `assert_changes` method | ||
# | ||
# `assert_changes`'s second argument is the failure message emitted when the | ||
# first argument (expression) is unchanged in the block. | ||
# | ||
# A common mistake is to use `assert_changes` with the expected change | ||
# value delta as the second argument. | ||
# In that case `assert_changes` will validate only that the expression changes, | ||
# not that it changes by a specific value. | ||
# | ||
# Users should provide the 'from' and 'to' parameters, | ||
# or use `assert_difference` instead, which does support deltas. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# assert_changes -> { @value }, 1 do | ||
# @value += 1 | ||
# end | ||
# | ||
# # good | ||
# assert_changes -> { @value }, from: 0, to: 1 do | ||
# @value += 1 | ||
# end | ||
# assert_changes -> { @value }, "Value should change" do | ||
# @value += 1 | ||
# end | ||
# assert_difference -> { @value }, 1 do | ||
# @value += 1 | ||
# end | ||
# | ||
class AssertChangesSecondArgument < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use assert_changes to assert when an expression changes from and to specific values. ' \ | ||
'Use assert_difference instead to assert when an expression changes by a specific delta. ' \ | ||
'The second argument to assert_changes is the message emitted on assert failure.' | ||
|
||
def on_send(node) | ||
return if node.receiver || !node.method?(:assert_changes) | ||
return if node.arguments[1].nil? | ||
|
||
return unless offense?(node.arguments[1]) | ||
|
||
add_offense(node.loc.selector) do |corrector| | ||
corrector.replace(node.loc.selector, 'assert_difference') | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense?(arg) | ||
!arg.hash_type? && !arg.str_type? && !arg.dstr_type? && !arg.sym_type? && !arg.dsym_type? && !arg.variable? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
104 changes: 104 additions & 0 deletions
104
spec/rubocop/cop/rails/assert_changes_second_argument_spec.rb
This file contains 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,104 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Rails::AssertChangesSecondArgument, :config) do | ||
let(:message) do | ||
'Use assert_changes to assert when an expression changes from and to specific values. ' \ | ||
'Use assert_difference instead to assert when an expression changes by a specific delta. ' \ | ||
'The second argument to assert_changes is the message emitted on assert failure.' | ||
end | ||
|
||
describe('offenses') do | ||
it 'adds offense when the second positional argument is an integer' do | ||
expect_offense(<<~RUBY) | ||
assert_changes @value, -1 do | ||
^^^^^^^^^^^^^^ #{message} | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds offense when the second positional argument is a float' do | ||
expect_offense(<<~RUBY) | ||
assert_changes @value, -1.0 do | ||
^^^^^^^^^^^^^^ #{message} | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when the second argument is a string' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value, "Value should change" do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when the second argument is an interpolated string' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value, "\#{thing} should change" do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when the second argument is a symbol' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value, :should_change do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when the second argument is an interpolated symbol' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value, :"\#{thing}_should_change" do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when the second argument is a variable' do | ||
expect_no_offenses(<<~RUBY) | ||
message = "Value should change" | ||
assert_changes @value, message do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when there is only one argument' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when there is only one positional argument' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_changes @value, from: 0 do | ||
@value += 1 | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
describe('autocorrect') do | ||
it 'autocorrects method from assert_changes to assert_difference' do | ||
source = <<-RUBY | ||
assert_changes @value, -1.0 do | ||
@value += 1 | ||
end | ||
RUBY | ||
|
||
corrected_source = <<-RUBY | ||
assert_difference @value, -1.0 do | ||
@value += 1 | ||
end | ||
RUBY | ||
|
||
expect(autocorrect_source(source)).to(eq(corrected_source)) | ||
end | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a way to optimize it by defining
RESTRICT_ON_SEND
array so we can use it instead of the check (docs)