Skip to content

Commit 487ff13

Browse files
authored
Merge pull request rails#42008 from iridakos/bugs/42007-runner-file-load
Expand path of user provided file in runner
2 parents e56a514 + 896733c commit 487ff13

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

railties/lib/rails/commands/runner/runner_command.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def perform(code_or_file = nil, *command_argv)
3838
if code_or_file == "-"
3939
eval($stdin.read, TOPLEVEL_BINDING, "stdin")
4040
elsif File.exist?(code_or_file)
41-
$0 = code_or_file
42-
Kernel.load code_or_file
41+
expanded_file_path = File.expand_path code_or_file
42+
$0 = expanded_file_path
43+
Kernel.load expanded_file_path
4344
else
4445
begin
4546
eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)

railties/test/commands/runner_test.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require "isolation/abstract_unit"
4+
require "rails/command"
5+
require "rails/commands/runner/runner_command"
6+
7+
class Rails::RunnerTest < ActiveSupport::TestCase
8+
include ActiveSupport::Testing::Isolation
9+
10+
setup :build_app
11+
teardown :teardown_app
12+
13+
def test_rails_runner_with_stdin
14+
command_output = `echo "puts 'Hello world'" | #{app_path}/bin/rails runner -`
15+
16+
assert_equal <<~OUTPUT, command_output
17+
Hello world
18+
OUTPUT
19+
end
20+
21+
def test_rails_runner_with_file
22+
# We intentionally define a file with a name that matches the one of the
23+
# script that we want to run to ensure that runner executes the latter one.
24+
app_file "lib/foo.rb", "# Lib file"
25+
26+
app_file "foo.rb", <<-RUBY
27+
puts "Hello world"
28+
RUBY
29+
30+
assert_equal <<~OUTPUT, run_runner_command("foo.rb")
31+
Hello world
32+
OUTPUT
33+
end
34+
35+
def test_rails_runner_with_ruby_code
36+
assert_equal <<~OUTPUT, run_runner_command('puts "Hello world"')
37+
Hello world
38+
OUTPUT
39+
end
40+
41+
def test_rails_runner_with_syntax_error_in_ruby_code
42+
command_output = run_runner_command("This is not ruby code", allow_failure: true)
43+
44+
assert_match(/Please specify a valid ruby command/, command_output)
45+
assert_equal 1, $?.exitstatus
46+
end
47+
48+
def test_rails_runner_with_name_error_in_ruby_code
49+
assert_raise(NameError) { IDoNotExist }
50+
51+
command_output = run_runner_command("IDoNotExist.new", allow_failure: true)
52+
53+
assert_match(/Please specify a valid ruby command/, command_output)
54+
assert_equal 1, $?.exitstatus
55+
end
56+
57+
private
58+
def run_runner_command(argument, allow_failure: false)
59+
rails "runner", argument, allow_failure: allow_failure
60+
end
61+
end

0 commit comments

Comments
 (0)