3 require 'rubygems/remote_fetcher'
6 # A fake Gem::RemoteFetcher for use in tests or to avoid real live HTTP
7 # requests when testing code that uses RubyGems.
11 # @fetcher = Gem::FakeFetcher.new
12 # @fetcher.data['http://gems.example.com/yaml'] = source_index.to_yaml
13 # Gem::RemoteFetcher.fetcher = @fetcher
15 # # invoke RubyGems code
17 # paths = @fetcher.paths
18 # assert_equal 'http://gems.example.com/yaml', paths.shift
19 # assert paths.empty?, paths.join(', ')
21 # See RubyGems' tests for more examples of FakeFetcher.
23 class Gem::FakeFetcher
36 raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
40 raise Gem::RemoteFetcher::FetchError.new('no data', path)
43 data.respond_to?(:call) ? data.call : data
49 raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
53 raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", nil)
56 data.respond_to?(:call) ? data.call : data.length
59 def download spec, source_uri, install_dir = Gem.dir
60 name = "#{spec.full_name}.gem"
61 path = File.join(install_dir, 'cache', name)
63 Gem.ensure_gem_subdirectories install_dir
65 if source_uri =~ /^http/ then
66 File.open(path, "wb") do |f|
67 f.write fetch_path(File.join(source_uri, "gems", name))
70 FileUtils.cp source_uri, path
79 class Gem::RemoteFetcher
81 def self.fetcher=(fetcher)
89 # A StringIO duck-typed class that uses Tempfile instead of String as the
92 # This class was added to flush out problems in Rubinius' IO implementation.
98 def initialize(string = '')
99 @tempfile = Tempfile.new "TempIO-#{@@count += 1}"
101 @tempfile.write string
105 def method_missing(meth, *args, &block)
106 @tempfile.send(meth, *args, &block)
109 def respond_to?(meth)
110 @tempfile.respond_to? meth
116 Gem.read_binary @tempfile.path