From 1a75c517c8e1c0157b7696d0b7ae6f0f6a8a0fc1 Mon Sep 17 00:00:00 2001 From: mkristian Date: Mon, 12 Jul 2010 12:00:25 +0530 Subject: [PATCH] extended boot.rb to allow to customize the Gemfile name via an environment variable --- .../generators/rails/app/templates/config/boot.rb | 5 +- .../test/application/initializers/bundler_test.rb | 79 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 railties/test/application/initializers/bundler_test.rb diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb index ab6cb37..3761e62 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -3,11 +3,12 @@ require 'rubygems' # Set up gems listed in the Gemfile. gemfile = File.expand_path('../../Gemfile', __FILE__) begin - ENV['BUNDLE_GEMFILE'] = gemfile + # give preference to value from outside + ENV['BUNDLE_GEMFILE'] ||= gemfile require 'bundler' Bundler.setup rescue Bundler::GemNotFound => e STDERR.puts e.message STDERR.puts "Try running `bundle install`." exit! -end if File.exist?(gemfile) +end if File.exist?(gemfile) || ENV['BUNDLE_GEMFILE'] diff --git a/railties/test/application/initializers/bundler_test.rb b/railties/test/application/initializers/bundler_test.rb new file mode 100644 index 0000000..f3e996b --- /dev/null +++ b/railties/test/application/initializers/bundler_test.rb @@ -0,0 +1,79 @@ +require "fileutils" +require 'test/unit' + +module ApplicationTests + class BundlerGemBooting < Test::Unit::TestCase + + def setup + @base_path = "tmp/app" + FileUtils.mkdir_p(@base_path) + @config_path = File.join(@base_path, 'config') + FileUtils.mkdir_p(@config_path) + FileUtils.cp("lib/rails/generators/rails/app/templates/config/boot.rb", @config_path) + end + + def gemfile(file, gem = nil) + if file + gemfile = File.join(@base_path, file) + File.open(gemfile, 'w') do |f| + f.puts "source 'http://rubygems.org'" + f.puts "gem '#{gem}', :require => '#{gem}'" + end + File.expand_path(gemfile) + else + Dir.glob(@base_path + "/Gemfile*").each do |f| + FileUtils.rm(f) + end + end + end + + def mimic_boot_loader + FileUtils.cd(@config_path) do + load("boot.rb") + end + end + + def test_bundler_setup + # delete all gemfiles + gemfile(nil) + + mimic_boot_loader + + # verify there is no bundler + assert false == Object.const_defined?('Bundler') + + # have a custom Gemfile + gem_file = gemfile('Gemfile.custom', 'builder') + + mimic_boot_loader + + # still there is no Bundler + assert false == Object.const_defined?('Bundler') + + # have a regular Gemfile + gemfile("Gemfile", "rdoc") + # and set the custom gemfile to be used + ENV['BUNDLE_GEMFILE'] = gem_file + + mimic_boot_loader + + # we have bundler now + assert Object.const_defined?('Bundler') + + # the custom gemfile was used + Bundler.require + assert Object.const_defined?('Builder') + # the regular gemfile was not used + assert false == Object.const_defined?('RDoc') + + # do not know how to "reuse" bundler with a new gemfile + + # ENV['BUNDLE_GEMFILE'] = nil + # FileUtils.cd(@config_path) do + # load("boot.rb") + # end + # Bundler.require + # assert true == Object.const_defined?('RDoc') + end + end +end -- 1.7.0.4