2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
require "env_helpers"
|
2010-06-22 09:14:46 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class RunnerTest < ActiveSupport::TestCase
|
2010-06-22 09:14:46 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
2012-12-06 07:05:45 -05:00
|
|
|
include EnvHelpers
|
2010-06-22 09:14:46 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
|
|
|
|
# Lets create a model so we have something to play with
|
|
|
|
app_file "app/models/user.rb", <<-MODEL
|
|
|
|
class User
|
|
|
|
def self.count
|
|
|
|
42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2011-06-10 09:16:27 -04:00
|
|
|
def test_should_include_runner_in_shebang_line_in_help_without_option
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "/rails runner", rails("runner", allow_failure: true)
|
2011-06-10 09:16:27 -04:00
|
|
|
end
|
|
|
|
|
2010-09-16 00:02:33 -04:00
|
|
|
def test_should_include_runner_in_shebang_line_in_help
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "/rails runner", rails("runner", "--help")
|
2010-09-16 00:02:33 -04:00
|
|
|
end
|
|
|
|
|
2010-06-22 09:14:46 -04:00
|
|
|
def test_should_run_ruby_statement
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "42", rails("runner", "puts User.count")
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
|
2017-03-21 23:07:07 -04:00
|
|
|
def test_should_set_argv_when_running_code
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("runner", "puts ARGV.join(',')", "--foo", "a1", "-b", "a2", "a3", "--moo")
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "--foo,a1,-b,a2,a3,--moo", output.chomp
|
2017-03-21 23:07:07 -04:00
|
|
|
end
|
|
|
|
|
2010-06-22 09:14:46 -04:00
|
|
|
def test_should_run_file
|
2013-01-09 16:59:07 -05:00
|
|
|
app_file "bin/count_users.rb", <<-SCRIPT
|
2010-06-22 09:14:46 -04:00
|
|
|
puts User.count
|
|
|
|
SCRIPT
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "42", rails("runner", "bin/count_users.rb")
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
|
2016-10-21 14:27:26 -04:00
|
|
|
def test_no_minitest_loaded_in_production_mode
|
|
|
|
app_file "bin/print_features.rb", <<-SCRIPT
|
|
|
|
p $LOADED_FEATURES.grep(/minitest/)
|
|
|
|
SCRIPT
|
|
|
|
assert_match "[]", Dir.chdir(app_path) {
|
|
|
|
`RAILS_ENV=production bin/rails runner "bin/print_features.rb"`
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-06-22 09:14:46 -04:00
|
|
|
def test_should_set_dollar_0_to_file
|
2013-01-09 16:59:07 -05:00
|
|
|
app_file "bin/dollar0.rb", <<-SCRIPT
|
2010-06-22 09:14:46 -04:00
|
|
|
puts $0
|
|
|
|
SCRIPT
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "bin/dollar0.rb", rails("runner", "bin/dollar0.rb")
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_set_dollar_program_name_to_file
|
2013-01-09 16:59:07 -05:00
|
|
|
app_file "bin/program_name.rb", <<-SCRIPT
|
2010-06-22 09:14:46 -04:00
|
|
|
puts $PROGRAM_NAME
|
|
|
|
SCRIPT
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "bin/program_name.rb", rails("runner", "bin/program_name.rb")
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
2012-05-29 10:31:27 -04:00
|
|
|
|
2016-11-20 10:02:39 -05:00
|
|
|
def test_passes_extra_args_to_file
|
|
|
|
app_file "bin/program_name.rb", <<-SCRIPT
|
|
|
|
p ARGV
|
|
|
|
SCRIPT
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match %w( a b ).to_s, rails("runner", "bin/program_name.rb", "a", "b")
|
2016-11-20 10:02:39 -05:00
|
|
|
end
|
|
|
|
|
2016-08-31 11:28:44 -04:00
|
|
|
def test_should_run_stdin
|
|
|
|
app_file "bin/count_users.rb", <<-SCRIPT
|
|
|
|
puts User.count
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
assert_match "42", Dir.chdir(app_path) { `cat bin/count_users.rb | bin/rails runner -` }
|
|
|
|
end
|
|
|
|
|
2012-05-29 10:31:27 -04:00
|
|
|
def test_with_hook
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
runner do |app|
|
|
|
|
app.config.ran = true
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "true", rails("runner", "puts Rails.application.config.ran")
|
2012-05-29 10:31:27 -04:00
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
|
|
|
|
def test_default_environment
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "development", rails("runner", "puts Rails.env")
|
2012-12-05 12:05:33 -05:00
|
|
|
end
|
|
|
|
|
2019-03-12 21:09:41 -04:00
|
|
|
def test_environment_option
|
|
|
|
assert_match "production", rails("runner", "-e", "production", "puts Rails.env")
|
|
|
|
end
|
|
|
|
|
2019-03-03 18:12:05 -05:00
|
|
|
def test_environment_option_is_properly_expanded
|
|
|
|
assert_match "production", rails("runner", "-e", "prod", "puts Rails.env")
|
|
|
|
end
|
|
|
|
|
2016-01-05 22:53:38 -05:00
|
|
|
def test_runner_detects_syntax_errors
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("runner", "puts 'hello world", allow_failure: true)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate $?, :success?
|
2016-08-07 17:17:24 -04:00
|
|
|
assert_match "unterminated string meets end of file", output
|
2016-01-05 22:53:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_runner_detects_bad_script_name
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("runner", "iuiqwiourowe", allow_failure: true)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate $?, :success?
|
2016-05-28 16:04:13 -04:00
|
|
|
assert_match "undefined local variable or method `iuiqwiourowe' for", output
|
2016-01-05 22:53:38 -05:00
|
|
|
end
|
|
|
|
|
2012-12-05 12:05:33 -05:00
|
|
|
def test_environment_with_rails_env
|
2012-12-06 07:05:45 -05:00
|
|
|
with_rails_env "production" do
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "production", rails("runner", "puts Rails.env")
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
end
|
2015-03-20 11:14:11 -04:00
|
|
|
|
|
|
|
def test_environment_with_rack_env
|
|
|
|
with_rack_env "production" do
|
2017-09-03 12:55:26 -04:00
|
|
|
assert_match "production", rails("runner", "puts Rails.env")
|
2015-03-20 11:14:11 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-21 21:29:23 -04:00
|
|
|
|
|
|
|
def test_can_call_same_name_class_as_defined_in_thor
|
|
|
|
app_file "app/models/task.rb", <<-MODEL
|
|
|
|
class Task
|
|
|
|
def self.count
|
|
|
|
42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
|
|
|
|
assert_match "42", rails("runner", "puts Task.count")
|
|
|
|
end
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
end
|