2009-09-25 22:32:28 -04:00
|
|
|
# Note:
|
|
|
|
# It is important to keep this file as light as possible
|
|
|
|
# the goal for tests that require this is to test booting up
|
|
|
|
# rails from an empty state, so anything added here could
|
|
|
|
# hide potential failures
|
|
|
|
#
|
|
|
|
# It is also good to know what is the bare minimum to get
|
|
|
|
# Rails booted up.
|
2009-10-07 20:02:36 -04:00
|
|
|
require 'fileutils'
|
|
|
|
|
2012-05-16 07:46:26 -04:00
|
|
|
require 'bundler/setup' unless defined?(Bundler)
|
2014-09-10 23:54:43 -04:00
|
|
|
require 'active_support'
|
2012-12-28 14:17:37 -05:00
|
|
|
require 'active_support/testing/autorun'
|
2015-01-15 01:42:33 -05:00
|
|
|
require 'active_support/testing/stream'
|
2012-01-05 20:30:17 -05:00
|
|
|
require 'active_support/test_case'
|
2009-09-25 22:32:28 -04:00
|
|
|
|
|
|
|
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")
|
|
|
|
|
|
|
|
# These files do not require any others and are needed
|
|
|
|
# to run the tests
|
2016-06-13 11:54:52 -04:00
|
|
|
require "active_support/core_ext/object/blank"
|
2012-05-11 12:56:05 -04:00
|
|
|
require "active_support/testing/isolation"
|
|
|
|
require "active_support/core_ext/kernel/reporting"
|
2012-06-19 17:27:52 -04:00
|
|
|
require 'tmpdir'
|
2009-09-25 22:32:28 -04:00
|
|
|
|
|
|
|
module TestHelpers
|
|
|
|
module Paths
|
2012-06-19 17:27:52 -04:00
|
|
|
def app_template_path
|
|
|
|
File.join Dir.tmpdir, 'app_template'
|
|
|
|
end
|
2009-10-14 19:13:45 -04:00
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
def tmp_path(*args)
|
2012-06-19 18:41:52 -04:00
|
|
|
@tmp_path ||= File.realpath(Dir.mktmpdir)
|
2012-06-19 19:27:54 -04:00
|
|
|
File.join(@tmp_path, *args)
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def app_path(*args)
|
|
|
|
tmp_path(*%w[app] + args)
|
|
|
|
end
|
2009-10-05 10:41:08 -04:00
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
def framework_path
|
|
|
|
RAILS_FRAMEWORK_ROOT
|
|
|
|
end
|
|
|
|
|
2009-10-05 10:41:08 -04:00
|
|
|
def rails_root
|
|
|
|
app_path
|
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Rack
|
2010-10-02 11:42:36 -04:00
|
|
|
def app(env = "production")
|
|
|
|
old_env = ENV["RAILS_ENV"]
|
|
|
|
@app ||= begin
|
|
|
|
ENV["RAILS_ENV"] = env
|
2015-09-27 17:34:13 -04:00
|
|
|
|
|
|
|
# FIXME: shush Sass warning spam, not relevant to testing Railties
|
|
|
|
Kernel.silence_warnings do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
Rails.application
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ENV["RAILS_ENV"] = old_env
|
|
|
|
end
|
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
def extract_body(response)
|
|
|
|
"".tap do |body|
|
|
|
|
response[2].each {|chunk| body << chunk }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(path)
|
|
|
|
@app.call(::Rack::MockRequest.env_for(path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_welcome(resp)
|
2016-01-25 11:26:20 -05:00
|
|
|
resp = Array(resp)
|
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
assert_equal 200, resp[0]
|
2015-01-17 11:45:15 -05:00
|
|
|
assert_match 'text/html', resp[1]["Content-Type"]
|
|
|
|
assert_match 'charset=utf-8', resp[1]["Content-Type"]
|
2016-01-25 11:26:20 -05:00
|
|
|
assert extract_body(resp).match(/Yay! You.*re on Rails!/)
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_success(resp)
|
|
|
|
assert_equal 202, resp[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_missing(resp)
|
|
|
|
assert_equal 404, resp[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_header(key, value, resp)
|
|
|
|
assert_equal value, resp[1][key.to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_body(expected, resp)
|
|
|
|
assert_equal expected, extract_body(resp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Generation
|
2011-04-15 12:14:12 -04:00
|
|
|
# Build an application by invoking the generator and going through the whole stack.
|
2009-09-29 19:07:29 -04:00
|
|
|
def build_app(options = {})
|
2011-06-06 12:46:30 -04:00
|
|
|
@prev_rails_env = ENV['RAILS_ENV']
|
2014-01-13 15:01:24 -05:00
|
|
|
ENV['RAILS_ENV'] = "development"
|
|
|
|
ENV['SECRET_KEY_BASE'] ||= SecureRandom.hex(16)
|
2011-06-06 08:54:05 -04:00
|
|
|
|
2009-09-29 19:07:29 -04:00
|
|
|
FileUtils.rm_rf(app_path)
|
2012-06-19 17:27:52 -04:00
|
|
|
FileUtils.cp_r(app_template_path, app_path)
|
2009-09-29 19:07:29 -04:00
|
|
|
|
|
|
|
# Delete the initializers unless requested
|
|
|
|
unless options[:initializers]
|
2016-05-28 10:11:20 -04:00
|
|
|
Dir["#{app_path}/config/initializers/**/*.rb"].each do |initializer|
|
2009-09-29 19:07:29 -04:00
|
|
|
File.delete(initializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-16 07:46:26 -04:00
|
|
|
gemfile_path = "#{app_path}/Gemfile"
|
|
|
|
if options[:gemfile].blank? && File.exist?(gemfile_path)
|
|
|
|
File.delete gemfile_path
|
2010-02-02 14:10:34 -05:00
|
|
|
end
|
|
|
|
|
2009-12-21 19:03:20 -05:00
|
|
|
routes = File.read("#{app_path}/config/routes.rb")
|
|
|
|
if routes =~ /(\n\s*end\s*)\Z/
|
|
|
|
File.open("#{app_path}/config/routes.rb", 'w') do |f|
|
2016-03-01 03:48:53 -05:00
|
|
|
f.puts $` + "\nActiveSupport::Deprecation.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
|
2009-12-21 19:03:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-24 03:26:34 -05:00
|
|
|
File.open("#{app_path}/config/database.yml", "w") do |f|
|
|
|
|
f.puts <<-YAML
|
|
|
|
default: &default
|
|
|
|
adapter: sqlite3
|
|
|
|
pool: 5
|
|
|
|
timeout: 5000
|
|
|
|
development:
|
|
|
|
<<: *default
|
|
|
|
database: db/development.sqlite3
|
|
|
|
test:
|
|
|
|
<<: *default
|
|
|
|
database: db/test.sqlite3
|
|
|
|
production:
|
|
|
|
<<: *default
|
|
|
|
database: db/production.sqlite3
|
|
|
|
YAML
|
|
|
|
end
|
|
|
|
|
2012-03-14 13:36:15 -04:00
|
|
|
add_to_config <<-RUBY
|
2012-08-01 09:07:01 -04:00
|
|
|
config.eager_load = false
|
2012-10-14 06:03:39 -04:00
|
|
|
config.session_store :cookie_store, key: "_myapp_session"
|
2012-03-14 13:36:15 -04:00
|
|
|
config.active_support.deprecation = :log
|
2014-09-10 21:34:55 -04:00
|
|
|
config.active_support.test_order = :random
|
2012-03-14 13:36:15 -04:00
|
|
|
config.action_controller.allow_forgery_protection = false
|
2014-11-24 18:39:11 -05:00
|
|
|
config.log_level = :info
|
2012-03-14 13:36:15 -04:00
|
|
|
RUBY
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown_app
|
|
|
|
ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env
|
|
|
|
end
|
|
|
|
|
2011-04-15 12:14:12 -04:00
|
|
|
# Make a very basic app, without creating the whole directory structure.
|
|
|
|
# This is faster and simpler than the method above.
|
2010-04-26 03:04:04 -04:00
|
|
|
def make_basic_app
|
|
|
|
require "rails"
|
|
|
|
require "action_controller/railtie"
|
2013-07-30 04:22:39 -04:00
|
|
|
require "action_view/railtie"
|
2015-09-25 18:53:15 -04:00
|
|
|
require 'action_dispatch/middleware/flash'
|
2010-04-26 03:04:04 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
@app = Class.new(Rails::Application)
|
|
|
|
@app.config.eager_load = false
|
|
|
|
@app.secrets.secret_key_base = "3b7cd727ee24e8444053437c36cc66c4"
|
|
|
|
@app.config.session_store :cookie_store, key: "_myapp_session"
|
|
|
|
@app.config.active_support.deprecation = :log
|
|
|
|
@app.config.active_support.test_order = :random
|
|
|
|
@app.config.log_level = :info
|
2010-04-26 03:04:04 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
yield @app if block_given?
|
|
|
|
@app.initialize!
|
2010-04-26 03:04:04 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
@app.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/" => "omg#index"
|
2010-04-26 03:04:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend ::Rack::Test::Methods
|
|
|
|
end
|
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
def simple_controller
|
|
|
|
controller :foo, <<-RUBY
|
|
|
|
class FooController < ApplicationController
|
|
|
|
def index
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: "foo"
|
2010-10-02 11:42:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action)'
|
2010-10-02 11:42:36 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2009-11-06 20:21:39 -05:00
|
|
|
class Bukkit
|
2010-01-26 08:58:00 -05:00
|
|
|
attr_reader :path
|
|
|
|
|
2009-11-06 20:21:39 -05:00
|
|
|
def initialize(path)
|
|
|
|
@path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(file, string)
|
|
|
|
path = "#{@path}/#{file}"
|
|
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
File.open(path, "w") {|f| f.puts string }
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(file)
|
|
|
|
File.delete("#{@path}/#{file}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-26 08:58:00 -05:00
|
|
|
def engine(name)
|
|
|
|
dir = "#{app_path}/random/#{name}"
|
|
|
|
FileUtils.mkdir_p(dir)
|
|
|
|
|
|
|
|
app = File.readlines("#{app_path}/config/application.rb")
|
|
|
|
app.insert(2, "$:.unshift(\"#{dir}/lib\")")
|
|
|
|
app.insert(3, "require #{name.inspect}")
|
|
|
|
|
|
|
|
File.open("#{app_path}/config/application.rb", 'r+') do |f|
|
|
|
|
f.puts app
|
|
|
|
end
|
|
|
|
|
2009-11-06 20:21:39 -05:00
|
|
|
Bukkit.new(dir).tap do |bukkit|
|
|
|
|
yield bukkit if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-21 18:45:11 -04:00
|
|
|
def script(script)
|
|
|
|
Dir.chdir(app_path) do
|
2013-01-06 18:13:47 -05:00
|
|
|
`#{Gem.ruby} #{app_path}/bin/rails #{script}`
|
2009-10-21 18:45:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-04 17:14:03 -05:00
|
|
|
def add_to_top_of_config(str)
|
|
|
|
environment = File.read("#{app_path}/config/application.rb")
|
|
|
|
if environment =~ /(Rails::Application\s*)/
|
|
|
|
File.open("#{app_path}/config/application.rb", 'w') do |f|
|
|
|
|
f.puts $` + $1 + "\n#{str}\n" + $'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
def add_to_config(str)
|
2009-10-15 17:50:02 -04:00
|
|
|
environment = File.read("#{app_path}/config/application.rb")
|
2009-12-21 19:57:23 -05:00
|
|
|
if environment =~ /(\n\s*end\s*end\s*)\Z/
|
2009-10-15 17:50:02 -04:00
|
|
|
File.open("#{app_path}/config/application.rb", 'w') do |f|
|
2009-10-14 19:13:45 -04:00
|
|
|
f.puts $` + "\n#{str}\n" + $1
|
2011-09-23 19:56:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_env_config(env, str)
|
|
|
|
environment = File.read("#{app_path}/config/environments/#{env}.rb")
|
|
|
|
if environment =~ /(\n\s*end\s*)\Z/
|
|
|
|
File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f|
|
|
|
|
f.puts $` + "\n#{str}\n" + $1
|
2009-09-29 19:07:29 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
2010-11-18 14:07:23 -05:00
|
|
|
def remove_from_config(str)
|
2016-02-09 19:31:00 -05:00
|
|
|
remove_from_file("#{app_path}/config/application.rb", str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_env_config(env, str)
|
|
|
|
remove_from_file("#{app_path}/config/environments/#{env}.rb", str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_file(file, str)
|
2010-11-18 15:10:37 -05:00
|
|
|
contents = File.read(file)
|
2016-02-09 19:31:00 -05:00
|
|
|
contents.sub!(/#{str}/, '')
|
|
|
|
File.write(file, contents)
|
2010-11-18 14:07:23 -05:00
|
|
|
end
|
|
|
|
|
2015-05-04 05:43:52 -04:00
|
|
|
def app_file(path, contents, mode = 'w')
|
2009-10-07 18:21:19 -04:00
|
|
|
FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
|
2015-05-04 05:43:52 -04:00
|
|
|
File.open("#{app_path}/#{path}", mode) do |f|
|
2009-09-25 22:32:28 -04:00
|
|
|
f.puts contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-21 10:18:50 -04:00
|
|
|
def remove_file(path)
|
|
|
|
FileUtils.rm_rf "#{app_path}/#{path}"
|
|
|
|
end
|
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
def controller(name, contents)
|
|
|
|
app_file("app/controllers/#{name}_controller.rb", contents)
|
|
|
|
end
|
2009-09-29 19:07:29 -04:00
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
def use_frameworks(arr)
|
2014-08-19 21:37:18 -04:00
|
|
|
to_remove = [:actionmailer, :activerecord] - arr
|
|
|
|
|
|
|
|
if to_remove.include?(:activerecord)
|
|
|
|
remove_from_config 'config.active_record.*'
|
|
|
|
end
|
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
|
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:30:17 -05:00
|
|
|
class ActiveSupport::TestCase
|
2009-09-25 22:32:28 -04:00
|
|
|
include TestHelpers::Paths
|
|
|
|
include TestHelpers::Rack
|
|
|
|
include TestHelpers::Generation
|
2015-01-15 01:42:33 -05:00
|
|
|
include ActiveSupport::Testing::Stream
|
2014-07-15 16:56:27 -04:00
|
|
|
|
2014-09-10 21:42:15 -04:00
|
|
|
self.test_order = :sorted
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a scope and build a fixture rails app
|
|
|
|
Module.new do
|
|
|
|
extend TestHelpers::Paths
|
2012-06-29 09:25:47 -04:00
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
# Build a rails app
|
2012-06-29 09:25:47 -04:00
|
|
|
FileUtils.rm_rf(app_template_path)
|
2012-06-19 17:27:52 -04:00
|
|
|
FileUtils.mkdir(app_template_path)
|
2009-10-21 18:45:11 -04:00
|
|
|
|
2016-02-27 13:02:02 -05:00
|
|
|
`#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/exe/rails new #{app_template_path} --skip-gemfile --skip-listen --no-rc`
|
2012-06-19 17:27:52 -04:00
|
|
|
File.open("#{app_template_path}/config/boot.rb", 'w') do |f|
|
2009-12-31 15:28:48 -05:00
|
|
|
f.puts "require 'rails/all'"
|
2009-10-21 18:45:11 -04:00
|
|
|
end
|
2011-06-07 06:16:05 -04:00
|
|
|
end unless defined?(RAILS_ISOLATED_ENGINE)
|