Permalink
Browse files

Added rubygems magic so that everything happy when you require rubyge…

…ms in an app with bundled gems
  • Loading branch information...
1 parent 5928cfa commit 6d2fc2a07416889bf4fa8806fb7adcb745c906d1 Yehuda Katz + Carl Lerche committed Jul 21, 2009
Showing with 69 additions and 3 deletions.
  1. +30 −0 lib/bundler/manifest.rb
  2. +29 −0 spec/bundler/manifest_spec.rb
  3. +10 −3 spec/matchers.rb
View
30 lib/bundler/manifest.rb
@@ -29,6 +29,7 @@ def install(options = {})
installer = Installer.new(@path)
installer.install # options come here
create_load_paths_files(File.join(@path, "environments"))
+ create_fake_rubygems(File.join(@path, "environments"))
end
def activate(environment = "default")
@@ -79,13 +80,42 @@ def create_load_paths_files(path)
environments.each do |environment|
gem_specs = gems_for(environment)
File.open(File.join(path, "#{environment}.rb"), "w") do |file|
+ file.puts <<-RUBY_EVAL
+ module Bundler
+ def self.rubygems_required
+ #{create_gem_stubs(path, gem_specs)}
+ end
+ end
+ RUBY_EVAL
+ file.puts "$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))"
load_paths_for_specs(gem_specs).each do |load_path|
file.puts "$LOAD_PATH.unshift #{load_path.inspect}"
end
end
end
end
+ def create_gem_stubs(path, gem_specs)
+ gem_specs.map do |spec|
+ path = File.expand_path(File.join(path, '..', 'specifications', "#{spec.full_name}.gemspec"))
+ %{
+ Gem.loaded_specs["#{spec.name}"] = eval(File.read("#{path}"))
+ }
+ end.join("\n")
+ end
+
+ def create_fake_rubygems(path)
+ File.open(File.join(path, "rubygems.rb"), "w") do |file|
+ file.puts <<-RUBY_EVAL
+ $:.delete File.expand_path(File.dirname(__FILE__))
+ load "rubygems.rb"
+ if defined?(Bundler) && Bundler.respond_to?(:rubygems_required)
+ Bundler.rubygems_required
+ end
+ RUBY_EVAL
+ end
+ end
+
def load_paths_for_specs(specs)
load_paths = []
specs.each do |spec|
View
29 spec/bundler/manifest_spec.rb
@@ -143,5 +143,34 @@ def dep(name, version, options = {})
File.exist?(tmp_file('environments', "production.rb")).should be_false
end
+
+ it "adds the environments path to the load paths" do
+ tmp_file('environments', 'testing.rb').should have_load_paths(tmp_dir, [
+ "environments"
+ ])
+ end
+
+ it "creates a rubygems.rb file in the environments directory" do
+ File.exist?(tmp_file('environments', 'rubygems.rb')).should be_true
+ end
+
+ it "requires the Rubygems library" do
+ env = tmp_file('environments', 'default.rb')
+ out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts Gem'`.strip
+ out.should == 'Gem'
+ end
+
+ it "removes the environments path from the load paths after rubygems is required" do
+ env = tmp_file('environments', 'default.rb')
+ out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts $:'`
+ out.should_not include(tmp_file('environments'))
+ end
+
+ it "Gem.loaded_specs has the gems that are included" do
+ env = tmp_file('environments', 'default.rb')
+ out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts Gem.loaded_specs.map{|k,v|"\#{k} - \#{v.version}"}'`
+ out = out.split("\n")
+ out.should include("rack - 1.0.0")
+ end
end
end
View
13 spec/matchers.rb
@@ -30,12 +30,19 @@ def only_have_specs(*names)
def have_load_paths(root, gem_load_paths)
flattened_paths = []
- gem_load_paths.each do |gem_name, paths|
- paths.each { |path| flattened_paths << File.join(root, "gems", gem_name, path) }
+
+ if gem_load_paths.is_a?(Hash)
+ gem_load_paths.each do |gem_name, paths|
+ paths.each { |path| flattened_paths << File.join(root, "gems", gem_name, path) }
+ end
+ else
+ gem_load_paths.each do |path|
+ flattened_paths << File.join(root, path)
+ end
end
simple_matcher("have load paths") do |given, matcher|
- actual = `ruby -r#{given} -e 'puts $:'`.split("\n")
+ actual = `#{Gem.ruby} -r#{given} -e 'puts $:'`.split("\n")
flattened_paths.all? do |path|
matcher.failure_message = "expected environment load paths to contain '#{path}', but it was:\n #{actual.join("\n ")}"

0 comments on commit 6d2fc2a

Please sign in to comment.