From 9a826150c30210b5759a9f2c9b07b6eaf7483b7b Mon Sep 17 00:00:00 2001 From: Andreas Fuchs Date: Sat, 7 Jun 2008 16:22:28 +0200 Subject: [PATCH] Add a gem's require_paths directories to the load path instead of guessing Make GemDependency read the gem's declared require_paths and add them on the load path. Adds a private method local_specification that returns a Gem specification for the frozen gem. (Maybe this method should be made public like specification was.) --- railties/lib/rails/gem_dependency.rb | 14 +++++++++++--- railties/test/gem_dependency_test.rb | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 9f088a1..e23ebf6 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -31,9 +31,8 @@ module Rails args << @requirement.to_s if @requirement gem *args else - $LOAD_PATH.unshift File.join(unpacked_paths.first, 'lib') - ext = File.join(unpacked_paths.first, 'ext') - $LOAD_PATH.unshift(ext) if File.exist?(ext) + spec = local_specification(unpacked_paths.first) + $LOAD_PATH.unshift(*spec.require_paths.map{|req| File.join(spec.installation_path, req)}) @frozen = true end @load_paths_added = true @@ -104,6 +103,15 @@ private ################################################################### @spec ||= Gem.source_index.search(Gem::Dependency.new(@name, @requirement)).sort_by { |s| s.version }.last end + def local_specification(path) + # That . component is relevant to rubygems: + # It expects to remove two dir components from the pathname in order to get to the source dir. + spec_path = File.join(path, '.', '.specification') + @local_spec ||= returning Gem::Specification.from_yaml(File.new(spec_path)) do |spec| + spec.loaded_from = spec_path + end + end + def gem_command RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem' end diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb index 3ae0189..f066c14 100644 --- a/railties/test/gem_dependency_test.rb +++ b/railties/test/gem_dependency_test.rb @@ -11,6 +11,7 @@ uses_mocha "Plugin Tests" do @gem_with_source = Rails::GemDependency.new "hpricot", :source => "http://code.whytheluckystiff.net" @gem_with_version = Rails::GemDependency.new "hpricot", :version => "= 0.6" @gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3" + @gem_with_bin = Rails::GemDependency.new "rmagick", :lib => 'RMagick' end def test_configuration_adds_gem_dependency @@ -62,5 +63,12 @@ uses_mocha "Plugin Tests" do @gem_with_lib.add_load_paths @gem_with_lib.load end + + def test_gem_with_bin_loading + @gem_with_bin.expects(:gem).with(@gem_with_bin.name) + @gem_with_bin.expects(:require).with(@gem_with_bin.lib) + @gem_with_bin.add_load_paths + @gem_with_bin.load + end end end \ No newline at end of file -- 1.5.5