Permalink
Browse files

Added tests for the :install_gem_spec_stubs initializer

  • Loading branch information...
1 parent d8406f0 commit 378a65a909439ebca909125fdfada23ed89cec63 Yehuda Katz + Carl Lerche committed Jul 3, 2009
@@ -125,11 +125,8 @@ def self.run(initializer = nil, config = nil)
if Rails.vendor_rails?
begin; require "rubygems"; rescue LoadError; return; end
- stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
- stubs.reject! { |s| Gem.loaded_specs.key?(s) }
-
- stubs.each do |stub|
- Gem.loaded_specs[stub] = Gem::Specification.new do |s|
+ %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub|
+ Gem.loaded_specs[stub] ||= Gem::Specification.new do |s|
s.name = stub
s.version = Rails::VERSION::STRING
s.loaded_from = ""
@@ -1,12 +1,4 @@
-require 'abstract_unit'
-require 'active_support/ruby/shim'
-require 'initializer'
-
-RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
-
-module Rails
- def self.vendor_rails? ; false ; end
-end
+require "initializer/test_helper"
module InitializerTests
class PathsTest < ActiveSupport::TestCase
@@ -0,0 +1,85 @@
+require "initializer/test_helper"
+
+module InitializerTests
+ class GemSpecStubsTest < ActiveSupport::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
+
+ 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
@@ -1,12 +1,4 @@
-require 'abstract_unit'
-require 'active_support/ruby/shim'
-require 'initializer'
-
-RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
-
-module Rails
- def self.vendor_rails? ; false ; end
-end
+require "initializer/test_helper"
class PathsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
@@ -0,0 +1,24 @@
+require 'abstract_unit'
+require 'active_support/ruby/shim'
+require 'initializer'
+
+RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
+
+module Rails
+ class << self
+ attr_accessor :vendor_rails
+ def vendor_rails?() @vendor_rails end
+ end
+end
+
+class ActiveSupport::TestCase
+ def assert_stderr(match)
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind
+ err = $stderr.read
+ assert_match match, err
+ ensure
+ $stderr = STDERR
+ end
+end
@@ -178,7 +178,7 @@ def assert_framework_path(path)
end
end
-require File.dirname(__FILE__) + '/plugin_test_helper'
+require 'plugin_test_helper'
class InitializerPluginLoadingTests < Test::Unit::TestCase
def setup
@@ -4,7 +4,7 @@
require 'test/unit'
require 'active_support'
require 'initializer'
-require File.join(File.dirname(__FILE__), 'abstract_unit')
+require 'abstract_unit'
# We need to set RAILS_ROOT if it isn't already set
RAILS_ROOT = '.' unless defined?(RAILS_ROOT)

0 comments on commit 378a65a

Please sign in to comment.