diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 2 | ||||
-rw-r--r-- | railties/lib/rails/app_rails_loader.rb | 55 | ||||
-rw-r--r-- | railties/lib/rails/generators/app_base.rb | 26 | ||||
-rw-r--r-- | railties/test/app_rails_loader_test.rb | 80 | ||||
-rw-r--r-- | railties/test/application/initializers/load_path_test.rb | 1 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 17 |
6 files changed, 108 insertions, 73 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index e4a08f68c1..f67177a047 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -58,7 +58,7 @@ * Improve service pages with new layout (404, etc). - *Stanislav Sobolev* + *Stanislav Sobolev* ## Rails 4.0.0.beta1 (February 25, 2013) ## 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/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index b62d1fff14..4fd1f00294 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -116,7 +116,7 @@ module Rails def database_gemfile_entry options[:skip_active_record] ? "" : - <<-GEMFILE.gsub(/^ {12}/, '').strip + <<-GEMFILE.strip_heredoc.chomp # Use #{options[:database]} as the database for ActiveRecord gem '#{gem_for_database}' GEMFILE @@ -182,20 +182,20 @@ module Rails return if options[:skip_sprockets] gemfile = if options.dev? || options.edge? - <<-GEMFILE.gsub(/^ {12}/, '') + <<-GEMFILE.strip_heredoc # Use edge version of sprockets-rails gem 'sprockets-rails', github: 'rails/sprockets-rails' # Use SCSS for stylesheets - gem 'sass-rails', github: 'rails/sass-rails' + gem 'sass-rails', github: 'rails/sass-rails' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '~> 1.3' GEMFILE else - <<-GEMFILE.gsub(/^ {12}/, '') + <<-GEMFILE.strip_heredoc # Use SCSS for stylesheets - gem 'sass-rails', '~> 4.0.0.beta1' + gem 'sass-rails', '~> 4.0.0.beta1' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '~> 1.3' @@ -203,34 +203,32 @@ module Rails end if options[:skip_javascript] - gemfile += <<-GEMFILE.gsub(/^ {12}/, '') + gemfile += <<-GEMFILE #{coffee_gemfile_entry} #{javascript_runtime_gemfile_entry} GEMFILE end - gemfile.strip_heredoc.gsub(/^[ \t]*$/, '') + gemfile.gsub(/^[ \t]+/, '') end def coffee_gemfile_entry - gemfile = if options.dev? || options.edge? - <<-GEMFILE.gsub(/^ {12}/, '') + if options.dev? || options.edge? + <<-GEMFILE # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', github: 'rails/coffee-rails' GEMFILE else - <<-GEMFILE.gsub(/^ {12}/, '') + <<-GEMFILE # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0.beta1' GEMFILE end - - gemfile.strip_heredoc.gsub(/^[ \t]*$/, '') end def javascript_gemfile_entry unless options[:skip_javascript] - <<-GEMFILE.gsub(/^ {12}/, '').strip_heredoc + <<-GEMFILE.gsub(/^[ \t]+/, '') #{coffee_gemfile_entry} #{javascript_runtime_gemfile_entry} # Use #{options[:javascript]} as the JavaScript library @@ -248,7 +246,7 @@ module Rails else "# gem 'therubyracer', platforms: :ruby" end - <<-GEMFILE.gsub(/^ {10}/, '') + <<-GEMFILE # See https://github.com/sstephenson/execjs#readme for more supported runtimes #{runtime} GEMFILE diff --git a/railties/test/app_rails_loader_test.rb b/railties/test/app_rails_loader_test.rb index 0402989705..ceae78ae80 100644 --- a/railties/test/app_rails_loader_test.rb +++ b/railties/test/app_rails_loader_test.rb @@ -1,48 +1,68 @@ +require 'tmpdir' require 'abstract_unit' require 'rails/app_rails_loader' class AppRailsLoaderTest < ActiveSupport::TestCase + def write(filename, contents=nil) + FileUtils.mkdir_p(File.dirname(filename)) + 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 + + Dir.chdir('foo/bar') - 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 + expects_exec exe + Rails::AppRailsLoader.exec_app_rails + + # 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. + assert_equal File.realpath("#@tmp/foo"), File.realpath(Dir.pwd) + end end end + + teardown do + Dir.chdir(@cwd) + FileUtils.rm_rf(@tmp) + end end diff --git a/railties/test/application/initializers/load_path_test.rb b/railties/test/application/initializers/load_path_test.rb index 9b18c329ec..0c66213caa 100644 --- a/railties/test/application/initializers/load_path_test.rb +++ b/railties/test/application/initializers/load_path_test.rb @@ -72,6 +72,7 @@ module ApplicationTests end test "load environment with global" do + $initialize_test_set_from_env = nil app_file "config/environments/development.rb", <<-RUBY $initialize_test_set_from_env = 'success' AppTemplate::Application.configure do diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index eb590da678..fa3ab969ae 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -79,6 +79,23 @@ module ApplicationTests assert_match "Hello world", output end + def test_should_not_eager_load_model_path_for_rake + add_to_config <<-RUBY + config.eager_load = true + + rake_tasks do + task do_nothing: :environment do + end + end + RUBY + + app_file "app/models/hello.rb", <<-RUBY + raise 'should not be pre-required for rake even `eager_load=true`' + RUBY + + Dir.chdir(app_path){ `rake do_nothing` } + end + def test_code_statistics_sanity assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0", Dir.chdir(app_path){ `rake stats` } |