public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
Refactor Plugin Loader.  Add plugin lib paths early, and add lots of 
tests.  Closes #9795 [lazyatom]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8115 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
rick (author)
Wed Nov 07 21:29:44 -0800 2007
commit  72ef776aad1356f20b9e746d172da414e06273d1
tree    76fc9e3e3ed72d6867ab5408decfd8651f5a49e8
parent  4dfb34dfb5bc43d95feb8e3b790246ef7cf949f0
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Refactor Plugin Loader. Add plugin lib paths early, and add lots of tests. Closes #9795 [lazyatom]
0
+
0
 * Added --skip-timestamps to generators that produce models #10036 [tpope]
0
 
0
 * Update Prototype to 1.6.0 and script.aculo.us to 1.8.0. [sam, madrobby]
...
34
35
36
37
 
38
39
40
...
64
65
66
 
67
68
69
...
83
84
85
86
 
87
88
 
89
90
91
...
161
162
163
 
 
 
 
 
 
164
165
166
167
168
169
170
171
 
 
 
172
173
174
...
178
179
180
181
182
183
184
185
186
187
 
 
 
 
 
188
189
190
...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
...
34
35
36
 
37
38
39
40
...
64
65
66
67
68
69
70
...
84
85
86
 
87
88
89
90
91
92
93
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 
 
 
177
178
179
180
181
182
...
186
187
188
 
 
 
 
 
 
 
189
190
191
192
193
194
195
196
...
343
344
345
 
 
 
 
 
 
 
 
 
346
347
348
0
@@ -34,7 +34,7 @@ module Rails
0
 
0
     # The set of loaded plugins.
0
     attr_reader :loaded_plugins
0
-
0
+
0
     # Runs the initializer. By default, this will invoke the #process method,
0
     # which simply executes all of the initialization routines. Alternately,
0
     # you can specify explicitly which initialization routine you want:
0
@@ -64,6 +64,7 @@ module Rails
0
     # * #set_load_path
0
     # * #require_frameworks
0
     # * #set_autoload_paths
0
+ # * add_plugin_load_paths
0
     # * #load_environment
0
     # * #initialize_encoding
0
     # * #initialize_database
0
@@ -83,9 +84,10 @@ module Rails
0
     def process
0
       check_ruby_version
0
       set_load_path
0
-
0
+
0
       require_frameworks
0
       set_autoload_paths
0
+ add_plugin_load_paths
0
       load_environment
0
 
0
       initialize_encoding
0
@@ -161,14 +163,20 @@ module Rails
0
     def add_support_load_paths
0
     end
0
 
0
+ # Adds all load paths from plugins to the global set of load paths, so that
0
+ # code from plugins can be required (explicitly or automatically via Dependencies).
0
+ def add_plugin_load_paths
0
+ plugin_loader.add_plugin_load_paths
0
+ end
0
+
0
     # Loads all plugins in <tt>config.plugin_paths</tt>. <tt>plugin_paths</tt>
0
     # defaults to <tt>vendor/plugins</tt> but may also be set to a list of
0
     # paths, such as
0
     # config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
0
     #
0
- # Each plugin discovered in <tt>plugin_paths</tt> is initialized:
0
- # * add its +lib+ directory, if present, to the beginning of the load path
0
- # * evaluate <tt>init.rb</tt> if present
0
+ # In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
0
+ # * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
0
+ # * <tt>init.rb</tt> is evalutated, if present
0
     #
0
     # After all plugins are loaded, duplicates are removed from the load path.
0
     # If an array of plugin names is specified in config.plugins, only those plugins will be loaded
0
@@ -178,13 +186,11 @@ module Rails
0
     # if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
0
     # plugins will be loaded in alphabetical order
0
     def load_plugins
0
- configuration.plugin_locators.each do |locator|
0
- locator.new(self).each do |plugin|
0
- plugin.load
0
- end
0
- end
0
- ensure_all_registered_plugins_are_loaded!
0
- $LOAD_PATH.uniq!
0
+ plugin_loader.load_plugins
0
+ end
0
+
0
+ def plugin_loader
0
+ @plugin_loader ||= configuration.plugin_loader.new(self)
0
     end
0
 
0
     # Loads the environment specified by Configuration#environment_path, which
0
@@ -337,15 +343,6 @@ module Rails
0
       end
0
     end
0
 
