diff options
-rw-r--r-- | railties/CHANGELOG | 3 | ||||
-rw-r--r-- | railties/environments/boot.rb | 110 | ||||
-rw-r--r-- | railties/test/boot_test.rb | 145 |
3 files changed, 232 insertions, 26 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 6a6561a25c..b09cbb3a59 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* config/boot.rb correctly detects RAILS_GEM_VERSION. #9834 [alexch, thewoolleyman] + * Fixed incorrect migration number if script/generate executed outside of Rails root #7080 [jeremymcanally] * Update Prototype to 1.6.0_rc1 and script.aculo.us to 1.8.0 preview 0. [sam, madrobby] @@ -15,6 +17,7 @@ databases on localhost. #9753 [Trevor Wennblom] * Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb [DHH] + * Add --prefix option to script/server when using mongrel. [dacat] *2.0.0 [Preview Release]* (September 29th, 2007) [Includes duplicates of changes from 1.1.4 - 1.2.3] diff --git a/railties/environments/boot.rb b/railties/environments/boot.rb index 0f034f07bf..3dec666c3a 100644 --- a/railties/environments/boot.rb +++ b/railties/environments/boot.rb @@ -1,39 +1,97 @@ -# Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb +# Don't change this file! +# Configure your app in config/environment.rb and config/environments/*.rb RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT) -unless defined?(Rails::Initializer) - if File.directory?("#{RAILS_ROOT}/vendor/rails") - require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer" - else - require 'rubygems' +module Rails + class << self + def boot! + pick_boot.run unless booted? + end + + def booted? + defined? Rails::Initializer + end + + def pick_boot + (vendor_rails? ? VendorBoot : GemBoot).new + end + + def vendor_rails? + File.exist?("#{RAILS_ROOT}/vendor/rails") + end + end + + class Boot + def run + load_initializer + Rails::Initializer.run(:set_load_path) + end + end + + class VendorBoot < Boot + def load_initializer + require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer" + end + end + + class GemBoot < Boot + def load_initializer + self.class.load_rubygems + load_rails_gem + require 'initializer' + end - rails_gem_version = - if defined? RAILS_GEM_VERSION - RAILS_GEM_VERSION + def load_rails_gem + if version = self.class.gem_version + gem 'rails', "=#{version}" else - File.read("#{File.dirname(__FILE__)}/environment.rb") =~ /^[^#]*RAILS_GEM_VERSION\s+=\s+'([\d.]+)'/ - $1 + gem 'rails' + end + rescue Gem::LoadError => load_error + STDERR.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.) + exit 1 + end + + class << self + def rubygems_version + Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion + end + + def gem_version + if defined? RAILS_GEM_VERSION + RAILS_GEM_VERSION + elsif ENV.include?('RAILS_GEM_VERSION') + ENV['RAILS_GEM_VERSION'] + else + parse_gem_version(read_environment_rb) + end end - if rails_gem_version - rails_gem = Gem.cache.search('rails', "=#{rails_gem_version}.0").sort_by { |g| g.version.version }.last + def load_rubygems + require 'rubygems' - if rails_gem - gem "rails", "=#{rails_gem.version.version}" - require rails_gem.full_gem_path + '/lib/initializer' - else - STDERR.puts %(Cannot find gem for Rails =#{rails_gem_version}.0: - Install the missing gem with 'gem install -v=#{rails_gem_version} rails', or - change environment.rb to define RAILS_GEM_VERSION with your desired version. - ) + unless rubygems_version >= '0.9.4' + STDERR.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.) + exit 1 + end + + rescue LoadError + STDERR.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org) exit 1 end - else - gem "rails" - require 'initializer' + + def parse_gem_version(text) + $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*'([\d.]+)'/ + end + + private + def read_environment_rb + File.read("#{RAILS_ROOT}/config/environment.rb") + end end end - - Rails::Initializer.run(:set_load_path) end + +# All that for this: +Rails.boot! diff --git a/railties/test/boot_test.rb b/railties/test/boot_test.rb new file mode 100644 index 0000000000..5d5fb09b41 --- /dev/null +++ b/railties/test/boot_test.rb @@ -0,0 +1,145 @@ +require "#{File.dirname(__FILE__)}/abstract_unit" +require 'initializer' +require "#{File.dirname(__FILE__)}/../environments/boot" + +uses_mocha 'boot tests' do + +class BootTest < Test::Unit::TestCase + def test_boot_returns_if_booted + Rails.expects(:booted?).returns(true) + Rails.expects(:pick_boot).never + assert_nil Rails.boot! + end + + def test_boot_picks_and_runs_if_not_booted + Rails.expects(:booted?).returns(false) + Rails.expects(:pick_boot).returns(mock(:run => 'result')) + assert_equal 'result', Rails.boot! + end + + def test_boot_vendor_rails_by_default + Rails.expects(:booted?).returns(false) + File.expects(:exist?).with("#{RAILS_ROOT}/vendor/rails").returns(true) + Rails::VendorBoot.any_instance.expects(:run).returns('result') + assert_equal 'result', Rails.boot! + end + + def test_boot_gem_rails_otherwise + Rails.expects(:booted?).returns(false) + File.expects(:exist?).with("#{RAILS_ROOT}/vendor/rails").returns(false) + Rails::GemBoot.any_instance.expects(:run).returns('result') + assert_equal 'result', Rails.boot! + end + + def test_run_loads_initializer_and_sets_load_path + boot = Rails::Boot.new + boot.expects(:load_initializer) + Rails::Initializer.expects(:run).with(:set_load_path) + boot.run + end +end + +class VendorBootTest < Test::Unit::TestCase + include Rails + + def test_load_initializer_requires_from_vendor_rails + boot = VendorBoot.new + boot.expects(:require).with("#{RAILS_ROOT}/vendor/rails/railties/lib/initializer") + boot.load_initializer + end +end + +class GemBootTest < Test::Unit::TestCase + include Rails + + def test_load_initializer_loads_rubygems_and_the_rails_gem + boot = GemBoot.new + GemBoot.expects(:load_rubygems) + boot.expects(:load_rails_gem) + boot.expects(:require).with('initializer') + boot.load_initializer + end + + def test_load_rubygems_exits_with_error_if_missing + GemBoot.expects(:require).with('rubygems').raises(LoadError, 'missing rubygems') + STDERR.expects(:puts) + GemBoot.expects(:exit).with(1) + GemBoot.load_rubygems + end + + def test_load_rubygems_exits_with_error_if_too_old + GemBoot.stubs(:rubygems_version).returns('0.0.1') + GemBoot.expects(:require).with('rubygems').returns(true) + STDERR.expects(:puts) + GemBoot.expects(:exit).with(1) + GemBoot.load_rubygems + end + + def test_load_rails_gem_activates_specific_gem_if_version_given + GemBoot.stubs(:gem_version).returns('0.0.1') + + boot = GemBoot.new + boot.expects(:gem).with('rails', '=0.0.1') + boot.load_rails_gem + end + + def test_load_rails_gem_activates_latest_gem_if_no_version_given + GemBoot.stubs(:gem_version).returns(nil) + + boot = GemBoot.new + boot.expects(:gem).with('rails') + boot.load_rails_gem + end + + def test_load_rails_gem_exits_with_error_if_missing + GemBoot.stubs(:gem_version).returns('0.0.1') + + boot = GemBoot.new + boot.expects(:gem).with('rails', '=0.0.1').raises(Gem::LoadError, 'missing rails 0.0.1 gem') + STDERR.expects(:puts) + boot.expects(:exit).with(1) + boot.load_rails_gem + end +end + +end # uses_mocha + + +class ParseGemVersionTest < Test::Unit::TestCase + def test_should_return_nil_if_no_lines_are_passed + assert_equal nil, parse('') + assert_equal nil, parse(nil) + end + + def test_should_return_nil_if_no_lines_match + assert_equal nil, parse('nothing matches on this line\nor on this line') + end + + def test_should_parse_with_no_leading_space + assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION") + assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3'") + end + + def test_should_parse_with_any_number_of_leading_spaces + assert_equal nil, parse([]) + assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION") + assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION") + assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3'") + assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3'") + end + + def test_should_ignore_unrelated_comments + assert_equal "1.2.3", parse("# comment\nRAILS_GEM_VERSION = '1.2.3'\n# comment") + end + + def test_should_ignore_commented_version_lines + assert_equal "1.2.3", parse("#RAILS_GEM_VERSION = '9.8.7'\nRAILS_GEM_VERSION = '1.2.3'") + assert_equal "1.2.3", parse("# RAILS_GEM_VERSION = '9.8.7'\nRAILS_GEM_VERSION = '1.2.3'") + assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3'\n# RAILS_GEM_VERSION = '9.8.7'") + end + + private + def parse(text) + Rails::GemBoot.parse_gem_version(text) + end +end |