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