aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/initializer/install_gem_spec_stubs_test.rb
blob: 2e94c9968fdc2b4791c7d7ded85d005094f192ea (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
require "initializer/test_helper"

module InitializerTests
  class GemSpecStubsTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      $stderr = StringIO.new
    end

    test "user has an old boot.rb (defined by having no Rails.vendor_rails?)" do
      class << Rails
        undef vendor_rails?
      end

      assert_stderr(/outdated/) do
        assert_raises(SystemExit) do
          Rails::Initializer.run { |c| c.frameworks = [] }
        end
      end
    end

    test "requires rubygems" do
      Kernel.module_eval do
        alias old_require require
        def require(name)
          $rubygems_required = true if name == "rubygems"
          old_require(name)
        end
      end

      Rails.vendor_rails = true
      Rails::Initializer.run { |c| c.frameworks = [] }
      assert $rubygems_required
    end

    test "does not fail if rubygems does not exist" do
      Kernel.module_eval do
        alias old_require require
        def require(name)
          raise LoadError if name == "rubygems"
          old_require(name)
        end
      end

      assert_nothing_raised do
        Rails::Initializer.run { |c| c.frameworks = [] }
      end
    end

    test "adds fake Rubygems stubs if a framework is not loaded in Rubygems and we've vendored" do
      Rails.vendor_rails = true

      Rails::Initializer.run { |c| c.frameworks = [] }

      %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub|
        gem_spec = Gem.loaded_specs[stub]
        assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
        assert_equal stub, gem_spec.name
        assert_equal "", gem_spec.loaded_from
      end
    end

    test "doesn't replace gem specs that are already loaded" do
      Rails.vendor_rails = true

      Gem.loaded_specs["rails"] = Gem::Specification.new do |s|
        s.name = "rails"
        s.version = Rails::VERSION::STRING
        s.loaded_from = "/foo/bar/baz"
      end

      Rails::Initializer.run { |c| c.frameworks = [] }

      assert_equal "/foo/bar/baz", Gem.loaded_specs["rails"].loaded_from

      %w(activesupport activerecord actionpack actionmailer activeresource).each do |stub|
        gem_spec = Gem.loaded_specs[stub]
        assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
        assert_equal stub, gem_spec.name
        assert_equal "", gem_spec.loaded_from
      end
    end
  end
end