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 70
70:   def download spec, source_uri, install_dir = Gem.dir
71:     name = "#{spec.full_name}.gem"
72:     path = File.join(install_dir, 'cache', name)
73: 
74:     Gem.ensure_gem_subdirectories install_dir
75: 
76:     if source_uri =~ /^http/ then
77:       File.open(path, "wb") do |f|
78:         f.write fetch_path(File.join(source_uri, "gems", name))
79:       end
80:     else
81:       FileUtils.cp source_uri, path
82:     end
83: 
84:     path
85:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 33
33:   def fetch_path path, mtime = nil
34:     path = path.to_s
35:     @paths << path
36:     raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
37: 
38:     unless @data.key? path then
39:       raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
40:     end
41: 
42:     data = @data[path]
43: 
44:     if data.respond_to?(:call) then
45:       data.call
46:     else
47:       if path.to_s =~ /gz$/ and not data.nil? and not data.empty? then
48:         data = Gem.gunzip data
49:       end
50: 
51:       data
52:     end
53:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 55
55:   def fetch_size(path)
56:     path = path.to_s
57:     @paths << path
58: 
59:     raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
60: 
61:     unless @data.key? path then
62:       raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
63:     end
64: 
65:     data = @data[path]
66: 
67:     data.respond_to?(:call) ? data.call : data.length
68:   end

[Validate]