Skip to content

Commit

Permalink
Merge pull request rails#41611 from jonathanhefner/populate-argv-in-c…
Browse files Browse the repository at this point in the history
…ommand-invoke

Populate ARGV with Rails::Command.invoke args
  • Loading branch information
eugeneius committed Apr 3, 2021
2 parents 2086355 + ddcfde2 commit 80bd07a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
8 changes: 3 additions & 5 deletions railties/lib/rails/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ def invoke(full_namespace, args = [], **config)
command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)

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

command = find_by_namespace(namespace, command_name)
if command && command.all_commands[command_name]
Expand All @@ -52,7 +50,7 @@ def invoke(full_namespace, args = [], **config)
find_by_namespace("rake").perform(full_namespace, args, config)
end
ensure
ARGV.replace(old_argv)
ARGV.replace(original_argv)
end

# Rails finds namespaces similar to Thor, it only adds one rule:
Expand Down
8 changes: 1 addition & 7 deletions railties/lib/rails/generators/app_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def self.add_shared_options_for(name)
desc: "Show this help message and quit"
end

def initialize(positional_argv, option_argv, *)
@argv = [*positional_argv, *option_argv]
def initialize(*)
@gem_filter = lambda { |gem| true }
super
end
Expand Down Expand Up @@ -149,14 +148,9 @@ def create_root # :doc:
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:
Expand Down
24 changes: 16 additions & 8 deletions railties/test/command/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@ class Rails::Command::BaseTest < ActiveSupport::TestCase
assert_equal %w(db:system:change), Rails::Command::Db::System::ChangeCommand.printing_commands
end

test "ARGV is populated" do
class Rails::Command::ArgvCommand < Rails::Command::Base
def check_populated(*args)
raise "not populated" if ARGV.empty? || ARGV != args
end
end

assert_nothing_raised { Rails::Command.invoke("argv:check_populated", %w[foo bar]) }
end

test "ARGV is isolated" do
class Rails::Command::ArgvCommand < Rails::Command::Base
def check_isolation
raise "not isolated" unless ARGV.empty?
def check_isolated
ARGV << "isolate this"
end
end

old_argv = ARGV.dup
new_argv = ["foo", "bar"]
ARGV.replace(new_argv)
original_argv = ARGV.dup
ARGV.clear

Rails::Command.invoke("argv:check_isolation") # should not raise
assert_equal new_argv, ARGV
Rails::Command.invoke("argv:check_isolated")
assert_empty ARGV
ensure
ARGV.replace(old_argv)
ARGV.replace(original_argv)
end
end
1 change: 0 additions & 1 deletion railties/test/fixtures/lib/template.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true

say "It works from file!"
say "With ARGV! #{ARGV.join(" ")}"
7 changes: 0 additions & 7 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,6 @@ def test_template_from_dir_pwd
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
Expand Down

0 comments on commit 80bd07a

Please sign in to comment.