2017-10-11 07:25:40 -04:00
|
|
|
require_relative '../../spec_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module RSpec
|
|
|
|
# This cop checks for ENV assignment in specs
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# before do
|
|
|
|
# ENV['FOO'] = 'bar'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# before do
|
|
|
|
# stub_env('FOO', 'bar')
|
|
|
|
# end
|
2017-09-19 11:25:42 -04:00
|
|
|
class EnvAssignment < RuboCop::Cop::Cop
|
2017-10-11 07:25:40 -04:00
|
|
|
include SpecHelpers
|
|
|
|
|
|
|
|
MESSAGE = "Don't assign to ENV, use `stub_env` instead.".freeze
|
|
|
|
|
|
|
|
def_node_search :env_assignment?, <<~PATTERN
|
|
|
|
(send (const nil? :ENV) :[]= ...)
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
# Following is what node.children looks like on a match
|
|
|
|
# [s(:const, nil, :ENV), :[]=, s(:str, "key"), s(:str, "value")]
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_spec?(node)
|
|
|
|
return unless env_assignment?(node)
|
|
|
|
|
2017-09-19 11:25:42 -04:00
|
|
|
add_offense(node, location: :expression, message: MESSAGE)
|
2017-10-11 07:25:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def autocorrect(node)
|
|
|
|
lambda do |corrector|
|
|
|
|
corrector.replace(node.loc.expression, stub_env(env_key(node), env_value(node)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def env_key(node)
|
|
|
|
node.children[2].source
|
|
|
|
end
|
|
|
|
|
|
|
|
def env_value(node)
|
|
|
|
node.children[3].source
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_env(key, value)
|
|
|
|
"stub_env(#{key}, #{value})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|