0
- private
0
- def ensure_all_registered_plugins_are_loaded!
0
- unless configuration.plugins.nil?
0
- if configuration.plugins.detect {|plugin| plugin != :all && !loaded_plugins.include?( plugin)}
0
- missing_plugins = configuration.plugins - (loaded_plugins + [:all])
0
- raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
0
- end
0
- end
0
- end
0
   end
0
 
0
   # The Configuration class holds all the parameters for the Initializer and
...
 
 
1
2
 
3
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
14
15
16
17
18
19
20
21
22
23
24
25
 
 
 
 
26
27
28
29
 
 
 
 
 
30
31
32
33
 
 
 
 
 
 
 
34
35
36
37
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
40
41
42
 
43
44
45
46
 
 
 
 
 
 
 
 
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
 
63
64
65
66
 
 
 
67
68
69
70
 
 
 
71
72
73
74
 
 
 
 
 
 
 
 
 
 
 
75
76
77
78
 
 
 
 
 
 
79
80
81
82
 
 
 
 
 
 
 
83
84
85
86
 
 
 
87
88
89
90
 
 
 
91
92
93
94
95
96
97
98
99
100
101
 
 
 
102
103
104
105
 
 
 
106
107
108
109
110
 
 
 
111
112
113
114
 
 
 
 
 
 
 
115
116
117
118
119
 
 
 
120
121
122
 
 
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 
 
 
138
139
140
141
142
143
 
144
145
146
147
...
1
2
3
 
4
5
 
 
 
 
 
 
 
 
 
 
6
7
8
9
10
11
12
13
14
15
16
17
 
 
18
 
 
 
 
 
 
 
 
19
20
21
22
23
 
 
 
24
25
26
27
28
29
 
 
 
30
31
32
33
34
35
36
37
38
 
 
 
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
 
 
55
56
 
 
 
57
58
59
60
61
62
63
64
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
67
68
 
 
 
69
70
71
72
73
 
 
74
75
76
77
 
 
 
78
79
80
81
82
83
84
85
86
87
88
89
90
 
 
91
92
93
94
95
96
97
 
 
 
98
99
100
101
102
103
104
105
 
 
 
106
107
108
109
 
 
 
110
111
112
113
 
 
 
 
 
 
 
 
 
 
114
115
116
117
 
 
 
118
119
120
121
 
 
 
 
122
123
124
125
 
 
 
126
127
128
129
130
131
132
133
 
 
 
 
134
135
136
137
 
 
138
139
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
142
143
144
 
 
 
145
146
147
148
149
150
151
0
@@ -1,146 +1,150 @@
0
+require "rails/plugin"
0
+
0
 module Rails
0
- module Plugin
0
+ class Plugin
0
     class Loader
0
- include Comparable
0
- attr_reader :initializer, :directory, :name
0
-
0
- class << self
0
- def load(*args)
0
- new(*args).load
0
- end
0
- end
0
-
0
- def initialize(initializer, directory)
0
+ attr_reader :initializer
0
+
0
+ # Creates a new Plugin::Loader instance, associated with the given
0
+ # Rails::Initializer. This default implementation automatically locates
0
+ # all plugins, and adds all plugin load paths, when it is created. The plugins
0
+ # are then fully loaded (init.rb is evaluated) when load_plugins is called.
0
+ #
0
+ # It is the loader's responsibilty to ensure that only the plugins specified
0
+ # in the configuration are actually loaded, and that the order defined
0
+ # is respected.
0
+ def initialize(initializer)
0
         @initializer = initializer
0
- @directory = directory
0
- @name = File.basename(directory).to_sym
0
       end
0
-
0
- def load
0
- return false if loaded?
0
- report_nonexistant_or_empty_plugin!
0
- add_to_load_path!
0
- register_plugin_as_loaded
0
- evaluate
0
- true
0
+
0
+ # Returns the plugins to be loaded, in the order they should be loaded.
0
+ def plugins
0
+ @plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
0
       end
0
-
0
- def loaded?
0
- initializer.loaded_plugins.include?(name)
0
+
0
+ # Returns all the plugins that could be found by the current locators.
0
+ def all_plugins
0
+ @all_plugins ||= locate_plugins
0
+ @all_plugins
0
       end
