Skip to content

Commit 80bd07a

Browse files
authored
Merge pull request rails#41611 from jonathanhefner/populate-argv-in-command-invoke
Populate ARGV with Rails::Command.invoke args
2 parents 2086355 + ddcfde2 commit 80bd07a

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

railties/lib/rails/command.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ def invoke(full_namespace, args = [], **config)
4040
command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
4141
command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)
4242

43-
# isolate ARGV to ensure that commands depend only on the args they are given
44-
args = args.dup # args might *be* ARGV so dup before clearing
45-
old_argv = ARGV.dup
46-
ARGV.clear
43+
original_argv = ARGV.dup
44+
ARGV.replace(args)
4745

4846
command = find_by_namespace(namespace, command_name)
4947
if command && command.all_commands[command_name]
@@ -52,7 +50,7 @@ def invoke(full_namespace, args = [], **config)
5250
find_by_namespace("rake").perform(full_namespace, args, config)
5351
end
5452
ensure
55-
ARGV.replace(old_argv)
53+
ARGV.replace(original_argv)
5654
end
5755

5856
# Rails finds namespaces similar to Thor, it only adds one rule:

railties/lib/rails/generators/app_base.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ def self.add_shared_options_for(name)
110110
desc: "Show this help message and quit"
111111
end
112112

113-
def initialize(positional_argv, option_argv, *)
114-
@argv = [*positional_argv, *option_argv]
113+
def initialize(*)
115114
@gem_filter = lambda { |gem| true }
116115
super
117116
end
@@ -149,14 +148,9 @@ def create_root # :doc:
149148
end
150149

151150
def apply_rails_template # :doc:
152-
original_argv = ARGV.dup
153-
ARGV.replace(@argv)
154-
155151
apply rails_template if rails_template
156152
rescue Thor::Error, LoadError, Errno::ENOENT => e
157153
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
158-
ensure
159-
ARGV.replace(original_argv)
160154
end
161155

162156
def set_default_accessors! # :doc:

railties/test/command/base_test.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@ class Rails::Command::BaseTest < ActiveSupport::TestCase
1313
assert_equal %w(db:system:change), Rails::Command::Db::System::ChangeCommand.printing_commands
1414
end
1515

16+
test "ARGV is populated" do
17+
class Rails::Command::ArgvCommand < Rails::Command::Base
18+
def check_populated(*args)
19+
raise "not populated" if ARGV.empty? || ARGV != args
20+
end
21+
end
22+
23+
assert_nothing_raised { Rails::Command.invoke("argv:check_populated", %w[foo bar]) }
24+
end
25+
1626
test "ARGV is isolated" do
1727
class Rails::Command::ArgvCommand < Rails::Command::Base
18-
def check_isolation
19-
raise "not isolated" unless ARGV.empty?
28+
def check_isolated
2029
ARGV << "isolate this"
2130
end
2231
end
2332

24-
old_argv = ARGV.dup
25-
new_argv = ["foo", "bar"]
26-
ARGV.replace(new_argv)
33+
original_argv = ARGV.dup
34+
ARGV.clear
2735

28-
Rails::Command.invoke("argv:check_isolation") # should not raise
29-
assert_equal new_argv, ARGV
36+
Rails::Command.invoke("argv:check_isolated")
37+
assert_empty ARGV
3038
ensure
31-
ARGV.replace(old_argv)
39+
ARGV.replace(original_argv)
3240
end
3341
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# frozen_string_literal: true
22

33
say "It works from file!"
4-
say "With ARGV! #{ARGV.join(" ")}"

railties/test/generators/app_generator_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -741,13 +741,6 @@ def test_template_from_dir_pwd
741741
assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
742742
end
743743

744-
def test_argv_is_populated_for_template
745-
FileUtils.cd(Rails.root)
746-
argv = [destination_root, "-m", "lib/template.rb"]
747-
748-
assert_match %r/With ARGV! #{Regexp.escape argv.join(" ")}/, run_generator(argv)
749-
end
750-
751744
def test_usage_read_from_file
752745
assert_called(File, :read, returns: "USAGE FROM FILE") do
753746
assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc

0 commit comments

Comments
 (0)