1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Don't add RAILS_ENV in generate action

In the case of generator, `RAILS_ENV` is interpreted as an argument as it
is. Avoid this because it will result unintended by the user.

Fixes #34979.
This commit is contained in:
yuuji.yaginuma 2019-01-19 16:39:18 +09:00
parent 3c6cfdf7ad
commit 82e9866703
2 changed files with 10 additions and 2 deletions

View file

@ -222,6 +222,7 @@ module Rails
log :generate, what
options = args.extract_options!
options[:without_rails_env] = true
argument = args.flat_map(&:to_s).join(" ")
execute_command :rails, "generate #{what} #{argument}", options
@ -284,14 +285,15 @@ module Rails
# based on the executor parameter provided.
def execute_command(executor, command, options = {}) # :doc:
log executor, command
env = options[:env] || ENV["RAILS_ENV"] || "development"
env = options[:env] || ENV["RAILS_ENV"] || "development"
rails_env = " RAILS_ENV=#{env}" unless options[:without_rails_env]
sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : ""
config = { verbose: false }
config[:capture] = options[:capture] if options[:capture]
config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) }
in_root { run("#{sudo}#{extify(executor)} #{command}#{rails_env}", config) }
end
# Add an extension to the given name based on the platform.

View file

@ -320,6 +320,12 @@ class ActionsTest < Rails::Generators::TestCase
assert_no_file "app/models/my_model.rb"
end
def test_generate_should_run_command_without_env
assert_called_with(generator, :run, ["rails generate model MyModel name:string", verbose: false]) do
action :generate, "model", "MyModel", "name:string"
end
end
def test_rake_should_run_rake_command_with_default_env
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do