PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/boot_test.rb

https://bitbucket.org/nicksieger/rails
Ruby | 178 lines | 147 code | 30 blank | 1 comment | 0 complexity | 6d6fb76699a07c435d17c1f34a31a942 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'initializer'
  3. require "#{File.dirname(__FILE__)}/../environments/boot"
  4. class BootTest < Test::Unit::TestCase
  5. def test_boot_returns_if_booted
  6. Rails.expects(:booted?).returns(true)
  7. Rails.expects(:pick_boot).never
  8. assert_nil Rails.boot!
  9. end
  10. def test_boot_preinitializes_then_picks_and_runs_if_not_booted
  11. Rails.expects(:booted?).returns(false)
  12. Rails.expects(:preinitialize)
  13. Rails.expects(:pick_boot).returns(mock(:run => 'result'))
  14. assert_equal 'result', Rails.boot!
  15. end
  16. def test_preinitialize_does_not_raise_exception_if_preinitializer_file_does_not_exist
  17. Rails.stubs(:preinitializer_path).returns('/there/is/no/such/file')
  18. assert_nothing_raised { Rails.preinitialize }
  19. end
  20. def test_load_preinitializer_loads_preinitializer_file
  21. Rails.stubs(:preinitializer_path).returns("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
  22. assert_nil $initialize_test_set_from_env
  23. Rails.preinitialize
  24. assert_equal "success", $initialize_test_set_from_env
  25. ensure
  26. $initialize_test_set_from_env = nil
  27. end
  28. def test_boot_vendor_rails_by_default
  29. Rails.expects(:booted?).returns(false)
  30. Rails.expects(:preinitialize)
  31. File.expects(:exist?).with("#{RAILS_ROOT}/vendor/rails").returns(true)
  32. Rails::VendorBoot.any_instance.expects(:run).returns('result')
  33. assert_equal 'result', Rails.boot!
  34. end
  35. def test_boot_gem_rails_otherwise
  36. Rails.expects(:booted?).returns(false)
  37. Rails.expects(:preinitialize)
  38. File.expects(:exist?).with("#{RAILS_ROOT}/vendor/rails").returns(false)
  39. Rails::GemBoot.any_instance.expects(:run).returns('result')
  40. assert_equal 'result', Rails.boot!
  41. end
  42. def test_run_loads_initializer_and_sets_load_path
  43. boot = Rails::Boot.new
  44. boot.expects(:load_initializer)
  45. Rails::Initializer.expects(:run).with(:set_load_path)
  46. boot.run
  47. end
  48. end
  49. class VendorBootTest < Test::Unit::TestCase
  50. include Rails
  51. def test_load_initializer_requires_from_vendor_rails
  52. boot = VendorBoot.new
  53. boot.expects(:require).with("#{RAILS_ROOT}/vendor/rails/railties/lib/initializer")
  54. Rails::Initializer.expects(:run).with(:install_gem_spec_stubs)
  55. Rails::GemDependency.expects(:add_frozen_gem_path)
  56. boot.load_initializer
  57. end
  58. end
  59. class GemBootTest < Test::Unit::TestCase
  60. include Rails
  61. def test_load_initializer_loads_rubygems_and_the_rails_gem
  62. boot = GemBoot.new
  63. GemBoot.expects(:load_rubygems)
  64. boot.expects(:load_rails_gem)
  65. boot.expects(:require).with('initializer')
  66. boot.load_initializer
  67. end
  68. def test_load_rubygems_exits_with_error_if_missing
  69. GemBoot.expects(:require).with('rubygems').raises(LoadError, 'missing rubygems')
  70. STDERR.expects(:puts)
  71. GemBoot.expects(:exit).with(1)
  72. GemBoot.load_rubygems
  73. end
  74. def test_load_rubygems_exits_with_error_if_too_old
  75. GemBoot.stubs(:rubygems_version).returns('0.0.1')
  76. GemBoot.expects(:require).with('rubygems').returns(true)
  77. STDERR.expects(:puts)
  78. GemBoot.expects(:exit).with(1)
  79. GemBoot.load_rubygems
  80. end
  81. def test_load_rails_gem_activates_specific_gem_if_version_given
  82. GemBoot.stubs(:gem_version).returns('0.0.1')
  83. boot = GemBoot.new
  84. boot.expects(:gem).with('rails', '0.0.1')
  85. boot.load_rails_gem
  86. end
  87. def test_load_rails_gem_activates_latest_gem_if_no_version_given
  88. GemBoot.stubs(:gem_version).returns(nil)
  89. boot = GemBoot.new
  90. boot.expects(:gem).with('rails')
  91. boot.load_rails_gem
  92. end
  93. def test_load_rails_gem_exits_with_error_if_missing
  94. GemBoot.stubs(:gem_version).returns('0.0.1')
  95. boot = GemBoot.new
  96. boot.expects(:gem).with('rails', '0.0.1').raises(Gem::LoadError, 'missing rails 0.0.1 gem')
  97. STDERR.expects(:puts)
  98. boot.expects(:exit).with(1)
  99. boot.load_rails_gem
  100. end
  101. end
  102. class ParseGemVersionTest < Test::Unit::TestCase
  103. def test_should_return_nil_if_no_lines_are_passed
  104. assert_equal nil, parse('')
  105. assert_equal nil, parse(nil)
  106. end
  107. def test_should_accept_either_single_or_double_quotes
  108. assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3'")
  109. assert_equal "1.2.3", parse('RAILS_GEM_VERSION = "1.2.3"')
  110. end
  111. def test_should_return_nil_if_no_lines_match
  112. assert_equal nil, parse('nothing matches on this line\nor on this line')
  113. end
  114. def test_should_parse_with_no_leading_space
  115. assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION")
  116. assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3'")
  117. end
  118. def test_should_parse_with_any_number_of_leading_spaces
  119. assert_equal nil, parse([])
  120. assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION")
  121. assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION")
  122. assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3'")
  123. assert_equal "1.2.3", parse(" RAILS_GEM_VERSION = '1.2.3'")
  124. end
  125. def test_should_ignore_unrelated_comments
  126. assert_equal "1.2.3", parse("# comment\nRAILS_GEM_VERSION = '1.2.3'\n# comment")
  127. end
  128. def test_should_ignore_commented_version_lines
  129. assert_equal "1.2.3", parse("#RAILS_GEM_VERSION = '9.8.7'\nRAILS_GEM_VERSION = '1.2.3'")
  130. assert_equal "1.2.3", parse("# RAILS_GEM_VERSION = '9.8.7'\nRAILS_GEM_VERSION = '1.2.3'")
  131. assert_equal "1.2.3", parse("RAILS_GEM_VERSION = '1.2.3'\n# RAILS_GEM_VERSION = '9.8.7'")
  132. end
  133. def test_should_allow_advanced_rubygems_version_specifications
  134. # See http://rubygems.org/read/chapter/16
  135. assert_equal "=1.2.3", parse("RAILS_GEM_VERSION = '=1.2.3'") # equal sign
  136. assert_equal "= 1.2.3", parse("RAILS_GEM_VERSION = '= 1.2.3'") # with space
  137. assert_equal "!=1.2.3", parse("RAILS_GEM_VERSION = '!=1.2.3'") # not equal
  138. assert_equal ">1.2.3", parse("RAILS_GEM_VERSION = '>1.2.3'") # greater than
  139. assert_equal "<1.2.3", parse("RAILS_GEM_VERSION = '<1.2.3'") # less than
  140. assert_equal ">=1.2.3", parse("RAILS_GEM_VERSION = '>=1.2.3'") # greater than or equal
  141. assert_equal "<=1.2.3", parse("RAILS_GEM_VERSION = '<=1.2.3'") # less than or equal
  142. assert_equal "~>1.2.3.0", parse("RAILS_GEM_VERSION = '~>1.2.3.0'") # approximately greater than
  143. end
  144. private
  145. def parse(text)
  146. Rails::GemBoot.parse_gem_version(text)
  147. end
  148. end