2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "tmpdir"
|
|
|
|
require "abstract_unit"
|
|
|
|
require "rails/app_loader"
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
class AppLoaderTest < ActiveSupport::TestCase
|
2014-07-07 21:05:30 -04:00
|
|
|
def loader
|
|
|
|
@loader ||= Class.new do
|
2015-04-22 23:05:30 -04:00
|
|
|
extend Rails::AppLoader
|
2014-07-07 21:05:30 -04:00
|
|
|
|
2018-03-29 22:29:55 -04:00
|
|
|
class << self
|
|
|
|
attr_accessor :exec_arguments
|
2014-07-07 21:05:30 -04:00
|
|
|
|
2018-03-29 22:29:55 -04:00
|
|
|
def exec(*args)
|
|
|
|
self.exec_arguments = args
|
|
|
|
end
|
2014-07-07 21:05:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def write(filename, contents = nil)
|
2013-04-11 20:31:23 -04:00
|
|
|
FileUtils.mkdir_p(File.dirname(filename))
|
2013-04-11 07:18:16 -04:00
|
|
|
File.write(filename, contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expects_exec(exe)
|
2015-04-22 23:05:30 -04:00
|
|
|
assert_equal [Rails::AppLoader::RUBY, exe], loader.exec_arguments
|
2013-04-11 07:18:16 -04:00
|
|
|
end
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2013-02-22 14:09:43 -05:00
|
|
|
setup do
|
2016-08-06 13:16:09 -04:00
|
|
|
@tmp = Dir.mktmpdir("railties-rails-loader-test-suite")
|
2013-04-11 07:18:16 -04:00
|
|
|
@cwd = Dir.pwd
|
|
|
|
Dir.chdir(@tmp)
|
2013-01-06 18:13:47 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
["bin", "script"].each do |script_dir|
|
2013-04-11 07:18:16 -04:00
|
|
|
exe = "#{script_dir}/rails"
|
|
|
|
|
|
|
|
test "is not in a Rails application if #{exe} is not found in the current or parent directories" do
|
2014-07-07 21:05:30 -04:00
|
|
|
def loader.find_executables; end
|
2014-01-24 14:10:46 -05:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not loader.exec_app
|
2014-01-24 14:10:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "is not in a Rails application if #{exe} exists but is a folder" do
|
|
|
|
FileUtils.mkdir_p(exe)
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not loader.exec_app
|
2013-02-22 14:09:43 -05:00
|
|
|
end
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
["APP_PATH", "ENGINE_PATH"].each do |keyword|
|
2013-04-11 07:18:16 -04:00
|
|
|
test "is in a Rails application if #{exe} exists and contains #{keyword}" do
|
|
|
|
write exe, keyword
|
2013-02-22 14:09:43 -05:00
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
loader.exec_app
|
2014-07-07 21:05:30 -04:00
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
expects_exec exe
|
|
|
|
end
|
2013-02-22 14:09:43 -05:00
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
test "is not in a Rails application if #{exe} exists but doesn't contain #{keyword}" do
|
|
|
|
write exe
|
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not loader.exec_app
|
2013-04-11 07:18:16 -04:00
|
|
|
end
|
|
|
|
|
2013-04-11 07:28:57 -04:00
|
|
|
test "is in a Rails application if parent directory has #{exe} containing #{keyword} and chdirs to the root directory" do
|
2013-04-11 07:18:16 -04:00
|
|
|
write "foo/bar/#{exe}"
|
|
|
|
write "foo/#{exe}", keyword
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
Dir.chdir("foo/bar")
|
2013-04-11 07:18:16 -04:00
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
loader.exec_app
|
2014-07-07 21:05:30 -04:00
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
expects_exec exe
|
2013-04-11 09:18:34 -04:00
|
|
|
|
|
|
|
# Compare the realpath in case either of them has symlinks.
|
|
|
|
#
|
2018-06-23 02:46:40 -04:00
|
|
|
# This happens in particular in macOS, where @tmp starts
|
2013-04-11 09:18:34 -04:00
|
|
|
# with "/var", and Dir.pwd with "/private/var", due to a
|
|
|
|
# default system symlink var -> private/var.
|
2013-04-11 20:31:23 -04:00
|
|
|
assert_equal File.realpath("#@tmp/foo"), File.realpath(Dir.pwd)
|
2013-04-11 07:18:16 -04:00
|
|
|
end
|
2013-02-22 14:09:43 -05:00
|
|
|
end
|
2013-01-06 18:13:47 -05:00
|
|
|
end
|
2013-04-11 07:18:16 -04:00
|
|
|
|
|
|
|
teardown do
|
|
|
|
Dir.chdir(@cwd)
|
|
|
|
FileUtils.rm_rf(@tmp)
|
|
|
|
end
|
2013-01-06 18:13:47 -05:00
|
|
|
end
|