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'
|
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
# TODO: Remove rubygems when possible
|
|
|
|
require 'rubygems'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
# TODO: Remove setting this magic constant
|
|
|
|
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")
|
|
|
|
|
|
|
|
# These files do not require any others and are needed
|
|
|
|
# to run the tests
|
|
|
|
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation"
|
|
|
|
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/declarative"
|
|
|
|
|
|
|
|
module TestHelpers
|
|
|
|
module Paths
|
|
|
|
module_function
|
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))
|
|
|
|
|
2009-09-25 22:32:28 -04:00
|
|
|
def tmp_path(*args)
|
2009-10-14 19:13:45 -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
|
|
|
|
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)
|
|
|
|
assert_equal 200, resp[0]
|
|
|
|
assert resp[1]["Content-Type"] = "text/html"
|
|
|
|
assert extract_body(resp).match(/Welcome aboard/)
|
|
|
|
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
|
2009-09-29 19:07:29 -04:00
|
|
|
def build_app(options = {})
|
|
|
|
FileUtils.rm_rf(app_path)
|
2009-09-25 22:32:28 -04:00
|
|
|
FileUtils.cp_r(tmp_path('app_template'), app_path)
|
2009-09-29 19:07:29 -04:00
|
|
|
|
|
|
|
# Delete the initializers unless requested
|
|
|
|
unless options[:initializers]
|
|
|
|
Dir["#{app_path}/config/initializers/*.rb"].each do |initializer|
|
|
|
|
File.delete(initializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-02 14:10:34 -05:00
|
|
|
unless options[:gemfile]
|
|
|
|
File.delete"#{app_path}/Gemfile"
|
|
|
|
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|
|
|
|
|
f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)'\n" + $1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"'
|
2009-10-14 19:13:45 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def plugin(name, string = "")
|
|
|
|
dir = "#{app_path}/vendor/plugins/#{name}"
|
|
|
|
FileUtils.mkdir_p(dir)
|
2010-01-26 08:58:00 -05:00
|
|
|
|
2009-11-06 20:21:39 -05:00
|
|
|
File.open("#{dir}/init.rb", 'w') do |f|
|
|
|
|
f.puts "::#{name.upcase} = 'loaded'"
|
|
|
|
f.puts string
|
|
|
|
end
|
2010-01-26 08:58:00 -05:00
|
|
|
|
|
|
|
Bukkit.new(dir).tap do |bukkit|
|
|
|
|
yield bukkit if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2010-02-06 11:18:10 -05:00
|
|
|
`#{Gem.ruby} #{app_path}/script/rails #{script}`
|
2009-10-21 18:45:11 -04:00
|
|
|
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
|
2009-09-29 19:07:29 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def app_file(path, contents)
|
2009-10-07 18:21:19 -04:00
|
|
|
FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
|
|
|
|
File.open("#{app_path}/#{path}", 'w') do |f|
|
2009-09-25 22:32:28 -04:00
|
|
|
f.puts contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
to_remove = [:actionmailer,
|
|
|
|
:activemodel,
|
|
|
|
:activerecord,
|
|
|
|
:activeresource] - arr
|
|
|
|
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
|
|
|
|
end
|
|
|
|
|
2009-09-29 19:07:29 -04:00
|
|
|
def boot_rails
|
2010-02-23 20:31:17 -05:00
|
|
|
require File.expand_path('../../../../load_paths', __FILE__)
|
2009-09-29 19:07:29 -04:00
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Test::Unit::TestCase
|
|
|
|
include TestHelpers::Paths
|
|
|
|
include TestHelpers::Rack
|
|
|
|
include TestHelpers::Generation
|
|
|
|
extend ActiveSupport::Testing::Declarative
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a scope and build a fixture rails app
|
|
|
|
Module.new do
|
|
|
|
extend TestHelpers::Paths
|
|
|
|
# Build a rails app
|
|
|
|
if File.exist?(tmp_path)
|
|
|
|
FileUtils.rm_rf(tmp_path)
|
|
|
|
end
|
|
|
|
FileUtils.mkdir(tmp_path)
|
2009-10-21 18:45:11 -04:00
|
|
|
|
2010-02-23 20:31:17 -05:00
|
|
|
environment = File.expand_path('../../../../load_paths', __FILE__)
|
|
|
|
if File.exist?("#{environment}.rb")
|
|
|
|
require_environment = "-r #{environment}"
|
|
|
|
end
|
2009-10-21 18:45:11 -04:00
|
|
|
|
2009-11-09 23:36:30 -05:00
|
|
|
`#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails #{tmp_path('app_template')}`
|
2009-10-21 18:45:11 -04:00
|
|
|
File.open("#{tmp_path}/app_template/config/boot.rb", 'w') do |f|
|
2010-02-23 20:31:17 -05:00
|
|
|
if require_environment
|
|
|
|
f.puts "Dir.chdir('#{File.dirname(environment)}') do"
|
|
|
|
f.puts " require '#{environment}'"
|
|
|
|
f.puts "end"
|
|
|
|
end
|
2009-12-31 15:28:48 -05:00
|
|
|
f.puts "require 'rails/all'"
|
2009-10-21 18:45:11 -04:00
|
|
|
end
|
2009-09-25 22:32:28 -04:00
|
|
|
end
|