0
-
0
- def plugin_path?
0
- File.directory?(directory) && (has_lib_directory? || has_init_file?)
0
+
0
+ def load_plugins
0
+ plugins.each do |plugin|
0
+ plugin.load(initializer)
0
+ register_plugin_as_loaded(plugin)
0
+ end
0
+ ensure_all_registered_plugins_are_loaded!
0
       end
0
       
0
- def enabled?
0
- !explicit_plugin_loading_order? || registered?
0
- end
0
+ # Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
0
+ # added *after* the application's <tt>lib</tt> directory, to ensure that an application
0
+ # can always override code within a plugin.
0
+ #
0
+ # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
0
+ def add_plugin_load_paths
0
+ plugins.each do |plugin|
0
+ plugin.load_paths.each do |path|
0
+ $LOAD_PATH.insert(application_lib_index + 1, path)
0
+ Dependencies.load_paths << path
0
+ Dependencies.load_once_paths << path
0
+ end
0
+ end
0
+ $LOAD_PATH.uniq!
0
+ end
0
       
0
- def explicitly_enabled?
0
- !explicit_plugin_loading_order? || explicitly_registered?
0
- end
0
+ protected
0
       
0
- def registered?
0
- explicit_plugin_loading_order? && registered_plugins_names_plugin?(name)
0
- end
0
+ # The locate_plugins method uses each class in config.plugin_locators to
0
+ # find the set of all plugins available to this Rails application.
0
+ def locate_plugins
0
+ configuration.plugin_locators.map { |locator|
0
+ locator.new(initializer).plugins
0
+ }.flatten
0
+ # TODO: sorting based on config.plugins
0
+ end
0
 
0
- def explicitly_registered?
0
- explicit_plugin_loading_order? && registered_plugins.include?(name)
0
- end
0
-
0
- def plugin_does_not_exist!(plugin_name = name)
0
- raise LoadError, "Can not find the plugin named: #{plugin_name}"
0
- end
0
-
0
- private
0
- # The plugins that have been explicitly listed with config.plugins. If this list is nil
0
- # then it means the client does not care which plugins or in what order they are loaded,
0
- # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
0
- # non empty, we load the named plugins in the order specified.
0
- def registered_plugins
0
- config.plugins
0
+ def register_plugin_as_loaded(plugin)
0
+ initializer.loaded_plugins << plugin
0
         end
0
-
0
- def registered_plugins_names_plugin?(plugin_name)
0
- registered_plugins.include?(plugin_name) || registered_plugins.include?(:all)
0
+
0
+ def configuration
0
+ initializer.configuration
0
         end
0
         
0
- def explicit_plugin_loading_order?
0
- !registered_plugins.nil?
0
+ def should_load?(plugin)
0
+ # uses Plugin#name and Plugin#valid?
0
+ enabled?(plugin) && plugin.valid?
0
         end
0
-
0
- def report_nonexistant_or_empty_plugin!
0
- plugin_does_not_exist! unless plugin_path?
0
+
0
+ def order_plugins(plugin_a, plugin_b)
0
+ if !explicit_plugin_loading_order?
0
+ plugin_a <=> plugin_b
0
+ else
0
+ if !explicitly_enabled?(plugin_a) && !explicitly_enabled?(plugin_b)
0
+ plugin_a <=> plugin_b
0
+ else
0
+ effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
0
+ end
0
+ end
0
         end
0
         
0
- def lib_path
0
- File.join(directory, 'lib')
0
+ def effective_order_of(plugin)
0
+ if explicitly_enabled?(plugin)
0
+ registered_plugin_names.index(plugin.name)
0
+ else
0
+ registered_plugin_names.index('all')
0
+ end
0
         end
0
-
0
- def init_path
0
- File.join(directory, 'init.rb')
0
+
0
+ def application_lib_index
0
+ $LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
0
+ end
0
+
0
+ def enabled?(plugin)
0
+ !explicit_plugin_loading_order? || registered?(plugin)
0
         end
0
-
0
- def has_lib_directory?
0
- File.directory?(lib_path)
0
+
0
+ def explicit_plugin_loading_order?
0
+ !registered_plugin_names.nil?
0
         end
0
-
0
- def has_init_file?
0
- File.file?(init_path)
0
+
0
+ def registered?(plugin)
0
+ explicit_plugin_loading_order? && registered_plugins_names_plugin?(plugin)
0
         end
