Skip to content

Commit

Permalink
application loader refactor and test suite complete rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Apr 11, 2013
1 parent 2a07db7 commit 85de183
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 58 deletions.
55 changes: 27 additions & 28 deletions railties/lib/rails/app_rails_loader.rb
Expand Up @@ -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
Expand All @@ -45,14 +24,34 @@ def self.exec_app_rails
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
Expand Down
74 changes: 44 additions & 30 deletions 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

2 comments on commit 85de183

@rafaelfranca
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’šπŸ’›β€οΈπŸ’œπŸ’™

@jeremy
Copy link
Member

@jeremy jeremy commented on 85de183 Apr 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🀘

Please sign in to comment.