Merge pull request #42008 from iridakos/bugs/42007-runner-file-load

Expand path of user provided file in runner
This commit is contained in:
Jean Boussier 2021-04-22 20:21:29 +02:00 committed by GitHub
commit 487ff1330c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View File

@ -38,8 +38,9 @@ module Rails
if code_or_file == "-"
eval($stdin.read, TOPLEVEL_BINDING, "stdin")
elsif File.exist?(code_or_file)
$0 = code_or_file
Kernel.load code_or_file
expanded_file_path = File.expand_path code_or_file
$0 = expanded_file_path
Kernel.load expanded_file_path
else
begin
eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)

View File

@ -0,0 +1,61 @@
# frozen_string_literal: true
require "isolation/abstract_unit"
require "rails/command"
require "rails/commands/runner/runner_command"
class Rails::RunnerTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
setup :build_app
teardown :teardown_app
def test_rails_runner_with_stdin
command_output = `echo "puts 'Hello world'" | #{app_path}/bin/rails runner -`
assert_equal <<~OUTPUT, command_output
Hello world
OUTPUT
end
def test_rails_runner_with_file
# We intentionally define a file with a name that matches the one of the
# script that we want to run to ensure that runner executes the latter one.
app_file "lib/foo.rb", "# Lib file"
app_file "foo.rb", <<-RUBY
puts "Hello world"
RUBY
assert_equal <<~OUTPUT, run_runner_command("foo.rb")
Hello world
OUTPUT
end
def test_rails_runner_with_ruby_code
assert_equal <<~OUTPUT, run_runner_command('puts "Hello world"')
Hello world
OUTPUT
end
def test_rails_runner_with_syntax_error_in_ruby_code
command_output = run_runner_command("This is not ruby code", allow_failure: true)
assert_match(/Please specify a valid ruby command/, command_output)
assert_equal 1, $?.exitstatus
end
def test_rails_runner_with_name_error_in_ruby_code
assert_raise(NameError) { IDoNotExist }
command_output = run_runner_command("IDoNotExist.new", allow_failure: true)
assert_match(/Please specify a valid ruby command/, command_output)
assert_equal 1, $?.exitstatus
end
private
def run_runner_command(argument, allow_failure: false)
rails "runner", argument, allow_failure: allow_failure
end
end