0
-
0
- def add_to_load_path!
0
- # Add lib to load path *after* the application lib, to allow
0
- # application libraries to override plugin libraries.
0
- if has_lib_directory?
0
- application_lib_index = $LOAD_PATH.index(application_library_path) || 0
0
- $LOAD_PATH.insert(application_lib_index + 1, lib_path)
0
- Dependencies.load_paths << lib_path
0
- Dependencies.load_once_paths << lib_path
0
- end
0
+
0
+ def explicitly_enabled?(plugin)
0
+ !explicit_plugin_loading_order? || explicitly_registered?(plugin)
0
         end
0
-
0
- def application_library_path
0
- File.join(RAILS_ROOT, 'lib')
0
+
0
+ def explicitly_registered?(plugin)
0
+ explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
0
         end
0
-
0
- # Allow plugins to reference the current configuration object
0
- def config
0
- initializer.configuration
0
+
0
+ def registered_plugins_names_plugin?(plugin)
0
+ registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
0
         end
0
-
0
- def register_plugin_as_loaded
0
- initializer.loaded_plugins << name
0
+
0
+ # The plugins that have been explicitly listed with config.plugins. If this list is nil
0
+ # then it means the client does not care which plugins or in what order they are loaded,
0
+ # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
0
+ # non empty, we load the named plugins in the order specified.
0
+ def registered_plugin_names
0
+ configuration.plugins ? configuration.plugins.map(&:to_s) : nil
0
         end
0
-
0
- # Evaluate in init.rb
0
- def evaluate
0
- silence_warnings { eval(IO.read(init_path), binding, init_path) } if has_init_file?
0
+
0
+ def loaded?(plugin_name)
0
+ initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
0
         end
0
-
0
- def <=>(other_plugin_loader)
0
+
0
+ def ensure_all_registered_plugins_are_loaded!
0
           if explicit_plugin_loading_order?
0
- if non_existent_plugin = [self, other_plugin_loader].detect { |plugin| !registered_plugins_names_plugin?(plugin.name) }
0
- plugin_does_not_exist!(non_existent_plugin.name)
0
- end
0
-
0
- if !explicitly_enabled? && !other_plugin_loader.explicitly_enabled?
0
- name.to_s <=> other_plugin_loader.name.to_s
0
- elsif registered_plugins.include?(:all) && (!explicitly_enabled? || !other_plugin_loader.explicitly_enabled?)
0
- effective_index = explicitly_enabled? ? registered_plugins.index(name) : registered_plugins.index(:all)
0
- other_effective_index = other_plugin_loader.explicitly_enabled? ?
0
- registered_plugins.index(other_plugin_loader.name) : registered_plugins.index(:all)
0
-
0
- effective_index <=> other_effective_index
0
- else
0
- registered_plugins.index(name) <=> registered_plugins.index(other_plugin_loader.name)
0
+ if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
0
+ missing_plugins = configuration.plugins - (plugins + [:all])
0
+ raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
0
             end
0
-
0
- else
0
- name.to_s <=> other_plugin_loader.name.to_s
0
           end
0
         end
0
+
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
1
2
 
 
 
 
 
3
4
 
5
6
7
8
9
10
 
 
 
11
12
 
13
14
15
...
19
20
21
22
23
24
25
26
27
28
 
 
 
 
 
 
29
30
31
32
33
34
35
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
 
57
58
59
60
...
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
20
21
22
23
...
27
28
29
 
 
 
 
 
30
31
32
33
34
35
36
37
38
 
 
 
 
 
 
 
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
0
@@ -1,15 +1,23 @@
0
 module Rails
0
- module Plugin
0
+ class Plugin
0
+
0
+ # The Plugin::Locator class should be subclasses to provide custom plugin-finding
0
+ # abilities to Rails (i.e. loading plugins from Gems, etc). Each subclass should implement
0
+ # the <tt>located_plugins</tt> method, which return an array of Plugin objects that have been found.
0
     class Locator
0
       include Enumerable
0
+
0
       attr_reader :initializer
0
       
0
       def initialize(initializer)
0
         @initializer = initializer
0
       end
0
       
0
+ # This method should return all the plugins which this Plugin::Locator can find
0
+ # These will then be used by the current Plugin::Loader, which is responsible for actually
0
+ # loading the plugins themselves
0
       def plugins
0
- located_plugins.select(&:enabled?).sort
0
+ raise "The `plugins' method must be defined by concrete subclasses of #{self.class}"
0
       end
0
       
0
       def each(&block)
