-
-
Notifications
You must be signed in to change notification settings - Fork 263
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 Rails/RakeEnvironment cop #130
Merged
+227
−0
Merged
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
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,91 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks rake task definition without `environment` dependency. | ||
# `environment` dependency is important because it loads application code | ||
# for the rake task. The rake task cannot use application code, such as | ||
# models, without `environment` dependency. | ||
# | ||
# You can ignore the offense if the task satisfies at least one of the | ||
# following conditions: | ||
# | ||
# * The task does not need application code. | ||
# * The task invokes :environment task. | ||
# | ||
# @example | ||
# # bad | ||
# task :foo do | ||
# do_something | ||
# end | ||
# | ||
# # good | ||
# task foo: :environment do | ||
# do_something | ||
# end | ||
# | ||
class RakeEnvironment < Cop | ||
MSG = 'Set `:environment` task as a dependency to all rake task.' | ||
|
||
def_node_matcher :task_definition?, <<-PATTERN | ||
(send nil? :task ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless task_definition?(node) | ||
return if task_name(node) == :default | ||
return if with_dependencies?(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
private | ||
|
||
def task_name(node) | ||
first_arg = node.arguments[0] | ||
case first_arg&.type | ||
when :sym, :str | ||
return first_arg.value.to_sym | ||
when :hash | ||
return nil if first_arg.children.size != 1 | ||
|
||
pair = first_arg.children.first | ||
key = pair.children.first | ||
case key.type | ||
when :sym, :str | ||
key.value.to_sym | ||
end | ||
end | ||
end | ||
|
||
def with_dependencies?(node) | ||
first_arg = node.arguments[0] | ||
return false unless first_arg | ||
|
||
if first_arg.hash_type? | ||
with_hash_style_dependencies?(first_arg) | ||
else | ||
task_args = node.arguments[1] | ||
return false unless task_args | ||
return false unless task_args.hash_type? | ||
|
||
with_hash_style_dependencies?(task_args) | ||
end | ||
end | ||
|
||
def with_hash_style_dependencies?(hash_node) | ||
deps = hash_node.pairs.first&.value | ||
return false unless deps | ||
|
||
case deps.type | ||
when :array | ||
!deps.values.empty? | ||
else | ||
true | ||
end | ||
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
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
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,86 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RakeEnvironment do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense to task without :environment' do | ||
expect_offense(<<~RUBY) | ||
task :foo do | ||
^^^^^^^^^ Set `:environment` task as a dependency to all rake task. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with :environment ' \ | ||
'but it has other dependency before it' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: [:bar, `:environment`] do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with an dependency' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: :bar do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with dependencies' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: [:foo, :bar] do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with a dependency ' \ | ||
'as a method call' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: [:bar, dep] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with :environment' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: `:environment` do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with :environment ' \ | ||
'and other dependencies' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: [`:environment`, :bar] do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with :environment and an argument' do | ||
expect_no_offenses(<<~RUBY) | ||
task :foo, [:arg] => `:environment` do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with a dependency ' \ | ||
'as a method call' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: dep | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to task with dependencies ' \ | ||
'as a method call' do | ||
expect_no_offenses(<<~RUBY) | ||
task foo: [dep, :bar] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense to the default task' do | ||
expect_no_offenses(<<~RUBY) | ||
task default: :spec | ||
RUBY | ||
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.
Probably not a problem in most cases, but it is likely that calling
:environment
task will break a behavior. It may be better to mark it asSafe: false
. WDYT?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.
I agree with you. I added
Safe: false
. Thanks!