2013-04-11 09:18:34 -04:00
|
|
|
require 'tmpdir'
|
2013-01-06 18:13:47 -05:00
|
|
|
require 'abstract_unit'
|
2015-05-27 20:48:01 -04:00
|
|
|
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
|
|
|
|
|
|
|
def self.exec_arguments
|
|
|
|
@exec_arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.exec(*args)
|
|
|
|
@exec_arguments = args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-11 07:18:16 -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
|
2013-04-11 07:18:16 -04:00
|
|
|
@tmp = Dir.mktmpdir('railties-rails-loader-test-suite')
|
|
|
|
@cwd = Dir.pwd
|
|
|
|
Dir.chdir(@tmp)
|
2013-01-06 18:13:47 -05:00
|
|
|
end
|
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
['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
|
2014-07-07 21:05:30 -04:00
|
|
|
def loader.find_executables; end
|
2014-01-24 14:10:46 -05:00
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
assert !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
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
assert !loader.exec_app
|
2013-02-22 14:09:43 -05:00
|
|
|
end
|
2013-01-06 18:13:47 -05:00
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
['APP_PATH', 'ENGINE_PATH'].each do |keyword|
|
|
|
|
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
|
|
|
|
|
2015-04-22 23:05:30 -04:00
|
|
|
assert !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
|
|
|
|
2013-04-11 07:18:16 -04:00
|
|
|
Dir.chdir('foo/bar')
|
|
|
|
|
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.
|
|
|
|
#
|
|
|
|
# This happens in particular in Mac OS X, where @tmp starts
|
|
|
|
# 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
|