Skip to content
This repository
Browse code

Ensure that plugins are not loaded twice

  • Loading branch information...
commit 15f95b98ac048407a1ce6873bb02fc5fbb6c991c 1 parent efe9555
Piotr Sarnacki authored July 14, 2010
9  railties/lib/rails/plugin.rb
@@ -18,6 +18,10 @@ module Rails
18 18
   # root during the boot process.
19 19
   #
20 20
   class Plugin < Engine
  21
+    def self.global_plugins
  22
+      @global_plugins ||= []
  23
+    end
  24
+
21 25
     def self.inherited(base)
22 26
       raise "You cannot inherit from Rails::Plugin"
23 27
     end
@@ -28,6 +32,11 @@ def self.all(list, paths)
28 32
         Dir["#{path}/*"].each do |plugin_path|
29 33
           plugin = new(plugin_path)
30 34
           next unless list.include?(plugin.name) || list.include?(:all)
  35
+          if global_plugins.include?(plugin.name)
  36
+            warn "WARNING: plugin #{plugin.name} from #{path} was not loaded. Plugin with the same name has been already loaded."
  37
+            next
  38
+          end
  39
+          global_plugins << plugin.name
31 40
           plugins << plugin
32 41
         end
33 42
       end
38  railties/test/railties/engine_test.rb
... ...
@@ -1,8 +1,23 @@
1 1
 require "isolation/abstract_unit"
2 2
 require "railties/shared_tests"
  3
+require 'stringio'
3 4
 
4 5
 module RailtiesTest
5 6
   class EngineTest < Test::Unit::TestCase
  7
+    # TODO: it's copied from generators/test_case, maybe make a module with such helpers?
  8
+    def capture(stream)
  9
+      begin
  10
+        stream = stream.to_s
  11
+        eval "$#{stream} = StringIO.new"
  12
+        yield
  13
+        result = eval("$#{stream}").string
  14
+      ensure
  15
+        eval("$#{stream} = #{stream.upcase}")
  16
+      end
  17
+
  18
+      result
  19
+    end
  20
+
6 21
     include ActiveSupport::Testing::Isolation
7 22
     include SharedTests
8 23
 
@@ -126,5 +141,28 @@ class Engine < ::Rails::Engine
126 141
 
127 142
       assert Bukkits::Engine.config.yaffle_loaded
128 143
     end
  144
+
  145
+    test "engine does not load plugins that already exists in application" do
  146
+      @plugin.write "lib/bukkits.rb", <<-RUBY
  147
+        class Bukkits
  148
+          class Engine < ::Rails::Engine
  149
+          end
  150
+        end
  151
+      RUBY
  152
+
  153
+      @plugin.write "vendor/plugins/yaffle/init.rb", <<-RUBY
  154
+        config.engine_yaffle_loaded = true
  155
+      RUBY
  156
+
  157
+      app_file "vendor/plugins/yaffle/init.rb", <<-RUBY
  158
+        config.app_yaffle_loaded = true
  159
+      RUBY
  160
+
  161
+      warnings = capture(:stderr) { boot_rails }
  162
+
  163
+      assert !warnings.empty?
  164
+      assert !Bukkits::Engine.config.respond_to?(:engine_yaffle_loaded)
  165
+      assert Rails.application.config.app_yaffle_loaded
  166
+    end
129 167
   end
130 168
 end

0 notes on commit 15f95b9

Please sign in to comment.
Something went wrong with that request. Please try again.