aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2007-03-02 00:20:32 +0000
committerMarcel Molina <marcel@vernix.org>2007-03-02 00:20:32 +0000
commitb0e1430c523cf09155f72d5996be2cc2bf8e2eb7 (patch)
treec1f0dfd39794fa284737386f6c7520529e581b3b /railties/test
parent36cf67e8e50056e178f2d43705a324778fddfd0f (diff)
downloadrails-b0e1430c523cf09155f72d5996be2cc2bf8e2eb7.tar.gz
rails-b0e1430c523cf09155f72d5996be2cc2bf8e2eb7.tar.bz2
rails-b0e1430c523cf09155f72d5996be2cc2bf8e2eb7.zip
Split plugin location and loading out of the initializer and into a new Plugin namespace, which includes Plugin::Locater and Plugin::Loader. The loader class that is used can be customized using the config.plugin_loader option. Those monkey patching the plugin loading subsystem take note, the internals changing here will likely break your modifications. The good news is that it should be substantially easier to hook into the plugin locating and loading process now. [Marcel Molina Jr.]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6277 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/fixtures/plugins/default/plugin_with_no_lib_dir/init.rb0
-rw-r--r--railties/test/plugin_loader_test.rb71
-rw-r--r--railties/test/plugin_locater_test.rb51
-rw-r--r--railties/test/plugin_test.rb111
-rw-r--r--railties/test/plugin_test_helper.rb14
5 files changed, 136 insertions, 111 deletions
diff --git a/railties/test/fixtures/plugins/default/plugin_with_no_lib_dir/init.rb b/railties/test/fixtures/plugins/default/plugin_with_no_lib_dir/init.rb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/railties/test/fixtures/plugins/default/plugin_with_no_lib_dir/init.rb
diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb
new file mode 100644
index 0000000000..7010a60298
--- /dev/null
+++ b/railties/test/plugin_loader_test.rb
@@ -0,0 +1,71 @@
+require File.dirname(__FILE__) + '/plugin_test_helper'
+
+class TestPluginLoader < Test::Unit::TestCase
+ def setup
+ @initializer = Rails::Initializer.new(Rails::Configuration.new)
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
+ @empty_plugin_path = plugin_fixture_path('default/empty')
+ end
+
+ def test_determining_whether_a_given_plugin_is_loaded
+ plugin_loader = loader_for(@valid_plugin_path)
+ assert !plugin_loader.loaded?
+ assert_nothing_raised do
+ plugin_loader.send(:register_plugin_as_loaded)
+ end
+ assert plugin_loader.loaded?
+ end
+
+ def test_if_a_path_is_a_plugin_path
+ # This is a plugin path, with a lib dir
+ assert loader_for(@valid_plugin_path).plugin_path?
+ # This just has an init.rb and no lib dir
+ assert loader_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).plugin_path?
+ # This would be a plugin path, but the directory is empty
+ assert !loader_for(plugin_fixture_path('default/empty')).plugin_path?
+ # This is a non sense path
+ assert !loader_for(plugin_fixture_path('default/this_directory_does_not_exist')).plugin_path?
+ end
+
+ def test_if_you_try_to_load_a_non_plugin_path_you_get_a_load_error
+ # This path is fine so nothing is raised
+ assert_nothing_raised do
+ loader_for(@valid_plugin_path).send(:report_nonexistant_or_empty_plugin!)
+ end
+
+ # This is an empty path so it raises
+ assert_raises(LoadError) do
+ loader_for(@empty_plugin_path).send(:report_nonexistant_or_empty_plugin!)
+ end
+
+ assert_raises(LoadError) do
+ loader_for('this_is_not_a_plugin_directory').send(:report_nonexistant_or_empty_plugin!)
+ end
+ end
+
+ def test_loading_a_plugin_gives_the_init_file_access_to_all_it_needs
+ failure_tip = "Perhaps someone has written another test that loads this same plugin and therefore makes the SubbyMixin constant defined already."
+ assert !defined?(StubbyMixin), failure_tip
+ assert !added_to_load_path?(@valid_plugin_path)
+ # The init.rb of this plugin raises if it doesn't have access to all the things it needs
+ assert_nothing_raised do
+ loader_for(@valid_plugin_path).load
+ end
+ assert added_to_load_path?(@valid_plugin_path)
+ assert defined?(StubbyMixin)
+ end
+
+ private
+ def loader_for(path, initializer = @initializer)
+ Rails::Plugin::Loader.new(initializer, path)
+ end
+
+ def plugin_fixture_path(path)
+ File.join(plugin_fixture_root_path, path)
+ end
+
+ def added_to_load_path?(path)
+ $LOAD_PATH.grep(/#{path}/).size == 1
+ end
+
+end \ No newline at end of file
diff --git a/railties/test/plugin_locater_test.rb b/railties/test/plugin_locater_test.rb
new file mode 100644
index 0000000000..ba843a8e45
--- /dev/null
+++ b/railties/test/plugin_locater_test.rb
@@ -0,0 +1,51 @@
+require File.dirname(__FILE__) + '/plugin_test_helper'
+
+class TestPluginLocater < Test::Unit::TestCase
+ def setup
+ configuration = Rails::Configuration.new
+ # We need to add our testing plugin directory to the plugin paths so
+ # the locater knows where to look for our plugins
+ configuration.plugin_paths << plugin_fixture_root_path
+ @initializer = Rails::Initializer.new(configuration)
+ @locater = new_locater
+ end
+
+ def test_determining_if_the_plugin_order_has_been_explicitly_set
+ assert !@locater.send(:explicit_plugin_loading_order?)
+ only_load_the_following_plugins! %w(stubby acts_as_chunky_bacon)
+ assert @locater.send(:explicit_plugin_loading_order?)
+ end
+
+ def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
+ only_load_the_following_plugins! []
+ assert_equal [], @locater.plugins
+ end
+
+ def test_only_the_specified_plugins_are_located_in_the_order_listed
+ plugin_names = %w(stubby acts_as_chunky_bacon)
+ only_load_the_following_plugins! plugin_names
+ assert_equal plugin_names, @locater.plugin_names
+ end
+
+ def test_registering_a_plugin_name_that_does_not_exist_raisesa_load_error
+ only_load_the_following_plugins! %w(stubby acts_as_non_existant_plugin)
+ assert_raises(LoadError) do
+ @locater.plugin_names
+ end
+ end
+
+ def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+ assert_equal %w(a acts_as_chunky_bacon plugin_with_no_lib_dir stubby), @locater.plugin_names, failure_tip
+ end
+
+ private
+ def new_locater(initializer = @initializer)
+ Rails::Plugin::Locater.new(initializer)
+ end
+
+ def only_load_the_following_plugins!(plugins)
+ @initializer.configuration.plugins = plugins
+ end
+
+end \ No newline at end of file
diff --git a/railties/test/plugin_test.rb b/railties/test/plugin_test.rb
deleted file mode 100644
index f46f1db615..0000000000
--- a/railties/test/plugin_test.rb
+++ /dev/null
@@ -1,111 +0,0 @@
-$:.unshift File.dirname(__FILE__) + "/../lib"
-$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
-
-require 'test/unit'
-require 'active_support'
-require 'initializer'
-
-unless defined?(RAILS_ROOT)
- module Rails
- class Initializer
- RAILS_ROOT = '.'
- end
- end
-end
-
-class PluginTest < Test::Unit::TestCase
- class TestConfig < Rails::Configuration
- protected
- def root_path
- File.dirname(__FILE__)
- end
- end
-
- def setup
- @init = Rails::Initializer.new(TestConfig.new)
- end
-
- def test_plugin_path?
- assert @init.send(:plugin_path?, "#{File.dirname(__FILE__)}/fixtures/plugins/default/stubby")
- assert !@init.send(:plugin_path?, "#{File.dirname(__FILE__)}/fixtures/plugins/default/empty")
- assert !@init.send(:plugin_path?, "#{File.dirname(__FILE__)}/fixtures/plugins/default/jalskdjflkas")
- end
-
- def test_find_plugins
- base = "#{File.dirname(__FILE__)}/fixtures/plugins"
- default = "#{base}/default"
- alt = "#{base}/alternate"
- acts = "#{default}/acts"
- assert_equal ["#{acts}/acts_as_chunky_bacon"], @init.send(:find_plugins, acts)
- assert_equal ["#{acts}/acts_as_chunky_bacon", "#{default}/stubby"], @init.send(:find_plugins, default).sort
- assert_equal ["#{alt}/a", "#{acts}/acts_as_chunky_bacon", "#{default}/stubby"], @init.send(:find_plugins, base).sort
- end
-
- def test_load_plugin
- stubby = "#{File.dirname(__FILE__)}/fixtures/plugins/default/stubby"
- expected = ['stubby']
-
- assert @init.send(:load_plugin, stubby)
- assert_equal expected, @init.loaded_plugins
-
- assert !@init.send(:load_plugin, stubby)
- assert_equal expected, @init.loaded_plugins
-
- assert_raise(LoadError) { @init.send(:load_plugin, 'lakjsdfkasljdf') }
- assert_equal expected, @init.loaded_plugins
- end
-
- def test_load_default_plugins
- assert_loaded_plugins %w(stubby acts_as_chunky_bacon), 'default'
- end
-
- def test_load_alternate_plugins
- assert_loaded_plugins %w(a), 'alternate'
- end
-
- def test_load_plugins_from_two_sources
- assert_loaded_plugins %w(a stubby acts_as_chunky_bacon), ['default', 'alternate']
- end
-
- def test_load_all_plugins_when_config_plugins_is_nil
- @init.configuration.plugins = nil
- assert_loaded_plugins %w(a stubby acts_as_chunky_bacon), ['default', 'alternate']
- end
-
- def test_load_no_plugins_when_config_plugins_is_empty_array
- @init.configuration.plugins = []
- assert_loaded_plugins [], ['default', 'alternate']
- end
-
- def test_load_only_selected_plugins
- plugins = %w(stubby a)
- @init.configuration.plugins = plugins
- assert_loaded_plugins plugins, ['default', 'alternate']
- end
-
- def test_load_plugins_in_order
- plugins = %w(stubby acts_as_chunky_bacon a)
- @init.configuration.plugins = plugins
- assert_plugin_load_order plugins, ['default', 'alternate']
- end
-
- def test_raise_error_when_plugin_not_found
- @init.configuration.plugins = %w(this_plugin_does_not_exist)
- assert_raise(LoadError) { load_plugins(['default', 'alternate']) }
- end
-
- protected
- def assert_loaded_plugins(plugins, paths)
- assert_equal plugins.sort, load_plugins(paths).sort
- end
-
- def assert_plugin_load_order(plugins, paths)
- assert_equal plugins, load_plugins(paths)
- end
-
- def load_plugins(*paths)
- @init.configuration.plugin_paths = paths.flatten.map { |p| "#{File.dirname(__FILE__)}/fixtures/plugins/#{p}" }
- @init.load_plugins
- @init.loaded_plugins
- end
-end
diff --git a/railties/test/plugin_test_helper.rb b/railties/test/plugin_test_helper.rb
new file mode 100644
index 0000000000..58649ea2fa
--- /dev/null
+++ b/railties/test/plugin_test_helper.rb
@@ -0,0 +1,14 @@
+$:.unshift File.dirname(__FILE__) + "/../lib"
+$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
+
+require 'test/unit'
+require 'active_support'
+require 'initializer'
+
+# We need to set RAILS_ROOT if it isn't already set
+RAILS_ROOT = '.' unless defined?(RAILS_ROOT)
+class Test::Unit::TestCase
+ def plugin_fixture_root_path
+ File.join(File.dirname(__FILE__), 'fixtures', 'plugins')
+ end
+end \ No newline at end of file