aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/initializers/load_path_test.rb
blob: cd05956356d56b5da5088ff4e533a6708afe1499 (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 LoadPathTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
      FileUtils.rm_rf "#{app_path}/config/environments"
    end

    def teardown
      teardown_app
    end

    test "initializing an application adds the application paths to the load path" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
      RUBY

      require "#{app_path}/config/environment"
      assert $:.include?("#{app_path}/app/models")
    end

    test "initializing an application allows to load code on lib path inside application class definition" do
      app_file "lib/foo.rb", <<-RUBY
        module Foo; end
      RUBY

      add_to_config <<-RUBY
        require "foo"
        raise "Expected Foo to be defined" unless defined?(Foo)
      RUBY

      assert_nothing_raised do
        require "#{app_path}/config/environment"
      end

      assert $:.include?("#{app_path}/lib")
    end

    test "initializing an application eager load any path under app" do
      app_file "app/anything/foo.rb", <<-RUBY
        module Foo; end
      RUBY

      add_to_config <<-RUBY
        config.root = "#{app_path}"
      RUBY

      require "#{app_path}/config/environment"
      assert Foo
    end

    test "eager loading loads parent classes before children" do
      app_file "lib/zoo.rb", <<-ZOO
        class Zoo ; include ReptileHouse ; end
      ZOO

      app_file "lib/zoo/reptile_house.rb", <<-ZOO
        module Zoo::ReptileHouse ; end
      ZOO

      add_to_config <<-RUBY
        config.root = "#{app_path}"
        config.eager_load_paths << "#{app_path}/lib"
      RUBY

      require "#{app_path}/config/environment"
      assert Zoo
    end

    test "eager loading accepts Pathnames" do
      app_file "lib/foo.rb", <<-RUBY
        module Foo; end
      RUBY

      add_to_config <<-RUBY
        config.eager_load = true
        config.eager_load_paths << Pathname.new("#{app_path}/lib")
      RUBY

      require "#{app_path}/config/environment"
      assert Foo
    end

    test "load environment with global" do
      $initialize_test_set_from_env = nil
      app_file "config/environments/development.rb", <<-RUBY
        $initialize_test_set_from_env = 'success'
        Rails.application.configure do
          config.cache_classes = true
          config.time_zone = "Brasilia"
        end
      RUBY

      assert_nil $initialize_test_set_from_env
      add_to_config <<-RUBY
        config.root = "#{app_path}"
          config.time_zone = "UTC"
      RUBY

      require "#{app_path}/config/environment"
      assert_equal "success", $initialize_test_set_from_env
      assert Rails.application.config.cache_classes
      assert_equal "Brasilia", Rails.application.config.time_zone
    end
  end
end