提交 6b086449 编写于 作者: C Carl Lerche

Fix the broken railties isolation tests

上级 6f6a589d
...@@ -31,10 +31,15 @@ def run(result) ...@@ -31,10 +31,15 @@ def run(result)
@_result = result @_result = result
proxy = run_in_isolation do |proxy| serialized = run_in_isolation do |proxy|
super(proxy) { } begin
super(proxy) { }
rescue Exception => e
proxy.add_error(Test::Unit::Error.new(name, e))
end
end end
proxy = Marshal.load(serialized)
proxy.__replay__(@_result) proxy.__replay__(@_result)
yield(Test::Unit::TestCase::FINISHED, name) yield(Test::Unit::TestCase::FINISHED, name)
...@@ -55,7 +60,7 @@ def run_in_isolation(&blk) ...@@ -55,7 +60,7 @@ def run_in_isolation(&blk)
write.close write.close
result = read.read result = read.read
Process.wait2(pid) Process.wait2(pid)
Marshal.load(result.unpack("m")[0]) return result.unpack("m")[0]
end end
end end
...@@ -83,7 +88,7 @@ def run_in_isolation(&blk) ...@@ -83,7 +88,7 @@ def run_in_isolation(&blk)
ENV.delete("ISOLATION_TEST") ENV.delete("ISOLATION_TEST")
ENV.delete("ISOLATION_OUTPUT") ENV.delete("ISOLATION_OUTPUT")
return Marshal.load(tmpfile.read.unpack("m")[0]) return tmpfile.read.unpack("m")[0]
end end
end end
end end
......
module Rails module Rails
class Application class Application
attr_accessor :routes, :config attr_accessor :config
def self.load(environment_file) def self.load(environment_file)
environment = File.read(environment_file) environment = File.read(environment_file)
Object.class_eval(environment, environment_file) Object.class_eval(environment, environment_file)
end end
def initialize def routes
@routes = ActionController::Routing::Routes ActionController::Routing::Routes
end end
def middleware def middleware
......
require "isolation/abstract_unit" require "isolation/abstract_unit"
require "rails" # require "rails"
require 'action_dispatch' # require 'action_dispatch'
module ApplicationTests module ApplicationTests
class LoadTest < Test::Unit::TestCase class LoadTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
def rackup def rackup
ActionDispatch::Utils.parse_config("#{app_path}/config.ru") config = "#{app_path}/config.ru"
# Copied from ActionDispatch::Utils.parse_config
# ActionDispatch is not necessarily available at this point.
require 'rack'
if config =~ /\.ru$/
cfgfile = ::File.read(config)
if cfgfile[/^#\\(.*)/]
opts.parse! $1.split(/\s+/)
end
inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
nil, config
else
require config
inner_app = Object.const_get(::File.basename(config, '.rb').capitalize)
end
end end
def setup def setup
build_app build_app
boot_rails
end end
test "rails app is present" do test "rails app is present" do
......
...@@ -62,7 +62,7 @@ class VendorBootTest < Test::Unit::TestCase ...@@ -62,7 +62,7 @@ class VendorBootTest < Test::Unit::TestCase
def test_load_initializer_requires_from_vendor_rails def test_load_initializer_requires_from_vendor_rails
boot = VendorBoot.new boot = VendorBoot.new
boot.expects(:require).with("rails/initializer") boot.expects(:require).with("rails")
Rails::Initializer.expects(:run).with(:install_gem_spec_stubs) Rails::Initializer.expects(:run).with(:install_gem_spec_stubs)
Rails::GemDependency.expects(:add_frozen_gem_path) Rails::GemDependency.expects(:add_frozen_gem_path)
boot.load_initializer boot.load_initializer
...@@ -76,7 +76,7 @@ def test_load_initializer_loads_rubygems_and_the_rails_gem ...@@ -76,7 +76,7 @@ def test_load_initializer_loads_rubygems_and_the_rails_gem
boot = GemBoot.new boot = GemBoot.new
GemBoot.expects(:load_rubygems) GemBoot.expects(:load_rubygems)
boot.expects(:load_rails_gem) boot.expects(:load_rails_gem)
boot.expects(:require).with('rails/initializer') boot.expects(:require).with('rails')
boot.load_initializer boot.load_initializer
end end
......
require "initializer/test_helper" require "isolation/abstract_unit"
module InitializerTests module InitializerTests
class PathsTest < Test::Unit::TestCase class PathsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
end
test "rails does not initialize with ruby version 1.8.1" do test "rails does not initialize with ruby version 1.8.1" do
assert_rails_does_not_boot "1.8.1" assert_rails_does_not_boot "1.8.1"
end end
......
require "initializer/test_helper"
module InitializerTests
class GemSpecStubsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
$stderr = StringIO.new
end
test "user has an old boot.rb (defined by having no Rails.vendor_rails?)" do
class << Rails
undef vendor_rails?
end
assert_stderr(/outdated/) do
assert_raises(SystemExit) do
Rails::Initializer.run { |c| c.frameworks = [] }
end
end
end
test "requires rubygems" do
Kernel.module_eval do
alias old_require require
def require(name)
$rubygems_required = true if name == "rubygems"
old_require(name)
end
end
Rails.vendor_rails = true
Rails::Initializer.run { |c| c.frameworks = [] }
assert $rubygems_required
end
# Pending until we're further along
# test "does not fail if rubygems does not exist" do
# Kernel.module_eval do
# alias old_require require
# def require(name)
# raise LoadError if name == "rubygems"
# old_require(name)
# end
# end
#
# assert_nothing_raised do
# Rails::Initializer.run { |c| c.frameworks = [] }
# end
# end
test "adds fake Rubygems stubs if a framework is not loaded in Rubygems and we've vendored" do
Rails.vendor_rails = true
Rails::Initializer.run { |c| c.frameworks = [] }
%w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub|
gem_spec = Gem.loaded_specs[stub]
assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
assert_equal stub, gem_spec.name
assert_equal "", gem_spec.loaded_from
end
end
test "doesn't replace gem specs that are already loaded" do
Rails.vendor_rails = true
Gem.loaded_specs["rails"] = Gem::Specification.new do |s|
s.name = "rails"
s.version = Rails::VERSION::STRING
s.loaded_from = "/foo/bar/baz"
end
Rails::Initializer.run { |c| c.frameworks = [] }
assert_equal "/foo/bar/baz", Gem.loaded_specs["rails"].loaded_from
%w(activesupport activerecord actionpack actionmailer activeresource).each do |stub|
gem_spec = Gem.loaded_specs[stub]
assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
assert_equal stub, gem_spec.name
assert_equal "", gem_spec.loaded_from
end
end
end
end
\ No newline at end of file
require "initializer/test_helper" require "isolation/abstract_unit"
class PathsTest < Test::Unit::TestCase class PathsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
def self.setup def setup
build_app
boot_rails
Rails::Initializer.run do |config| Rails::Initializer.run do |config|
config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record] config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record]
config.after_initialize do config.after_initialize do
ActionController::Base.session_store = nil ActionController::Base.session_store = nil
end end
end end
end
def setup
@paths = Rails::Initializer.default.config.paths @paths = Rails::Initializer.default.config.paths
end end
def root(*path) def root(*path)
File.expand_path(File.join(File.dirname(__FILE__), "root", *path)) app_path(*path).to_s
end end
def assert_path(paths, *dir) def assert_path(paths, *dir)
assert_equal [root(*dir)], paths.paths assert_equal [root(*dir)], paths.paths
end end
def assert_in_load_path(*path)
assert $:.any? { |p| File.expand_path(p) == root(*path) }, "Load path does not include '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
end
def assert_not_in_load_path(*path)
assert !$:.any? { |p| File.expand_path(p) == root(*path) }, "Load path includes '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
end
test "booting up Rails yields a valid paths object" do test "booting up Rails yields a valid paths object" do
assert_path @paths.app, "app" assert_path @paths.app, "app"
assert_path @paths.app.metals, "app", "metal" assert_path @paths.app.metals, "app", "metal"
...@@ -39,8 +46,7 @@ def assert_path(paths, *dir) ...@@ -39,8 +46,7 @@ def assert_path(paths, *dir)
assert_path @paths.config.locales, "config", "locales" assert_path @paths.config.locales, "config", "locales"
assert_path @paths.config.environments, "config", "environments" assert_path @paths.config.environments, "config", "environments"
assert_equal Pathname.new(File.dirname(__FILE__)).join("root", "app", "controllers").expand_path, assert_equal root("app", "controllers"), @paths.app.controllers.to_a.first
Pathname.new(@paths.app.controllers.to_a.first).expand_path
assert_equal Pathname.new(File.dirname(__FILE__)).join("..", "..", "builtin", "rails_info").expand_path, assert_equal Pathname.new(File.dirname(__FILE__)).join("..", "..", "builtin", "rails_info").expand_path,
Pathname.new(@paths.app.controllers.to_a[1]).expand_path Pathname.new(@paths.app.controllers.to_a[1]).expand_path
end end
...@@ -56,24 +62,16 @@ def assert_path(paths, *dir) ...@@ -56,24 +62,16 @@ def assert_path(paths, *dir)
assert_equal "#{RAILS_ENV}.rb", @paths.config.environments.glob assert_equal "#{RAILS_ENV}.rb", @paths.config.environments.glob
end end
def assert_in_load_path(*path)
assert $:.any? { |p| File.expand_path(p) == root(*path) }, "Load path does not include '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
end
def assert_not_in_load_path(*path)
assert !$:.any? { |p| File.expand_path(p) == root(*path) }, "Load path includes '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
end
test "load path includes each of the paths in config.paths as long as the directories exist" do test "load path includes each of the paths in config.paths as long as the directories exist" do
assert_in_load_path "app" assert_in_load_path "app"
assert_in_load_path "app", "controllers" assert_in_load_path "app", "controllers"
assert_in_load_path "app", "metal"
assert_in_load_path "app", "models" assert_in_load_path "app", "models"
assert_in_load_path "app", "helpers" assert_in_load_path "app", "helpers"
assert_in_load_path "lib" assert_in_load_path "lib"
assert_in_load_path "vendor" assert_in_load_path "vendor"
assert_not_in_load_path "app", "views" assert_not_in_load_path "app", "views"
assert_not_in_load_path "app", "metal"
assert_not_in_load_path "app", "services" assert_not_in_load_path "app", "services"
assert_not_in_load_path "config" assert_not_in_load_path "config"
assert_not_in_load_path "config", "locales" assert_not_in_load_path "config", "locales"
......
...@@ -14,10 +14,6 @@ ...@@ -14,10 +14,6 @@
# TODO: Remove setting this magic constant # TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..") RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")
# These load paths usually get set inside of boot.rb
$:.unshift "#{RAILS_FRAMEWORK_ROOT}/railties/lib"
$:.unshift "#{RAILS_FRAMEWORK_ROOT}/actionpack/lib"
# These files do not require any others and are needed # These files do not require any others and are needed
# to run the tests # to run the tests
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation" require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation"
...@@ -71,8 +67,23 @@ def assert_body(expected, resp) ...@@ -71,8 +67,23 @@ def assert_body(expected, resp)
end end
module Generation module Generation
def build_app def build_app(options = {})
FileUtils.rm_rf(app_path)
FileUtils.cp_r(tmp_path('app_template'), app_path) FileUtils.cp_r(tmp_path('app_template'), app_path)
# Delete the initializers unless requested
unless options[:initializers]
Dir["#{app_path}/config/initializers/*.rb"].each do |initializer|
File.delete(initializer)
end
end
environment = File.read("#{app_path}/config/environment.rb")
if environment =~ /(\n\s*end\s*)\Z/
File.open("#{app_path}/config/environment.rb", 'w') do |f|
f.puts $` + %'\nconfig.action_controller.session = { :key => "_myapp_session", :secret => "bac838a849c1d5c4de2e6a50af826079" }\n' + $1
end
end
end end
def app_file(path, contents) def app_file(path, contents)
...@@ -84,6 +95,23 @@ def app_file(path, contents) ...@@ -84,6 +95,23 @@ def app_file(path, contents)
def controller(name, contents) def controller(name, contents)
app_file("app/controllers/#{name}_controller.rb", contents) app_file("app/controllers/#{name}_controller.rb", contents)
end end
def boot_rails
# return if defined?(RAILS)
# TODO: Get this working with boot.rb
$:.unshift "#{RAILS_FRAMEWORK_ROOT}/railties/lib"
Object.class_eval <<-RUBY
RAILS_ROOT = "#{app_path}"
module ::Rails
def self.vendor_rails?
true
end
end
RUBY
require "rails"
Rails::Initializer.run(:install_gem_spec_stubs)
Rails::GemDependency.add_frozen_gem_path
end
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册