2009-10-08 15:14:57 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'rails/initializable'
|
|
|
|
|
|
|
|
module InitializableTests
|
|
|
|
|
|
|
|
class Foo
|
2009-11-02 20:19:03 -05:00
|
|
|
include Rails::Initializable
|
2010-06-24 05:39:37 -04:00
|
|
|
attr_accessor :foo, :bar
|
2009-10-08 15:14:57 -04:00
|
|
|
|
2010-01-31 00:27:24 -05:00
|
|
|
initializer :start do
|
2009-10-08 15:14:57 -04:00
|
|
|
@foo ||= 0
|
|
|
|
@foo += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Bar < Foo
|
2010-01-22 19:29:29 -05:00
|
|
|
initializer :bar do
|
2009-10-08 15:14:57 -04:00
|
|
|
@bar ||= 0
|
|
|
|
@bar += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-27 19:48:35 -04:00
|
|
|
class Parent
|
2009-11-02 20:19:03 -05:00
|
|
|
include Rails::Initializable
|
2009-10-27 19:48:35 -04:00
|
|
|
|
2010-01-22 19:29:29 -05:00
|
|
|
initializer :one do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 1
|
|
|
|
end
|
|
|
|
|
2010-01-22 19:29:29 -05:00
|
|
|
initializer :two do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Child < Parent
|
2009-11-02 20:19:03 -05:00
|
|
|
include Rails::Initializable
|
2009-10-27 19:48:35 -04:00
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :three, before: :one do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 3
|
|
|
|
end
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :four, after: :one, before: :two do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Parent
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :five, before: :one do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Instance
|
|
|
|
include Rails::Initializable
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :one, group: :assets do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer :two do
|
|
|
|
$arr << 2
|
|
|
|
end
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :three, group: :all do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 3
|
|
|
|
end
|
|
|
|
|
2010-01-22 19:29:29 -05:00
|
|
|
initializer :four do
|
2009-10-27 19:48:35 -04:00
|
|
|
$arr << 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-02 20:19:03 -05:00
|
|
|
class WithArgs
|
|
|
|
include Rails::Initializable
|
|
|
|
|
|
|
|
initializer :foo do |arg|
|
|
|
|
$with_arg = arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class OverriddenInitializer
|
|
|
|
class MoreInitializers
|
|
|
|
include Rails::Initializable
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :startup, before: :last do
|
2009-11-06 20:21:39 -05:00
|
|
|
$arr << 3
|
|
|
|
end
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer :terminate, after: :first, before: :startup do
|
2009-11-05 17:45:53 -05:00
|
|
|
$arr << two
|
|
|
|
end
|
|
|
|
|
|
|
|
def two
|
|
|
|
2
|
2009-11-02 20:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Rails::Initializable
|
|
|
|
|
|
|
|
initializer :first do
|
|
|
|
$arr << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer :last do
|
2009-11-06 20:21:39 -05:00
|
|
|
$arr << 4
|
2009-11-02 20:19:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.initializers
|
2009-11-05 17:45:53 -05:00
|
|
|
super + MoreInitializers.new.initializers
|
2009-11-02 20:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-27 01:08:50 -05:00
|
|
|
module Interdependent
|
|
|
|
class PluginA
|
|
|
|
include Rails::Initializable
|
|
|
|
|
|
|
|
initializer "plugin_a.startup" do
|
|
|
|
$arr << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer "plugin_a.terminate" do
|
|
|
|
$arr << 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PluginB
|
|
|
|
include Rails::Initializable
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer "plugin_b.startup", after: "plugin_a.startup" do
|
2010-01-27 01:08:50 -05:00
|
|
|
$arr << 2
|
|
|
|
end
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
initializer "plugin_b.terminate", before: "plugin_a.terminate" do
|
2010-01-27 01:08:50 -05:00
|
|
|
$arr << 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Application
|
|
|
|
include Rails::Initializable
|
|
|
|
def self.initializers
|
|
|
|
PluginB.initializers + PluginA.initializers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-08 15:14:57 -04:00
|
|
|
class Basic < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
test "initializers run" do
|
2010-06-24 05:39:37 -04:00
|
|
|
foo = Foo.new
|
|
|
|
foo.run_initializers
|
|
|
|
assert_equal 1, foo.foo
|
2009-10-08 15:14:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "initializers are inherited" do
|
2010-06-24 05:39:37 -04:00
|
|
|
bar = Bar.new
|
|
|
|
bar.run_initializers
|
|
|
|
assert_equal [1, 1], [bar.foo, bar.bar]
|
2009-10-08 15:14:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "initializers only get run once" do
|
2010-06-24 05:39:37 -04:00
|
|
|
foo = Foo.new
|
|
|
|
foo.run_initializers
|
|
|
|
foo.run_initializers
|
|
|
|
assert_equal 1, foo.foo
|
2009-10-08 15:30:58 -04:00
|
|
|
end
|
2009-12-23 16:45:55 -05:00
|
|
|
|
|
|
|
test "creating initializer without a block raises an error" do
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
Class.new do
|
|
|
|
include Rails::Initializable
|
|
|
|
|
|
|
|
initializer :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-10-08 15:14:57 -04:00
|
|
|
end
|
2009-10-27 19:48:35 -04:00
|
|
|
|
|
|
|
class BeforeAfter < ActiveSupport::TestCase
|
|
|
|
test "running on parent" do
|
|
|
|
$arr = []
|
2010-06-24 05:39:37 -04:00
|
|
|
Parent.new.run_initializers
|
2009-10-27 19:48:35 -04:00
|
|
|
assert_equal [5, 1, 2], $arr
|
|
|
|
end
|
|
|
|
|
|
|
|
test "running on child" do
|
|
|
|
$arr = []
|
2010-06-24 05:39:37 -04:00
|
|
|
Child.new.run_initializers
|
2009-10-27 19:48:35 -04:00
|
|
|
assert_equal [5, 3, 1, 4, 2], $arr
|
|
|
|
end
|
2010-01-27 01:08:50 -05:00
|
|
|
|
|
|
|
test "handles dependencies introduced before all initializers are loaded" do
|
|
|
|
$arr = []
|
2010-06-24 05:39:37 -04:00
|
|
|
Interdependent::Application.new.run_initializers
|
2010-01-27 01:08:50 -05:00
|
|
|
assert_equal [1, 2, 3, 4], $arr
|
|
|
|
end
|
2009-10-27 19:48:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class InstanceTest < ActiveSupport::TestCase
|
|
|
|
test "running locals" do
|
|
|
|
$arr = []
|
|
|
|
instance = Instance.new
|
2009-10-27 22:05:29 -04:00
|
|
|
instance.run_initializers
|
2011-10-02 16:02:12 -04:00
|
|
|
assert_equal [2, 3, 4], $arr
|
2009-10-27 19:48:35 -04:00
|
|
|
end
|
2011-09-23 20:40:02 -04:00
|
|
|
|
|
|
|
test "running locals with groups" do
|
|
|
|
$arr = []
|
|
|
|
instance = Instance.new
|
|
|
|
instance.run_initializers(:assets)
|
|
|
|
assert_equal [1, 3], $arr
|
|
|
|
end
|
2009-10-27 19:48:35 -04:00
|
|
|
end
|
2009-11-02 20:19:03 -05:00
|
|
|
|
|
|
|
class WithArgsTest < ActiveSupport::TestCase
|
|
|
|
test "running initializers with args" do
|
|
|
|
$with_arg = nil
|
2011-10-02 16:02:12 -04:00
|
|
|
WithArgs.new.run_initializers(:default, 'foo')
|
2009-11-02 20:19:03 -05:00
|
|
|
assert_equal 'foo', $with_arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class OverriddenInitializerTest < ActiveSupport::TestCase
|
|
|
|
test "merges in the initializers from the parent in the right order" do
|
|
|
|
$arr = []
|
|
|
|
OverriddenInitializer.new.run_initializers
|
2009-11-06 20:21:39 -05:00
|
|
|
assert_equal [1, 2, 3, 4], $arr
|
2009-11-02 20:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
2013-05-01 22:17:44 -04:00
|
|
|
end
|