0
@@ -19,41 +27,52 @@ module Rails
0
       def plugin_names
0
         plugins.map(&:name)
0
       end
0
-
0
- private
0
- def located_plugins
0
- raise "The `located_plugins' method must be defined by concrete subclasses of #{self.class}"
0
- end
0
     end
0
     
0
+ # The Rails::Plugin::FileSystemLocator will try to locate plugins by examining the directories
0
+ # the the paths given in configuration.plugin_paths. Any plugins that can be found are returned
0
+ # in a list.
0
+ #
0
+ # The criteria for a valid plugin in this case is found in Rails::Plugin#valid?, although
0
+ # other subclasses of Rails::Plugin::Locator can of course use different conditions.
0
     class FileSystemLocator < Locator
0
- private
0
- def located_plugins
0
- initializer.configuration.plugin_paths.flatten.inject([]) do |plugins, path|
0
- plugins.concat locate_plugins_under(path)
0
- plugins
0
- end.flatten
0
- end
0
+
0
+ # Returns all the plugins which can be loaded in the filesystem, under the paths given
0
+ # by configuration.plugin_paths.
0
+ def plugins
0
+ initializer.configuration.plugin_paths.flatten.inject([]) do |plugins, path|
0
+ plugins.concat locate_plugins_under(path)
0
+ plugins
0
+ end.flatten
0
+ end
0
+
0
+ private
0
+
0
+ # Attempts to create a plugin from the given path. If the created plugin is valid?
0
+ # (see Rails::Plugin#valid?) then the plugin instance is returned; otherwise nil.
0
+ def create_plugin(path)
0
+ plugin = Rails::Plugin.new(path)
0
+ plugin.valid? ? plugin : nil
0
+ end
0
 
0
- # This starts at the base path looking for directories that pass the plugin_path? test of the Plugin::Loader.
0
- # Since plugins can be nested arbitrarily deep within an unspecified number of intermediary directories,
0
- # this method runs recursively until it finds a plugin directory.
0
- #
0
- # e.g.
0
- #
0
- # locate_plugins_under('vendor/plugins/acts/acts_as_chunky_bacon')
0
- # => 'acts_as_chunky_bacon'
0
- def locate_plugins_under(base_path)
0
- Dir.glob(File.join(base_path, '*')).inject([]) do |plugins, path|
0
- plugin_loader = initializer.configuration.plugin_loader.new(initializer, path)
0
- if plugin_loader.plugin_path? && plugin_loader.enabled?
0
- plugins << plugin_loader
0
- elsif File.directory?(path)
0
- plugins.concat locate_plugins_under(path)
0
- end
0
- plugins
0
+ # This starts at the base path looking for valid plugins (see Rails::Plugin#valid?).
0
+ # Since plugins can be nested arbitrarily deep within an unspecified number of intermediary
0
+ # directories, this method runs recursively until it finds a plugin directory, e.g.
0
+ #
0
+ # locate_plugins_under('vendor/plugins/acts/acts_as_chunky_bacon')
0
+ # => <Rails::Plugin name: 'acts_as_chunky_bacon' ... >
0
+ #
0
+ def locate_plugins_under(base_path)
0
+ Dir.glob(File.join(base_path, '*')).inject([]) do |plugins, path|
0
+ if plugin = create_plugin(path)
0
+ plugins << plugin
0
+ elsif File.directory?(path)
0
+ plugins.concat locate_plugins_under(path)
0
             end
0
+ plugins
0
           end
0
+ end
0
+
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
135
136
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
0
@@ -135,3 +135,84 @@ uses_mocha 'framework paths' do
0
       end
0
   end
0
 end
