mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Populate ARGV for app template
In #38495, `ARGV` was isolated to prevent commands from depending on its contents, which might be indeterminate. However, app templates may depend on `ARGV`, so populate it before evaluating them. Fixes #40945.
This commit is contained in:
parent
afc79e3cb0
commit
2e6dae1a26
3 changed files with 16 additions and 2 deletions
|
@ -110,7 +110,8 @@ module Rails
|
||||||
desc: "Show this help message and quit"
|
desc: "Show this help message and quit"
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(*args)
|
def initialize(positional_argv, option_argv, *)
|
||||||
|
@argv = [*positional_argv, *option_argv]
|
||||||
@gem_filter = lambda { |gem| true }
|
@gem_filter = lambda { |gem| true }
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
@ -148,9 +149,14 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_rails_template # :doc:
|
def apply_rails_template # :doc:
|
||||||
|
original_argv = ARGV.dup
|
||||||
|
ARGV.replace(@argv)
|
||||||
|
|
||||||
apply rails_template if rails_template
|
apply rails_template if rails_template
|
||||||
rescue Thor::Error, LoadError, Errno::ENOENT => e
|
rescue Thor::Error, LoadError, Errno::ENOENT => e
|
||||||
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
|
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
|
||||||
|
ensure
|
||||||
|
ARGV.replace(original_argv)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_default_accessors! # :doc:
|
def set_default_accessors! # :doc:
|
||||||
|
|
1
railties/test/fixtures/lib/template.rb
vendored
1
railties/test/fixtures/lib/template.rb
vendored
|
@ -1,3 +1,4 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
say "It works from file!"
|
say "It works from file!"
|
||||||
|
say "With ARGV! #{ARGV.join(" ")}"
|
||||||
|
|
|
@ -731,6 +731,13 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
||||||
assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
|
assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
|
||||||
end
|
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
|
def test_usage_read_from_file
|
||||||
assert_called(File, :read, returns: "USAGE FROM FILE") do
|
assert_called(File, :read, returns: "USAGE FROM FILE") do
|
||||||
assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc
|
assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc
|
||||||
|
|
Loading…
Reference in a new issue