Permalink
Browse files

Mov Gem::GemPathSearcher out of lib/rubygems/custom_require.rb and ad…

…d tests.

git-svn-id: svn+ssh://rubyforge.org/var/svn/rubygems/trunk@1257 3d4018f9-ac1a-0410-99e9-8a154d859a19
  • Loading branch information...
1 parent 27d2643 commit 00bd18bbe9274e58c22219fb14656f743b180579 @drbrain drbrain committed Jun 17, 2007
View
5 ChangeLog
@@ -1,3 +1,8 @@
+2007-06-16 Eric Hodel <drbrain@segment7.net>
+
+ * lib/rubygems/gem_path_searcher.rb (Gem::GemPathSearcher): Moved out
+ of lib/rubygems/custom_require.rb and added tests.
+
2007-06-02 Jim Weirich <jim@weirichhouse.org>
* lib/rubygems/source_info_cache.rb (Gem::try_file): Fix for issue
View
2 bin/gem
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
View
2 bin/gem_mirror
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
View
2 bin/gem_server
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
require 'rubygems/server'
View
2 bin/gemlock
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
# -*- ruby -*-
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
View
2 bin/gemri
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
# -*- ruby -*-
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
View
2 bin/gemwhich
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
View
2 bin/index_gem_repository.rb
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
View
2 bin/update_rubygems
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#! /usr/local/bin/ruby186
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
View
81 lib/rubygems/custom_require.rb
@@ -5,6 +5,7 @@
#++
require 'rubygems/source_index'
+require 'rubygems/gem_path_searcher'
module Kernel
alias gem_original_require require # :nodoc:
@@ -37,83 +38,3 @@ def require(path) # :nodoc:
end
end # module Kernel
-module Gem
-
- #
- # GemPathSearcher has the capability to find loadable files inside
- # gems. It generates data up front to speed up searches later.
- #
- class GemPathSearcher
-
- #
- # Initialise the data we need to make searches later.
- #
- def initialize
- # We want a record of all the installed gemspecs, in the order
- # we wish to examine them.
- @gemspecs = init_gemspecs
- # Map gem spec to glob of full require_path directories.
- # Preparing this information may speed up searches later.
- @lib_dirs = {}
- @gemspecs.each do |spec|
- @lib_dirs[spec.object_id] = lib_dirs(spec)
- end
- end
-
- #
- # Look in all the installed gems until a matching _path_ is found.
- # Return the _gemspec_ of the gem where it was found. If no match
- # is found, return nil.
- #
- # The gems are searched in alphabetical order, and in reverse
- # version order.
- #
- # For example:
- #
- # find('log4r') # -> (log4r-1.1 spec)
- # find('log4r.rb') # -> (log4r-1.1 spec)
- # find('rake/rdoctask') # -> (rake-0.4.12 spec)
- # find('foobarbaz') # -> nil
- #
- # Matching paths can have various suffixes ('.rb', '.so', and
- # others), which may or may not already be attached to _file_.
- # This method doesn't care about the full filename that matches;
- # only that there is a match.
- #
- def find(path)
- @gemspecs.each do |spec|
- return spec if matching_file(spec, path)
- end
- nil
- end
-
- private
-
- # Attempts to find a matching path using the require_paths of the
- # given _spec_.
- #
- # Some of the intermediate results are cached in @lib_dirs for
- # speed.
- def matching_file(spec, path) # :doc:
- glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}"
- return true unless Dir[glob].select { |f| File.file?(f.untaint) }.empty?
- end
-
- # Return a list of all installed gemspecs, sorted by alphabetical
- # order and in reverse version order.
- def init_gemspecs
- Gem.source_index.map { |_, spec| spec }.sort { |a,b|
- (a.name <=> b.name).nonzero? || (b.version <=> a.version)
- }
- end
-
- # Returns library directories glob for a gemspec. For example,
- # '/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'
- def lib_dirs(spec)
- "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}"
- end
-
- end # class Gem::GemPathLoader
-
-end # module Gem
-
View
84 lib/rubygems/gem_path_searcher.rb
@@ -0,0 +1,84 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+#
+# GemPathSearcher has the capability to find loadable files inside
+# gems. It generates data up front to speed up searches later.
+#
+class Gem::GemPathSearcher
+
+ #
+ # Initialise the data we need to make searches later.
+ #
+ def initialize
+ # We want a record of all the installed gemspecs, in the order
+ # we wish to examine them.
+ @gemspecs = init_gemspecs
+ # Map gem spec to glob of full require_path directories.
+ # Preparing this information may speed up searches later.
+ @lib_dirs = {}
+ @gemspecs.each do |spec|
+ @lib_dirs[spec.object_id] = lib_dirs_for(spec)
+ end
+ end
+
+ #
+ # Look in all the installed gems until a matching _path_ is found.
+ # Return the _gemspec_ of the gem where it was found. If no match
+ # is found, return nil.
+ #
+ # The gems are searched in alphabetical order, and in reverse
+ # version order.
+ #
+ # For example:
+ #
+ # find('log4r') # -> (log4r-1.1 spec)
+ # find('log4r.rb') # -> (log4r-1.1 spec)
+ # find('rake/rdoctask') # -> (rake-0.4.12 spec)
+ # find('foobarbaz') # -> nil
+ #
+ # Matching paths can have various suffixes ('.rb', '.so', and
+ # others), which may or may not already be attached to _file_.
+ # This method doesn't care about the full filename that matches;
+ # only that there is a match.
+ #
+ def find(path)
+ @gemspecs.each do |spec|
+ return spec if matching_file(spec, path)
+ end
+ nil
+ end
+
+ private
+
+ # Attempts to find a matching path using the require_paths of the
+ # given _spec_.
+ #
+ # Some of the intermediate results are cached in @lib_dirs for
+ # speed.
+ def matching_file(spec, path) # :doc:
+ glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}"
+ return true unless Dir[glob].select { |f| File.file?(f.untaint) }.empty?
+ end
+
+ # Return a list of all installed gemspecs, sorted by alphabetical
+ # order and in reverse version order.
+ def init_gemspecs
+ Gem.source_index.map { |_, spec| spec }.sort { |a,b|
+ (a.name <=> b.name).nonzero? || (b.version <=> a.version)
+ }
+ end
+
+ # Returns library directories glob for a gemspec. For example,
+ # '/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'
+ def lib_dirs_for(spec)
+ "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}"
+ end
+
+end
+
View
12 test/gemutilities.rb
@@ -14,6 +14,12 @@
require 'test/yaml_data'
require 'test/mockgemui'
+module Gem
+ def self.source_index=(si)
+ @@source_index = si
+ end
+end
+
module Utilities
def make_cache_area(path, *uris)
fn = File.join(path, 'source_cache')
@@ -101,6 +107,7 @@ def write_file(path)
File.open(path, "w") { |io|
yield(io)
}
+ path
end
def quick_gem(gemname, version='0.0.2')
@@ -117,10 +124,13 @@ def quick_gem(gemname, version='0.0.2')
yield(s) if block_given?
end
- write_file(File.join("specifications", spec.full_name + ".gemspec")) do |io|
+ path = File.join "specifications", "#{spec.full_name}.gemspec"
+ written_path = write_file path do |io|
io.write(spec.to_ruby)
end
+ spec.loaded_from = written_path
+
return spec
end
View
57 test/test_gem_gem_path_searcher.rb
@@ -0,0 +1,57 @@
+require 'test/unit'
+require 'test/gemutilities'
+require 'rubygems/gem_path_searcher'
+
+class Gem::GemPathSearcher
+ attr_accessor :gemspecs
+ attr_accessor :lib_dirs
+
+ public :init_gemspecs
+ public :matching_file
+ public :lib_dirs_for
+end
+
+class TestGemPathSearcher < RubyGemTestCase
+
+ def setup
+ super
+
+ @foo1 = quick_gem 'foo', '0.1' do |s|
+ s.require_paths << 'lib2'
+ s.files << 'lib/foo.rb'
+ end
+
+ path = File.join 'gems', @foo1.full_name, 'lib', 'foo.rb'
+ write_file(path) { |fp| fp.puts "# #{path}" }
+
+ @foo2 = quick_gem 'foo', '0.2'
+ @bar1 = quick_gem 'bar', '0.1'
+ @bar2 = quick_gem 'bar', '0.2'
+
+ Gem.source_index = util_setup_source_info_cache @foo1, @foo2, @bar1, @bar2
+
+ @gps = Gem::GemPathSearcher.new
+ end
+
+ def test_find
+ assert_equal @foo1, @gps.find('foo')
+ end
+
+ def test_init_gemspecs
+ assert_equal [@bar2, @bar1, @foo2, @foo1], @gps.init_gemspecs
+ end
+
+ def test_lib_dirs_for
+ lib_dirs = @gps.lib_dirs_for(@foo1)
+ expected = File.join @gemhome, 'gems', @foo1.full_name, '{lib,lib2}'
+
+ assert_equal expected, lib_dirs
+ end
+
+ def test_matching_file
+ assert !@gps.matching_file(@foo1, 'bar')
+ assert @gps.matching_file(@foo1, 'foo')
+ end
+
+end
+

0 comments on commit 00bd18b

Please sign in to comment.