Merge pull request #28527 from rst/fix_runner_argv_28515

Correctly reset ARGV for "rails runner `CODE' arg arg arg..."
This commit is contained in:
Rafael França 2017-03-22 19:19:16 -04:00 committed by GitHub
commit 36334bf989
2 changed files with 11 additions and 2 deletions

@ -16,7 +16,7 @@ def self.banner(*)
"#{super} [<'Some.ruby(code)'> | <filename.rb>]" "#{super} [<'Some.ruby(code)'> | <filename.rb>]"
end end
def perform(code_or_file = nil, *file_argv) def perform(code_or_file = nil, *command_argv)
unless code_or_file unless code_or_file
help help
exit 1 exit 1
@ -27,9 +27,10 @@ def perform(code_or_file = nil, *file_argv)
require_application_and_environment! require_application_and_environment!
Rails.application.load_runner Rails.application.load_runner
ARGV.replace(command_argv)
if File.exist?(code_or_file) if File.exist?(code_or_file)
$0 = code_or_file $0 = code_or_file
ARGV.replace(file_argv)
Kernel.load code_or_file Kernel.load code_or_file
else else
begin begin

@ -35,6 +35,14 @@ def test_should_run_ruby_statement
assert_match "42", Dir.chdir(app_path) { `bin/rails runner "puts User.count"` } assert_match "42", Dir.chdir(app_path) { `bin/rails runner "puts User.count"` }
end end
def test_should_set_argv_when_running_code
output = Dir.chdir(app_path) {
# Both long and short args, at start and end of ARGV
`bin/rails runner "puts ARGV.join(',')" --foo a1 -b a2 a3 --moo`
}
assert_equal "--foo,a1,-b,a2,a3,--moo", output.chomp
end
def test_should_run_file def test_should_run_file
app_file "bin/count_users.rb", <<-SCRIPT app_file "bin/count_users.rb", <<-SCRIPT
puts User.count puts User.count