aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/plugins_test.rb
blob: a4cf32213991c8058d7f1839a7d7bd02ce72117a (plain) (blame)
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
require "isolation/abstract_unit"

module ApplicationTests
  class PluginTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def assert_plugins(list_of_names, array_of_plugins, message=nil)
      assert_equal list_of_names.map { |n| n.to_s }, array_of_plugins.map { |p| p.name }, message
    end

    def setup
      build_app
      boot_rails
      @failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
      # Tmp hax to get tests working
      FileUtils.cp_r "#{File.dirname(__FILE__)}/../fixtures/plugins", "#{app_path}/vendor"
    end

    test "all plugins are loaded when registered plugin list is untouched" do
      Rails::Initializer.run { }
      Rails.application.new
      assert_plugins [
        :a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby
      ], Rails.application.config.loaded_plugins, @failure_tip
    end

    test "no plugins are loaded if the configuration has an empty plugin list" do
      Rails::Initializer.run { |c| c.plugins = [] }
      assert_plugins [], Rails.application.config.loaded_plugins
    end

    test "only the specified plugins are located in the order listed" do
      plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
      Rails::Initializer.run { |c| c.plugins = plugin_names }
      Rails.application.new
      assert_plugins plugin_names, Rails.application.config.loaded_plugins
    end

    test "all plugins loaded after all" do
      Rails::Initializer.run do |config|
        config.plugins = [:stubby, :all, :acts_as_chunky_bacon]
      end
      Rails.application.new
      assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], Rails.application.config.loaded_plugins, @failure_tip
    end

    test "plugin names may be strings" do
      plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
      Rails::Initializer.run do |config|
        config.plugins = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
      end
      Rails.application.new

      assert_plugins plugin_names, Rails.application.config.loaded_plugins, @failure_tip
    end

    test "all plugins loaded when all is used" do
      Rails::Initializer.run do |config|
        config.plugins = [:stubby, :acts_as_chunky_bacon, :all]
      end
      Rails.application.new
      
      assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], Rails.application.config.loaded_plugins, @failure_tip
    end

    test "all loaded plugins are added to the load paths" do
      Rails::Initializer.run do |config|
        config.plugins = [:stubby, :acts_as_chunky_bacon]
      end
      Rails.application.new

      assert $LOAD_PATH.include?("#{app_path}/vendor/plugins/default/stubby/lib")
      assert $LOAD_PATH.include?("#{app_path}/vendor/plugins/default/acts/acts_as_chunky_bacon/lib")
    end

    test "registering a plugin name that does not exist raises a load error" do
      Rails::Initializer.run do |config|
        config.plugins = [:stubby, :acts_as_a_non_existant_plugin]
      end

      assert_raise(LoadError) do
        Rails.application.new
      end
    end

    test "load error messages mention missing plugins and no others" do
      valid_plugins   = [:stubby, :acts_as_chunky_bacon]
      invalid_plugins = [:non_existant_plugin1, :non_existant_plugin2]

      begin
        Rails::Initializer.run do |config|
          config.plugins = [:stubby, :acts_as_chunky_bacon, :non_existant_plugin1, :non_existant_plugin2]
        end
        Rails.application.new
        flunk "Expected a LoadError but did not get one"
      rescue LoadError => e
        assert_plugins valid_plugins, Rails.application.config.loaded_plugins, @failure_tip

        invalid_plugins.each do |plugin|
          assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'")
        end

        valid_plugins.each do |plugin|
          assert_no_match(/#{plugin.to_s}/, e.message, "LoadError message should not mention '#{plugin}'")
        end
      end
    end

  end
end