aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/app_loader_test.rb
blob: 0deb1a76df114d5af4adc1b3477ebde16fdb7d51 (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
# frozen_string_literal: true

require "tmpdir"
require "abstract_unit"
require "rails/app_loader"

class AppLoaderTest < ActiveSupport::TestCase
  def loader
    @loader ||= Class.new do
      extend Rails::AppLoader

      class << self
        attr_accessor :exec_arguments

        def exec(*args)
          self.exec_arguments = args
        end
      end
    end
  end

  def write(filename, contents = nil)
    FileUtils.mkdir_p(File.dirname(filename))
    File.write(filename, contents)
  end

  def expects_exec(exe)
    assert_equal [Rails::AppLoader::RUBY, exe], loader.exec_arguments
  end

  setup do
    @tmp = Dir.mktmpdir("railties-rails-loader-test-suite")
    @cwd = Dir.pwd
    Dir.chdir(@tmp)
  end

  ["bin", "script"].each do |script_dir|
    exe = "#{script_dir}/rails"

    test "is not in a Rails application if #{exe} is not found in the current or parent directories" do
      def loader.find_executables; end

      assert_not loader.exec_app
    end

    test "is not in a Rails application if #{exe} exists but is a folder" do
      FileUtils.mkdir_p(exe)

      assert_not loader.exec_app
    end

    ["APP_PATH", "ENGINE_PATH"].each do |keyword|
      test "is in a Rails application if #{exe} exists and contains #{keyword}" do
        write exe, keyword

        loader.exec_app

        expects_exec exe
      end

      test "is not in a Rails application if #{exe} exists but doesn't contain #{keyword}" do
        write exe

        assert_not loader.exec_app
      end

      test "is in a Rails application if parent directory has #{exe} containing #{keyword} and chdirs to the root directory" do
        write "foo/bar/#{exe}"
        write "foo/#{exe}", keyword

        Dir.chdir("foo/bar")

        loader.exec_app

        expects_exec exe

        # Compare the realpath in case either of them has symlinks.
        #
        # This happens in particular in macOS, where @tmp starts
        # with "/var", and Dir.pwd with "/private/var", due to a
        # default system symlink var -> private/var.
        assert_equal File.realpath("#@tmp/foo"), File.realpath(Dir.pwd)
      end
    end
  end

  teardown do
    Dir.chdir(@cwd)
    FileUtils.rm_rf(@tmp)
  end
end