aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/railties/plugin_test.rb
blob: 0f5f29468c19976431f8804616b23a156bd8167a (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "isolation/abstract_unit"
require "railties/shared_tests"

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

    def setup
      build_app

      @plugin = plugin "bukkits", "::LEVEL = config.log_level" do |plugin|
        plugin.write "lib/bukkits.rb", "class Bukkits; end"
        plugin.write "lib/another.rb", "class Another; end"
      end
    end

    def reload_config
      :reload_plugins
    end

    test "Rails::Plugin itself does not respond to config" do
      boot_rails
      assert !Rails::Plugin.respond_to?(:config)
    end

    test "cannot inherit from Rails::Plugin" do
      boot_rails
      assert_raise RuntimeError do
        class Foo < Rails::Plugin; end
      end
    end

    test "plugin can load the file with the same name in lib" do
      boot_rails
      require "bukkits"
      assert_equal "Bukkits", Bukkits.name
    end

    test "it loads the plugin's init.rb file" do
      boot_rails
      assert_equal "loaded", BUKKITS
    end

    test "the init.rb file has access to the config object" do
      boot_rails
      assert_equal :debug, LEVEL
    end

    test "plugin_init_is_run_before_application_ones" do
      plugin "foo", "$foo = true" do |plugin|
        plugin.write "lib/foo.rb", "module Foo; end"
      end

      app_file 'config/initializers/foo.rb', <<-RUBY
        raise "no $foo" unless $foo
        raise "no Foo" unless Foo
      RUBY

      boot_rails
      assert $foo
    end

    test "plugin should work without init.rb" do
      @plugin.delete("init.rb")

      boot_rails

      require "bukkits"
      assert_nothing_raised { Bukkits }
    end

    test "plugin cannot declare an engine for it" do
      @plugin.write "lib/bukkits.rb", <<-RUBY
        class Bukkits
          class Engine < Rails::Engine
          end
        end
      RUBY

      @plugin.write "init.rb", <<-RUBY
        require "bukkits"
      RUBY

      rescued = false

      begin
        boot_rails
      rescue Exception => e
        rescued = true
        assert_equal '"bukkits" is a Railtie/Engine and cannot be installed as plugin', e.message
      end

      assert rescued, "Expected boot rails to fail"
    end

    test "loads deprecated rails/init.rb" do
      @plugin.write "rails/init.rb", <<-RUBY
        $loaded = true
      RUBY

      boot_rails
      assert $loaded
    end

    test "deprecated tasks are also loaded" do
      $executed = false
      @plugin.write "tasks/foo.rake", <<-RUBY
        task :foo do
          $executed = true
        end
      RUBY

      boot_rails
      require 'rake'
      require 'rake/rdoctask'
      require 'rake/testtask'
      Rails.application.load_tasks
      Rake::Task[:foo].invoke
      assert $executed
    end

  end
end