PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/rubygems-0.8.11/test/test_remote_installer.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 178 lines | 135 code | 32 blank | 11 comment | 1 complexity | 6a817c2ea111a91a2956ace004128465 MD5 | raw file
  1. #---
  2. # Excerpted from "Everyday Scripting in Ruby"
  3. # We make no guarantees that this code is fit for any purpose.
  4. # Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
  5. #---
  6. require 'test/unit'
  7. require 'rubygems'
  8. Gem::manage_gems
  9. require 'net/http'
  10. require 'yaml'
  11. require 'test/onegem'
  12. class MockFetcher
  13. def initialize(uri, proxy)
  14. @uri = uri
  15. end
  16. def size
  17. 1000
  18. end
  19. def source_index
  20. if @uri =~ /non.existent.url/
  21. fail Gem::RemoteSourceException,
  22. "Error fetching remote gem cache: Mock Socket Exception"
  23. end
  24. result = {
  25. 'foo-1.2.3' => Gem::Specification.new do |s|
  26. s.name = 'foo'
  27. s.version = "1.2.3"
  28. s.summary = "This is a cool package"
  29. end,
  30. 'foo-tools-2.0.0' => Gem::Specification.new do |s|
  31. s.name = 'foo-tools'
  32. s.version = "2.0.0"
  33. s.summary = "This is an even cooler package"
  34. end,
  35. 'foo-2-2.0.0' => Gem::Specification.new do |s|
  36. s.name = 'foo-2'
  37. s.version = "2.0.0"
  38. s.summary = "This is the coolest package evar!~!"
  39. end,
  40. }
  41. result
  42. end
  43. def fetch_path(path)
  44. end
  45. def self.finish
  46. end
  47. end
  48. class TestRemoteInstaller < Test::Unit::TestCase
  49. PROPER_SOURCES = %w( http://gems.rubyforge.org )
  50. def setup
  51. Gem.clear_paths
  52. @installer = Gem::RemoteInstaller.new
  53. @installer.instance_variable_set("@fetcher_class", MockFetcher)
  54. end
  55. def test_create
  56. assert_not_nil(@installer)
  57. end
  58. # Make sure that the installer knows the proper list of places to go
  59. # to get packages.
  60. def test_source_list
  61. assert_equal PROPER_SOURCES, @installer.sources
  62. end
  63. def test_source_index_hash
  64. source_hash = @installer.source_index_hash
  65. assert source_hash.has_key?("http://gems.rubyforge.org")
  66. assert_equal 1, source_hash.size
  67. gem_hash = source_hash['http://gems.rubyforge.org']
  68. spec = gem_hash['foo-1.2.3']
  69. assert_equal 'foo', spec.name
  70. assert_equal '1.2.3', spec.version.to_s
  71. end
  72. def test_missing_source_exception
  73. @installer.instance_variable_set("@sources", ["http://non.existent.url"])
  74. assert_raise(Gem::RemoteSourceException) {
  75. info = @installer.source_index_hash
  76. }
  77. end
  78. def test_find_gem_to_install
  79. version = Gem::Version::Requirement.new "> 0.0.0"
  80. gems = @installer.find_gem_to_install("foo", version,
  81. @installer.source_index_hash)
  82. assert_equal "foo-1.2.3", gems.first.full_name
  83. end
  84. end
  85. # This test suite has a number of TODOs in the test cases. The
  86. # TestRemoteInstaller test suite is a reworking of this class from
  87. # scratch.
  88. class RemoteInstallerTest < Test::Unit::TestCase
  89. class RInst < Gem::RemoteInstaller
  90. include Test::Unit::Assertions
  91. attr_accessor :expected_destination_files
  92. attr_accessor :expected_bodies
  93. attr_accessor :caches
  94. attr_accessor :responses
  95. def source_index_hash
  96. @caches
  97. end
  98. def fetch(uri)
  99. @reponses ||= {}
  100. @responses[uri]
  101. end
  102. def write_gem_to_file(body, destination_file)
  103. expected_destination_file = expected_destination_files.pop
  104. expected_body = expected_bodies.pop
  105. assert_equal expected_body, body, "Unexpected body"
  106. assert_equal expected_destination_file, destination_file, "Unexpected destination file"
  107. end
  108. def new_installer(gem)
  109. return MockInstaller.new(gem)
  110. end
  111. end
  112. CACHE_SOURCES = ["http://gems.rubyforge.org"]
  113. def setup
  114. Gem.clear_paths
  115. @remote_installer = Gem::RemoteInstaller.new
  116. @remote_installer.instance_eval { @fetcher_class = MockFetcher }
  117. end
  118. def test_sources
  119. assert_equal CACHE_SOURCES, @remote_installer.sources
  120. end
  121. def test_source_index_hash
  122. source_index_hash = @remote_installer.source_index_hash
  123. assert_equal 1, source_index_hash.keys.size
  124. end
  125. def test_find_latest_valid_package_in_caches(cache)
  126. end
  127. def test_download_file
  128. end
  129. SAMPLE_SPEC = Gem::Specification.new do |s|
  130. s.name = 'foo'
  131. s.version = "1.2.3"
  132. s.platform = Gem::Platform::RUBY
  133. s.summary = "This is a cool package"
  134. s.files = []
  135. end
  136. SAMPLE_CACHE = { 'foo-1.2.3' => SAMPLE_SPEC }
  137. SAMPLE_CACHE_YAML = SAMPLE_CACHE.to_yaml
  138. FOO_GEM = '' # TODO
  139. CACHE_DIR = File.join(Gem.dir, 'cache')
  140. # TODO: Disable this test for now. We will come back to revisit this.
  141. def disable_test_install
  142. Gem.use_paths("test/data/gemhome")
  143. result = @remote_installer.install('foo')
  144. assert_equal [nil], result
  145. end
  146. end