From 82e986670389aef7a61c860bacd347c60ff0b25c Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sat, 19 Jan 2019 16:39:18 +0900 Subject: 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. --- railties/lib/rails/generators/actions.rb | 6 ++++-- railties/test/generators/actions_test.rb | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 3856a74a39..1a5f2ff203 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -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. diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 4932100ea2..44d4e92256 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -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 -- cgit v1.2.3