Class Gem::FakeFetcher
In: lib/rubygems/test_utilities.rb
Parent: Object

A fake Gem::RemoteFetcher for use in tests or to avoid real live HTTP requests when testing code that uses RubyGems.

Example:

  @fetcher = Gem::FakeFetcher.new
  @fetcher.data['http://gems.example.com/yaml'] = source_index.to_yaml
  Gem::RemoteFetcher.fetcher = @fetcher

  # invoke RubyGems code

  paths = @fetcher.paths
  assert_equal 'http://gems.example.com/yaml', paths.shift
  assert paths.empty?, paths.join(', ')

See RubyGems’ tests for more examples of FakeFetcher.

Methods

download   fetch_path   fetch_size   new  

Attributes

data  [R] 
paths  [RW] 

Public Class methods

[Source]

    # File lib/rubygems/test_utilities.rb, line 28
28:   def initialize
29:     @data = {}
30:     @paths = []
31:   end

Public Instance methods

[Source]

    # File lib/rubygems/test_utilities.rb, line 59
59:   def download spec, source_uri, install_dir = Gem.dir
60:     name = "#{spec.full_name}.gem"
61:     path = File.join(install_dir, 'cache', name)
62: 
63:     Gem.ensure_gem_subdirectories install_dir
64: 
65:     if source_uri =~ /^http/ then
66:       File.open(path, "wb") do |f|
67:         f.write fetch_path(File.join(source_uri, "gems", name))
68:       end
69:     else
70:       FileUtils.cp source_uri, path
71:     end
72: 
73:     path
74:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 33
33:   def fetch_path(path)
34:     path = path.to_s
35:     @paths << path
36:     raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
37:     data = @data[path]
38: 
39:     if data.nil? then
40:       raise Gem::RemoteFetcher::FetchError.new('no data', path)
41:     end
42: 
43:     data.respond_to?(:call) ? data.call : data
44:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 46
46:   def fetch_size(path)
47:     path = path.to_s
48:     @paths << path
49:     raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
50:     data = @data[path]
51: 
52:     if data.nil? then
53:       raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", nil)
54:     end
55: 
56:     data.respond_to?(:call) ? data.call : data.length
57:   end

[Validate]