0
+
0
+uses_mocha "Initializer plugin loading tests" do
0
+ require File.dirname(__FILE__) + '/plugin_test_helper'
0
+
0
+ class InitializerPluginLoadingTests < Test::Unit::TestCase
0
+ def setup
0
+ @configuration = Rails::Configuration.new
0
+ @configuration.plugin_paths << plugin_fixture_root_path
0
+ @initializer = Rails::Initializer.new(@configuration)
0
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
0
+ @empty_plugin_path = plugin_fixture_path('default/empty')
0
+ end
0
+
0
+ def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
0
+ only_load_the_following_plugins! []
0
+ @initializer.load_plugins
0
+ assert_equal [], @initializer.loaded_plugins
0
+ end
0
+
0
+ def test_only_the_specified_plugins_are_located_in_the_order_listed
0
+ plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
0
+ only_load_the_following_plugins! plugin_names
0
+ load_plugins!
0
+ assert_plugins plugin_names, @initializer.loaded_plugins
0
+ end
0
+
0
+ def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
0
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
+ load_plugins!
0
+ assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
0
+ end
0
+
0
+ def test_all_plugins_loaded_when_all_is_used
0
+ plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
0
+ only_load_the_following_plugins! plugin_names
0
+ load_plugins!
0
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
+ assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
0
+ end
0
+
0
+ def test_all_plugins_loaded_after_all
0
+ plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
0
+ only_load_the_following_plugins! plugin_names
0
+ load_plugins!
0
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
+ assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
0
+ end
0
+
0
+ def test_plugin_names_may_be_strings
0
+ plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
0
+ only_load_the_following_plugins! plugin_names
0
+ load_plugins!
0
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
+ assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
0
+ end
0
+
0
+ def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
0
+ only_load_the_following_plugins! [:stubby, :acts_as_a_non_existant_plugin]
0
+ assert_raises(LoadError) do
0
+ load_plugins!
0
+ end
0
+ end
0
+
0
+ def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
0
+ only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
+
0
+ @initializer.add_plugin_load_paths
0
+
0
+ assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
0
+ assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
0
+ end
0
+
0
+ private
0
+
0
+ def load_plugins!
0
+ @initializer.add_plugin_load_paths
0
+ @initializer.load_plugins
0
+ end
0
+ end
0
+
0
+end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
 
 
 
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
 
 
 
 
53
54
55
56
57
 
 
 
58
59
60
61
 
 
 
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
81
82
83
 
 
 
 
84
85
86
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
91
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
 
 
 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
33
34
35
 
 
 
36
37
38
39
40
 
 
41
42
43
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
 
75
76
77
78
79
80
 
 
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
0
@@ -1,90 +1,140 @@
0
 require File.dirname(__FILE__) + '/plugin_test_helper'
0
 
0
-class TestPluginLoader < Test::Unit::TestCase
0
- def setup
0
- @initializer = Rails::Initializer.new(Rails::Configuration.new)
0
- @valid_plugin_path = plugin_fixture_path('default/stubby')
0
- @empty_plugin_path = plugin_fixture_path('default/empty')
0
- end
0
-
0
- def test_determining_if_the_plugin_order_has_been_explicitly_set
0
- loader = loader_for(@valid_plugin_path)
0
- assert !loader.send(:explicit_plugin_loading_order?)
0
- only_load_the_following_plugins! %w(stubby acts_as_chunky_bacon)
0
- assert loader.send(:explicit_plugin_loading_order?)
0
- end
0
-
0
- def test_enabled_if_not_named_explicitly
0
- stubby_loader = loader_for(@valid_plugin_path)
0
- acts_as_loader = loader_for('acts_as/acts_as_chunky_bacon')
0
+uses_mocha "Plugin Loader Tests" do
0
+
0
+ class TestPluginLoader < Test::Unit::TestCase
0
+ ORIGINAL_LOAD_PATH = $LOAD_PATH.dup
0
     
0
- only_load_the_following_plugins! ['stubby', :all]
0
- assert stubby_loader.send(:enabled?)
0
- assert acts_as_loader.send(:enabled?)
0
+ def setup
0
+ reset_load_path!
0
+
0
+ @configuration = Rails::Configuration.new
0
+ @configuration.plugin_paths << plugin_fixture_root_path
0
+ @initializer = Rails::Initializer.new(@configuration)
0
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
0
+ @empty_plugin_path = plugin_fixture_path('default/empty')
0
+
0
+ @loader = Rails::Plugin::Loader.new(@initializer)
0
+ end
0
+
0
+ def test_should_locate_plugins_by_asking_each_locator_specifed_in_configuration_for_its_plugins_result
0
+ locator_1 = stub(:plugins => [:a, :b, :c])
0
+ locator_2 = stub(:plugins => [:d, :e, :f])
0
+ locator_class_1 = stub(:new => locator_1)
0
+ locator_class_2 = stub(:new => locator_2)
0
+ @configuration.plugin_locators = [locator_class_1, locator_class_2]
0
+ assert_equal [:a, :b, :c, :d, :e, :f], @loader.send(:locate_plugins)
0
+