| 1 |
require File.dirname(__FILE__) + '/plugin_test_helper' |
|---|
| 2 |
|
|---|
| 3 |
uses_mocha "Plugin Tests" do |
|---|
| 4 |
|
|---|
| 5 |
class PluginTest < Test::Unit::TestCase |
|---|
| 6 |
|
|---|
| 7 |
def setup |
|---|
| 8 |
@initializer = Rails::Initializer.new(Rails::Configuration.new) |
|---|
| 9 |
@valid_plugin_path = plugin_fixture_path('default/stubby') |
|---|
| 10 |
@empty_plugin_path = plugin_fixture_path('default/empty') |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
def test_should_determine_plugin_name_from_the_directory_of_the_plugin |
|---|
| 14 |
assert_equal 'stubby', plugin_for(@valid_plugin_path).name |
|---|
| 15 |
assert_equal 'empty', plugin_for(@empty_plugin_path).name |
|---|
| 16 |
end |
|---|
| 17 |
|
|---|
| 18 |
def test_should_not_be_loaded_when_created |
|---|
| 19 |
assert !plugin_for(@valid_plugin_path).loaded? |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
def test_should_be_marked_as_loaded_when_load_is_called |
|---|
| 23 |
plugin = plugin_for(@valid_plugin_path) |
|---|
| 24 |
assert !plugin.loaded? |
|---|
| 25 |
plugin.stubs(:evaluate_init_rb) |
|---|
| 26 |
assert_nothing_raised do |
|---|
| 27 |
plugin.send(:load, anything) |
|---|
| 28 |
end |
|---|
| 29 |
assert plugin.loaded? |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def test_should_determine_validity_of_given_path |
|---|
| 33 |
|
|---|
| 34 |
assert plugin_for(@valid_plugin_path).valid? |
|---|
| 35 |
|
|---|
| 36 |
assert plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).valid? |
|---|
| 37 |
|
|---|
| 38 |
assert !plugin_for(plugin_fixture_path('default/empty')).valid? |
|---|
| 39 |
|
|---|
| 40 |
assert !plugin_for(plugin_fixture_path('default/this_directory_does_not_exist')).valid? |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def test_should_return_empty_array_for_load_paths_when_plugin_has_no_lib_directory |
|---|
| 44 |
assert_equal [], plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).load_paths |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def test_should_return_array_with_lib_path_for_load_paths_when_plugin_has_a_lib_directory |
|---|
| 48 |
expected_lib_dir = File.join(plugin_fixture_path('default/stubby'), 'lib') |
|---|
| 49 |
assert_equal [expected_lib_dir], plugin_for(plugin_fixture_path('default/stubby')).load_paths |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
def test_should_raise_a_load_error_when_trying_to_determine_the_load_paths_from_an_invalid_plugin |
|---|
| 53 |
assert_nothing_raised do |
|---|
| 54 |
plugin_for(@valid_plugin_path).load_paths |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
assert_raises(LoadError) do |
|---|
| 58 |
plugin_for(@empty_plugin_path).load_paths |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
assert_raises(LoadError) do |
|---|
| 62 |
plugin_for('this_is_not_a_plugin_directory').load_paths |
|---|
| 63 |
end |
|---|
| 64 |
end |
|---|
| 65 |
|
|---|
| 66 |
def test_should_raise_a_load_error_when_trying_to_load_an_invalid_plugin |
|---|
| 67 |
|
|---|
| 68 |
assert_nothing_raised do |
|---|
| 69 |
plugin = plugin_for(@valid_plugin_path) |
|---|
| 70 |
plugin.stubs(:evaluate_init_rb) |
|---|
| 71 |
plugin.send(:load, @initializer) |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
assert_raises(LoadError) do |
|---|
| 76 |
plugin = plugin_for(@empty_plugin_path) |
|---|
| 77 |
plugin.stubs(:evaluate_init_rb) |
|---|
| 78 |
plugin.send(:load, @initializer) |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
assert_raises(LoadError) do |
|---|
| 82 |
plugin = plugin_for('this_is_not_a_plugin_directory') |
|---|
| 83 |
plugin.stubs(:evaluate_init_rb) |
|---|
| 84 |
plugin.send(:load, @initializer) |
|---|
| 85 |
end |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
def test_should_raise_a_load_error_when_trying_to_access_load_paths_of_an_invalid_plugin |
|---|
| 89 |
|
|---|
| 90 |
assert_nothing_raised do |
|---|
| 91 |
plugin_for(@valid_plugin_path).load_paths |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
assert_raises(LoadError) do |
|---|
| 96 |
plugin_for(@empty_plugin_path).load_paths |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
assert_raises(LoadError) do |
|---|
| 100 |
plugin_for('this_is_not_a_plugin_directory').load_paths |
|---|
| 101 |
end |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
def test_loading_a_plugin_gives_the_init_file_access_to_all_it_needs |
|---|
| 105 |
failure_tip = "Perhaps someone has written another test that loads this same plugin and therefore makes the StubbyMixin constant defined already." |
|---|
| 106 |
assert !defined?(StubbyMixin), failure_tip |
|---|
| 107 |
plugin = plugin_for(@valid_plugin_path) |
|---|
| 108 |
plugin.load_paths.each { |path| $LOAD_PATH.unshift(path) } |
|---|
| 109 |
|
|---|
| 110 |
assert_nothing_raised do |
|---|
| 111 |
plugin.load(@initializer) |
|---|
| 112 |
end |
|---|
| 113 |
assert defined?(StubbyMixin) |
|---|
| 114 |
end |
|---|
| 115 |
|
|---|
| 116 |
def test_should_sort_naturally_by_name |
|---|
| 117 |
a = plugin_for("path/a") |
|---|
| 118 |
b = plugin_for("path/b") |
|---|
| 119 |
z = plugin_for("path/z") |
|---|
| 120 |
assert_equal [a, b, z], [b, z, a].sort |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
def test_should_only_be_loaded_once |
|---|
| 124 |
plugin = plugin_for(@valid_plugin_path) |
|---|
| 125 |
assert !plugin.loaded? |
|---|
| 126 |
plugin.expects(:evaluate_init_rb) |
|---|
| 127 |
assert_nothing_raised do |
|---|
| 128 |
plugin.send(:load, @initializer) |
|---|
| 129 |
plugin.send(:load, @initializer) |
|---|
| 130 |
end |
|---|
| 131 |
assert plugin.loaded? |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
private |
|---|
| 135 |
|
|---|
| 136 |
def plugin_for(path) |
|---|
| 137 |
Rails::Plugin.new(path) |
|---|
| 138 |
end |
|---|
| 139 |
end |
|---|
| 140 |
|
|---|
| 141 |
end |
|---|