|
| 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