Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/railties/test/plugin_loader_test.rb

Revision 8115, 6.4 kB (checked in by rick, 1 year ago)

Refactor Plugin Loader. Add plugin lib paths early, and add lots of tests. Closes #9795 [lazyatom]

Line 
1 require File.dirname(__FILE__) + '/plugin_test_helper'
2
3 uses_mocha "Plugin Loader Tests" do
4
5   class TestPluginLoader < Test::Unit::TestCase
6     ORIGINAL_LOAD_PATH = $LOAD_PATH.dup
7    
8     def setup
9       reset_load_path!
10      
11       @configuration     = Rails::Configuration.new
12       @configuration.plugin_paths << plugin_fixture_root_path
13       @initializer       = Rails::Initializer.new(@configuration)
14       @valid_plugin_path = plugin_fixture_path('default/stubby')
15       @empty_plugin_path = plugin_fixture_path('default/empty')
16      
17       @loader = Rails::Plugin::Loader.new(@initializer)
18     end
19
20     def test_should_locate_plugins_by_asking_each_locator_specifed_in_configuration_for_its_plugins_result
21       locator_1 = stub(:plugins => [:a, :b, :c])
22       locator_2 = stub(:plugins => [:d, :e, :f])
23       locator_class_1 = stub(:new => locator_1)
24       locator_class_2 = stub(:new => locator_2)
25       @configuration.plugin_locators = [locator_class_1, locator_class_2]
26       assert_equal [:a, :b, :c, :d, :e, :f], @loader.send(:locate_plugins)
27     end
28    
29     def test_should_memoize_the_result_of_locate_plugins_as_all_plugins
30       plugin_list = [:a, :b, :c]
31       @loader.expects(:locate_plugins).once.returns(plugin_list)
32       assert_equal plugin_list, @loader.all_plugins
33       assert_equal plugin_list, @loader.all_plugins # ensuring that locate_plugins isn't called again
34     end
35    
36     def test_should_return_empty_array_if_configuration_plugins_is_empty
37       @configuration.plugins = []
38       assert_equal [], @loader.plugins
39     end
40    
41     def test_should_find_all_availble_plugins_and_return_as_all_plugins
42       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
43       assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.all_plugins, failure_tip     
44     end
45
46     def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched
47       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
48       assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
49     end
50    
51     def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil
52       @configuration.plugins = nil
53       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
54       assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
55     end
56
57     def test_should_return_specific_plugins_named_in_config_plugins_array_if_set
58       plugin_names = [:acts_as_chunky_bacon, :stubby]
59       only_load_the_following_plugins! plugin_names
60       assert_plugins plugin_names, @loader.plugins
61     end
62    
63     def test_should_respect_the_order_of_plugins_given_in_configuration
64       plugin_names = [:stubby, :acts_as_chunky_bacon]
65       only_load_the_following_plugins! plugin_names
66       assert_plugins plugin_names, @loader.plugins     
67     end
68    
69     def test_should_load_all_plugins_in_natural_order_when_all_is_used
70       only_load_the_following_plugins! [:all]
71       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
72       assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
73     end
74    
75     def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used
76       only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all]
77       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
78       assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, failure_tip
79     end
80    
81     def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all
82       only_load_the_following_plugins!  [:stubby, :all, :acts_as_chunky_bacon]
83       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
84       assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, failure_tip
85     end
86
87     def test_should_accept_plugin_names_given_as_strings
88       only_load_the_following_plugins! ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
89       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
90       assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, failure_tip
91     end
92    
93     def test_should_add_plugin_load_paths_to_global_LOAD_PATH_array
94       only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
95       stubbed_application_lib_index_in_LOAD_PATHS = 5
96       @loader.stubs(:application_lib_index).returns(stubbed_application_lib_index_in_LOAD_PATHS)
97      
98       @loader.add_plugin_load_paths
99      
100       assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/stubby'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS
101       assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS   
102     end   
103    
104     def test_should_add_plugin_load_paths_to_Dependencies_load_paths
105       only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
106
107       @loader.add_plugin_load_paths
108      
109       assert Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
110       assert Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))   
111     end
112    
113     def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths
114       only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
115
116       @loader.add_plugin_load_paths
117      
118       assert Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
119       assert Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))   
120     end
121    
122     def test_should_add_all_load_paths_from_a_plugin_to_LOAD_PATH_array
123       plugin_load_paths = ["a", "b"]
124       plugin = stub(:load_paths => plugin_load_paths)
125       @loader.stubs(:plugins).returns([plugin])
126      
127       @loader.add_plugin_load_paths
128      
129       plugin_load_paths.each { |path| assert $LOAD_PATH.include?(path) }
130     end
131    
132     private
133    
134       def reset_load_path!
135         $LOAD_PATH.clear
136         ORIGINAL_LOAD_PATH.each { |path| $LOAD_PATH << path }       
137       end
138   end
139    
140 end
Note: See TracBrowser for help on using the browser.