1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/command/base_test.rb
Jonathan Hefner 8ec7a2b7aa Isolate ARGV in Rails::Command.invoke
Follow-up to #38463.

By isolating ARGV, we guard against commands inadvertently depending on
prior ARGV contents.  Any such command will now behave consistently when
run via `Rails::Command.invoke`, whether coming from the Rails CLI or
from library code.  Likewise, any ARGV mutations done by a command will
not affect code that executes after `Rails::Command.invoke`.
2020-02-18 15:02:56 -06:00

33 lines
1 KiB
Ruby

# frozen_string_literal: true
require "abstract_unit"
require "rails/command"
require "rails/commands/generate/generate_command"
require "rails/commands/secrets/secrets_command"
require "rails/commands/db/system/change/change_command"
class Rails::Command::BaseTest < ActiveSupport::TestCase
test "printing commands" do
assert_equal %w(generate), Rails::Command::GenerateCommand.printing_commands
assert_equal %w(secrets:setup secrets:edit secrets:show), Rails::Command::SecretsCommand.printing_commands
assert_equal %w(db:system:change), Rails::Command::Db::System::ChangeCommand.printing_commands
end
test "ARGV is isolated" do
class Rails::Command::ArgvCommand < Rails::Command::Base
def check_isolation
raise "not isolated" unless ARGV.empty?
ARGV << "isolate this"
end
end
old_argv = ARGV.dup
new_argv = ["foo", "bar"]
ARGV.replace(new_argv)
Rails::Command.invoke("argv:check_isolation") # should not raise
assert_equal new_argv, ARGV
ensure
ARGV.replace(old_argv)
end
end