From 9a42e06dd8ac5d9abd50b0e47e8de0ac3ab00a9d Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Mon, 6 Jul 2009 10:04:57 -0700 Subject: Reapply Rails::Application::Path tweaks Reapplies: a4bdc00fec623f72592e663e6d7830eea0bc6ea4 3c1dab72259d01c6335bf359d7f9b3af69d45bb4 --- railties/test/paths_test.rb | 92 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index fa2f6ceee2..d50882110f 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -17,17 +17,37 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/foo/bar"], @root.app.to_a end + test "creating a root level path without assignment" do + @root.app "/foo/bar" + assert_equal ["/foo/bar"], @root.app.to_a + end + + test "trying to access a path that does not exist raises NoMethodError" do + assert_raises(NoMethodError) { @root.app } + end + test "relative paths are relative to the paths root" do @root.app = "app" assert_equal ["/foo/bar/app"], @root.app.to_a end + test "relative paths are relative to the paths root without assignment" do + @root.app "app" + assert_equal ["/foo/bar/app"], @root.app.to_a + end + test "creating a child level path" do @root.app = "/foo/bar" @root.app.models = "/foo/bar/baz" assert_equal ["/foo/bar/baz"], @root.app.models.to_a end + test "creating a child level path without assignment" do + @root.app = "/foo/bar" + @root.app.models "/foo/bar/baz" + assert_equal ["/foo/bar/baz"], @root.app.models.to_a + end + test "child level paths are relative from the root" do @root.app = "/app" @root.app.models = "baz" @@ -40,6 +60,11 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/app", "/app2"], @root.app.to_a end + test "adding multiple physical paths as an array without assignment" do + @root.app "/app", "/app2" + assert_equal ["/app", "/app2"], @root.app.to_a + end + test "adding multiple physical paths using #push" do @root.app = "/app" @root.app.push "/app2" @@ -66,10 +91,10 @@ class PathsTest < ActiveSupport::TestCase test "the root can only have one physical path" do assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) } - assert_raise(NoMethodError) { @root.push "/biz" } - assert_raise(NoMethodError) { @root.unshift "/biz" } - assert_raise(NoMethodError) { @root.concat ["/biz"]} - assert_raise(NoMethodError) { @root << "/biz" } + assert_raise(RuntimeError) { @root.push "/biz" } + assert_raise(RuntimeError) { @root.unshift "/biz" } + assert_raise(RuntimeError) { @root.concat ["/biz"]} + assert_raise(RuntimeError) { @root << "/biz" } end test "it is possible to add a path that should be loaded only once" do @@ -79,6 +104,19 @@ class PathsTest < ActiveSupport::TestCase assert @root.load_once.include?(@root.app.paths.first) end + test "it is possible to add a path without assignment and specify it should be loaded only once" do + @root.app "/app", :load_once => true + assert @root.app.load_once? + assert @root.load_once.include?("/app") + end + + test "it is possible to add multiple paths without assignment and specify it should be loaded only once" do + @root.app "/app", "/app2", :load_once => true + assert @root.app.load_once? + assert @root.load_once.include?("/app") + assert @root.load_once.include?("/app2") + end + test "making a path load_once more than once only includes it once in @root.load_once" do @root.app = "/app" @root.app.load_once! @@ -86,6 +124,13 @@ class PathsTest < ActiveSupport::TestCase assert_equal 1, @root.load_once.select {|p| p == @root.app.paths.first }.size end + test "paths added to a load_once path should be added to the load_once collection" do + @root.app = "/app" + @root.app.load_once! + @root.app << "/app2" + assert_equal 2, @root.load_once.size + end + test "it is possible to mark a path as eager" do @root.app = "/app" @root.app.eager_load! @@ -93,6 +138,27 @@ class PathsTest < ActiveSupport::TestCase assert @root.eager_load.include?(@root.app.paths.first) end + test "it is possible to add a path without assignment and mark it as eager" do + @root.app "/app", :eager_load => true + assert @root.app.eager_load? + assert @root.eager_load.include?("/app") + end + + test "it is possible to add multiple paths without assignment and mark them as eager" do + @root.app "/app", "/app2", :eager_load => true + assert @root.app.eager_load? + assert @root.eager_load.include?("/app") + assert @root.eager_load.include?("/app2") + end + + test "it is possible to create a path without assignment and mark it both as eager and load once" do + @root.app "/app", :eager_load => true, :load_once => true + assert @root.app.eager_load? + assert @root.app.load_once? + assert @root.eager_load.include?("/app") + assert @root.load_once.include?("/app") + end + test "making a path eager more than once only includes it once in @root.eager_paths" do @root.app = "/app" @root.app.eager_load! @@ -100,6 +166,13 @@ class PathsTest < ActiveSupport::TestCase assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size end + test "paths added to a eager_load path should be added to the eager_load collection" do + @root.app = "/app" + @root.app.eager_load! + @root.app << "/app2" + assert_equal 2, @root.eager_load.size + end + test "a path should have a glob that defaults to **/*.rb" do @root.app = "/app" assert_equal "**/*.rb", @root.app.glob @@ -111,6 +184,11 @@ class PathsTest < ActiveSupport::TestCase assert_equal "*.rb", @root.app.glob end + test "it should be possible to override a path's default glob without assignment" do + @root.app "/app", :glob => "*.rb" + assert_equal "*.rb", @root.app.glob + end + test "a path can be added to the load path" do @root.app = "app" @root.app.load_path! @@ -118,6 +196,12 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/foo/bar/app"], @root.load_paths end + test "a path can be added to the load path on creation" do + @root.app "/app", :load_path => true + assert @root.app.load_path? + assert_equal ["/app"], @root.load_paths + end + test "adding a path to the eager paths also adds it to the load path" do @root.app = "app" @root.app.eager_load! -- cgit v1.2.3 From 61604feec0fa04810f5903d13b74bad06e67b3bb Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Mon, 6 Jul 2009 12:25:34 -0700 Subject: Get Initializer tests running without requiring parts of Rails being loaded first --- .../test/initializer/check_ruby_version_test.rb | 2 +- .../initializer/install_gem_spec_stubs_test.rb | 29 ++++++------- railties/test/initializer/path_test.rb | 2 +- railties/test/initializer/test_helper.rb | 48 ++++++++++++++++------ 4 files changed, 53 insertions(+), 28 deletions(-) (limited to 'railties/test') diff --git a/railties/test/initializer/check_ruby_version_test.rb b/railties/test/initializer/check_ruby_version_test.rb index 33de653906..68feba058e 100644 --- a/railties/test/initializer/check_ruby_version_test.rb +++ b/railties/test/initializer/check_ruby_version_test.rb @@ -1,7 +1,7 @@ require "initializer/test_helper" module InitializerTests - class PathsTest < ActiveSupport::TestCase + class PathsTest < Test::Unit::TestCase include ActiveSupport::Testing::Isolation test "rails does not initialize with ruby version 1.8.1" do diff --git a/railties/test/initializer/install_gem_spec_stubs_test.rb b/railties/test/initializer/install_gem_spec_stubs_test.rb index 2e94c9968f..cfb12d7405 100644 --- a/railties/test/initializer/install_gem_spec_stubs_test.rb +++ b/railties/test/initializer/install_gem_spec_stubs_test.rb @@ -1,7 +1,7 @@ require "initializer/test_helper" module InitializerTests - class GemSpecStubsTest < ActiveSupport::TestCase + class GemSpecStubsTest < Test::Unit::TestCase include ActiveSupport::Testing::Isolation def setup @@ -34,19 +34,20 @@ module InitializerTests 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 + # 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 diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb index 26f796f93d..1b73cdc73e 100644 --- a/railties/test/initializer/path_test.rb +++ b/railties/test/initializer/path_test.rb @@ -1,6 +1,6 @@ require "initializer/test_helper" -class PathsTest < ActiveSupport::TestCase +class PathsTest < Test::Unit::TestCase include ActiveSupport::Testing::Isolation def self.setup diff --git a/railties/test/initializer/test_helper.rb b/railties/test/initializer/test_helper.rb index ddb03397ab..9d7dfff1c0 100644 --- a/railties/test/initializer/test_helper.rb +++ b/railties/test/initializer/test_helper.rb @@ -1,17 +1,18 @@ -require 'abstract_unit' -require 'active_support/ruby/shim' -require 'initializer' +# This is a test helper file that simulates a rails application being +# boot from scratch in vendored mode. This file should really only be +# required in test cases that use the isolation helper so that requires +# can be reset correctly. +RAILS_ROOT = File.join(File.dirname(__FILE__), "root") +RAILS_FRAMEWORK_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..')) -RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root") +require "test/unit" +# We are purposely avoiding adding things to the load path to catch bugs that only happen in the genuine article +require File.join(RAILS_FRAMEWORK_ROOT, 'activesupport', 'lib', 'active_support', 'testing', 'isolation') +require File.join(RAILS_FRAMEWORK_ROOT, 'activesupport', 'lib', 'active_support', 'testing', 'declarative') -module Rails - class << self - attr_accessor :vendor_rails - def vendor_rails?() @vendor_rails end - end -end +class Test::Unit::TestCase + extend ActiveSupport::Testing::Declarative -class ActiveSupport::TestCase def assert_stderr(match) $stderr = StringIO.new yield @@ -21,4 +22,27 @@ class ActiveSupport::TestCase ensure $stderr = STDERR end -end \ No newline at end of file +end + +# Fake boot.rb +module Rails + class << self + attr_accessor :vendor_rails + + def vendor_rails? + @vendor_rails + end + + def boot! + # Require the initializer + require File.join(RAILS_FRAMEWORK_ROOT, 'railties', 'lib', 'initializer') + # Run the initializer the same way boot.rb does it + Rails::Initializer.run(:install_gem_spec_stubs) + Rails::GemDependency.add_frozen_gem_path + Rails::Initializer.run(:set_load_path) + end + end +end + +# All that for this: +Rails.boot! \ No newline at end of file -- cgit v1.2.3