aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--railties/lib/rails/app_rails_loader.rb55
-rw-r--r--railties/test/app_rails_loader_test.rb74
2 files changed, 71 insertions, 58 deletions
diff --git a/railties/lib/rails/app_rails_loader.rb b/railties/lib/rails/app_rails_loader.rb
index aae628290d..4a17803f1c 100644
--- a/railties/lib/rails/app_rails_loader.rb
+++ b/railties/lib/rails/app_rails_loader.rb
@@ -4,28 +4,7 @@ module Rails
module AppRailsLoader
RUBY = File.join(*RbConfig::CONFIG.values_at("bindir", "ruby_install_name")) + RbConfig::CONFIG["EXEEXT"]
EXECUTABLES = ['bin/rails', 'script/rails']
-
- def self.exec_app_rails
- original_cwd = Dir.pwd
-
- until exe = find_executable
- # If we exhaust the search there is no executable, this could be a
- # call to generate a new application, so restore the original cwd.
- Dir.chdir(original_cwd) and return if Pathname.new(Dir.pwd).root?
-
- # Otherwise keep moving upwards in search of a executable.
- Dir.chdir('..')
- end
-
- contents = File.read(exe)
-
- # This is the Rails executable, let's use it
- if contents =~ /(APP|ENGINE)_PATH/
- exec RUBY, exe, *ARGV
-
- # This is a Bundler binstub. Stop and explain how to upgrade.
- elsif exe =~ /bin\/rails$/ && contents =~ /This file was generated by Bundler/
- $stderr.puts <<-end_bin_upgrade_warning
+ BUNDLER_WARNING = <<EOS
Looks like your app's ./bin/rails is a stub that was generated by Bundler.
In Rails 4, your app's bin/ directory contains executables that are versioned
@@ -45,14 +24,34 @@ generate it and add it to source control:
bundle binstubs some-gem-name
git add bin/new-executable
- end_bin_upgrade_warning
+EOS
+
+ def self.exec_app_rails
+ original_cwd = Dir.pwd
+
+ loop do
+ if exe = find_executable
+ contents = File.read(exe)
+
+ if contents =~ /(APP|ENGINE)_PATH/
+ exec RUBY, exe, *ARGV
+ break # non reachable, hack to be able to stub exec in the test suite
+ elsif exe.end_with?('bin/rails') && contents.include?('This file was generated by Bundler')
+ $stderr.puts(BUNDLER_WARNING)
+ Object.const_set(:APP_PATH, File.expand_path('config/application', Dir.pwd))
+ require File.expand_path('../boot', APP_PATH)
+ require 'rails/commands'
+ break
+ end
+ end
- Object.const_set(:APP_PATH, File.expand_path('config/application', Dir.pwd))
- require File.expand_path('../boot', APP_PATH)
- require 'rails/commands'
+ # If we exhaust the search there is no executable, this could be a
+ # call to generate a new application, so restore the original cwd.
+ Dir.chdir(original_cwd) and return if Pathname.new(Dir.pwd).root?
+
+ # Otherwise keep moving upwards in search of a executable.
+ Dir.chdir('..')
end
- rescue SystemCallError
- # could not chdir, no problem just return
end
def self.find_executable
diff --git a/railties/test/app_rails_loader_test.rb b/railties/test/app_rails_loader_test.rb
index 0402989705..f1f9119966 100644
--- a/railties/test/app_rails_loader_test.rb
+++ b/railties/test/app_rails_loader_test.rb
@@ -1,48 +1,62 @@
+require 'pathname'
require 'abstract_unit'
require 'rails/app_rails_loader'
class AppRailsLoaderTest < ActiveSupport::TestCase
+ def write(filename, contents=nil)
+ FileUtils.mkdir_p(Pathname.new(filename).dirname)
+ File.write(filename, contents)
+ end
+
+ def expects_exec(exe)
+ Rails::AppRailsLoader.expects(:exec).with(Rails::AppRailsLoader::RUBY, exe)
+ end
setup do
- File.stubs(:exists?).returns(false)
+ @tmp = Dir.mktmpdir('railties-rails-loader-test-suite')
+ @cwd = Dir.pwd
+ Dir.chdir(@tmp)
end
- ['bin/rails', 'script/rails'].each do |exe|
- test "is in a rails application if #{exe} exists and contains APP_PATH" do
- File.stubs(:exists?).with(exe).returns(true)
- File.stubs(:read).with(exe).returns('APP_PATH')
- assert Rails::AppRailsLoader.find_executable
- 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
+ File.stubs(:exists?).with('bin/rails').returns(false)
+ File.stubs(:exists?).with('script/rails').returns(false)
- test "is not in a rails application if #{exe} exists but doesn't contain APP_PATH" do
- File.stubs(:exists?).with(exe).returns(true)
- File.stubs(:read).with(exe).returns("railties #{exe}")
assert !Rails::AppRailsLoader.exec_app_rails
end
- test "is in a rails application if parent directory has #{exe} containing APP_PATH" do
- File.stubs(:exists?).with("/foo/bar/#{exe}").returns(false)
- File.stubs(:exists?).with("/foo/#{exe}").returns(true)
- File.stubs(:read).with("/foo/#{exe}").returns('APP_PATH')
- assert_equal Rails::AppRailsLoader.find_executable_in_parent_path(Pathname.new("/foo/bar")), "/foo/#{exe}"
- end
+ ['APP_PATH', 'ENGINE_PATH'].each do |keyword|
+ test "is in a Rails application if #{exe} exists and contains #{keyword}" do
+ write exe, keyword
- test "is not in a rails application if at the root directory and doesn't have #{exe}" do
- Pathname.any_instance.stubs(:root?).returns true
- assert !Rails::AppRailsLoader.find_executable
- end
+ expects_exec exe
+ Rails::AppRailsLoader.exec_app_rails
+ end
- test "is in a rails engine if parent directory has #{exe} containing ENGINE_PATH" do
- File.stubs(:exists?).with("/foo/bar/#{exe}").returns(false)
- File.stubs(:exists?).with("/foo/#{exe}").returns(true)
- File.stubs(:read).with("/foo/#{exe}").returns('ENGINE_PATH')
- assert Rails::AppRailsLoader.find_executable_in_parent_path(Pathname.new("/foo/bar")), "/foo/#{exe}"
- end
+ test "is not in a Rails application if #{exe} exists but doesn't contain #{keyword}" do
+ write exe
+
+ assert !Rails::AppRailsLoader.exec_app_rails
+ 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
- test "is in a rails engine if #{exe} exists containing ENGINE_PATH" do
- File.stubs(:exists?).with(exe).returns(true)
- File.stubs(:read).with(exe).returns('ENGINE_PATH')
- assert Rails::AppRailsLoader.find_executable
+ Dir.chdir('foo/bar')
+
+ expects_exec exe
+ Rails::AppRailsLoader.exec_app_rails
+ assert_equal File.join(@tmp, 'foo'), Dir.pwd
+ end
end
end
+
+ teardown do
+ Dir.chdir(@cwd)
+ FileUtils.rm_rf(@tmp)
+ end
end