2010-06-22 09:14:46 -04:00
|
|
|
require 'isolation/abstract_unit'
|
2012-12-06 07:05:45 -05:00
|
|
|
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
|
|
|
|
boot_rails
|
|
|
|
|
|
|
|
# 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
|
|
|
|
assert_match "/rails runner", Dir.chdir(app_path) { `bundle exec rails runner` }
|
|
|
|
end
|
|
|
|
|
2010-09-16 00:02:33 -04:00
|
|
|
def test_should_include_runner_in_shebang_line_in_help
|
2010-09-19 03:39:42 -04:00
|
|
|
assert_match "/rails runner", Dir.chdir(app_path) { `bundle exec 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
|
2010-07-02 14:32:05 -04:00
|
|
|
assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "puts User.count"` }
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_run_file
|
|
|
|
app_file "script/count_users.rb", <<-SCRIPT
|
|
|
|
puts User.count
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "script/count_users.rb"` }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_set_dollar_0_to_file
|
|
|
|
app_file "script/dollar0.rb", <<-SCRIPT
|
|
|
|
puts $0
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
assert_match "script/dollar0.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/dollar0.rb"` }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_set_dollar_program_name_to_file
|
|
|
|
app_file "script/program_name.rb", <<-SCRIPT
|
|
|
|
puts $PROGRAM_NAME
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
assert_match "script/program_name.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/program_name.rb"` }
|
|
|
|
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
|
|
|
|
|
|
|
|
assert_match "true", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.application.config.ran"` }
|
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
|
|
|
|
def test_default_environment
|
|
|
|
assert_match "development", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_environment_with_rails_env
|
2012-12-06 07:05:45 -05:00
|
|
|
with_rails_env "production" do
|
|
|
|
assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
|
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
end
|
|
|
|
|
2012-12-05 12:27:17 -05:00
|
|
|
def test_environment_with_rack_env
|
2012-12-06 07:05:45 -05:00
|
|
|
with_rack_env "production" do
|
|
|
|
assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
|
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
end
|
2010-06-22 09:14:46 -04:00
|
|
|
end
|
|
|
|
end
|