diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index c59f710f18..9152f2003f 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -110,8 +110,9 @@ module Rails desc: "Show this help message and quit" end - def initialize(*args) - @gem_filter = lambda { |gem| true } + def initialize(positional_argv, option_argv, *) + @argv = [*positional_argv, *option_argv] + @gem_filter = lambda { |gem| true } super end @@ -148,9 +149,14 @@ module Rails end def apply_rails_template # :doc: + original_argv = ARGV.dup + ARGV.replace(@argv) + apply rails_template if rails_template rescue Thor::Error, LoadError, Errno::ENOENT => e raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}" + ensure + ARGV.replace(original_argv) end def set_default_accessors! # :doc: diff --git a/railties/test/fixtures/lib/template.rb b/railties/test/fixtures/lib/template.rb index 44083c25e8..7a1adc30b4 100644 --- a/railties/test/fixtures/lib/template.rb +++ b/railties/test/fixtures/lib/template.rb @@ -1,3 +1,4 @@ # frozen_string_literal: true say "It works from file!" +say "With ARGV! #{ARGV.join(" ")}" diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 1da24b871b..60f5b6b8d2 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -731,6 +731,13 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) end + def test_argv_is_populated_for_template + FileUtils.cd(Rails.root) + argv = [destination_root, "-m", "lib/template.rb"] + + assert_match %r/With ARGV! #{Regexp.escape argv.join(" ")}/, run_generator(argv) + end + def test_usage_read_from_file assert_called(File, :read, returns: "USAGE FROM FILE") do assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc