summaryrefslogtreecommitdiff
path: root/trunk/test/rubygems
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
commit0dc342de848a642ecce8db697b8fecd83a63e117 (patch)
tree2b7ed4724aff1f86073e4740134bda9c4aac1a39 /trunk/test/rubygems
parentef70cf7138ab8034b5b806f466e4b484b24f0f88 (diff)
added tag v1_9_0_4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_9_0_4@18845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'trunk/test/rubygems')
-rw-r--r--trunk/test/rubygems/bogussources.rb8
-rw-r--r--trunk/test/rubygems/data/gem-private_key.pem27
-rw-r--r--trunk/test/rubygems/data/gem-public_cert.pem20
-rw-r--r--trunk/test/rubygems/fake_certlib/openssl.rb7
-rw-r--r--trunk/test/rubygems/functional.rb95
-rw-r--r--trunk/test/rubygems/gem_installer_test_case.rb86
-rw-r--r--trunk/test/rubygems/gem_package_tar_test_case.rb146
-rw-r--r--trunk/test/rubygems/gemutilities.rb441
-rw-r--r--trunk/test/rubygems/insure_session.rb51
-rw-r--r--trunk/test/rubygems/mockgemui.rb43
-rw-r--r--trunk/test/rubygems/private_key.pem27
-rw-r--r--trunk/test/rubygems/public_cert.pem20
-rw-r--r--trunk/test/rubygems/simple_gem.rb72
-rw-r--r--trunk/test/rubygems/test_config.rb21
-rw-r--r--trunk/test/rubygems/test_gem.rb449
-rw-r--r--trunk/test/rubygems/test_gem_builder.rb34
-rw-r--r--trunk/test/rubygems/test_gem_command.rb196
-rw-r--r--trunk/test/rubygems/test_gem_command_manager.rb213
-rw-r--r--trunk/test/rubygems/test_gem_commands_build_command.rb75
-rw-r--r--trunk/test/rubygems/test_gem_commands_cert_command.rb126
-rw-r--r--trunk/test/rubygems/test_gem_commands_check_command.rb25
-rw-r--r--trunk/test/rubygems/test_gem_commands_contents_command.rb92
-rw-r--r--trunk/test/rubygems/test_gem_commands_dependency_command.rb227
-rw-r--r--trunk/test/rubygems/test_gem_commands_environment_command.rb134
-rw-r--r--trunk/test/rubygems/test_gem_commands_fetch_command.rb55
-rw-r--r--trunk/test/rubygems/test_gem_commands_generate_index_command.rb32
-rw-r--r--trunk/test/rubygems/test_gem_commands_install_command.rb166
-rw-r--r--trunk/test/rubygems/test_gem_commands_mirror_command.rb61
-rw-r--r--trunk/test/rubygems/test_gem_commands_outdated_command.rb43
-rw-r--r--trunk/test/rubygems/test_gem_commands_pristine_command.rb109
-rw-r--r--trunk/test/rubygems/test_gem_commands_query_command.rb288
-rw-r--r--trunk/test/rubygems/test_gem_commands_server_command.rb27
-rw-r--r--trunk/test/rubygems/test_gem_commands_sources_command.rb275
-rw-r--r--trunk/test/rubygems/test_gem_commands_specification_command.rb97
-rw-r--r--trunk/test/rubygems/test_gem_commands_stale_command.rb39
-rw-r--r--trunk/test/rubygems/test_gem_commands_unpack_command.rb115
-rw-r--r--trunk/test/rubygems/test_gem_commands_update_command.rb165
-rw-r--r--trunk/test/rubygems/test_gem_config_file.rb276
-rw-r--r--trunk/test/rubygems/test_gem_dependency.rb140
-rw-r--r--trunk/test/rubygems/test_gem_dependency_installer.rb637
-rw-r--r--trunk/test/rubygems/test_gem_dependency_list.rb212
-rwxr-xr-xtrunk/test/rubygems/test_gem_digest.rb44
-rw-r--r--trunk/test/rubygems/test_gem_doc_manager.rb32
-rw-r--r--trunk/test/rubygems/test_gem_ext_configure_builder.rb86
-rw-r--r--trunk/test/rubygems/test_gem_ext_ext_conf_builder.rb122
-rw-r--r--trunk/test/rubygems/test_gem_ext_rake_builder.rb76
-rw-r--r--trunk/test/rubygems/test_gem_format.rb69
-rw-r--r--trunk/test/rubygems/test_gem_gem_path_searcher.rb60
-rw-r--r--trunk/test/rubygems/test_gem_gem_runner.rb35
-rw-r--r--trunk/test/rubygems/test_gem_indexer.rb263
-rw-r--r--trunk/test/rubygems/test_gem_install_update_options.rb61
-rw-r--r--trunk/test/rubygems/test_gem_installer.rb868
-rw-r--r--trunk/test/rubygems/test_gem_local_remote_options.rb85
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_header.rb137
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_input.rb119
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_output.rb104
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_reader.rb53
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_reader_entry.rb116
-rw-r--r--trunk/test/rubygems/test_gem_package_tar_writer.rb151
-rw-r--r--trunk/test/rubygems/test_gem_platform.rb259
-rw-r--r--trunk/test/rubygems/test_gem_remote_fetcher.rb692
-rw-r--r--trunk/test/rubygems/test_gem_requirement.rb223
-rw-r--r--trunk/test/rubygems/test_gem_server.rb301
-rw-r--r--trunk/test/rubygems/test_gem_source_index.rb834
-rw-r--r--trunk/test/rubygems/test_gem_source_info_cache.rb448
-rw-r--r--trunk/test/rubygems/test_gem_source_info_cache_entry.rb79
-rw-r--r--trunk/test/rubygems/test_gem_spec_fetcher.rb303
-rw-r--r--trunk/test/rubygems/test_gem_specification.rb954
-rw-r--r--trunk/test/rubygems/test_gem_stream_ui.rb117
-rw-r--r--trunk/test/rubygems/test_gem_uninstaller.rb66
-rw-r--r--trunk/test/rubygems/test_gem_validator.rb70
-rw-r--r--trunk/test/rubygems/test_gem_version.rb215
-rw-r--r--trunk/test/rubygems/test_gem_version_option.rb77
-rw-r--r--trunk/test/rubygems/test_kernel.rb65
74 files changed, 12556 insertions, 0 deletions
diff --git a/trunk/test/rubygems/bogussources.rb b/trunk/test/rubygems/bogussources.rb
new file mode 100644
index 0000000000..008e3a1de5
--- /dev/null
+++ b/trunk/test/rubygems/bogussources.rb
@@ -0,0 +1,8 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+Gem.use_paths("test/mock/gems")
diff --git a/trunk/test/rubygems/data/gem-private_key.pem b/trunk/test/rubygems/data/gem-private_key.pem
new file mode 100644
index 0000000000..3e4be4cd9a
--- /dev/null
+++ b/trunk/test/rubygems/data/gem-private_key.pem
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpQIBAAKCAQEAz0tTOtsJuHDKAEXrQx0f6DUEzBEUTSLR1fk0iEHsY9rDCQxm
+sw5Bf2UnVhdD03B4/XzIK+pat2CMQc37/vLIBuVgS7g/fzatGiM0m5rAHtycr0XU
+8Ek6zjx4iSv70OLjybY+/utHCEc838awGDMCFR21jYxgATPVwqAIyasvwbKh/Vhw
+uErFPqT9G8BKTHsaX+H+ADIRH001OmWkjB6EyjF05114kNMa0+2C7daV9hoBL3md
+hCt6zOGcapl/9LkGxhcNEUB/So16V1ZQldg9macGyWktyNTSfctlF+f8okAmicG3
+XIwaW8UTmjFCmvDs/h1R/uKpe2IOHz87n29d2QIDAQABAoIBAQCR6n/nyg+JmTtX
+/d+hGns/RTLfQpZ7xarXZ9gmoeD4WSE42VXhbIOGXXnXDAFecKl6Jb/xycGZm4if
+OZPM3rEWyZeDNWrc7WvkHiwF7GSYVMqmRg2iJqoSSla+mAtl+pBFiNfHMW6K0Tp0
+erOyFRW+L2+A9/MMZaRun6AP9URkn0jz2kwmMFf+6szmzVn6fPFzZDRI+hEeaDmi
+LBzSrfrddrIBX+xGEoBj6RmfnKBCSUVSSxOauYjd4mVjVYxvMH4SV1hXDUS5GPl5
+MbCiBb7bpNIg/8ljMoRrQiqk0XwwS7MaCqPtMhUtpSmC/zSjAfmoN7AOc/Xh69cQ
+OCMNZH9BAoGBAPBlsuuU6fg0gVTKDdR12jHx03uRRt8/nPxHnpJkZCIh9XKh1LtY
+bkumi9HZpp3mzDiaGg/rwfCwNckKx8NLhICLgkric6ClrKftxTu6C8tBAb5YDi6u
+74KYnV8lMY/unzBtIloPgM3uluS292POmrWZpKwhvHLD71MewzMor5HFAoGBANy/
+mwsBs8i3Gzk8Twjq8effhPpE7kpxhC7bhwmjX3q41EjQWDT8M6xb1P9dRSsCIebi
+kqP1yhl27dJpA8r5WqE/z89xhBvObAGRv41eXxOI0LaH2k5lJQrUeSC+51dy+BEB
+T3GXD4C5ezZHQ8Wz/oL73uikrfhD+AqOZT2YbMEFAoGBAJvWEWpOGm3f+4bvhI+Z
+5lxCG4oa3wqRvj58XvsfQRovUWGCLtlTtgwsZq8enLf3iaOXohV4Czzvva4Z4u1i
+4v5BcbEBo1scixRBOn5BWKvl9C9j/a2dkX3jWQD4p2xaj69gz8f6DNFyPTb+tNhq
+cjgO5YUASZ1MDrSfWIKteULRAoGAZkZv8x2KyofrmQ0UITGZerDYz4t4TA1kDMGx
+QwnqhtVzpXjCJWpkFotFmDsCfPaz9mErR8PtKvcrIL1/AF+fWe5Sve3+I1P0PpXk
+hf8fVdGhwbAXuRKrouTmagGI9b9Sp65PvHUcvasyJufFwqeuV8mScX87CzeSiHGI
+/ozMdnECgYEAq4+losrhe0DEmiC9zVPvwRXjbSixDsSJxHfOcqIsZqhUgBiZ4TJD
+SrkuukrMZib6BAD+PtCJS1TBbJyyvL3QecizhHSIh3ZnT0HnaRPatLEYmU65+3kE
+kTqL4ik92bJnnWowy677sydl1lzBJDVa9ZlTs7BFSd8y/0DZaUxGg2I=
+-----END RSA PRIVATE KEY-----
diff --git a/trunk/test/rubygems/data/gem-public_cert.pem b/trunk/test/rubygems/data/gem-public_cert.pem
new file mode 100644
index 0000000000..885bf7f369
--- /dev/null
+++ b/trunk/test/rubygems/data/gem-public_cert.pem
@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMREwDwYDVQQDDAhydWJ5
+Z2VtczEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUxEzARBgoJkiaJk/IsZAEZFgNj
+b20wHhcNMDcwODAyMDMyNTQyWhcNMDgwODAxMDMyNTQyWjBBMREwDwYDVQQDDAhy
+dWJ5Z2VtczEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUxEzARBgoJkiaJk/IsZAEZ
+FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDPS1M62wm4cMoA
+RetDHR/oNQTMERRNItHV+TSIQexj2sMJDGazDkF/ZSdWF0PTcHj9fMgr6lq3YIxB
+zfv+8sgG5WBLuD9/Nq0aIzSbmsAe3JyvRdTwSTrOPHiJK/vQ4uPJtj7+60cIRzzf
+xrAYMwIVHbWNjGABM9XCoAjJqy/BsqH9WHC4SsU+pP0bwEpMexpf4f4AMhEfTTU6
+ZaSMHoTKMXTnXXiQ0xrT7YLt1pX2GgEveZ2EK3rM4ZxqmX/0uQbGFw0RQH9KjXpX
+VlCV2D2ZpwbJaS3I1NJ9y2UX5/yiQCaJwbdcjBpbxROaMUKa8Oz+HVH+4ql7Yg4f
+Pzufb13ZAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
+BBRYTAoj4cn8CWZMHFnHGQgoO5jyFTANBgkqhkiG9w0BAQUFAAOCAQEATRrJC05l
+dOmx67Sy3bU+AVXkOr7B9nn2Myqo9uSIAncPoElN6aHr/Q8wOOjtok4r0JcHPe1e
+eotDCZUE1Jkl13Tpv26rOfOOUHtGlyAIAtpsUGOraaJkSut4WKLr1/KckyAAEtgP
+c13A0s0mEiWFRuYxIdEi54561pTT2qQBE/DUPGoYD5rUg9XYAlSovMMwG99Oca7L
+cI6vCymr1bzzddExoywBNOy0fbBT62I3ICBGbH5yOVVKVmlxeo2Zp10FCj0kDrnq
+OuMJSDr5I2XPYqoC+W4YSbwn55o2jGIUX1lOq2Hvj4tFgSxlnJZn0tUhBfR3gSOn
+IFnrqu8PlZsLFw==
+-----END CERTIFICATE-----
diff --git a/trunk/test/rubygems/fake_certlib/openssl.rb b/trunk/test/rubygems/fake_certlib/openssl.rb
new file mode 100644
index 0000000000..948110f078
--- /dev/null
+++ b/trunk/test/rubygems/fake_certlib/openssl.rb
@@ -0,0 +1,7 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+fail LoadError, "no such file to load -- openssl"
diff --git a/trunk/test/rubygems/functional.rb b/trunk/test/rubygems/functional.rb
new file mode 100644
index 0000000000..4838167324
--- /dev/null
+++ b/trunk/test/rubygems/functional.rb
@@ -0,0 +1,95 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require 'rubygems'
+require 'test/insure_session'
+require 'rubygems/format'
+require 'rubygems/command_manager'
+
+class FunctionalTest < Test::Unit::TestCase
+ def setup
+ @gem_path = File.expand_path("bin/gem")
+ lib_path = File.expand_path("lib")
+ @ruby_options = "-I#{lib_path} -I."
+ @verbose = false
+ end
+
+ def test_gem_help_options
+ gem_nossl 'help options'
+ assert_match(/Usage:/, @out, @err)
+ assert_status
+ end
+
+ def test_gem_help_commands
+ gem_nossl 'help commands'
+ assert_match(/gem install/, @out)
+ assert_status
+ end
+
+ def test_gem_no_args_shows_help
+ gem_nossl
+ assert_match(/Usage:/, @out)
+ assert_status 1
+ end
+
+ # This test is disabled because of the insanely long time it takes
+ # to time out.
+ def xtest_bogus_source_hoses_up_remote_install_but_gem_command_gives_decent_error_message
+ @ruby_options << " -rtest/bogussources"
+ gem_nossl "install asdf --remote"
+ assert_match(/error/im, @err)
+ assert_status 1
+ end
+
+ def test_all_command_helps
+ mgr = Gem::CommandManager.new
+ mgr.command_names.each do |cmdname|
+ gem_nossl "help #{cmdname}"
+ assert_match(/Usage: gem #{cmdname}/, @out,
+ "should see help for #{cmdname}")
+ end
+ end
+
+ # :section: Help Methods
+
+ # Run a gem command without the SSL library.
+ def gem_nossl(options="")
+ old_options = @ruby_options.dup
+ @ruby_options << " -Itest/fake_certlib"
+ gem(options)
+ ensure
+ @ruby_options = old_options
+ end
+
+ # Run a gem command with the SSL library.
+ def gem_withssl(options="")
+ gem(options)
+ end
+
+ # Run a gem command for the functional test.
+ def gem(options="")
+ shell = Session::Shell.new
+ options = options + " --config-file missing_file" if options !~ /--config-file/
+ command = "#{Gem.ruby} #{@ruby_options} #{@gem_path} #{options}"
+ puts "\n\nCOMMAND: [#{command}]" if @verbose
+ @out, @err = shell.execute command
+ @status = shell.exit_status
+ puts "STATUS: [#{@status}]" if @verbose
+ puts "OUTPUT: [#{@out}]" if @verbose
+ puts "ERROR: [#{@err}]" if @verbose
+ puts "PWD: [#{Dir.pwd}]" if @verbose
+ shell.close
+ end
+
+ private
+
+ def assert_status(expected_status=0)
+ assert_equal expected_status, @status
+ end
+
+end
diff --git a/trunk/test/rubygems/gem_installer_test_case.rb b/trunk/test/rubygems/gem_installer_test_case.rb
new file mode 100644
index 0000000000..0d684eb1eb
--- /dev/null
+++ b/trunk/test/rubygems/gem_installer_test_case.rb
@@ -0,0 +1,86 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/installer'
+
+class Gem::Installer
+ attr_accessor :gem_dir
+
+ attr_writer :format
+ attr_writer :gem_home
+ attr_writer :env_shebang
+ attr_writer :ignore_dependencies
+ attr_writer :format_executable
+ attr_writer :security_policy
+ attr_writer :spec
+ attr_writer :wrappers
+end
+
+class GemInstallerTestCase < RubyGemTestCase
+
+ def setup
+ super
+
+ @spec = quick_gem "a"
+ @gem = File.join @tempdir, "#{@spec.full_name}.gem"
+
+ util_build_gem @spec
+ FileUtils.mv File.join(@gemhome, 'cache', "#{@spec.full_name}.gem"),
+ @tempdir
+
+ @installer = Gem::Installer.new @gem
+ @installer.gem_dir = util_gem_dir
+ @installer.gem_home = @gemhome
+ @installer.spec = @spec
+ end
+
+ def util_gem_bindir(version = '2')
+ File.join util_gem_dir(version), "bin"
+ end
+
+ def util_gem_dir(version = '2')
+ File.join @gemhome, "gems", "a-#{version}" # HACK
+ end
+
+ def util_inst_bindir
+ File.join @gemhome, "bin"
+ end
+
+ def util_make_exec(version = '2', shebang = "#!/usr/bin/ruby")
+ @spec.executables = ["my_exec"]
+
+ FileUtils.mkdir_p util_gem_bindir(version)
+ exec_file = @installer.formatted_program_filename "my_exec"
+ exec_path = File.join util_gem_bindir(version), exec_file
+ File.open exec_path, 'w' do |f|
+ f.puts shebang
+ end
+ end
+
+ def util_setup_gem(ui = @ui) # HACK fix use_ui to make this automatic
+ @spec.files = File.join('lib', 'code.rb')
+ @spec.executables << 'executable'
+ @spec.extensions << File.join('ext', 'a', 'mkrf_conf.rb')
+
+ Dir.chdir @tempdir do
+ FileUtils.mkdir_p 'bin'
+ FileUtils.mkdir_p 'lib'
+ FileUtils.mkdir_p File.join('ext', 'a')
+ File.open File.join('bin', 'executable'), 'w' do |f| f.puts '1' end
+ File.open File.join('lib', 'code.rb'), 'w' do |f| f.puts '1' end
+ File.open File.join('ext', 'a', 'mkrf_conf.rb'), 'w' do |f|
+ f << <<-EOF
+ File.open 'Rakefile', 'w' do |rf| rf.puts "task :default" end
+ EOF
+ end
+
+ use_ui ui do
+ FileUtils.rm @gem
+ Gem::Builder.new(@spec).build
+ end
+ end
+
+ @installer = Gem::Installer.new @gem
+ end
+
+end
+
diff --git a/trunk/test/rubygems/gem_package_tar_test_case.rb b/trunk/test/rubygems/gem_package_tar_test_case.rb
new file mode 100644
index 0000000000..756b30ef27
--- /dev/null
+++ b/trunk/test/rubygems/gem_package_tar_test_case.rb
@@ -0,0 +1,146 @@
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/package'
+
+class File
+
+ # straight from setup.rb
+ def self.dir?(path)
+ # for corrupted windows stat()
+ File.directory?((path[-1,1] == '/') ? path : path + '/')
+ end
+
+ def self.read_b(name)
+ File.open(name, "rb") { |f| f.read }
+ end
+
+end
+
+class TarTestCase < RubyGemTestCase
+
+ def ASCIIZ(str, length)
+ str + "\0" * (length - str.length)
+ end
+
+ def SP(s)
+ s + " "
+ end
+
+ def SP_Z(s)
+ s + " \0"
+ end
+
+ def Z(s)
+ s + "\0"
+ end
+
+ def assert_headers_equal(expected, actual)
+ expected = expected.to_s unless String === expected
+ actual = actual.to_s unless String === actual
+
+ fields = %w[
+ name 100
+ mode 8
+ uid 8
+ gid 8
+ size 12
+ mtime 12
+ checksum 8
+ typeflag 1
+ linkname 100
+ magic 6
+ version 2
+ uname 32
+ gname 32
+ devmajor 8
+ devminor 8
+ prefix 155
+ ]
+
+ offset = 0
+
+ until fields.empty? do
+ name = fields.shift
+ length = fields.shift.to_i
+
+ if name == "checksum" then
+ chksum_off = offset
+ offset += length
+ next
+ end
+
+ assert_equal expected[offset, length], actual[offset, length],
+ "Field #{name} of the tar header differs."
+
+ offset += length
+ end
+
+ assert_equal expected[chksum_off, 8], actual[chksum_off, 8]
+ end
+
+ def calc_checksum(header)
+ sum = header.unpack("C*").inject{|s,a| s + a}
+ SP(Z(to_oct(sum, 6)))
+ end
+
+ def header(type, fname, dname, length, mode, checksum = nil)
+ checksum ||= " " * 8
+
+ arr = [ # struct tarfile_entry_posix
+ ASCIIZ(fname, 100), # char name[100]; ASCII + (Z unless filled)
+ Z(to_oct(mode, 7)), # char mode[8]; 0 padded, octal null
+ Z(to_oct(0, 7)), # char uid[8]; ditto
+ Z(to_oct(0, 7)), # char gid[8]; ditto
+ Z(to_oct(length, 11)), # char size[12]; 0 padded, octal, null
+ Z(to_oct(0, 11)), # char mtime[12]; 0 padded, octal, null
+ checksum, # char checksum[8]; 0 padded, octal, null, space
+ type, # char typeflag[1]; file: "0" dir: "5"
+ "\0" * 100, # char linkname[100]; ASCII + (Z unless filled)
+ "ustar\0", # char magic[6]; "ustar\0"
+ "00", # char version[2]; "00"
+ ASCIIZ("wheel", 32), # char uname[32]; ASCIIZ
+ ASCIIZ("wheel", 32), # char gname[32]; ASCIIZ
+ Z(to_oct(0, 7)), # char devmajor[8]; 0 padded, octal, null
+ Z(to_oct(0, 7)), # char devminor[8]; 0 padded, octal, null
+ ASCIIZ(dname, 155) # char prefix[155]; ASCII + (Z unless filled)
+ ]
+
+ format = "C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155"
+ h = if RUBY_VERSION >= "1.9" then
+ arr.join
+ else
+ arr = arr.join("").split(//).map{|x| x[0]}
+ arr.pack format
+ end
+ ret = h + "\0" * (512 - h.size)
+ assert_equal(512, ret.size)
+ ret
+ end
+
+ def tar_dir_header(name, prefix, mode)
+ h = header("5", name, prefix, 0, mode)
+ checksum = calc_checksum(h)
+ header("5", name, prefix, 0, mode, checksum)
+ end
+
+ def tar_file_header(fname, dname, mode, length)
+ h = header("0", fname, dname, length, mode)
+ checksum = calc_checksum(h)
+ header("0", fname, dname, length, mode, checksum)
+ end
+
+ def to_oct(n, pad_size)
+ "%0#{pad_size}o" % n
+ end
+
+ def util_entry(tar)
+ io = TempIO.new tar
+ header = Gem::Package::TarHeader.from io
+ entry = Gem::Package::TarReader::Entry.new header, io
+ end
+
+ def util_dir_entry
+ util_entry tar_dir_header("foo", "bar", 0)
+ end
+
+end
+
diff --git a/trunk/test/rubygems/gemutilities.rb b/trunk/test/rubygems/gemutilities.rb
new file mode 100644
index 0000000000..0ab2c5272c
--- /dev/null
+++ b/trunk/test/rubygems/gemutilities.rb
@@ -0,0 +1,441 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+at_exit { $SAFE = 1 }
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'fileutils'
+require 'test/unit'
+require 'tmpdir'
+require 'uri'
+require 'rubygems/package'
+require 'rubygems/test_utilities'
+
+require File.join(File.expand_path(File.dirname(__FILE__)), 'mockgemui')
+
+module Gem
+ def self.source_index=(si)
+ @@source_index = si
+ end
+
+ def self.win_platform=(val)
+ @@win_platform = val
+ end
+
+ module DefaultUserInteraction
+ @ui = MockGemUi.new
+ end
+end
+
+class RubyGemTestCase < Test::Unit::TestCase
+
+ include Gem::DefaultUserInteraction
+
+ undef_method :default_test if instance_methods.include? 'default_test' or
+ instance_methods.include? :default_test
+
+ def setup
+ super
+
+ @ui = MockGemUi.new
+ tmpdir = nil
+ Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp
+ @tempdir = File.join tmpdir, "test_rubygems_#{$$}"
+ @tempdir.untaint
+ @gemhome = File.join @tempdir, "gemhome"
+ @gemcache = File.join(@gemhome, "source_cache")
+ @usrcache = File.join(@gemhome, ".gem", "user_cache")
+ @latest_usrcache = File.join(@gemhome, ".gem", "latest_user_cache")
+ @userhome = File.join @tempdir, 'userhome'
+
+ ENV['HOME'] = @userhome
+ Gem.instance_variable_set :@user_home, nil
+
+ FileUtils.mkdir_p @gemhome
+ FileUtils.mkdir_p @userhome
+
+ ENV['GEMCACHE'] = @usrcache
+ Gem.use_paths(@gemhome)
+ Gem.loaded_specs.clear
+
+ Gem.configuration.verbose = true
+ Gem.configuration.update_sources = true
+
+ @gem_repo = "http://gems.example.com/"
+ @uri = URI.parse @gem_repo
+ Gem.sources.replace [@gem_repo]
+
+ Gem::SpecFetcher.fetcher = nil
+
+ @orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
+ Gem::ConfigMap[:BASERUBY] = Gem::ConfigMap[:RUBY_INSTALL_NAME]
+
+ @orig_arch = Gem::ConfigMap[:arch]
+
+ if win_platform?
+ util_set_arch 'i386-mswin32'
+ else
+ util_set_arch 'i686-darwin8.10.1'
+ end
+
+ @marshal_version = "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
+
+ @private_key = File.expand_path File.join(File.dirname(__FILE__),
+ 'private_key.pem')
+ @public_cert = File.expand_path File.join(File.dirname(__FILE__),
+ 'public_cert.pem')
+ end
+
+ def teardown
+ Gem::ConfigMap[:BASERUBY] = @orig_BASERUBY
+ Gem::ConfigMap[:arch] = @orig_arch
+
+ if defined? Gem::RemoteFetcher then
+ Gem::RemoteFetcher.fetcher = nil
+ end
+
+ FileUtils.rm_rf @tempdir
+
+ ENV.delete 'GEMCACHE'
+ ENV.delete 'GEM_HOME'
+ ENV.delete 'GEM_PATH'
+
+ Gem.clear_paths
+ end
+
+ def install_gem gem
+ require 'rubygems/installer'
+
+ use_ui MockGemUi.new do
+ Dir.chdir @tempdir do
+ Gem::Builder.new(gem).build
+ end
+ end
+
+ gem = File.join(@tempdir, "#{gem.full_name}.gem").untaint
+ Gem::Installer.new(gem, :wrappers => true).install
+ end
+
+ def prep_cache_files(lc)
+ @usr_si ||= Gem::SourceIndex.new
+ @usr_sice ||= Gem::SourceInfoCacheEntry.new @usr_si, 0
+
+ @sys_si ||= Gem::SourceIndex.new
+ @sys_sice ||= Gem::SourceInfoCacheEntry.new @sys_si, 0
+
+ latest_si = Gem::SourceIndex.new
+ latest_si.add_specs(*@sys_si.latest_specs)
+ latest_sys_sice = Gem::SourceInfoCacheEntry.new latest_si, 0
+
+ latest_si = Gem::SourceIndex.new
+ latest_si.add_specs(*@usr_si.latest_specs)
+ latest_usr_sice = Gem::SourceInfoCacheEntry.new latest_si, 0
+
+ [ [lc.system_cache_file, @sys_sice],
+ [lc.latest_system_cache_file, latest_sys_sice],
+ [lc.user_cache_file, @usr_sice],
+ [lc.latest_user_cache_file, latest_usr_sice],
+ ].each do |filename, data|
+ FileUtils.mkdir_p File.dirname(filename).untaint
+
+ open filename.dup.untaint, 'wb' do |f|
+ f.write Marshal.dump({ @gem_repo => data })
+ end
+ end
+ end
+
+ def read_cache(path)
+ open path.dup.untaint, 'rb' do |io|
+ Marshal.load io.read
+ end
+ end
+
+ def read_binary(path)
+ Gem.read_binary path
+ end
+
+ def write_file(path)
+ path = File.join(@gemhome, path)
+ dir = File.dirname path
+ FileUtils.mkdir_p dir
+
+ open path, 'wb' do |io|
+ yield io
+ end
+
+ path
+ end
+
+ def quick_gem(gemname, version='2')
+ require 'rubygems/specification'
+
+ spec = Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
+ s.name = gemname
+ s.version = version
+ s.author = 'A User'
+ s.email = 'example@example.com'
+ s.homepage = 'http://example.com'
+ s.has_rdoc = true
+ s.summary = "this is a summary"
+ s.description = "This is a test description"
+
+ yield(s) if block_given?
+ end
+
+ 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
+
+ Gem.source_index.add_spec spec
+
+ return spec
+ end
+
+ def util_build_gem(spec)
+ dir = File.join(@gemhome, 'gems', spec.full_name)
+ FileUtils.mkdir_p dir
+
+ Dir.chdir dir do
+ spec.files.each do |file|
+ next if File.exist? file
+ FileUtils.mkdir_p File.dirname(file)
+ File.open file, 'w' do |fp| fp.puts "# #{file}" end
+ end
+
+ use_ui MockGemUi.new do
+ Gem::Builder.new(spec).build
+ end
+
+ FileUtils.mv "#{spec.full_name}.gem",
+ File.join(@gemhome, 'cache', "#{spec.original_name}.gem")
+ end
+ end
+
+ def util_clear_gems
+ FileUtils.rm_r File.join(@gemhome, 'gems')
+ FileUtils.rm_r File.join(@gemhome, 'specifications')
+ Gem.source_index.refresh!
+ end
+
+ def util_gem(name, version, &block)
+ spec = quick_gem(name, version, &block)
+
+ util_build_gem spec
+
+ cache_file = File.join @tempdir, 'gems', "#{spec.original_name}.gem"
+ FileUtils.mv File.join(@gemhome, 'cache', "#{spec.original_name}.gem"),
+ cache_file
+ FileUtils.rm File.join(@gemhome, 'specifications',
+ "#{spec.full_name}.gemspec")
+
+ spec.loaded_from = nil
+ spec.loaded = false
+
+ [spec, cache_file]
+ end
+
+ def util_gzip(data)
+ out = StringIO.new
+
+ Zlib::GzipWriter.wrap out do |io|
+ io.write data
+ end
+
+ out.string
+ end
+
+ def util_make_gems
+ init = proc do |s|
+ s.files = %w[lib/code.rb]
+ s.require_paths = %w[lib]
+ end
+
+ @a1 = quick_gem('a', '1', &init)
+ @a2 = quick_gem('a', '2', &init)
+ @a_evil9 = quick_gem('a_evil', '9', &init)
+ @b2 = quick_gem('b', '2', &init)
+ @c1_2 = quick_gem('c', '1.2', &init)
+ @pl1 = quick_gem 'pl', '1' do |s| # l for legacy
+ s.files = %w[lib/code.rb]
+ s.require_paths = %w[lib]
+ s.platform = Gem::Platform.new 'i386-linux'
+ s.instance_variable_set :@original_platform, 'i386-linux'
+ end
+
+ write_file File.join(*%W[gems #{@a1.original_name} lib code.rb]) do end
+ write_file File.join(*%W[gems #{@a2.original_name} lib code.rb]) do end
+ write_file File.join(*%W[gems #{@b2.original_name} lib code.rb]) do end
+ write_file File.join(*%W[gems #{@c1_2.original_name} lib code.rb]) do end
+ write_file File.join(*%W[gems #{@pl1.original_name} lib code.rb]) do end
+
+ [@a1, @a2, @a_evil9, @b2, @c1_2, @pl1].each { |spec| util_build_gem spec }
+
+ FileUtils.rm_r File.join(@gemhome, 'gems', @pl1.original_name)
+
+ Gem.source_index = nil
+ end
+
+ ##
+ # Set the platform to +arch+
+
+ def util_set_arch(arch)
+ Gem::ConfigMap[:arch] = arch
+ platform = Gem::Platform.new arch
+
+ Gem.instance_variable_set :@platforms, nil
+ Gem::Platform.instance_variable_set :@local, nil
+
+ platform
+ end
+
+ def util_setup_fake_fetcher
+ require 'zlib'
+ require 'socket'
+ require 'rubygems/remote_fetcher'
+
+ @fetcher = Gem::FakeFetcher.new
+
+ util_make_gems
+
+ @all_gems = [@a1, @a2, @a_evil9, @b2, @c1_2].sort
+ @all_gem_names = @all_gems.map { |gem| gem.full_name }
+
+ gem_names = [@a1.full_name, @a2.full_name, @b2.full_name]
+ @gem_names = gem_names.sort.join("\n")
+
+ @source_index = Gem::SourceIndex.new
+ @source_index.add_spec @a1
+ @source_index.add_spec @a2
+ @source_index.add_spec @a_evil9
+ @source_index.add_spec @c1_2
+
+ Gem::RemoteFetcher.fetcher = @fetcher
+ end
+
+ def util_setup_source_info_cache(*specs)
+ require 'rubygems/source_info_cache'
+ require 'rubygems/source_info_cache_entry'
+
+ specs = Hash[*specs.map { |spec| [spec.full_name, spec] }.flatten]
+ si = Gem::SourceIndex.new specs
+
+ sice = Gem::SourceInfoCacheEntry.new si, 0
+ sic = Gem::SourceInfoCache.new
+
+ sic.set_cache_data( { @gem_repo => sice } )
+ sic.update
+ sic.write_cache
+ sic.reset_cache_data
+
+ Gem::SourceInfoCache.instance_variable_set :@cache, sic
+
+ si
+ end
+
+ def util_setup_spec_fetcher(*specs)
+ specs = Hash[*specs.map { |spec| [spec.full_name, spec] }.flatten]
+ si = Gem::SourceIndex.new specs
+
+ spec_fetcher = Gem::SpecFetcher.fetcher
+
+ spec_fetcher.specs[@uri] = []
+ si.gems.sort_by { |_, spec| spec }.each do |_, spec|
+ spec_tuple = [spec.name, spec.version, spec.original_platform]
+ spec_fetcher.specs[@uri] << spec_tuple
+ end
+
+ spec_fetcher.latest_specs[@uri] = []
+ si.latest_specs.sort.each do |spec|
+ spec_tuple = [spec.name, spec.version, spec.original_platform]
+ spec_fetcher.latest_specs[@uri] << spec_tuple
+ end
+
+ si.gems.sort_by { |_,spec| spec }.each do |_, spec|
+ path = "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{spec.original_name}.gemspec.rz"
+ data = Marshal.dump spec
+ data_deflate = Zlib::Deflate.deflate data
+ @fetcher.data[path] = data_deflate
+ end
+
+ si
+ end
+
+ def util_zip(data)
+ Zlib::Deflate.deflate data
+ end
+
+ def self.win_platform?
+ Gem.win_platform?
+ end
+
+ def win_platform?
+ Gem.win_platform?
+ end
+
+ # NOTE Allow tests to use a random (but controlled) port number instead of
+ # a hardcoded one. This helps CI tools when running parallels builds on
+ # the same builder slave.
+ def self.process_based_port
+ @@process_based_port ||= 8000 + $$ % 1000
+ end
+
+ def process_based_port
+ self.class.process_based_port
+ end
+
+ def build_rake_in
+ gem_ruby = Gem.ruby
+ ruby = @@ruby
+ Gem.module_eval {@ruby = ruby}
+ env_rake = ENV["rake"]
+ ENV["rake"] = @@rake
+ yield @@rake
+ ensure
+ Gem.module_eval {@ruby = gem_ruby}
+ if env_rake
+ ENV["rake"] = env_rake
+ else
+ ENV.delete("rake")
+ end
+ end
+
+ def self.rubybin
+ if ruby = ENV["RUBY"]
+ return ruby
+ end
+ ruby = "ruby"
+ rubyexe = ruby+".exe"
+ 3.times do
+ if File.exist? ruby and File.executable? ruby and !File.directory? ruby
+ return File.expand_path(ruby)
+ end
+ if File.exist? rubyexe and File.executable? rubyexe
+ return File.expand_path(rubyexe)
+ end
+ ruby = File.join("..", ruby)
+ end
+ begin
+ require "rbconfig"
+ File.join(
+ RbConfig::CONFIG["bindir"],
+ RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
+ )
+ rescue LoadError
+ "ruby"
+ end
+ end
+
+ @@ruby = rubybin
+ @@rake = ENV["rake"] || (@@ruby + " " + File.expand_path("../../../bin/rake", __FILE__))
+
+end
+
diff --git a/trunk/test/rubygems/insure_session.rb b/trunk/test/rubygems/insure_session.rb
new file mode 100644
index 0000000000..e56f9abcb8
--- /dev/null
+++ b/trunk/test/rubygems/insure_session.rb
@@ -0,0 +1,51 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+require 'rubygems'
+
+def install_session
+ path_to_gem = File.join("redist", "session.gem")
+ begin
+ Gem::Installer.new(path_to_gem).install
+ rescue Errno::EACCES => ex
+ puts
+ puts "*****************************************************************"
+ puts "Unable to install Gem 'Session'."
+ puts "Reason: #{ex.message}"
+ puts "Try running:"
+ puts
+ puts " gem -Li #{path_to_gem}"
+ puts
+ puts "with the appropriate admin privileges."
+ puts "*****************************************************************"
+ puts
+ exit
+ end
+ gem 'session'
+end
+
+begin
+ require 'session'
+rescue LoadError => e
+ puts
+ puts "Required Gem 'Session' missing."
+ puts "We can attempt to install from the RubyGems Distribution,"
+ puts "but installation may require admin privileges on your system."
+ puts
+ print "Install now from RubyGems distribution? [Yn]"
+ answer = gets
+ if(answer =~ /^y/i || answer =~ /^[^a-zA-Z0-9]$/) then
+ install_session
+ puts
+ puts "Retry running the functional tests."
+ exit(0)
+ else
+ puts "Test cancelled...quitting"
+ exit(1)
+ end
+end
diff --git a/trunk/test/rubygems/mockgemui.rb b/trunk/test/rubygems/mockgemui.rb
new file mode 100644
index 0000000000..95a95fbf98
--- /dev/null
+++ b/trunk/test/rubygems/mockgemui.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+require 'stringio'
+require 'rubygems/user_interaction'
+
+class MockGemUi < Gem::StreamUI
+ class TermError < RuntimeError; end
+
+ def initialize(input="")
+ super(StringIO.new(input), StringIO.new, StringIO.new)
+ @terminated = false
+ end
+
+ def input
+ @ins.string
+ end
+
+ def output
+ @outs.string
+ end
+
+ def error
+ @errs.string
+ end
+
+ def terminated?
+ @terminated
+ end
+
+ def terminate_interaction(status=0)
+ @terminated = true
+
+ raise TermError
+ end
+
+end
+
diff --git a/trunk/test/rubygems/private_key.pem b/trunk/test/rubygems/private_key.pem
new file mode 100644
index 0000000000..95b3dc76d8
--- /dev/null
+++ b/trunk/test/rubygems/private_key.pem
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEAm24C6xixiAxO+i1f3L8XRMwrmLkt6BvT60mZ7g8HsklH3af7
+KNHA6vo/G6sujs2UsNO4HY8BTEneiVOXXWQlcsJ+Z5wEPlIu4zFueAmLefx+n9lE
+ulNIUDoyUenKX4spoMRnX8k4lXL05ho/6JFq0JdDY2DmAaQ4vvTz5mh9kZiybtHQ
+fzcpbA51uY+sjdQRCPDHyUUfh0SmWJlLYMwcBdVeCiGUPBLi+iP5x1btO4uiJK6Q
+IMaV1H3SUCYtKGQKl7qwFd8k8ZBcHYOtmK61tupg3vqWQc0em6SxPj5lws8+1MVK
+twBNIDx24jF4ntxBRNKMZ7FN5SHbobAgDYkPAQIDAQABAoIBAGQilgK8X/PUajVH
+clEXU3hhSV0VQHwfIYKeYms6h6zXBVPKW0dLC0zXeDztJgueasMZQ67XaPCrTpGO
+px/l2zJ6F1HM8/bqn4aDXDY9f/xRLYryQRMBgL8fHzgitNylHWaT4j2Vt7yg2SI9
+mxrMRNKqASJPVR+Nm3l6+n9gpjVb99wEucWplPPHI6KhXLYPZOqSwt+zaH5roz3k
+UQmMs0Bs4hF1SzVl0n+KNoXHOwswVrmBWXgWvm2OhnwY2e26jfejc8toJc/ShAJ7
+C9exnrdimcgEKbd22Sum4G00CDYhcrG5LHHqkgwifcAEVctrvBZBZHGgpxlO8a8U
+eF2Vr7kCgYEAykdrBlzp7Fn9xzUInBQ3NXTTYAq51lpuJdmHQmPuTSY0buoHkd9f
+xbUCZ2qR9QAesrx4hI0qGLetc8IOKDoWx2rPepCCvO3Kx61o1SB5fAvBue03qVoq
+HqACX3Uk24Em8zAz9xuP13ETH/wU7sUbUxRHMCre6ZDmlxn4g5l+Nl8CgYEAxLVl
+22yBx0dfRr3UsHY9rxll2gIlnfnYfiJzq8wetzt/TfztRV5ILz7FyWqL5d7IoqkA
+fT2V4HAasRJASnKohwJe7z5M/H2ExwkGNFvY+jefb2CoUl5WouK9AlhbqBk3zmHi
+sY5GqQkAp/kHMntEin+sErJw6mkgAGdser3a9p8CgYEAqi31w++tunRnxw4+RRnY
+7Pdx0k6T1NxV6TAe1ONAHNY0rM/mOHqml65W7GzDiU1lhlh8SIB/VzZJDqfHw15D
+xdh94A7uf0bMILwrA4wDyTIW9Xa3Kpq57vQNqwPiU25QN69pOM+Ob+IpBfLOJafc
++kOINOUMj5Kh/aQS6Zzci58CgYEAk24dlFKEBjbRCvU2FrfYTYcsljPru7ZJc2gg
+588J6m0WYf5CWy5pzbcviGFpzvSlzXv7GOLylQ+QgcxbETFUbDPzsT4xd0AgJwj1
+dIKuYgMUZOa94VZBer2TydEtiRS1heJJhKhM/1329u4nXceTvHYqIq1JAfeee48I
+eAoZtaMCgYBz1FjWFQnMTD5nmyPEEZneoBPAR5+9jwOps+IYOoHtazoMFszzd0qo
+JZW3Ihn9KRrVSxfFApKS/ZwjiZ+tJUk7DE/v/0l0sszefY7s8b0pL1lpeZSoL71e
+QoG1WLXUiDV3BRlmyOAF1h3p12KRTLgwubN51ajECwcs3QwE+ZT8Gg==
+-----END RSA PRIVATE KEY-----
diff --git a/trunk/test/rubygems/public_cert.pem b/trunk/test/rubygems/public_cert.pem
new file mode 100644
index 0000000000..9b7c3d8e98
--- /dev/null
+++ b/trunk/test/rubygems/public_cert.pem
@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMRAwDgYDVQQDDAdkcmJy
+YWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZFgNu
+ZXQwHhcNMDcxMjIxMDIwNDE0WhcNMDgxMjIwMDIwNDE0WjBBMRAwDgYDVQQDDAdk
+cmJyYWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZ
+FgNuZXQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbbgLrGLGIDE76
+LV/cvxdEzCuYuS3oG9PrSZnuDweySUfdp/so0cDq+j8bqy6OzZSw07gdjwFMSd6J
+U5ddZCVywn5nnAQ+Ui7jMW54CYt5/H6f2US6U0hQOjJR6cpfiymgxGdfyTiVcvTm
+Gj/okWrQl0NjYOYBpDi+9PPmaH2RmLJu0dB/NylsDnW5j6yN1BEI8MfJRR+HRKZY
+mUtgzBwF1V4KIZQ8EuL6I/nHVu07i6IkrpAgxpXUfdJQJi0oZAqXurAV3yTxkFwd
+g62YrrW26mDe+pZBzR6bpLE+PmXCzz7UxUq3AE0gPHbiMXie3EFE0oxnsU3lIduh
+sCANiQ8BAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
+BBS5k4Z75VSpdM0AclG2UvzFA/VW5DANBgkqhkiG9w0BAQUFAAOCAQEAHagT4lfX
+kP/hDaiwGct7XPuVGbrOsKRVD59FF5kETBxEc9UQ1clKWngf8JoVuEoKD774dW19
+bU0GOVWO+J6FMmT/Cp7nuFJ79egMf/gy4gfUfQMuvfcr6DvZUPIs9P/TlK59iMYF
+DIOQ3DxdF3rMzztNUCizN4taVscEsjCcgW6WkUJnGdqlu3OHWpQxZBJkBTjPCoc6
+UW6on70SFPmAy/5Cq0OJNGEWBfgD9q7rrs/X8GGwUWqXb85RXnUVi/P8Up75E0ag
+14jEc90kN+C7oI/AGCBN0j6JnEtYIEJZibjjDJTSMWlUKKkj30kq7hlUC2CepJ4v
+x52qPcexcYZR7w==
+-----END CERTIFICATE-----
diff --git a/trunk/test/rubygems/simple_gem.rb b/trunk/test/rubygems/simple_gem.rb
new file mode 100644
index 0000000000..a6f14bc3c7
--- /dev/null
+++ b/trunk/test/rubygems/simple_gem.rb
@@ -0,0 +1,72 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+ SIMPLE_GEM = <<-GEMDATA
+ MD5SUM = "e3701f9db765a2358aef94c40ded71c8"
+ if $0 == __FILE__
+ require 'optparse'
+
+ options = {}
+ ARGV.options do |opts|
+ opts.on_tail("--help", "show this message") {puts opts; exit}
+ opts.on('--dir=DIRNAME', "Installation directory for the Gem") {|options[:directory]|}
+ opts.on('--force', "Force Gem to intall, bypassing dependency checks") {|options[:force]|}
+ opts.on('--gen-rdoc', "Generate RDoc documentation for the Gem") {|options[:gen_rdoc]|}
+ opts.parse!
+ end
+
+ require 'rubygems'
+ @directory = options[:directory] || Gem.dir
+ @force = options[:force]
+
+ gem = Gem::Installer.new(__FILE__).install(@force, @directory)
+ if options[:gen_rdoc]
+ Gem::DocManager.new(gem).generate_rdoc
+ end
+end
+
+__END__
+--- !ruby/object:Gem::Specification
+rubygems_version: "1.0"
+name: testing
+version: !ruby/object:Gem::Version
+ version: 1.2.3
+date: 2004-03-18 22:01:52.859121 -05:00
+platform:
+summary: This exercise the gem testing stuff.
+require_paths:
+ - lib
+files:
+ - lib/foo.rb
+ - lib/test
+ - lib/test.rb
+ - lib/test/wow.rb
+autorequire: test
+test_suite_file: foo
+requirements:
+ - a computer processor
+---
+-
+ size: 109
+ mode: 420
+ path: lib/foo.rb
+-
+ size: 0
+ mode: 420
+ path: lib/test.rb
+-
+ size: 15
+ mode: 420
+ path: lib/test/wow.rb
+---
+eJwVjDEKgDAUQ/eeIpsKguhY3ARPoHMp9quF0mL7e39/h5DwQpLpqz4TOqbC
+U42eO6WuYEvBntIhECuaaX1KqXXLmy2kAEc32szExK+PjyBAlpTZyK0N/Twu
+g1CKTjX9BGAj1w==
+---
+eJwDAAAAAAE=
+---
+eJwrKC0pVlAvzy9XyE3MU+cCACwiBP4=
+ GEMDATA
diff --git a/trunk/test/rubygems/test_config.rb b/trunk/test/rubygems/test_config.rb
new file mode 100644
index 0000000000..0568996c4a
--- /dev/null
+++ b/trunk/test/rubygems/test_config.rb
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rbconfig'
+require 'rubygems'
+
+class TestConfig < RubyGemTestCase
+
+ def test_datadir
+ datadir = Config::CONFIG['datadir']
+ assert_equal "#{datadir}/xyz", Config.datadir('xyz')
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem.rb b/trunk/test/rubygems/test_gem.rb
new file mode 100644
index 0000000000..c26eeb573d
--- /dev/null
+++ b/trunk/test/rubygems/test_gem.rb
@@ -0,0 +1,449 @@
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems'
+require 'rubygems/gem_openssl'
+require 'rubygems/installer'
+require 'pathname'
+require 'tmpdir'
+
+class TestGem < RubyGemTestCase
+
+ def setup
+ super
+
+ @additional = %w[a b].map { |d| File.join @tempdir, d }
+ @default_dir_re = %r|/[Rr]uby/[Gg]ems/[0-9.]+|
+ end
+
+ def test_self_all_load_paths
+ util_make_gems
+
+ expected = [
+ File.join(@gemhome, *%W[gems #{@a1.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@a2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@a_evil9.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@b2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@c1_2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@pl1.full_name} lib]),
+ ]
+
+ assert_equal expected, Gem.all_load_paths.sort
+ end
+
+ def test_self_available?
+ util_make_gems
+ assert(Gem.available?("a"))
+ assert(Gem.available?("a", "1"))
+ assert(Gem.available?("a", ">1"))
+ assert(!Gem.available?("monkeys"))
+ end
+
+ def test_self_bindir
+ assert_equal File.join(@gemhome, 'bin'), Gem.bindir
+ assert_equal File.join(@gemhome, 'bin'), Gem.bindir(Gem.dir)
+ assert_equal File.join(@gemhome, 'bin'), Gem.bindir(Pathname.new(Gem.dir))
+ end
+
+ def test_self_bindir_default_dir
+ default = Gem.default_dir
+ bindir = (defined? RUBY_FRAMEWORK_VERSION) ? '/usr/bin' : Config::CONFIG['bindir']
+ assert_equal bindir, Gem.bindir(default)
+ assert_equal bindir, Gem.bindir(Pathname.new(default))
+ end
+
+ def test_self_clear_paths
+ Gem.dir
+ Gem.path
+ searcher = Gem.searcher
+ source_index = Gem.source_index
+
+ Gem.clear_paths
+
+ assert_equal nil, Gem.instance_variable_get(:@gem_home)
+ assert_equal nil, Gem.instance_variable_get(:@gem_path)
+ assert_not_equal searcher, Gem.searcher
+ assert_not_equal source_index.object_id, Gem.source_index.object_id
+ end
+
+ def test_self_configuration
+ expected = Gem::ConfigFile.new []
+ Gem.configuration = nil
+
+ assert_equal expected, Gem.configuration
+ end
+
+ def test_self_datadir
+ foo = nil
+
+ Dir.chdir @tempdir do
+ FileUtils.mkdir_p 'data'
+ File.open File.join('data', 'foo.txt'), 'w' do |fp|
+ fp.puts 'blah'
+ end
+
+ foo = quick_gem 'foo' do |s| s.files = %w[data/foo.txt] end
+ install_gem foo
+ end
+
+ Gem.source_index = nil
+
+ gem 'foo'
+
+ expected = File.join @gemhome, 'gems', foo.full_name, 'data', 'foo'
+
+ assert_equal expected, Gem.datadir('foo')
+ end
+
+ def test_self_datadir_nonexistent_package
+ assert_nil Gem.datadir('xyzzy')
+ end
+
+ def test_self_default_dir
+ assert_match @default_dir_re, Gem.default_dir
+ end
+
+ def test_self_default_exec_format
+ orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
+ Gem::ConfigMap[:BASERUBY] = 'ruby'
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'ruby'
+
+ assert_equal '%s', Gem.default_exec_format
+ ensure
+ Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ end
+
+ def test_self_default_exec_format_18
+ orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
+ Gem::ConfigMap[:BASERUBY] = 'ruby'
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'ruby18'
+
+ assert_equal '%s18', Gem.default_exec_format
+ ensure
+ Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ end
+
+ def test_self_default_exec_format_jruby
+ orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
+ Gem::ConfigMap[:BASERUBY] = 'ruby'
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'jruby'
+
+ assert_equal 'j%s', Gem.default_exec_format
+ ensure
+ Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
+ Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ end
+
+ def test_self_default_sources
+ assert_equal %w[http://gems.rubyforge.org/], Gem.default_sources
+ end
+
+ def test_self_dir
+ assert_equal @gemhome, Gem.dir
+
+ Gem::DIRECTORIES.each do |filename|
+ assert File.directory?(File.join(Gem.dir, filename)),
+ "expected #{filename} to exist"
+ end
+ end
+
+ def test_self_ensure_gem_directories
+ FileUtils.rm_r @gemhome
+ Gem.use_paths @gemhome
+
+ Gem.ensure_gem_subdirectories @gemhome
+
+ assert File.directory?(File.join(@gemhome, "cache"))
+ end
+
+ def test_self_ensure_gem_directories_missing_parents
+ gemdir = File.join @tempdir, 'a/b/c/gemdir'
+ FileUtils.rm_rf File.join(@tempdir, 'a') rescue nil
+ assert !File.exist?(File.join(@tempdir, 'a')),
+ "manually remove #{File.join @tempdir, 'a'}, tests are broken"
+ Gem.use_paths gemdir
+
+ Gem.ensure_gem_subdirectories gemdir
+
+ assert File.directory?("#{gemdir}/cache")
+ end
+
+ unless win_platform? then # only for FS that support write protection
+ def test_self_ensure_gem_directories_write_protected
+ gemdir = File.join @tempdir, "egd"
+ FileUtils.rm_r gemdir rescue nil
+ assert !File.exist?(gemdir), "manually remove #{gemdir}, tests are broken"
+ FileUtils.mkdir_p gemdir
+ FileUtils.chmod 0400, gemdir
+ Gem.use_paths gemdir
+
+ Gem.ensure_gem_subdirectories gemdir
+
+ assert !File.exist?("#{gemdir}/cache")
+ ensure
+ FileUtils.chmod 0600, gemdir
+ end
+
+ def test_self_ensure_gem_directories_write_protected_parents
+ parent = File.join(@tempdir, "egd")
+ gemdir = "#{parent}/a/b/c"
+
+ FileUtils.rm_r parent rescue nil
+ assert !File.exist?(parent), "manually remove #{parent}, tests are broken"
+ FileUtils.mkdir_p parent
+ FileUtils.chmod 0400, parent
+ Gem.use_paths(gemdir)
+
+ Gem.ensure_gem_subdirectories gemdir
+
+ assert !File.exist?("#{gemdir}/cache")
+ ensure
+ FileUtils.chmod 0600, parent
+ end
+ end
+
+ def test_ensure_ssl_available
+ orig_Gem_ssl_available = Gem.ssl_available?
+
+ Gem.ssl_available = true
+ assert_nothing_raised do Gem.ensure_ssl_available end
+
+ Gem.ssl_available = false
+ e = assert_raise Gem::Exception do Gem.ensure_ssl_available end
+ assert_equal 'SSL is not installed on this system', e.message
+ ensure
+ Gem.ssl_available = orig_Gem_ssl_available
+ end
+
+ def test_self_latest_load_paths
+ util_make_gems
+
+ expected = [
+ File.join(@gemhome, *%W[gems #{@a2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@a_evil9.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@b2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@c1_2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@pl1.full_name} lib]),
+ ]
+
+ assert_equal expected, Gem.latest_load_paths.sort
+ end
+
+ def test_self_loaded_specs
+ foo = quick_gem 'foo'
+ install_gem foo
+ Gem.source_index = nil
+
+ Gem.activate 'foo'
+
+ assert_equal true, Gem.loaded_specs.keys.include?('foo')
+ end
+
+ def test_self_path
+ assert_equal [Gem.dir], Gem.path
+ end
+
+ def test_self_path_default
+ if defined? APPLE_GEM_HOME
+ orig_APPLE_GEM_HOME = APPLE_GEM_HOME
+ Object.send :remove_const, :APPLE_GEM_HOME
+ end
+ Gem.instance_variable_set :@gem_path, nil
+
+ assert_equal [Gem.default_path, Gem.dir].flatten, Gem.path
+ ensure
+ Object.const_set :APPLE_GEM_HOME, orig_APPLE_GEM_HOME
+ end
+
+ unless win_platform?
+ def test_self_path_APPLE_GEM_HOME
+ Gem.clear_paths
+ Dir.mktmpdir("apple_gem_home") {|d|
+ Gem.const_set :APPLE_GEM_HOME, d
+ assert Gem.path.include?(d)
+ }
+ ensure
+ Gem.send :remove_const, :APPLE_GEM_HOME
+ end
+
+ def test_self_path_APPLE_GEM_HOME_GEM_PATH
+ Gem.clear_paths
+ ENV['GEM_PATH'] = @gemhome
+ Gem.const_set :APPLE_GEM_HOME, '/tmp/apple_gem_home'
+
+ assert !Gem.path.include?('/tmp/apple_gem_home')
+ ensure
+ Gem.send :remove_const, :APPLE_GEM_HOME
+ end
+ end
+
+ def test_self_path_ENV_PATH
+ Gem.send :set_paths, nil
+ path_count = Gem.path.size
+ Gem.clear_paths
+
+ ENV['GEM_PATH'] = @additional.join(File::PATH_SEPARATOR)
+
+ assert_equal @additional, Gem.path[0,2]
+
+ assert_equal path_count + @additional.size, Gem.path.size,
+ "extra path components: #{Gem.path[2..-1].inspect}"
+ assert_match Gem.dir, Gem.path.last
+ end
+
+ def test_self_path_duplicate
+ Gem.clear_paths
+ util_ensure_gem_dirs
+ dirs = @additional + [@gemhome] + [File.join(@tempdir, 'a')]
+
+ ENV['GEM_HOME'] = @gemhome
+ ENV['GEM_PATH'] = dirs.join File::PATH_SEPARATOR
+
+ assert_equal @gemhome, Gem.dir
+
+ paths = [Gem.dir]
+ assert_equal @additional + paths, Gem.path
+ end
+
+ def test_self_path_overlap
+ Gem.clear_paths
+
+ util_ensure_gem_dirs
+ ENV['GEM_HOME'] = @gemhome
+ ENV['GEM_PATH'] = @additional.join(File::PATH_SEPARATOR)
+
+ assert_equal @gemhome, Gem.dir
+
+ paths = [Gem.dir]
+ assert_equal @additional + paths, Gem.path
+ end
+
+ def test_self_platforms
+ assert_equal [Gem::Platform::RUBY, Gem::Platform.local], Gem.platforms
+ end
+
+ def test_self_prefix
+ file_name = File.expand_path __FILE__
+
+ prefix = File.dirname File.dirname(file_name)
+ prefix = File.dirname prefix if File.basename(prefix) == 'test'
+
+ assert_equal prefix, Gem.prefix
+ end
+
+ def test_self_prefix_libdir
+ orig_libdir = Gem::ConfigMap[:libdir]
+
+ file_name = File.expand_path __FILE__
+ prefix = File.dirname File.dirname(file_name)
+ prefix = File.dirname prefix if File.basename(prefix) == 'test'
+
+ Gem::ConfigMap[:libdir] = prefix
+
+ assert_nil Gem.prefix
+ ensure
+ Gem::ConfigMap[:libdir] = orig_libdir
+ end
+
+ def test_self_prefix_sitelibdir
+ orig_sitelibdir = Gem::ConfigMap[:sitelibdir]
+
+ file_name = File.expand_path __FILE__
+ prefix = File.dirname File.dirname(file_name)
+ prefix = File.dirname prefix if File.basename(prefix) == 'test'
+
+ Gem::ConfigMap[:sitelibdir] = prefix
+
+ assert_nil Gem.prefix
+ ensure
+ Gem::ConfigMap[:sitelibdir] = orig_sitelibdir
+ end
+
+ def test_self_refresh
+ util_make_gems
+
+ a1_spec = File.join @gemhome, "specifications", "#{@a1.full_name}.gemspec"
+
+ FileUtils.mv a1_spec, @tempdir
+
+ assert !Gem.source_index.gems.include?(@a1.full_name)
+
+ FileUtils.mv File.join(@tempdir, "#{@a1.full_name}.gemspec"), a1_spec
+
+ Gem.refresh
+
+ assert Gem.source_index.gems.include?(@a1.full_name)
+ assert_equal nil, Gem.instance_variable_get(:@searcher)
+ end
+
+ def test_self_required_location
+ util_make_gems
+
+ assert_equal File.join(@tempdir, *%w[gemhome gems c-1.2 lib code.rb]),
+ Gem.required_location("c", "code.rb")
+ assert_equal File.join(@tempdir, *%w[gemhome gems a-1 lib code.rb]),
+ Gem.required_location("a", "code.rb", "< 2")
+ assert_equal File.join(@tempdir, *%w[gemhome gems a-2 lib code.rb]),
+ Gem.required_location("a", "code.rb", "= 2")
+ end
+
+ def test_self_ruby_version
+ version = RUBY_VERSION.dup
+ version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+
+ assert_equal Gem::Version.new(version), Gem.ruby_version
+ end
+
+ def test_self_searcher
+ assert_kind_of Gem::GemPathSearcher, Gem.searcher
+ end
+
+ def test_self_source_index
+ assert_kind_of Gem::SourceIndex, Gem.source_index
+ end
+
+ def test_self_sources
+ assert_equal %w[http://gems.example.com/], Gem.sources
+ end
+
+ def test_ssl_available_eh
+ orig_Gem_ssl_available = Gem.ssl_available?
+
+ Gem.ssl_available = true
+ assert_equal true, Gem.ssl_available?
+
+ Gem.ssl_available = false
+ assert_equal false, Gem.ssl_available?
+ ensure
+ Gem.ssl_available = orig_Gem_ssl_available
+ end
+
+ def test_self_use_paths
+ util_ensure_gem_dirs
+
+ Gem.use_paths @gemhome, @additional
+
+ assert_equal @gemhome, Gem.dir
+ assert_equal @additional + [Gem.dir], Gem.path
+ end
+
+ def test_self_user_home
+ if ENV['HOME'] then
+ assert_equal ENV['HOME'], Gem.user_home
+ else
+ assert true, 'count this test'
+ end
+ end
+
+ def util_ensure_gem_dirs
+ Gem.ensure_gem_subdirectories @gemhome
+ @additional.each do |dir|
+ Gem.ensure_gem_subdirectories @gemhome
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_builder.rb b/trunk/test/rubygems/test_gem_builder.rb
new file mode 100644
index 0000000000..31a0d71880
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_builder.rb
@@ -0,0 +1,34 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/builder'
+
+class TestGemBuilder < RubyGemTestCase
+
+ def test_build
+ builder = Gem::Builder.new quick_gem('a')
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ builder.build
+ end
+ end
+
+ assert_match %r|Successfully built RubyGem\n Name: a|, @ui.output
+ end
+
+ def test_build_validates
+ builder = Gem::Builder.new Gem::Specification.new
+
+ assert_raises Gem::InvalidSpecificationException do
+ builder.build
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_command.rb b/trunk/test/rubygems/test_gem_command.rb
new file mode 100644
index 0000000000..9ed57b3692
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_command.rb
@@ -0,0 +1,196 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/command'
+
+class Gem::Command
+ public :parser
+end
+
+class TestGemCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @xopt = nil
+
+ Gem::Command.common_options.clear
+ Gem::Command.common_options << [
+ ['-x', '--exe', 'Execute'], lambda do |*a|
+ @xopt = true
+ end
+ ]
+
+ @cmd_name = 'doit'
+ @cmd = Gem::Command.new @cmd_name, 'summary'
+ end
+
+ def test_self_add_specific_extra_args
+ added_args = %w[--all]
+ @cmd.add_option '--all' do |v,o| end
+
+ Gem::Command.add_specific_extra_args @cmd_name, added_args
+
+ assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
+
+ h = @cmd.add_extra_args []
+
+ assert_equal added_args, h
+ end
+
+ def test_self_add_specific_extra_args_unknown
+ added_args = %w[--definitely_not_there]
+
+ Gem::Command.add_specific_extra_args @cmd_name, added_args
+
+ assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
+
+ h = @cmd.add_extra_args []
+
+ assert_equal [], h
+ end
+
+ def test_add_option_overlapping_common_and_local_options
+ @cmd.add_option('-x', '--zip', 'BAD!') do end
+ @cmd.add_option('-z', '--exe', 'BAD!') do end
+ @cmd.add_option('-x', '--exe', 'BAD!') do end
+
+ assert_match %r|-x, --zip|, @cmd.parser.to_s
+ assert_match %r|-z, --exe|, @cmd.parser.to_s
+ assert_no_match %r|-x, --exe|, @cmd.parser.to_s
+ end
+
+ def test_basic_accessors
+ assert_equal "doit", @cmd.command
+ assert_equal "gem doit", @cmd.program_name
+ assert_equal "summary", @cmd.summary
+ end
+
+ def test_common_option_in_class
+ assert Array === Gem::Command.common_options
+ end
+
+ def test_defaults
+ @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
+ options[:help] = value
+ end
+
+ @cmd.defaults = { :help => true }
+
+ @cmd.when_invoked do |options|
+ assert options[:help], "Help options should default true"
+ end
+
+ use_ui @ui do
+ @cmd.invoke
+ end
+
+ assert_match %r|Usage: gem doit|, @ui.output
+ end
+
+ def test_invoke
+ done = false
+ @cmd.when_invoked { done = true }
+
+ use_ui @ui do
+ @cmd.invoke
+ end
+
+ assert done
+ end
+
+ def test_invode_with_bad_options
+ use_ui @ui do
+ @cmd.when_invoked do true end
+
+ ex = assert_raise(OptionParser::InvalidOption) do
+ @cmd.invoke('-zzz')
+ end
+
+ assert_match(/invalid option:/, ex.message)
+ end
+ end
+
+ def test_invoke_with_common_options
+ @cmd.when_invoked do true end
+
+ use_ui @ui do
+ @cmd.invoke "-x"
+ end
+
+ assert @xopt, "Should have done xopt"
+ end
+
+ # Returning false from the command handler invokes the usage output.
+ def test_invoke_with_help
+ done = false
+
+ use_ui @ui do
+ @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
+ options[:help] = true
+ done = true
+ end
+
+ @cmd.invoke('--help')
+
+ assert done
+ end
+
+ assert_match(/Usage/, @ui.output)
+ assert_match(/gem doit/, @ui.output)
+ assert_match(/\[options\]/, @ui.output)
+ assert_match(/-h/, @ui.output)
+ assert_match(/--help \[COMMAND\]/, @ui.output)
+ assert_match(/Get help on COMMAND/, @ui.output)
+ assert_match(/-x/, @ui.output)
+ assert_match(/--exe/, @ui.output)
+ assert_match(/Execute/, @ui.output)
+ assert_match(/Common Options:/, @ui.output)
+ end
+
+ def test_invoke_with_options
+ @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
+ options[:help] = true
+ end
+
+ @cmd.when_invoked do |opts|
+ assert opts[:help]
+ end
+
+ use_ui @ui do
+ @cmd.invoke '-h'
+ end
+
+ assert_match %r|Usage: gem doit|, @ui.output
+ end
+
+ def test_option_recognition
+ @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
+ options[:help] = true
+ end
+ @cmd.add_option('-f', '--file FILE', 'File option') do |value, options|
+ options[:help] = true
+ end
+ assert @cmd.handles?(['-x'])
+ assert @cmd.handles?(['-h'])
+ assert @cmd.handles?(['-h', 'command'])
+ assert @cmd.handles?(['--help', 'command'])
+ assert @cmd.handles?(['-f', 'filename'])
+ assert @cmd.handles?(['--file=filename'])
+ assert ! @cmd.handles?(['-z'])
+ assert ! @cmd.handles?(['-f'])
+ assert ! @cmd.handles?(['--toothpaste'])
+
+ args = ['-h', 'command']
+ @cmd.handles?(args)
+ assert_equal ['-h', 'command'], args
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_command_manager.rb b/trunk/test/rubygems/test_gem_command_manager.rb
new file mode 100644
index 0000000000..ee58e89844
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_command_manager.rb
@@ -0,0 +1,213 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/command_manager'
+
+class InterruptCommand < Gem::Command
+
+ def initialize
+ super('interrupt', 'Raises an Interrupt Exception', {})
+ end
+
+ def execute
+ raise Interrupt, "Interrupt exception"
+ end
+
+end
+
+class TestGemCommandManager < RubyGemTestCase
+
+ def setup
+ super
+
+ @command_manager = Gem::CommandManager.new
+ end
+
+ def test_run_interrupt
+ use_ui @ui do
+ @command_manager.register_command :interrupt
+ assert_raises MockGemUi::TermError do
+ @command_manager.run 'interrupt'
+ end
+ assert_equal '', ui.output
+ assert_equal "ERROR: Interrupted\n", ui.error
+ end
+ end
+
+ def test_process_args_bad_arg
+ use_ui @ui do
+ assert_raises(MockGemUi::TermError) {
+ @command_manager.process_args("--bad-arg")
+ }
+ end
+
+ assert_match(/invalid option: --bad-arg/i, @ui.error)
+ end
+
+ def test_process_args_install
+ #capture all install options
+ use_ui @ui do
+ check_options = nil
+ @command_manager['install'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("install")
+ assert_equal false, check_options[:test]
+ assert_equal true, check_options[:generate_rdoc]
+ assert_equal false, check_options[:force]
+ assert_equal :both, check_options[:domain]
+ assert_equal true, check_options[:wrappers]
+ assert_equal Gem::Requirement.default, check_options[:version]
+ assert_equal nil, check_options[:install_dir]
+ assert_equal nil, check_options[:bin_dir]
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args(
+ "install --force --test --local --rdoc --install-dir . --version 3.0 --no-wrapper --bindir . ")
+ assert_equal true, check_options[:test]
+ assert_equal true, check_options[:generate_rdoc]
+ assert_equal true, check_options[:force]
+ assert_equal :local, check_options[:domain]
+ assert_equal false, check_options[:wrappers]
+ assert_equal Gem::Requirement.new('3.0'), check_options[:version]
+ assert_equal Dir.pwd, check_options[:install_dir]
+ assert_equal Dir.pwd, check_options[:bin_dir]
+
+ #check remote domain
+ check_options = nil
+ @command_manager.process_args("install --remote")
+ assert_equal :remote, check_options[:domain]
+
+ #check both domain
+ check_options = nil
+ @command_manager.process_args("install --both")
+ assert_equal :both, check_options[:domain]
+
+ #check both domain
+ check_options = nil
+ @command_manager.process_args("install --both")
+ assert_equal :both, check_options[:domain]
+ end
+ end
+
+ def test_process_args_uninstall
+ #capture all uninstall options
+ check_options = nil
+ @command_manager['uninstall'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("uninstall")
+ assert_equal Gem::Requirement.default, check_options[:version]
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args("uninstall foobar --version 3.0")
+ assert_equal "foobar", check_options[:args].first
+ assert_equal Gem::Requirement.new('3.0'), check_options[:version]
+ end
+
+ def test_process_args_check
+ #capture all check options
+ check_options = nil
+ @command_manager['check'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("check")
+ assert_equal false, check_options[:verify]
+ assert_equal false, check_options[:alien]
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args("check --verify foobar --alien")
+ assert_equal "foobar", check_options[:verify]
+ assert_equal true, check_options[:alien]
+ end
+
+ def test_process_args_build
+ #capture all build options
+ check_options = nil
+ @command_manager['build'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("build")
+ #NOTE: Currently no defaults
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args("build foobar.rb")
+ assert_equal 'foobar.rb', check_options[:args].first
+ end
+
+ def test_process_args_query
+ #capture all query options
+ check_options = nil
+ @command_manager['query'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("query")
+ assert_equal(//, check_options[:name])
+ assert_equal :local, check_options[:domain]
+ assert_equal false, check_options[:details]
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args("query --name foobar --local --details")
+ assert_equal(/foobar/i, check_options[:name])
+ assert_equal :local, check_options[:domain]
+ assert_equal true, check_options[:details]
+
+ #remote domain
+ check_options = nil
+ @command_manager.process_args("query --remote")
+ assert_equal :remote, check_options[:domain]
+
+ #both (local/remote) domains
+ check_options = nil
+ @command_manager.process_args("query --both")
+ assert_equal :both, check_options[:domain]
+ end
+
+ def test_process_args_update
+ #capture all update options
+ check_options = nil
+ @command_manager['update'].when_invoked do |options|
+ check_options = options
+ true
+ end
+
+ #check defaults
+ @command_manager.process_args("update")
+ assert_equal true, check_options[:generate_rdoc]
+
+ #check settings
+ check_options = nil
+ @command_manager.process_args("update --force --test --rdoc --install-dir .")
+ assert_equal true, check_options[:test]
+ assert_equal true, check_options[:generate_rdoc]
+ assert_equal true, check_options[:force]
+ assert_equal Dir.pwd, check_options[:install_dir]
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_build_command.rb b/trunk/test/rubygems/test_gem_commands_build_command.rb
new file mode 100644
index 0000000000..a49b6777bd
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_build_command.rb
@@ -0,0 +1,75 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/build_command'
+require 'rubygems/format'
+
+class TestGemCommandsBuildCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @gem = quick_gem 'some_gem' do |s|
+ s.rubyforge_project = 'example'
+ end
+
+ @cmd = Gem::Commands::BuildCommand.new
+ end
+
+ def test_execute
+ gemspec_file = File.join(@tempdir, "#{@gem.full_name}.gemspec")
+
+ File.open gemspec_file, 'w' do |gs|
+ gs.write @gem.to_ruby
+ end
+
+ util_test_build_gem @gem, gemspec_file
+ end
+
+ def test_execute_yaml
+ gemspec_file = File.join(@tempdir, "#{@gem.full_name}.gemspec")
+
+ File.open gemspec_file, 'w' do |gs|
+ gs.write @gem.to_yaml
+ end
+
+ util_test_build_gem @gem, gemspec_file
+ end
+
+ def test_execute_bad_gem
+ @cmd.options[:args] = %w[some_gem]
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal '', @ui.output
+ assert_equal "ERROR: Gemspec file not found: some_gem\n", @ui.error
+ end
+
+ def util_test_build_gem(gem, gemspec_file)
+ @cmd.options[:args] = [gemspec_file]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ output = @ui.output.split "\n"
+ assert_equal " Successfully built RubyGem", output.shift
+ assert_equal " Name: some_gem", output.shift
+ assert_equal " Version: 2", output.shift
+ assert_equal " File: some_gem-2.gem", output.shift
+ assert_equal [], output
+ assert_equal '', @ui.error
+
+ gem_file = File.join @tempdir, "#{gem.full_name}.gem"
+ assert File.exist?(gem_file)
+
+ spec = Gem::Format.from_file_by_path(gem_file).spec
+
+ assert_equal "some_gem", spec.name
+ assert_equal "this is a summary", spec.summary
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_cert_command.rb b/trunk/test/rubygems/test_gem_commands_cert_command.rb
new file mode 100644
index 0000000000..79e3e72158
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_cert_command.rb
@@ -0,0 +1,126 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+
+require 'rubygems/commands/cert_command'
+
+unless defined? OpenSSL then
+ warn "`gem cert` tests are being skipped, module OpenSSL not found"
+end
+
+class TestGemCommandsCertCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @orig_security_trust_dir = Gem::Security::OPT[:trust_dir]
+ Gem::Security::OPT[:trust_dir] = @tempdir
+
+ @cmd = Gem::Commands::CertCommand.new
+
+ root = File.expand_path(File.dirname(__FILE__))
+
+ FileUtils.cp File.join(root, 'data', 'gem-private_key.pem'), @tempdir
+ FileUtils.cp File.join(root, 'data', 'gem-public_cert.pem'), @tempdir
+
+ @cert_file_name = File.join @tempdir, 'gem-public_cert.pem'
+ @pkey_file_name = File.join @tempdir, 'gem-private_key.pem'
+ end
+
+ def teardown
+ Gem::Security::OPT[:trust_dir] = @orig_security_trust_dir
+
+ super
+ end
+
+ def test_execute_add
+ use_ui @ui do
+ @cmd.send :handle_options, %W[--add #{@cert_file_name}]
+ end
+
+ assert_equal "Added '/CN=rubygems/DC=example/DC=com'\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_build
+ FileUtils.rm @cert_file_name
+ FileUtils.rm @pkey_file_name
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.send :handle_options, %W[--build nobody@example.com]
+ end
+ end
+
+ output = @ui.output.split "\n"
+
+ assert_equal 'Public Cert: gem-public_cert.pem', output.shift
+ assert_equal 'Private Key: gem-private_key.pem', output.shift
+ assert_equal 'Don\'t forget to move the key file to somewhere private...',
+ output.shift
+ assert_equal [], output
+
+ assert_equal '', @ui.error
+
+ assert File.exist?(File.join(@tempdir, 'gem-private_key.pem'))
+ assert File.exist?(File.join(@tempdir, 'gem-public_cert.pem'))
+ end
+
+ def test_execute_certificate
+ use_ui @ui do
+ @cmd.send :handle_options, %W[--certificate #{@cert_file_name}]
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal File.read(@cert_file_name),
+ Gem::Security::OPT[:issuer_cert].to_s
+ end
+
+ def test_execute_list
+ use_ui @ui do
+ @cmd.send :handle_options, %W[--list]
+ end
+
+ assert_equal "/CN=rubygems/DC=example/DC=com\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_private_key
+ use_ui @ui do
+ @cmd.send :handle_options, %W[--private-key #{@pkey_file_name}]
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal File.read(@pkey_file_name),
+ Gem::Security::OPT[:issuer_key].to_s
+ end
+
+ def test_execute_remove
+ use_ui @ui do
+ @cmd.send :handle_options, %W[--remove rubygems]
+ end
+
+ assert_equal "Removed '/CN=rubygems/DC=example/DC=com'\n", @ui.output
+ assert_equal '', @ui.error
+
+ assert !File.exist?(@cert_file_name)
+ end
+
+ def test_execute_sign
+ use_ui @ui do
+ @cmd.send :handle_options, %W[
+ -K #{@pkey_file_name} -C #{@cert_file_name} --sign #{@cert_file_name}
+ ]
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+
+ # HACK this test sucks
+ end
+
+end if defined? OpenSSL
+
diff --git a/trunk/test/rubygems/test_gem_commands_check_command.rb b/trunk/test/rubygems/test_gem_commands_check_command.rb
new file mode 100644
index 0000000000..eea7cc5cfa
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_check_command.rb
@@ -0,0 +1,25 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/check_command'
+
+class TestGemCommandsCheckCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::CheckCommand.new
+ end
+
+ def test_initialize
+ assert_equal "check", @cmd.command
+ assert_equal "gem check", @cmd.program_name
+ assert_match(/Check/, @cmd.summary)
+ end
+
+end
diff --git a/trunk/test/rubygems/test_gem_commands_contents_command.rb b/trunk/test/rubygems/test_gem_commands_contents_command.rb
new file mode 100644
index 0000000000..cdb89673da
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_contents_command.rb
@@ -0,0 +1,92 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/contents_command'
+
+class TestGemCommandsContentsCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::ContentsCommand.new
+ end
+
+ def test_execute
+ @cmd.options[:args] = %w[foo]
+ quick_gem 'foo' do |gem|
+ gem.files = %w[lib/foo.rb Rakefile]
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|lib/foo\.rb|, @ui.output
+ assert_match %r|Rakefile|, @ui.output
+ assert_equal "", @ui.error
+ end
+
+ def test_execute_bad_gem
+ @cmd.options[:args] = %w[foo]
+
+ assert_raise MockGemUi::TermError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_match %r|Unable to find gem 'foo' in default gem paths|, @ui.output
+ assert_match %r|Directories searched:|, @ui.output
+ assert_equal "", @ui.error
+ end
+
+ def test_execute_exact_match
+ @cmd.options[:args] = %w[foo]
+ quick_gem 'foo' do |gem|
+ gem.files = %w[lib/foo.rb Rakefile]
+ end
+
+ quick_gem 'foo_bar' do |gem|
+ gem.files = %w[lib/foo_bar.rb Rakefile]
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|lib/foo\.rb|, @ui.output
+ assert_match %r|Rakefile|, @ui.output
+ assert_equal "", @ui.error
+ end
+
+ def test_execute_lib_only
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:lib_only] = true
+
+ quick_gem 'foo' do |gem|
+ gem.files = %w[lib/foo.rb Rakefile]
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|lib/foo\.rb|, @ui.output
+ assert_no_match %r|Rakefile|, @ui.output
+
+ assert_equal "", @ui.error
+ end
+
+ def test_handle_options
+ assert_equal false, @cmd.options[:lib_only]
+ assert_equal [], @cmd.options[:specdirs]
+ assert_equal nil, @cmd.options[:version]
+
+ @cmd.send :handle_options, %w[-l -s foo --version 0.0.2]
+
+ assert_equal true, @cmd.options[:lib_only]
+ assert_equal %w[foo], @cmd.options[:specdirs]
+ assert_equal Gem::Requirement.new('0.0.2'), @cmd.options[:version]
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_dependency_command.rb b/trunk/test/rubygems/test_gem_commands_dependency_command.rb
new file mode 100644
index 0000000000..e143110a71
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_dependency_command.rb
@@ -0,0 +1,227 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/dependency_command'
+
+class TestGemCommandsDependencyCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::DependencyCommand.new
+ @cmd.options[:domain] = :local
+
+ util_setup_fake_fetcher
+ end
+
+ def test_execute
+ quick_gem 'foo' do |gem|
+ gem.add_dependency 'bar', '> 1'
+ end
+
+ Gem.source_index = nil
+
+ @cmd.options[:args] = %w[foo]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "Gem foo-2\n bar (> 1, runtime)\n\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_no_args
+ Gem.source_index = nil
+
+ @cmd.options[:args] = []
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+Gem a-1
+
+Gem a-2
+
+Gem a_evil-9
+
+Gem b-2
+
+Gem c-1.2
+
+Gem pl-1-x86-linux
+
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_no_match
+ @cmd.options[:args] = %w[foo]
+
+ assert_raise MockGemUi::TermError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal "No gems found matching foo (>= 0)\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_pipe_format
+ quick_gem 'foo' do |gem|
+ gem.add_dependency 'bar', '> 1'
+ end
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:pipe_format] = true
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "bar --version '> 1'\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_regexp
+ Gem.source_index = nil
+
+ @cmd.options[:args] = %w[/[ab]/]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+Gem a-1
+
+Gem a-2
+
+Gem a_evil-9
+
+Gem b-2
+
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_reverse
+ quick_gem 'foo' do |gem|
+ gem.add_dependency 'bar', '> 1'
+ end
+
+ quick_gem 'baz' do |gem|
+ gem.add_dependency 'foo'
+ end
+
+ Gem.source_index = nil
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:reverse_dependencies] = true
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+Gem foo-2
+ bar (> 1, runtime)
+ Used by
+ baz-2 (foo (>= 0, runtime))
+
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_reverse_remote
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:reverse_dependencies] = true
+ @cmd.options[:domain] = :remote
+
+ assert_raise MockGemUi::TermError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ expected = <<-EOF
+ERROR: Only reverse dependencies for local gems are supported.
+ EOF
+
+ assert_equal '', @ui.output
+ assert_equal expected, @ui.error
+ end
+
+ def test_execute_remote
+ foo = quick_gem 'foo' do |gem|
+ gem.add_dependency 'bar', '> 1'
+ end
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ util_setup_spec_fetcher foo
+
+ FileUtils.rm File.join(@gemhome, 'specifications',
+ "#{foo.full_name}.gemspec")
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:domain] = :remote
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "Gem foo-2\n bar (> 1, runtime)\n\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_remote_legacy
+ foo = quick_gem 'foo' do |gem|
+ gem.add_dependency 'bar', '> 1'
+ end
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ Gem::SpecFetcher.fetcher = nil
+ si = util_setup_source_info_cache foo
+
+ @fetcher.data["#{@gem_repo}yaml"] = YAML.dump si
+ @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] =
+ si.dump
+
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] = nil
+
+ FileUtils.rm File.join(@gemhome, 'specifications',
+ "#{foo.full_name}.gemspec")
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:domain] = :remote
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "Gem foo-2\n bar (> 1, runtime)\n\n", @ui.output
+
+ expected = <<-EOF
+WARNING: RubyGems 1.2+ index not found for:
+\t#{@gem_repo}
+
+RubyGems will revert to legacy indexes degrading performance.
+ EOF
+
+ assert_equal expected, @ui.error
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_environment_command.rb b/trunk/test/rubygems/test_gem_commands_environment_command.rb
new file mode 100644
index 0000000000..78246b0301
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_environment_command.rb
@@ -0,0 +1,134 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/environment_command'
+
+class TestGemCommandsEnvironmentCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::EnvironmentCommand.new
+ end
+
+ def test_execute
+ orig_sources = Gem.sources.dup
+ Gem.sources.replace %w[http://gems.example.com]
+
+ @cmd.send :handle_options, %w[]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|RUBYGEMS VERSION: (\d\.)+\d|, @ui.output
+ assert_match %r|RUBY VERSION: \d\.\d\.\d \(.*\) \[.*\]|, @ui.output
+ assert_match %r|INSTALLATION DIRECTORY: #{Regexp.escape @gemhome}|,
+ @ui.output
+ assert_match %r|RUBYGEMS PREFIX: |, @ui.output
+ assert_match %r|RUBY EXECUTABLE:.*#{Gem::ConfigMap[:RUBY_INSTALL_NAME]}|,
+ @ui.output
+ assert_match %r|EXECUTABLE DIRECTORY:|, @ui.output
+ assert_match %r|RUBYGEMS PLATFORMS:|, @ui.output
+ assert_match %r|- #{Gem::Platform.local}|, @ui.output
+ assert_match %r|GEM PATHS:|, @ui.output
+ assert_match %r|- #{Regexp.escape @gemhome}|, @ui.output
+ assert_match %r|GEM CONFIGURATION:|, @ui.output
+ assert_match %r|:verbose => |, @ui.output
+ assert_match %r|REMOTE SOURCES:|, @ui.output
+ assert_equal '', @ui.error
+
+ ensure
+ Gem.sources.replace orig_sources
+ end
+
+ def test_execute_gemdir
+ @cmd.send :handle_options, %w[gemdir]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#{@gemhome}\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_gempath
+ @cmd.send :handle_options, %w[gempath]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#{@gemhome}\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_gempath_multiple
+ Gem.clear_paths
+ path = [@gemhome, "#{@gemhome}2"].join File::PATH_SEPARATOR
+ ENV['GEM_PATH'] = path
+
+ @cmd.send :handle_options, %w[gempath]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#{Gem.path.join File::PATH_SEPARATOR}\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_packageversion
+ @cmd.send :handle_options, %w[packageversion]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#{Gem::RubyGemsPackageVersion}\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_remotesources
+ orig_sources = Gem.sources.dup
+ Gem.sources.replace %w[http://gems.example.com]
+
+ @cmd.send :handle_options, %w[remotesources]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "http://gems.example.com\n", @ui.output
+ assert_equal '', @ui.error
+
+ ensure
+ Gem.sources.replace orig_sources
+ end
+
+ def test_execute_unknown
+ @cmd.send :handle_options, %w[unknown]
+
+ assert_raise Gem::CommandLineError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_version
+ @cmd.send :handle_options, %w[version]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#{Gem::RubyGemsVersion}\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_fetch_command.rb b/trunk/test/rubygems/test_gem_commands_fetch_command.rb
new file mode 100644
index 0000000000..eaa13595b7
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_fetch_command.rb
@@ -0,0 +1,55 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/package'
+require 'rubygems/security'
+require 'rubygems/commands/fetch_command'
+
+class TestGemCommandsFetchCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::FetchCommand.new
+ end
+
+ def test_execute
+ util_setup_fake_fetcher
+ util_setup_spec_fetcher @a2
+
+ @fetcher.data["#{@gem_repo}gems/#{@a2.full_name}.gem"] =
+ File.read(File.join(@gemhome, 'cache', "#{@a2.full_name}.gem"))
+
+ @cmd.options[:args] = [@a2.name]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, "#{@a2.full_name}.gem")),
+ "#{@a2.full_name} fetched"
+ end
+
+ def test_execute_legacy
+ util_setup_fake_fetcher
+ util_setup_source_info_cache @a2
+
+ @fetcher.data["#{@gem_repo}yaml"] = ''
+ @fetcher.data["#{@gem_repo}gems/#{@a2.full_name}.gem"] =
+ File.read(File.join(@gemhome, 'cache', "#{@a2.full_name}.gem"))
+
+ @cmd.options[:args] = [@a2.name]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, "#{@a2.full_name}.gem")),
+ "#{@a2.full_name} fetched"
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_generate_index_command.rb b/trunk/test/rubygems/test_gem_commands_generate_index_command.rb
new file mode 100644
index 0000000000..548197841b
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_generate_index_command.rb
@@ -0,0 +1,32 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/indexer'
+require 'rubygems/commands/generate_index_command'
+
+class TestGemCommandsGenerateIndexCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::GenerateIndexCommand.new
+ @cmd.options[:directory] = @gemhome
+ end
+
+ def test_execute
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ yaml = File.join @gemhome, 'yaml'
+ yaml_z = File.join @gemhome, 'yaml.Z'
+ quick_index = File.join @gemhome, 'quick', 'index'
+ quick_index_rz = File.join @gemhome, 'quick', 'index.rz'
+
+ assert File.exist?(yaml), yaml
+ assert File.exist?(yaml_z), yaml_z
+ assert File.exist?(quick_index), quick_index
+ assert File.exist?(quick_index_rz), quick_index_rz
+ end
+
+end if ''.respond_to? :to_xs
+
diff --git a/trunk/test/rubygems/test_gem_commands_install_command.rb b/trunk/test/rubygems/test_gem_commands_install_command.rb
new file mode 100644
index 0000000000..ef04072b93
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_install_command.rb
@@ -0,0 +1,166 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/install_command'
+
+class TestGemCommandsInstallCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::InstallCommand.new
+ @cmd.options[:generate_rdoc] = false
+ @cmd.options[:generate_ri] = false
+ end
+
+ def test_execute_include_dependencies
+ @cmd.options[:include_dependencies] = true
+ @cmd.options[:args] = []
+
+ assert_raise Gem::CommandLineError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ output = @ui.output.split "\n"
+ assert_equal "INFO: `gem install -y` is now default and will be removed",
+ output.shift
+ assert_equal "INFO: use --ignore-dependencies to install only the gems you list",
+ output.shift
+ assert output.empty?, output.inspect
+ end
+
+ def test_execute_local
+ util_setup_fake_fetcher
+ @cmd.options[:domain] = :local
+
+ FileUtils.mv File.join(@gemhome, 'cache', "#{@a2.full_name}.gem"),
+ File.join(@tempdir)
+
+ @cmd.options[:args] = [@a2.name]
+
+ use_ui @ui do
+ orig_dir = Dir.pwd
+ begin
+ Dir.chdir @tempdir
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 0, e.exit_code
+ ensure
+ Dir.chdir orig_dir
+ end
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "1 gem installed", out.shift
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_local_missing
+ util_setup_fake_fetcher
+ @cmd.options[:domain] = :local
+
+ @cmd.options[:args] = %w[no_such_gem]
+
+ use_ui @ui do
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 2, e.exit_code
+ end
+
+ # HACK no repository was checked
+ assert_equal "ERROR: could not find gem no_such_gem locally or in a repository\n",
+ @ui.error
+ end
+
+ def test_execute_no_gem
+ @cmd.options[:args] = %w[]
+
+ assert_raise Gem::CommandLineError do
+ @cmd.execute
+ end
+ end
+
+ def test_execute_nonexistent
+ util_setup_fake_fetcher
+ util_setup_spec_fetcher
+
+ @cmd.options[:args] = %w[nonexistent]
+
+ use_ui @ui do
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 2, e.exit_code
+ end
+
+ assert_equal "ERROR: could not find gem nonexistent locally or in a repository\n",
+ @ui.error
+ end
+
+ def test_execute_remote
+ @cmd.options[:generate_rdoc] = true
+ @cmd.options[:generate_ri] = true
+
+ util_setup_fake_fetcher
+ util_setup_spec_fetcher @a2
+
+ @fetcher.data["#{@gem_repo}gems/#{@a2.full_name}.gem"] =
+ read_binary(File.join(@gemhome, 'cache', "#{@a2.full_name}.gem"))
+
+ @cmd.options[:args] = [@a2.name]
+
+ use_ui @ui do
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 0, e.exit_code
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "1 gem installed", out.shift
+ assert_equal "Installing ri documentation for #{@a2.full_name}...",
+ out.shift
+ assert_equal "Installing RDoc documentation for #{@a2.full_name}...",
+ out.shift
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_two
+ util_setup_fake_fetcher
+ @cmd.options[:domain] = :local
+
+ FileUtils.mv File.join(@gemhome, 'cache', "#{@a2.full_name}.gem"),
+ File.join(@tempdir)
+
+ FileUtils.mv File.join(@gemhome, 'cache', "#{@b2.full_name}.gem"),
+ File.join(@tempdir)
+
+ @cmd.options[:args] = [@a2.name, @b2.name]
+
+ use_ui @ui do
+ orig_dir = Dir.pwd
+ begin
+ Dir.chdir @tempdir
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 0, e.exit_code
+ ensure
+ Dir.chdir orig_dir
+ end
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "Successfully installed #{@b2.full_name}", out.shift
+ assert_equal "2 gems installed", out.shift
+ assert out.empty?, out.inspect
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_mirror_command.rb b/trunk/test/rubygems/test_gem_commands_mirror_command.rb
new file mode 100644
index 0000000000..359ba5481e
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_mirror_command.rb
@@ -0,0 +1,61 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/indexer'
+require 'rubygems/commands/mirror_command'
+
+class TestGemCommandsMirrorCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::MirrorCommand.new
+ end
+
+ def test_execute
+ util_make_gems
+
+ gems_dir = File.join @tempdir, 'gems'
+ mirror = File.join @tempdir, 'mirror'
+
+ FileUtils.mkdir_p gems_dir
+ FileUtils.mkdir_p mirror
+
+ Dir[File.join(@gemhome, 'cache', '*.gem')].each do |gem|
+ FileUtils.mv gem, gems_dir
+ end
+
+ use_ui @ui do
+ Gem::Indexer.new(@tempdir).generate_index
+ end
+
+ orig_HOME = ENV['HOME']
+ ENV['HOME'] = @tempdir
+ Gem.instance_variable_set :@user_home, nil
+
+ File.open File.join(Gem.user_home, '.gemmirrorrc'), 'w' do |fp|
+ fp.puts "---"
+ # tempdir could be a drive+path (under windows)
+ if @tempdir.match(/[a-z]:/i)
+ fp.puts "- from: file:///#{@tempdir}"
+ else
+ fp.puts "- from: file://#{@tempdir}"
+ end
+ fp.puts " to: #{mirror}"
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert File.exist?(File.join(mirror, 'gems', "#{@a1.full_name}.gem"))
+ assert File.exist?(File.join(mirror, 'gems', "#{@a2.full_name}.gem"))
+ assert File.exist?(File.join(mirror, 'gems', "#{@b2.full_name}.gem"))
+ assert File.exist?(File.join(mirror, 'gems', "#{@c1_2.full_name}.gem"))
+ assert File.exist?(File.join(mirror, "Marshal.#{@marshal_version}"))
+ ensure
+ orig_HOME.nil? ? ENV.delete('HOME') : ENV['HOME'] = orig_HOME
+ Gem.instance_variable_set :@user_home, nil
+ end
+
+end if ''.respond_to? :to_xs
+
diff --git a/trunk/test/rubygems/test_gem_commands_outdated_command.rb b/trunk/test/rubygems/test_gem_commands_outdated_command.rb
new file mode 100644
index 0000000000..a6668c01fc
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_outdated_command.rb
@@ -0,0 +1,43 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/outdated_command'
+
+class TestGemCommandsOutdatedCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::OutdatedCommand.new
+ end
+
+ def test_initialize
+ assert @cmd.handles?(%W[--platform #{Gem::Platform.local}])
+ end
+
+ def test_execute
+ local_01 = quick_gem 'foo', '0.1'
+ local_02 = quick_gem 'foo', '0.2'
+ remote_10 = quick_gem 'foo', '1.0'
+ remote_20 = quick_gem 'foo', '2.0'
+
+ remote_spec_file = File.join @gemhome, 'specifications',
+ remote_10.full_name + ".gemspec"
+ FileUtils.rm remote_spec_file
+
+ remote_spec_file = File.join @gemhome, 'specifications',
+ remote_20.full_name + ".gemspec"
+ FileUtils.rm remote_spec_file
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ util_setup_spec_fetcher remote_10, remote_20
+
+ use_ui @ui do @cmd.execute end
+
+ assert_equal "foo (0.2 < 2.0)\n", @ui.output
+ assert_equal "", @ui.error
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_pristine_command.rb b/trunk/test/rubygems/test_gem_commands_pristine_command.rb
new file mode 100644
index 0000000000..d5d2d7f339
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_pristine_command.rb
@@ -0,0 +1,109 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/pristine_command'
+
+class TestGemCommandsPristineCommand < RubyGemTestCase
+
+ def setup
+ super
+ @cmd = Gem::Commands::PristineCommand.new
+ end
+
+ def test_execute
+ a = quick_gem 'a' do |s| s.executables = %w[foo] end
+ FileUtils.mkdir_p File.join(@tempdir, 'bin')
+ File.open File.join(@tempdir, 'bin', 'foo'), 'w' do |fp|
+ fp.puts "#!/usr/bin/ruby"
+ end
+
+ install_gem a
+
+ foo_path = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
+
+ File.open foo_path, 'w' do |io|
+ io.puts 'I changed it!'
+ end
+
+ @cmd.options[:args] = %w[a]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "#!/usr/bin/ruby\n", File.read(foo_path), foo_path
+
+ out = @ui.output.split "\n"
+
+ assert_equal "Restoring gem(s) to pristine condition...", out.shift
+ assert_equal "Restored #{a.full_name}", out.shift
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_all
+ a = quick_gem 'a' do |s| s.executables = %w[foo] end
+ FileUtils.mkdir_p File.join(@tempdir, 'bin')
+ File.open File.join(@tempdir, 'bin', 'foo'), 'w' do |fp|
+ fp.puts "#!/usr/bin/ruby"
+ end
+
+ install_gem a
+
+ gem_bin = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
+
+ FileUtils.rm gem_bin
+
+ @cmd.handle_options %w[--all]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert File.exist?(gem_bin)
+
+ out = @ui.output.split "\n"
+
+ assert_equal "Restoring gem(s) to pristine condition...", out.shift
+ assert_equal "Restored #{a.full_name}", out.shift
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_missing_cache_gem
+ a = quick_gem 'a' do |s| s.executables = %w[foo] end
+ FileUtils.mkdir_p File.join(@tempdir, 'bin')
+ File.open File.join(@tempdir, 'bin', 'foo'), 'w' do |fp|
+ fp.puts "#!/usr/bin/ruby"
+ end
+
+ install_gem a
+
+ FileUtils.rm File.join(@gemhome, 'cache', "#{a.full_name}.gem")
+
+ @cmd.options[:args] = %w[a]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+
+ assert_equal "Restoring gem\(s\) to pristine condition...", out.shift
+ assert out.empty?, out.inspect
+
+ assert_equal "ERROR: Cached gem for #{a.full_name} not found, use `gem install` to restore\n",
+ @ui.error
+ end
+
+ def test_execute_no_gem
+ @cmd.options[:args] = %w[]
+
+ e = assert_raise Gem::CommandLineError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_match %r|specify a gem name|, e.message
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_query_command.rb b/trunk/test/rubygems/test_gem_commands_query_command.rb
new file mode 100644
index 0000000000..1b65fc7633
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_query_command.rb
@@ -0,0 +1,288 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/query_command'
+
+class TestGemCommandsQueryCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::QueryCommand.new
+
+ util_setup_fake_fetcher
+
+ @si = util_setup_spec_fetcher @a1, @a2, @pl1
+
+ @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] = proc do
+ raise Gem::RemoteFetcher::FetchError
+ end
+ end
+
+ def test_execute
+ @cmd.handle_options %w[-r]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** REMOTE GEMS ***
+
+a (2)
+pl (1)
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_all
+ a1_name = @a1.full_name
+ a2_name = @a2.full_name
+
+ @cmd.handle_options %w[-r --all]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** REMOTE GEMS ***
+
+a (2, 1)
+pl (1)
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_details
+ @a2.summary = 'This is a lot of text. ' * 4
+ @a2.authors = ['Abraham Lincoln', 'Hirohito']
+ @a2.homepage = 'http://a.example.com/'
+ @a2.rubyforge_project = 'rubygems'
+
+ @si = util_setup_spec_fetcher @a1, @a2, @pl1
+
+ @cmd.handle_options %w[-r -d]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** REMOTE GEMS ***
+
+a (2)
+ Authors: Abraham Lincoln, Hirohito
+ Rubyforge: http://rubyforge.org/projects/rubygems
+ Homepage: http://a.example.com/
+
+ This is a lot of text. This is a lot of text. This is a lot of text.
+ This is a lot of text.
+
+pl (1)
+ Author: A User
+ Homepage: http://example.com
+
+ this is a summary
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_installed
+ @cmd.handle_options %w[-n c --installed]
+
+ e = assert_raise Gem::SystemExitException do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal 0, e.exit_code
+
+ assert_equal "true\n", @ui.output
+
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_installed_no_name
+ @cmd.handle_options %w[--installed]
+
+ e = assert_raise Gem::SystemExitException do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal '', @ui.output
+ assert_equal "ERROR: You must specify a gem name\n", @ui.error
+
+ assert_equal 4, e.exit_code
+ end
+
+ def test_execute_installed_not_installed
+ @cmd.handle_options %w[-n not_installed --installed]
+
+ e = assert_raise Gem::SystemExitException do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal "false\n", @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal 1, e.exit_code
+ end
+
+ def test_execute_installed_version
+ @cmd.handle_options %w[-n c --installed --version 1.2]
+
+ e = assert_raise Gem::SystemExitException do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal "true\n", @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal 0, e.exit_code
+ end
+
+ def test_execute_installed_version_not_installed
+ @cmd.handle_options %w[-n c --installed --version 2]
+
+ e = assert_raise Gem::SystemExitException do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal "false\n", @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal 1, e.exit_code
+ end
+
+ def test_execute_legacy
+ Gem::SpecFetcher.fetcher = nil
+ si = util_setup_source_info_cache @a1, @a2, @pl1
+
+ @fetcher.data["#{@gem_repo}yaml"] = YAML.dump si
+ @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] =
+ si.dump
+
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] = nil
+
+ @cmd.handle_options %w[-r]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** REMOTE GEMS ***
+
+a (2)
+pl (1)
+ EOF
+
+ assert_equal expected, @ui.output
+
+ expected = <<-EOF
+WARNING: RubyGems 1.2+ index not found for:
+\t#{@gem_repo}
+
+RubyGems will revert to legacy indexes degrading performance.
+ EOF
+
+ assert_equal expected, @ui.error
+ end
+
+ def test_execute_local_details
+ @a2.summary = 'This is a lot of text. ' * 4
+ @a2.authors = ['Abraham Lincoln', 'Hirohito']
+ @a2.homepage = 'http://a.example.com/'
+ @a2.rubyforge_project = 'rubygems'
+
+ @cmd.handle_options %w[--local --details]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** LOCAL GEMS ***
+
+a (2, 1)
+ Author: A User
+ Homepage: http://example.com
+ Installed at (2): #{@gemhome}
+ (1): #{@gemhome}
+
+ this is a summary
+
+a_evil (9)
+ Author: A User
+ Homepage: http://example.com
+ Installed at: #{@gemhome}
+
+ this is a summary
+
+b (2)
+ Author: A User
+ Homepage: http://example.com
+ Installed at: #{@gemhome}
+
+ this is a summary
+
+c (1.2)
+ Author: A User
+ Homepage: http://example.com
+ Installed at: #{@gemhome}
+
+ this is a summary
+
+pl (1)
+ Author: A User
+ Homepage: http://example.com
+ Installed at: #{@gemhome}
+
+ this is a summary
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_no_versions
+ @cmd.handle_options %w[-r --no-versions]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+
+*** REMOTE GEMS ***
+
+a
+pl
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_server_command.rb b/trunk/test/rubygems/test_gem_commands_server_command.rb
new file mode 100644
index 0000000000..2985b036d8
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_server_command.rb
@@ -0,0 +1,27 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/server_command'
+
+class TestGemCommandsServerCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::ServerCommand.new
+ end
+
+ def test_handle_options
+ @cmd.send :handle_options, %w[-p 8808 --no-daemon]
+
+ assert_equal false, @cmd.options[:daemon]
+ assert_equal @gemhome, @cmd.options[:gemdir]
+ assert_equal 8808, @cmd.options[:port]
+
+ @cmd.send :handle_options, %w[-p 9999 -d /nonexistent --daemon]
+
+ assert_equal true, @cmd.options[:daemon]
+ assert_equal File.expand_path('/nonexistent'), @cmd.options[:gemdir]
+ assert_equal 9999, @cmd.options[:port]
+ end
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_sources_command.rb b/trunk/test/rubygems/test_gem_commands_sources_command.rb
new file mode 100644
index 0000000000..623c732e50
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_sources_command.rb
@@ -0,0 +1,275 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/sources_command'
+
+class TestGemCommandsSourcesCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::SourcesCommand.new
+
+ @new_repo = "http://beta-gems.example.com"
+ end
+
+ def test_execute
+ util_setup_spec_fetcher
+ @cmd.handle_options []
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+*** CURRENT SOURCES ***
+
+#{@gem_repo}
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_add
+ util_setup_fake_fetcher
+
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ specs = si.map do |_, spec|
+ [spec.name, spec.version, spec.original_platform]
+ end
+
+ specs_dump_gz = StringIO.new
+ Zlib::GzipWriter.wrap specs_dump_gz do |io|
+ Marshal.dump specs, io
+ end
+
+ @fetcher.data["#{@new_repo}/specs.#{@marshal_version}.gz"] =
+ specs_dump_gz.string
+
+ @cmd.handle_options %W[--add #{@new_repo}]
+
+ util_setup_spec_fetcher
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal [@gem_repo, @new_repo], Gem.sources
+
+ expected = <<-EOF
+#{@new_repo} added to sources
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_add_nonexistent_source
+ util_setup_fake_fetcher
+
+ uri = "http://beta-gems.example.com/specs.#{@marshal_version}.gz"
+ @fetcher.data[uri] = proc do
+ raise Gem::RemoteFetcher::FetchError.new('it died', uri)
+ end
+
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ @cmd.handle_options %w[--add http://beta-gems.example.com]
+
+ util_setup_spec_fetcher
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+Error fetching http://beta-gems.example.com:
+\tit died (#{uri})
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_add_bad_uri
+ @cmd.handle_options %w[--add beta-gems.example.com]
+
+ util_setup_spec_fetcher
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal [@gem_repo], Gem.sources
+
+ expected = <<-EOF
+beta-gems.example.com is not a URI
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_add_legacy
+ util_setup_fake_fetcher
+ util_setup_source_info_cache
+
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ @fetcher.data["#{@new_repo}/yaml"] = ''
+
+ @cmd.handle_options %W[--add #{@new_repo}]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal [@gem_repo], Gem.sources
+
+ expected = <<-EOF
+WARNING: RubyGems 1.2+ index not found for:
+\t#{@new_repo}
+
+Will cause RubyGems to revert to legacy indexes, degrading performance.
+ EOF
+
+ assert_equal "#{@new_repo} added to sources\n", @ui.output
+ assert_equal expected, @ui.error
+ end
+
+ def test_execute_clear_all
+ @cmd.handle_options %w[--clear-all]
+
+ util_setup_source_info_cache
+
+ cache = Gem::SourceInfoCache.cache
+ cache.update
+ cache.write_cache
+
+ assert File.exist?(cache.system_cache_file),
+ 'system cache file'
+ assert File.exist?(cache.latest_system_cache_file),
+ 'latest system cache file'
+
+ util_setup_spec_fetcher
+
+ fetcher = Gem::SpecFetcher.fetcher
+
+ # HACK figure out how to force directory creation via fetcher
+ #assert File.directory?(fetcher.dir), 'cache dir exists'
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+*** Removed specs cache ***
+*** Removed user source cache ***
+*** Removed latest user source cache ***
+*** Removed system source cache ***
+*** Removed latest system source cache ***
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+
+ assert !File.exist?(cache.system_cache_file),
+ 'system cache file'
+ assert !File.exist?(cache.latest_system_cache_file),
+ 'latest system cache file'
+
+ assert !File.exist?(fetcher.dir), 'cache dir removed'
+ end
+
+ def test_execute_remove
+ @cmd.handle_options %W[--remove #{@gem_repo}]
+
+ util_setup_spec_fetcher
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = "#{@gem_repo} removed from sources\n"
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_remove_no_network
+ @cmd.handle_options %W[--remove #{@gem_repo}]
+
+ util_setup_fake_fetcher
+
+ @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] = proc do
+ raise Gem::RemoteFetcher::FetchError
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = "#{@gem_repo} removed from sources\n"
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_update
+ @cmd.handle_options %w[--update]
+
+ util_setup_fake_fetcher
+ source_index = util_setup_spec_fetcher @a1
+
+ specs = source_index.map do |name, spec|
+ [spec.name, spec.version, spec.original_platform]
+ end
+
+ @fetcher.data["#{@gem_repo}specs.#{Gem.marshal_version}.gz"] =
+ util_gzip Marshal.dump(specs)
+
+ latest_specs = source_index.latest_specs.map do |spec|
+ [spec.name, spec.version, spec.original_platform]
+ end
+
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] =
+ util_gzip Marshal.dump(latest_specs)
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "source cache successfully updated\n", @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_update_legacy
+ @cmd.handle_options %w[--update]
+
+ util_setup_fake_fetcher
+ util_setup_source_info_cache
+ Gem::SourceInfoCache.reset
+
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ @fetcher.data["#{@gem_repo}yaml"] = YAML.dump si
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ expected = <<-EOF
+Bulk updating Gem source index for: #{@gem_repo}
+source cache successfully updated
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_specification_command.rb b/trunk/test/rubygems/test_gem_commands_specification_command.rb
new file mode 100644
index 0000000000..7ac0429f32
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_specification_command.rb
@@ -0,0 +1,97 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/specification_command'
+
+class TestGemCommandsSpecificationCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::SpecificationCommand.new
+ end
+
+ def test_execute
+ foo = quick_gem 'foo'
+ Gem.source_index.add_spec foo
+
+ @cmd.options[:args] = %w[foo]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|Gem::Specification|, @ui.output
+ assert_match %r|name: foo|, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_all
+ foo1 = quick_gem 'foo', '0.0.1'
+ foo2 = quick_gem 'foo', '0.0.2'
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:all] = true
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|Gem::Specification|, @ui.output
+ assert_match %r|name: foo|, @ui.output
+ assert_match %r|version: 0.0.1|, @ui.output
+ assert_match %r|version: 0.0.2|, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_bad_name
+ @cmd.options[:args] = %w[foo]
+
+ assert_raise MockGemUi::TermError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_equal '', @ui.output
+ assert_equal "ERROR: Unknown gem 'foo'\n", @ui.error
+ end
+
+ def test_execute_exact_match
+ foo = quick_gem 'foo'
+ foo_bar = quick_gem 'foo_bar'
+
+ @cmd.options[:args] = %w[foo]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|Gem::Specification|, @ui.output
+ assert_match %r|name: foo|, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_execute_remote
+ foo = quick_gem 'foo'
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ util_setup_spec_fetcher foo
+
+ FileUtils.rm File.join(@gemhome, 'specifications',
+ "#{foo.full_name}.gemspec")
+
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:domain] = :remote
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_match %r|\A--- !ruby/object:Gem::Specification|, @ui.output
+ assert_match %r|name: foo|, @ui.output
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_stale_command.rb b/trunk/test/rubygems/test_gem_commands_stale_command.rb
new file mode 100644
index 0000000000..6f66c854a5
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_stale_command.rb
@@ -0,0 +1,39 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/stale_command'
+
+class TestGemCommandsStaleCommand < RubyGemTestCase
+
+ def setup
+ super
+ @cmd = Gem::Commands::StaleCommand.new
+ end
+
+ def test_execute_sorts
+ files = %w[lib/foo_bar.rb Rakefile]
+ foo_bar = quick_gem 'foo_bar' do |gem|
+ gem.files = files
+ end
+ bar_baz = quick_gem 'bar_baz' do |gem|
+ gem.files = files
+ end
+
+ files.each do |file|
+ filename = bar_baz.full_gem_path + "/#{file}"
+ FileUtils.mkdir_p(File.dirname(filename))
+ FileUtils.touch(filename, :mtime => Time.now)
+
+ filename = foo_bar.full_gem_path + "/#{file}"
+ FileUtils.mkdir_p(File.dirname(filename))
+ FileUtils.touch(filename, :mtime => Time.now - 86400)
+ end
+
+ use_ui @ui do
+ @cmd.execute
+ end
+ lines = @ui.output.split("\n")
+ assert_equal("#{foo_bar.name}-#{foo_bar.version}", lines[0].split.first)
+ assert_equal("#{bar_baz.name}-#{bar_baz.version}", lines[1].split.first)
+ end
+
+end
diff --git a/trunk/test/rubygems/test_gem_commands_unpack_command.rb b/trunk/test/rubygems/test_gem_commands_unpack_command.rb
new file mode 100644
index 0000000000..3a62a914a4
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_unpack_command.rb
@@ -0,0 +1,115 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/unpack_command'
+
+class TestGemCommandsUnpackCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ Dir.chdir @tempdir do
+ @cmd = Gem::Commands::UnpackCommand.new
+ end
+ end
+
+ def test_execute
+ util_make_gems
+
+ @cmd.options[:args] = %w[a]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, 'a-2'))
+ end
+
+ def test_execute_gem_path
+ util_make_gems
+
+ Gem.clear_paths
+
+ gemhome2 = File.join @tempdir, 'gemhome2'
+
+ Gem.send :set_paths, [gemhome2, @gemhome].join(File::PATH_SEPARATOR)
+ Gem.send :set_home, gemhome2
+
+ @cmd.options[:args] = %w[a]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, 'a-2'))
+ end
+
+ def test_execute_gem_path_missing
+ util_make_gems
+
+ Gem.clear_paths
+
+ gemhome2 = File.join @tempdir, 'gemhome2'
+
+ Gem.send :set_paths, [gemhome2, @gemhome].join(File::PATH_SEPARATOR)
+ Gem.send :set_home, gemhome2
+
+ @cmd.options[:args] = %w[z]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert_equal '', @ui.output
+ end
+
+ def test_execute_with_target_option
+ util_make_gems
+
+ target = 'with_target'
+ @cmd.options[:args] = %w[a]
+ @cmd.options[:target] = target
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, target, 'a-2'))
+ end
+
+ def test_execute_exact_match
+ foo_spec = quick_gem 'foo'
+ foo_bar_spec = quick_gem 'foo_bar'
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ Gem::Builder.new(foo_spec).build
+ Gem::Builder.new(foo_bar_spec).build
+ end
+ end
+
+ foo_path = File.join(@tempdir, "#{foo_spec.full_name}.gem")
+ foo_bar_path = File.join(@tempdir, "#{foo_bar_spec.full_name}.gem")
+ Gem::Installer.new(foo_path).install
+ Gem::Installer.new(foo_bar_path).install
+
+ @cmd.options[:args] = %w[foo]
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ @cmd.execute
+ end
+ end
+
+ assert File.exist?(File.join(@tempdir, foo_spec.full_name))
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_commands_update_command.rb b/trunk/test/rubygems/test_gem_commands_update_command.rb
new file mode 100644
index 0000000000..11da1f8a83
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_commands_update_command.rb
@@ -0,0 +1,165 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/commands/update_command'
+
+class TestGemCommandsUpdateCommand < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::UpdateCommand.new
+
+ util_setup_fake_fetcher
+
+ @a1_path = File.join @gemhome, 'cache', "#{@a1.full_name}.gem"
+ @a2_path = File.join @gemhome, 'cache', "#{@a2.full_name}.gem"
+
+ util_setup_spec_fetcher @a1, @a2
+
+ @fetcher.data["#{@gem_repo}gems/#{@a1.full_name}.gem"] =
+ read_binary @a1_path
+ @fetcher.data["#{@gem_repo}gems/#{@a2.full_name}.gem"] =
+ read_binary @a2_path
+ end
+
+ def test_execute
+ util_clear_gems
+
+ Gem::Installer.new(@a1_path).install
+
+ @cmd.options[:args] = []
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Updating installed gems", out.shift
+ assert_equal "Updating #{@a2.name}", out.shift
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "Gems updated: #{@a2.name}", out.shift
+
+ assert out.empty?, out.inspect
+ end
+
+ # before:
+ # a1 -> c1.2
+ # after:
+ # a2 -> b2 # new dependency
+ # a2 -> c2
+
+ def test_execute_dependencies
+ @a1.add_dependency 'c', '1.2'
+
+ @c2 = quick_gem 'c', '2' do |s|
+ s.files = %w[lib/code.rb]
+ s.require_paths = %w[lib]
+ end
+
+ @a2.add_dependency 'c', '2'
+ @a2.add_dependency 'b', '2'
+
+ @b2_path = File.join @gemhome, 'cache', "#{@b2.full_name}.gem"
+ @c1_2_path = File.join @gemhome, 'cache', "#{@c1_2.full_name}.gem"
+ @c2_path = File.join @gemhome, 'cache', "#{@c2.full_name}.gem"
+
+ @source_index = Gem::SourceIndex.new
+ @source_index.add_spec @a1
+ @source_index.add_spec @a2
+ @source_index.add_spec @b2
+ @source_index.add_spec @c1_2
+ @source_index.add_spec @c2
+
+ util_build_gem @a1
+ util_build_gem @a2
+ util_build_gem @c2
+
+ @fetcher.data["#{@gem_repo}gems/#{@a1.full_name}.gem"] = read_binary @a1_path
+ @fetcher.data["#{@gem_repo}gems/#{@a2.full_name}.gem"] = read_binary @a2_path
+ @fetcher.data["#{@gem_repo}gems/#{@b2.full_name}.gem"] = read_binary @b2_path
+ @fetcher.data["#{@gem_repo}gems/#{@c1_2.full_name}.gem"] =
+ read_binary @c1_2_path
+ @fetcher.data["#{@gem_repo}gems/#{@c2.full_name}.gem"] = read_binary @c2_path
+
+ util_setup_spec_fetcher @a1, @a2, @b2, @c1_2, @c2
+ util_clear_gems
+
+ Gem::Installer.new(@c1_2_path).install
+ Gem::Installer.new(@a1_path).install
+
+ @cmd.options[:args] = []
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Updating installed gems", out.shift
+ assert_equal "Updating #{@a2.name}", out.shift
+ assert_equal "Successfully installed #{@c2.full_name}", out.shift
+ assert_equal "Successfully installed #{@b2.full_name}", out.shift
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "Gems updated: #{@c2.name}, #{@b2.name}, #{@a2.name}",
+ out.shift
+
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_named
+ util_clear_gems
+
+ Gem::Installer.new(@a1_path).install
+
+ @cmd.options[:args] = [@a1.name]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Updating installed gems", out.shift
+ assert_equal "Updating #{@a2.name}", out.shift
+ assert_equal "Successfully installed #{@a2.full_name}", out.shift
+ assert_equal "Gems updated: #{@a2.name}", out.shift
+
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_named_up_to_date
+ util_clear_gems
+
+ Gem::Installer.new(@a2_path).install
+
+ @cmd.options[:args] = [@a2.name]
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Updating installed gems", out.shift
+ assert_equal "Nothing to update", out.shift
+
+ assert out.empty?, out.inspect
+ end
+
+ def test_execute_up_to_date
+ util_clear_gems
+
+ Gem::Installer.new(@a2_path).install
+
+ @cmd.options[:args] = []
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ out = @ui.output.split "\n"
+ assert_equal "Updating installed gems", out.shift
+ assert_equal "Nothing to update", out.shift
+
+ assert out.empty?, out.inspect
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_config_file.rb b/trunk/test/rubygems/test_gem_config_file.rb
new file mode 100644
index 0000000000..06321d4e7c
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_config_file.rb
@@ -0,0 +1,276 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/config_file'
+
+class TestGemConfigFile < RubyGemTestCase
+
+ def setup
+ super
+
+ @temp_conf = File.join @tempdir, '.gemrc'
+
+ @cfg_args = %W[--config-file #{@temp_conf}]
+
+ @orig_SYSTEM_WIDE_CONFIG_FILE = Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE
+ Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
+ Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
+ File.join(@tempdir, 'system-gemrc')
+ Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
+ Gem::ConfigFile::PLATFORM_DEFAULTS.clear
+
+ util_config_file
+ end
+
+ def teardown
+ Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
+ Gem::ConfigFile::PLATFORM_DEFAULTS.clear
+ Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
+ Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
+ @orig_SYSTEM_WIDE_CONFIG_FILE
+
+ super
+ end
+
+ def test_initialize
+ assert_equal @temp_conf, @cfg.config_file_name
+
+ assert_equal false, @cfg.backtrace
+ assert_equal true, @cfg.update_sources
+ assert_equal false, @cfg.benchmark
+ assert_equal Gem::ConfigFile::DEFAULT_BULK_THRESHOLD, @cfg.bulk_threshold
+ assert_equal true, @cfg.verbose
+ assert_equal [@gem_repo], Gem.sources
+
+ File.open @temp_conf, 'w' do |fp|
+ fp.puts ":backtrace: true"
+ fp.puts ":update_sources: false"
+ fp.puts ":benchmark: true"
+ fp.puts ":bulk_threshold: 10"
+ fp.puts ":verbose: false"
+ fp.puts ":sources:"
+ fp.puts " - http://more-gems.example.com"
+ fp.puts "install: --wrappers"
+ end
+
+ util_config_file
+
+ assert_equal true, @cfg.backtrace
+ assert_equal true, @cfg.benchmark
+ assert_equal 10, @cfg.bulk_threshold
+ assert_equal false, @cfg.verbose
+ assert_equal false, @cfg.update_sources
+ assert_equal %w[http://more-gems.example.com], Gem.sources
+ assert_equal '--wrappers', @cfg[:install]
+ end
+
+ def test_initialize_handle_arguments_config_file
+ util_config_file %W[--config-file #{@temp_conf}]
+
+ assert_equal @temp_conf, @cfg.config_file_name
+ end
+
+ def test_initialize_handle_arguments_config_file_with_other_params
+ util_config_file %W[--config-file #{@temp_conf} --backtrace]
+
+ assert_equal @temp_conf, @cfg.config_file_name
+ end
+
+ def test_initialize_handle_arguments_config_file_equals
+ util_config_file %W[--config-file=#{@temp_conf}]
+
+ assert_equal @temp_conf, @cfg.config_file_name
+ end
+
+ def test_initialize_operating_system_override
+ Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS[:bulk_threshold] = 1
+ Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS['install'] = '--no-env-shebang'
+
+ Gem::ConfigFile::PLATFORM_DEFAULTS[:bulk_threshold] = 2
+
+ util_config_file
+
+ assert_equal 2, @cfg.bulk_threshold
+ assert_equal '--no-env-shebang', @cfg[:install]
+ end
+
+ def test_initialize_platform_override
+ Gem::ConfigFile::PLATFORM_DEFAULTS[:bulk_threshold] = 2
+ Gem::ConfigFile::PLATFORM_DEFAULTS['install'] = '--no-env-shebang'
+
+ File.open Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE, 'w' do |fp|
+ fp.puts ":bulk_threshold: 3"
+ end
+
+ util_config_file
+
+ assert_equal 3, @cfg.bulk_threshold
+ assert_equal '--no-env-shebang', @cfg[:install]
+ end
+
+ def test_initialize_system_wide_override
+ File.open Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE, 'w' do |fp|
+ fp.puts ":backtrace: false"
+ fp.puts ":bulk_threshold: 2048"
+ end
+
+ File.open @temp_conf, 'w' do |fp|
+ fp.puts ":backtrace: true"
+ end
+
+ util_config_file
+
+ assert_equal 2048, @cfg.bulk_threshold
+ assert_equal true, @cfg.backtrace
+ end
+
+ def test_handle_arguments
+ args = %w[--backtrace --bunch --of --args here]
+
+ @cfg.handle_arguments args
+
+ assert_equal %w[--bunch --of --args here], @cfg.args
+ end
+
+ def test_handle_arguments_backtrace
+ assert_equal false, @cfg.backtrace
+
+ args = %w[--backtrace]
+
+ @cfg.handle_arguments args
+
+ assert_equal true, @cfg.backtrace
+ end
+
+ def test_handle_arguments_benchmark
+ assert_equal false, @cfg.benchmark
+
+ args = %w[--benchmark]
+
+ @cfg.handle_arguments args
+
+ assert_equal true, @cfg.benchmark
+ end
+
+ def test_handle_arguments_debug
+ old_dollar_DEBUG = $DEBUG
+ assert_equal false, $DEBUG
+
+ args = %w[--debug]
+
+ @cfg.handle_arguments args
+
+ assert_equal true, $DEBUG
+ ensure
+ $DEBUG = old_dollar_DEBUG
+ end
+
+ def test_handle_arguments_override
+ File.open @temp_conf, 'w' do |fp|
+ fp.puts ":benchmark: false"
+ end
+
+ util_config_file %W[--benchmark --config-file=#{@temp_conf}]
+
+ assert_equal true, @cfg.benchmark
+ end
+
+ def test_handle_arguments_traceback
+ assert_equal false, @cfg.backtrace
+
+ args = %w[--traceback]
+
+ @cfg.handle_arguments args
+
+ assert_equal true, @cfg.backtrace
+ end
+
+ def test_really_verbose
+ assert_equal false, @cfg.really_verbose
+
+ @cfg.verbose = true
+
+ assert_equal false, @cfg.really_verbose
+
+ @cfg.verbose = 1
+
+ assert_equal true, @cfg.really_verbose
+ end
+
+ def test_write
+ @cfg.backtrace = true
+ @cfg.benchmark = true
+ @cfg.update_sources = false
+ @cfg.bulk_threshold = 10
+ @cfg.verbose = false
+ Gem.sources.replace %w[http://more-gems.example.com]
+ @cfg[:install] = '--wrappers'
+
+ @cfg.write
+
+ util_config_file
+
+ # These should not be written out to the config file.
+ assert_equal false, @cfg.backtrace, 'backtrace'
+ assert_equal false, @cfg.benchmark, 'benchmark'
+ assert_equal Gem::ConfigFile::DEFAULT_BULK_THRESHOLD, @cfg.bulk_threshold,
+ 'bulk_threshold'
+ assert_equal true, @cfg.update_sources, 'update_sources'
+ assert_equal true, @cfg.verbose, 'verbose'
+
+ assert_equal '--wrappers', @cfg[:install], 'install'
+
+ # this should be written out to the config file.
+ assert_equal %w[http://more-gems.example.com], Gem.sources
+ end
+
+ def test_write_from_hash
+ File.open @temp_conf, 'w' do |fp|
+ fp.puts ":backtrace: true"
+ fp.puts ":benchmark: true"
+ fp.puts ":bulk_threshold: 10"
+ fp.puts ":update_sources: false"
+ fp.puts ":verbose: false"
+ fp.puts ":sources:"
+ fp.puts " - http://more-gems.example.com"
+ fp.puts "install: --wrappers"
+ end
+
+ util_config_file
+
+ @cfg.backtrace = :junk
+ @cfg.benchmark = :junk
+ @cfg.update_sources = :junk
+ @cfg.bulk_threshold = 20
+ @cfg.verbose = :junk
+ Gem.sources.replace %w[http://even-more-gems.example.com]
+ @cfg[:install] = '--wrappers --no-rdoc'
+
+ @cfg.write
+
+ util_config_file
+
+ # These should not be written out to the config file
+ assert_equal true, @cfg.backtrace, 'backtrace'
+ assert_equal true, @cfg.benchmark, 'benchmark'
+ assert_equal 10, @cfg.bulk_threshold, 'bulk_threshold'
+ assert_equal false, @cfg.update_sources, 'update_sources'
+ assert_equal false, @cfg.verbose, 'verbose'
+
+ assert_equal '--wrappers --no-rdoc', @cfg[:install], 'install'
+
+ assert_equal %w[http://even-more-gems.example.com], Gem.sources
+ end
+
+ def util_config_file(args = @cfg_args)
+ @cfg = Gem::ConfigFile.new args
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_dependency.rb b/trunk/test/rubygems/test_gem_dependency.rb
new file mode 100644
index 0000000000..315c49d6ce
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_dependency.rb
@@ -0,0 +1,140 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/version'
+
+class TestGemDependency < RubyGemTestCase
+
+ def setup
+ super
+
+ @pkg1_0 = Gem::Dependency.new 'pkg', ['> 1.0']
+ @pkg1_1 = Gem::Dependency.new 'pkg', ['> 1.1']
+
+ @oth1_0 = Gem::Dependency.new 'other', ['> 1.0']
+
+ @r1_0 = Gem::Requirement.new ['> 1.0']
+ end
+
+ def test_initialize
+ assert_equal "pkg", @pkg1_0.name
+ assert_equal @r1_0, @pkg1_0.version_requirements
+ end
+
+ def test_initialize_double
+ dep = Gem::Dependency.new("pkg", ["> 1.0", "< 2.0"])
+
+ assert_equal Gem::Requirement.new(["> 1.0", "< 2.0"]),
+ dep.version_requirements
+ end
+
+ def test_initialize_empty
+ dep = Gem::Dependency.new("pkg", [])
+ req = @r1_0
+
+ req.instance_eval do
+ @version = ">= 1.0"
+ @op = ">="
+ @nums = [1,0]
+ @requirements = nil
+ end
+
+ dep.instance_eval do
+ @version_requirement = req
+ @version_requirements = nil
+ end
+
+ assert_equal Gem::Requirement.new([">= 1.0"]), dep.version_requirements
+ end
+
+ def test_initialize_version
+ dep = Gem::Dependency.new 'pkg', Gem::Version.new('2')
+
+ assert_equal 'pkg', dep.name
+
+ assert_equal Gem::Requirement.new('= 2'), dep.version_requirements
+ end
+
+ def test_initialize_with_type
+ dep = Gem::Dependency.new("pkg", [], :development)
+ assert_equal(:development, dep.type)
+ end
+
+ def test_type_is_runtime_by_default
+ assert_equal(:runtime, Gem::Dependency.new("pkg", []).type)
+ end
+
+ def test_type_is_restricted
+ assert_raise(ArgumentError) do
+ Gem::Dependency.new("pkg", [:sometimes])
+ end
+ end
+
+ def test_equals2
+ assert_equal @pkg1_0, @pkg1_0.dup
+ assert_equal @pkg1_0.dup, @pkg1_0
+
+ assert_not_equal @pkg1_0, @pkg1_1, "requirements different"
+ assert_not_equal @pkg1_1, @pkg1_0, "requirements different"
+
+ assert_not_equal @pkg1_0, @oth1_0, "names different"
+ assert_not_equal @oth1_0, @pkg1_0, "names different"
+
+ assert_not_equal @pkg1_0, Object.new
+ assert_not_equal Object.new, @pkg1_0
+ end
+
+ def test_equals2_type
+ runtime = Gem::Dependency.new("pkg", [])
+ development = Gem::Dependency.new("pkg", [], :development)
+
+ assert_not_equal(runtime, development)
+ end
+
+ def test_equals_tilde
+ def dep(name, version)
+ Gem::Dependency.new name, version
+ end
+
+ a0 = dep 'a', '0'
+ a1 = dep 'a', '1'
+ b0 = dep 'b', '0'
+
+ pa0 = dep 'a', '>= 0'
+ pa0r = dep(/a/, '>= 0')
+ pab0r = dep(/a|b/, '>= 0')
+
+ assert((a0 =~ a0), 'match self')
+ assert((pa0 =~ a0), 'match version exact')
+ assert((pa0 =~ a1), 'match version')
+ assert((pa0r =~ a0), 'match regex simple')
+ assert((pab0r =~ a0), 'match regex complex')
+
+ assert(!(pa0r =~ b0), 'fail match regex')
+ assert(!(pa0r =~ Object.new), 'fail match Object')
+ end
+
+ def test_hash
+ assert_equal @pkg1_0.hash, @pkg1_0.dup.hash
+ assert_equal @pkg1_0.dup.hash, @pkg1_0.hash
+
+ assert_not_equal @pkg1_0.hash, @pkg1_1.hash, "requirements different"
+ assert_not_equal @pkg1_1.hash, @pkg1_0.hash, "requirements different"
+
+ assert_not_equal @pkg1_0.hash, @oth1_0.hash, "names different"
+ assert_not_equal @oth1_0.hash, @pkg1_0.hash, "names different"
+ end
+
+ def test_hash_type
+ runtime = Gem::Dependency.new("pkg", [])
+ development = Gem::Dependency.new("pkg", [], :development)
+
+ assert_not_equal(runtime.hash, development.hash)
+ end
+end
+
diff --git a/trunk/test/rubygems/test_gem_dependency_installer.rb b/trunk/test/rubygems/test_gem_dependency_installer.rb
new file mode 100644
index 0000000000..10e7fdfbda
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_dependency_installer.rb
@@ -0,0 +1,637 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/dependency_installer'
+
+class TestGemDependencyInstaller < RubyGemTestCase
+
+ def setup
+ super
+
+ @gems_dir = File.join @tempdir, 'gems'
+ @cache_dir = File.join @gemhome, 'cache'
+ FileUtils.mkdir @gems_dir
+
+ write_file File.join('gems', 'a-1', 'bin', 'a_bin') do |fp|
+ fp.puts "#!/usr/bin/ruby"
+ end
+ @a1, @a1_gem = util_gem 'a', '1' do |s| s.executables << 'a_bin' end
+ @aa1, @aa1_gem = util_gem 'aa', '1'
+
+ @b1, @b1_gem = util_gem 'b', '1' do |s|
+ s.add_dependency 'a'
+ s.add_development_dependency 'aa'
+ end
+
+ @d1, @d1_gem = util_gem 'd', '1'
+ @d2, @d2_gem = util_gem 'd', '2'
+
+ @x1_m, @x1_m_gem = util_gem 'x', '1' do |s|
+ s.platform = Gem::Platform.new %w[cpu my_platform 1]
+ end
+
+ @x1_o, @x1_o_gem = util_gem 'x', '1' do |s|
+ s.platform = Gem::Platform.new %w[cpu other_platform 1]
+ end
+
+ @w1, @w1_gem = util_gem 'w', '1' do |s| s.add_dependency 'x' end
+
+ @y1, @y1_gem = util_gem 'y', '1'
+ @y1_1_p, @y1_1_p_gem = util_gem 'y', '1.1' do |s|
+ s.platform = Gem::Platform.new %w[cpu my_platform 1]
+ end
+
+ @z1, @z1_gem = util_gem 'z', '1' do |s| s.add_dependency 'y' end
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ si = util_setup_spec_fetcher @a1, @b1, @d1, @d2, @x1_m, @x1_o, @w1, @y1,
+ @y1_1_p, @z1
+
+ util_clear_gems
+ end
+
+ def test_install
+ FileUtils.mv @a1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'a'
+ end
+
+ assert_equal Gem::SourceIndex.new(@a1.full_name => @a1),
+ Gem::SourceIndex.from_installed_gems
+
+ assert_equal [@a1], inst.installed_gems
+ end
+
+ def test_install_all_dependencies
+ e1, e1_gem = util_gem 'e', '1' do |s|
+ s.add_dependency 'b'
+ end
+
+ util_clear_gems
+
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ FileUtils.mv e1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :ignore_dependencies => true
+ inst.install 'b'
+ end
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'e'
+ end
+
+ assert_equal %w[e-1 a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_cache_dir
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :cache_dir => @tempdir
+ inst.install 'b'
+ end
+
+ assert_equal %w[a-1 b-1], inst.installed_gems.map { |s| s.full_name }
+
+ assert File.exist?(File.join(@tempdir, 'cache', "#{@a1.full_name}.gem"))
+ assert File.exist?(File.join(@tempdir, 'cache', "#{@b1.full_name}.gem"))
+ end
+
+ def test_install_dependencies_satisfied
+ a2, a2_gem = util_gem 'a', '2'
+
+ FileUtils.rm_rf File.join(@gemhome, 'gems')
+ Gem.source_index.refresh!
+
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv a2_gem, @tempdir # not in index
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'a-2'
+ end
+
+ FileUtils.rm File.join(@tempdir, "#{a2.full_name}.gem")
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'b'
+ end
+
+ installed = Gem::SourceIndex.from_installed_gems.map { |n,s| s.full_name }
+
+ assert_equal %w[a-2 b-1], installed.sort
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_dependency
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'b'
+ end
+
+ assert_equal %w[a-1 b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_dependency_development
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @aa1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new(:development => true)
+ inst.install 'b'
+ end
+
+ assert_equal %w[a-1 aa-1 b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_dependency_existing
+ Gem::Installer.new(@a1_gem).install
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'b'
+ end
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_dependency_old
+ e1, e1_gem = util_gem 'e', '1'
+ f1, f1_gem = util_gem 'f', '1' do |s| s.add_dependency 'e' end
+ f2, f2_gem = util_gem 'f', '2'
+
+ FileUtils.mv e1_gem, @tempdir
+ FileUtils.mv f1_gem, @tempdir
+ FileUtils.mv f2_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'f'
+ end
+
+ assert_equal %w[f-2], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_local
+ FileUtils.mv @a1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :domain => :local
+ inst.install 'a-1.gem'
+ end
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_local_dependency
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :domain => :local
+ inst.install 'b-1.gem'
+ end
+
+ assert_equal %w[a-1 b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_local_dependency_installed
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+
+ inst = nil
+
+ Dir.chdir @tempdir do
+ Gem::Installer.new('a-1.gem').install
+
+ inst = Gem::DependencyInstaller.new :domain => :local
+ inst.install 'b-1.gem'
+ end
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_local_subdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :domain => :local
+ inst.install 'gems/a-1.gem'
+ end
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_env_shebang
+ FileUtils.mv @a1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :env_shebang => true, :wrappers => true
+ inst.install 'a'
+ end
+
+ assert_match %r|\A#!/usr/bin/env #{Gem::ConfigMap[:RUBY_INSTALL_NAME]}\n|,
+ File.read(File.join(@gemhome, 'bin', 'a_bin'))
+ end
+
+ def test_install_force
+ FileUtils.mv @b1_gem, @tempdir
+ si = util_setup_spec_fetcher @b1
+ @fetcher.data['http://gems.example.com/gems/yaml'] = si.to_yaml
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :force => true
+ inst.install 'b'
+ end
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_ignore_dependencies
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :ignore_dependencies => true
+ inst.install 'b'
+ end
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_install_dir
+ FileUtils.mv @a1_gem, @tempdir
+ gemhome2 = File.join @tempdir, 'gemhome2'
+ Dir.mkdir gemhome2
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :install_dir => gemhome2
+ inst.install 'a'
+ end
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+
+ assert File.exist?(File.join(gemhome2, 'specifications',
+ "#{@a1.full_name}.gemspec"))
+ assert File.exist?(File.join(gemhome2, 'cache',
+ "#{@a1.full_name}.gem"))
+ end
+
+ def test_install_domain_both
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = a1_data
+
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :domain => :both
+ inst.install 'b'
+ end
+
+ assert_equal %w[a-1 b-1], inst.installed_gems.map { |s| s.full_name }
+ a1, b1 = inst.installed_gems
+
+ a1_expected = File.join(@gemhome, 'specifications',
+ "#{a1.full_name}.gemspec")
+ b1_expected = File.join(@gemhome, 'specifications',
+ "#{b1.full_name}.gemspec")
+
+ assert_equal a1_expected, a1.loaded_from
+ assert_equal b1_expected, b1.loaded_from
+ end
+
+ def test_install_domain_both_no_network
+ @fetcher.data["http://gems.example.com/gems/Marshal.#{@marshal_version}"] =
+ proc do
+ raise Gem::RemoteFetcher::FetchError
+ end
+
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :domain => :both
+ inst.install 'b'
+ end
+
+ assert_equal %w[a-1 b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_domain_local
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ Gem.source_index.gems.delete @a1.full_name
+
+ Dir.chdir @tempdir do
+ e = assert_raise Gem::InstallError do
+ inst = Gem::DependencyInstaller.new :domain => :local
+ inst.install 'b'
+ end
+ assert_equal 'b requires a (>= 0, runtime)', e.message
+ end
+
+ assert_equal [], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_domain_remote
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = a1_data
+
+ inst = Gem::DependencyInstaller.new :domain => :remote
+ inst.install 'a'
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_dual_repository
+ FileUtils.mv @a1_gem, @tempdir
+ FileUtils.mv @b1_gem, @tempdir
+ inst = nil
+
+ gemhome2 = "#{@gemhome}2"
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new :install_dir => gemhome2
+ inst.install 'a'
+ end
+
+ ENV['GEM_HOME'] = @gemhome
+ ENV['GEM_PATH'] = [@gemhome, gemhome2].join File::PATH_SEPARATOR
+ Gem.clear_paths
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'b'
+ end
+
+ assert_equal %w[b-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_remote
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = a1_data
+
+ inst = Gem::DependencyInstaller.new
+
+ Dir.chdir @tempdir do
+ inst.install 'a'
+ end
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_remote_dep
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = a1_data
+
+ inst = Gem::DependencyInstaller.new
+
+ Dir.chdir @tempdir do
+ dep = Gem::Dependency.new @a1.name, @a1.version
+ inst.install dep
+ end
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_domain_remote_platform_newer
+ a2_o, a2_o_gem = util_gem 'a', '2' do |s|
+ s.platform = Gem::Platform.new %w[cpu other_platform 1]
+ end
+
+ util_clear_gems
+
+ si = util_setup_spec_fetcher @a1, a2_o
+
+ @fetcher.data['http://gems.example.com/gems/yaml'] = si.to_yaml
+
+ a1_data = nil
+ a2_o_data = nil
+
+ File.open @a1_gem, 'rb' do |fp| a1_data = fp.read end
+ File.open a2_o_gem, 'rb' do |fp| a2_o_data = fp.read end
+
+ @fetcher.data["http://gems.example.com/gems/#{@a1.full_name}.gem"] =
+ a1_data
+ @fetcher.data["http://gems.example.com/gems/#{a2_o.full_name}.gem"] =
+ a2_o_data
+
+ inst = Gem::DependencyInstaller.new :domain => :remote
+ inst.install 'a'
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_reinstall
+ Gem::Installer.new(@a1_gem).install
+ FileUtils.mv @a1_gem, @tempdir
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::DependencyInstaller.new
+ inst.install 'a'
+ end
+
+ assert_equal Gem::SourceIndex.new(@a1.full_name => @a1),
+ Gem::SourceIndex.from_installed_gems
+
+ assert_equal %w[a-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ if defined? OpenSSL then
+ def test_install_security_policy
+ data = File.open(@a1_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = data
+
+ data = File.open(@b1_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/b-1.gem'] = data
+
+ policy = Gem::Security::HighSecurity
+ inst = Gem::DependencyInstaller.new :security_policy => policy
+
+ e = assert_raise Gem::Exception do
+ inst.install 'b'
+ end
+
+ assert_equal 'Unsigned gem', e.message
+
+ assert_equal %w[], inst.installed_gems.map { |s| s.full_name }
+ end
+ end
+
+ # Wrappers don't work on mswin
+ unless win_platform? then
+ def test_install_no_wrappers
+ @fetcher.data['http://gems.example.com/gems/a-1.gem'] = read_binary(@a1_gem)
+
+ inst = Gem::DependencyInstaller.new :wrappers => false
+ inst.install 'a'
+
+ assert_no_match(%r|This file was generated by RubyGems.|,
+ File.read(File.join(@gemhome, 'bin', 'a_bin')))
+ end
+ end
+
+ def test_install_version
+ data = File.open(@d2_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/d-2.gem'] = data
+
+ data = File.open(@d1_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/d-1.gem'] = data
+
+ inst = Gem::DependencyInstaller.new
+
+ inst.install 'd', '= 1'
+
+ assert_equal %w[d-1], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_install_version_default
+ data = File.open(@d2_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/d-2.gem'] = data
+
+ data = File.open(@d1_gem, 'rb') { |f| f.read }
+ @fetcher.data['http://gems.example.com/gems/d-1.gem'] = data
+
+ inst = Gem::DependencyInstaller.new
+ inst.install 'd'
+
+ assert_equal %w[d-2], inst.installed_gems.map { |s| s.full_name }
+ end
+
+ def test_find_gems_gems_with_sources
+ inst = Gem::DependencyInstaller.new
+ dep = Gem::Dependency.new 'b', '>= 0'
+
+ assert_equal [[@b1, @gem_repo]],
+ inst.find_gems_with_sources(dep)
+ end
+
+ def test_find_gems_with_sources_local
+ FileUtils.mv @a1_gem, @tempdir
+ inst = Gem::DependencyInstaller.new
+ dep = Gem::Dependency.new 'a', '>= 0'
+ gems = nil
+
+ Dir.chdir @tempdir do
+ gems = inst.find_gems_with_sources dep
+ end
+
+ assert_equal 2, gems.length
+ remote = gems.first
+ assert_equal 'a-1', remote.first.full_name, 'remote spec'
+ assert_equal @gem_repo, remote.last, 'remote path'
+
+ local = gems.last
+ assert_equal 'a-1', local.first.full_name, 'local spec'
+ assert_equal File.join(@tempdir, "#{@a1.full_name}.gem"),
+ local.last, 'local path'
+ end
+
+ def test_gather_dependencies
+ inst = Gem::DependencyInstaller.new
+ inst.find_spec_by_name_and_version 'b'
+ inst.gather_dependencies
+
+ assert_equal %w[a-1 b-1], inst.gems_to_install.map { |s| s.full_name }
+ end
+
+ def test_gather_dependencies_dropped
+ b2, = util_gem 'b', '2'
+ c1, = util_gem 'c', '1' do |s| s.add_dependency 'b' end
+
+ util_clear_gems
+
+ si = util_setup_spec_fetcher @a1, @b1, b2, c1
+
+ inst = Gem::DependencyInstaller.new
+ inst.find_spec_by_name_and_version 'c'
+ inst.gather_dependencies
+
+ assert_equal %w[b-2 c-1], inst.gems_to_install.map { |s| s.full_name }
+ end
+
+ def test_gather_dependencies_platform_alternate
+ util_set_arch 'cpu-my_platform1'
+
+ inst = Gem::DependencyInstaller.new
+ inst.find_spec_by_name_and_version 'w'
+ inst.gather_dependencies
+
+ assert_equal %w[x-1-cpu-my_platform-1 w-1],
+ inst.gems_to_install.map { |s| s.full_name }
+ end
+
+ def test_gather_dependencies_platform_bump
+ inst = Gem::DependencyInstaller.new
+ inst.find_spec_by_name_and_version 'z'
+ inst.gather_dependencies
+
+ assert_equal %w[y-1 z-1], inst.gems_to_install.map { |s| s.full_name }
+ end
+
+ def test_gather_dependencies_old_required
+ e1, = util_gem 'e', '1' do |s| s.add_dependency 'd', '= 1' end
+
+ util_clear_gems
+
+ si = util_setup_spec_fetcher @d1, @d2, e1
+
+ inst = Gem::DependencyInstaller.new
+ inst.find_spec_by_name_and_version 'e'
+ inst.gather_dependencies
+
+ assert_equal %w[d-1 e-1], inst.gems_to_install.map { |s| s.full_name }
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_dependency_list.rb b/trunk/test/rubygems/test_gem_dependency_list.rb
new file mode 100644
index 0000000000..5fdc227f05
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_dependency_list.rb
@@ -0,0 +1,212 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/dependency_list'
+
+class TestGemDependencyList < RubyGemTestCase
+
+ def setup
+ super
+
+ @deplist = Gem::DependencyList.new
+
+ @a1 = quick_gem 'a', '1'
+ @a2 = quick_gem 'a', '2'
+ @a3 = quick_gem 'a', '3'
+
+ @b1 = quick_gem 'b', '1' do |s| s.add_dependency 'a', '>= 1' end
+ @b2 = quick_gem 'b', '2' do |s| s.add_dependency 'a', '>= 1' end
+
+ @c1 = quick_gem 'c', '1' do |s| s.add_dependency 'b', '>= 1' end
+ @c2 = quick_gem 'c', '2'
+
+ @d1 = quick_gem 'd', '1' do |s| s.add_dependency 'c', '>= 1' end
+ end
+
+ def test_self_from_source_index
+ hash = {
+ 'a-1' => @a1,
+ 'b-2' => @b2,
+ }
+
+ si = Gem::SourceIndex.new hash
+ deps = Gem::DependencyList.from_source_index si
+
+ assert_equal %w[b-2 a-1], deps.dependency_order.map { |s| s.full_name }
+ end
+
+ def test_active_count
+ assert_equal 0, @deplist.send(:active_count, [], {})
+ assert_equal 1, @deplist.send(:active_count, [@a1], {})
+ assert_equal 0, @deplist.send(:active_count, [@a1],
+ { @a1.full_name => true })
+ end
+
+ def test_add
+ assert_equal [], @deplist.dependency_order
+
+ @deplist.add @a1, @b2
+
+ assert_equal [@b2, @a1], @deplist.dependency_order
+ end
+
+ def test_dependency_order
+ @deplist.add @a1, @b1, @c1, @d1
+
+ order = @deplist.dependency_order
+
+ assert_equal %w[d-1 c-1 b-1 a-1], order.map { |s| s.full_name }
+ end
+
+ def test_dependency_order_circle
+ @a1.add_dependency 'c', '>= 1'
+ @deplist.add @a1, @b1, @c1
+
+ order = @deplist.dependency_order
+
+ assert_equal %w[b-1 c-1 a-1], order.map { |s| s.full_name }
+ end
+
+ def test_dependency_order_diamond
+ util_diamond
+ e1 = quick_gem 'e', '1'
+ @deplist.add e1
+ @a1.add_dependency 'e', '>= 1'
+
+ order = @deplist.dependency_order
+
+ assert_equal %w[d-1 c-2 b-1 a-2 e-1], order.map { |s| s.full_name },
+ 'deps of trimmed specs not included'
+ end
+
+ def test_dependency_order_no_dependendencies
+ @deplist.add @a1, @c2
+
+ order = @deplist.dependency_order
+
+ assert_equal %w[c-2 a-1], order.map { |s| s.full_name }
+ end
+
+ def test_find_name
+ @deplist.add @a1, @b2
+
+ assert_equal "a-1", @deplist.find_name("a-1").full_name
+ assert_equal "b-2", @deplist.find_name("b-2").full_name
+
+ assert_nil @deplist.find_name("c-2")
+ end
+
+ def test_ok_eh
+ assert @deplist.ok?, 'no dependencies'
+
+ @deplist.add @b2
+
+ assert ! @deplist.ok?, 'unsatisfied dependency'
+
+ @deplist.add @a1
+
+ assert @deplist.ok?, 'satisfied dependency'
+ end
+
+ def test_ok_eh_mismatch
+ a1 = quick_gem 'a', '1'
+ a2 = quick_gem 'a', '2'
+
+ b = quick_gem 'b', '1' do |s| s.add_dependency 'a', '= 1' end
+ c = quick_gem 'c', '1' do |s| s.add_dependency 'a', '= 2' end
+
+ d = quick_gem 'd', '1' do |s|
+ s.add_dependency 'b'
+ s.add_dependency 'c'
+ end
+
+ @deplist.add a1, a2, b, c, d
+
+ assert @deplist.ok?, 'this will break on require'
+ end
+
+ def test_ok_eh_redundant
+ @deplist.add @a1, @a3, @b2
+
+ @deplist.remove_by_name("a-1")
+
+ assert @deplist.ok?
+ end
+
+ def test_ok_to_remove_eh
+ @deplist.add @a1
+
+ assert @deplist.ok_to_remove?("a-1")
+
+ @deplist.add @b2
+
+ assert ! @deplist.ok_to_remove?("a-1")
+
+ @deplist.add @a2
+
+ assert @deplist.ok_to_remove?("a-1")
+ assert @deplist.ok_to_remove?("a-2")
+ assert @deplist.ok_to_remove?("b-2")
+ end
+
+ def test_ok_to_remove_eh_after_sibling_removed
+ @deplist.add @a1, @a2, @b2
+
+ assert @deplist.ok_to_remove?("a-1")
+ assert @deplist.ok_to_remove?("a-2")
+
+ @deplist.remove_by_name("a-1")
+
+ assert ! @deplist.ok_to_remove?("a-2")
+ end
+
+ def test_remove_by_name
+ @deplist.add @a1, @b2
+
+ @deplist.remove_by_name "a-1"
+
+ assert ! @deplist.ok?
+ end
+
+ def test_tsort_each_node
+ util_diamond
+
+ order = %w[a-1 a-2 b-1 c-2 d-1]
+
+ @deplist.tsort_each_node do |node|
+ assert_equal order.shift, node.full_name
+ end
+
+ assert order.empty?
+ end
+
+ def test_tsort_each_child
+ util_diamond
+
+ order = %w[a-2]
+
+ @deplist.tsort_each_child(@b1) do |node|
+ assert_equal order.shift, node.full_name
+ end
+
+ assert order.empty?
+ end
+
+ # d1 -> b1 -> a1
+ # d1 -> c2 -> a2
+ def util_diamond
+ @c2.add_dependency 'a', '>= 2'
+ @d1.add_dependency 'b'
+
+ @deplist.add @a1, @a2, @b1, @c2, @d1
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_digest.rb b/trunk/test/rubygems/test_gem_digest.rb
new file mode 100755
index 0000000000..9d825b2796
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_digest.rb
@@ -0,0 +1,44 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require "test/unit"
+require "rubygems/digest/md5"
+require "rubygems/digest/sha1"
+require "rubygems/digest/sha2"
+
+class TestRubygemsGemDigest < Test::Unit::TestCase
+ def test_sha256_hex_digest_works
+ digester = Gem::SHA256.new
+ assert_equal "b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78", digester.hexdigest("ABC")
+ end
+
+ def test_sha256_digest_works
+ digester = Gem::SHA256.new
+ assert_equal "\265\324\004\\?Fo\251\037\342\314j\276y#*\032W\315\361\004\367\242nqn\n\036'\211\337x",
+ digester.digest("ABC")
+ end
+
+ def test_sha1_hex_digest_works
+ digester = Gem::SHA1.new
+ assert_equal "3c01bdbb26f358bab27f267924aa2c9a03fcfdb8", digester.hexdigest("ABC")
+ end
+
+ def test_sha1_digest_works
+ digester = Gem::SHA1.new
+ assert_equal "<\001\275\273&\363X\272\262\177&y$\252,\232\003\374\375\270", digester.digest("ABC")
+ end
+
+ def test_md5_hex_digest_works
+ digester = Gem::MD5.new
+ assert_equal "902fbdd2b1df0c4f70b4a5d23525e932", digester.hexdigest("ABC")
+ end
+
+ def test_md5_digest_works
+ digester = Gem::MD5.new
+ assert_equal "\220/\275\322\261\337\fOp\264\245\3225%\3512", digester.digest("ABC")
+ end
+end \ No newline at end of file
diff --git a/trunk/test/rubygems/test_gem_doc_manager.rb b/trunk/test/rubygems/test_gem_doc_manager.rb
new file mode 100644
index 0000000000..e52fb9f0cd
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_doc_manager.rb
@@ -0,0 +1,32 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/doc_manager'
+
+class TestGemDocManager < RubyGemTestCase
+
+ def setup
+ super
+
+ @spec = quick_gem 'a'
+ @manager = Gem::DocManager.new(@spec)
+ end
+
+ def test_uninstall_doc_unwritable
+ orig_mode = File.stat(@spec.installation_path).mode
+ File.chmod 0, @spec.installation_path
+
+ assert_raise Gem::FilePermissionError do
+ @manager.uninstall_doc
+ end
+ ensure
+ File.chmod orig_mode, @spec.installation_path
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_ext_configure_builder.rb b/trunk/test/rubygems/test_gem_ext_configure_builder.rb
new file mode 100644
index 0000000000..9ce17075bc
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_ext_configure_builder.rb
@@ -0,0 +1,86 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/ext'
+
+class TestGemExtConfigureBuilder < RubyGemTestCase
+
+ def setup
+ super
+
+ @makefile_body = "all:\n\t@echo ok\ninstall:\n\t@echo ok"
+
+ @ext = File.join @tempdir, 'ext'
+ @dest_path = File.join @tempdir, 'prefix'
+
+ FileUtils.mkdir_p @ext
+ FileUtils.mkdir_p @dest_path
+ end
+
+ def test_self_build
+ return if RUBY_PLATFORM =~ /mswin/ # HACK
+
+ File.open File.join(@ext, './configure'), 'w' do |configure|
+ configure.puts "#!/bin/sh\necho \"#{@makefile_body}\" > Makefile"
+ end
+
+ output = []
+
+ Dir.chdir @ext do
+ Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
+ end
+
+ assert_equal "sh ./configure --prefix=#{@dest_path}", output.shift
+ assert_equal "", output.shift
+ assert_equal "make", output.shift
+ assert_match(/^ok$/m, output.shift)
+ assert_equal "make install", output.shift
+ assert_match(/^ok$/m, output.shift)
+ end
+
+ def test_self_build_fail
+ return if RUBY_PLATFORM =~ /mswin/ # HACK
+ output = []
+
+ error = assert_raise Gem::InstallError do
+ Dir.chdir @ext do
+ Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
+ end
+ end
+
+ shell_error_msg = %r{(\./configure: .*)|(Can't open \./configure)}
+ sh_prefix_configure = "sh ./configure --prefix="
+
+ expected = %r(configure failed:
+
+#{Regexp.escape sh_prefix_configure}#{Regexp.escape @dest_path}
+.*?: #{shell_error_msg})
+
+ assert_match expected, error.message
+
+ assert_equal "#{sh_prefix_configure}#{@dest_path}", output.shift
+ assert_match %r(#{shell_error_msg}\n), output.shift
+ assert_equal true, output.empty?
+ end
+
+ def test_self_build_has_makefile
+ File.open File.join(@ext, 'Makefile'), 'w' do |makefile|
+ makefile.puts @makefile_body
+ end
+
+ output = []
+ Dir.chdir @ext do
+ Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
+ end
+
+ case RUBY_PLATFORM
+ when /mswin/ then
+ assert_equal 'nmake', output[0]
+ assert_equal 'nmake install', output[2]
+ else
+ assert_equal 'make', output[0]
+ assert_equal 'make install', output[2]
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_ext_ext_conf_builder.rb b/trunk/test/rubygems/test_gem_ext_ext_conf_builder.rb
new file mode 100644
index 0000000000..fb21fa0755
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_ext_ext_conf_builder.rb
@@ -0,0 +1,122 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/ext'
+
+class TestGemExtExtConfBuilder < RubyGemTestCase
+
+ def setup
+ super
+
+ @ext = File.join @tempdir, 'ext'
+ @dest_path = File.join @tempdir, 'prefix'
+
+ FileUtils.mkdir_p @ext
+ FileUtils.mkdir_p @dest_path
+ end
+
+ def test_class_build
+ File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
+ extconf.puts "require 'mkmf'\ncreate_makefile 'foo'"
+ end
+
+ output = []
+
+ Dir.chdir @ext do
+ Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
+ end
+
+ expected = [
+ "ruby extconf.rb",
+ "creating Makefile\n",
+ "make",
+ "make: Nothing to be done for `all'.\n",
+ "make install",
+ "make: Nothing to be done for `install'.\n"
+ ]
+
+ assert_match(/^#{Gem.ruby} extconf.rb/, output[0])
+ assert_equal "creating Makefile\n", output[1]
+ case RUBY_PLATFORM
+ when /mswin/ then
+ assert_equal "nmake", output[2]
+ assert_equal "nmake install", output[4]
+ else
+ assert_equal "make", output[2]
+ assert_equal "make install", output[4]
+ end
+ end
+
+ def test_class_build_extconf_fail
+ File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
+ extconf.puts "require 'mkmf'"
+ extconf.puts "have_library 'nonexistent' or abort 'need libnonexistent'"
+ extconf.puts "create_makefile 'foo'"
+ end
+
+ output = []
+
+ error = assert_raise Gem::InstallError do
+ Dir.chdir @ext do
+ Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
+ end
+ end
+
+ assert_match(/\Aextconf failed:
+
+#{Gem.ruby} extconf.rb.*
+checking for main\(\) in .*?nonexistent/m, error.message)
+
+ assert_match(/^#{Gem.ruby} extconf.rb/, output[0])
+ end
+
+ def test_class_make
+ output = []
+ makefile_path = File.join(@ext, 'Makefile')
+ File.open makefile_path, 'w' do |makefile|
+ makefile.puts "RUBYARCHDIR = $(foo)$(target_prefix)"
+ makefile.puts "RUBYLIBDIR = $(bar)$(target_prefix)"
+ makefile.puts "all:"
+ makefile.puts "install:"
+ end
+
+ Dir.chdir @ext do
+ Gem::Ext::ExtConfBuilder.make @ext, output
+ end
+
+ case RUBY_PLATFORM
+ when /mswin/ then
+ assert_equal 'nmake', output[0]
+ assert_equal 'nmake install', output[2]
+ else
+ assert_equal 'make', output[0]
+ assert_equal 'make install', output[2]
+ end
+
+ edited_makefile = <<-EOF
+RUBYARCHDIR = #{@ext}$(target_prefix)
+RUBYLIBDIR = #{@ext}$(target_prefix)
+all:
+install:
+ EOF
+
+ assert_equal edited_makefile, File.read(makefile_path)
+ end
+
+ def test_class_make_no_Makefile
+ error = assert_raise Gem::InstallError do
+ Dir.chdir @ext do
+ Gem::Ext::ExtConfBuilder.make @ext, ['output']
+ end
+ end
+
+ expected = <<-EOF.strip
+Makefile not found:
+
+output
+ EOF
+
+ assert_equal expected, error.message
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_ext_rake_builder.rb b/trunk/test/rubygems/test_gem_ext_rake_builder.rb
new file mode 100644
index 0000000000..6d9184e804
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_ext_rake_builder.rb
@@ -0,0 +1,76 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/ext'
+
+class TestGemExtRakeBuilder < RubyGemTestCase
+ def setup
+ super
+
+ @ext = File.join @tempdir, 'ext'
+ @dest_path = File.join @tempdir, 'prefix'
+
+ FileUtils.mkdir_p @ext
+ FileUtils.mkdir_p @dest_path
+ end
+
+ def test_class_build
+ File.open File.join(@ext, 'mkrf_conf.rb'), 'w' do |mkrf_conf|
+ mkrf_conf.puts <<-EO_MKRF
+ File.open("Rakefile","w") do |f|
+ f.puts "task :default"
+ end
+ EO_MKRF
+ end
+
+ output = []
+ realdir = nil # HACK /tmp vs. /private/tmp
+
+ build_rake_in do
+ Dir.chdir @ext do
+ realdir = Dir.pwd
+ Gem::Ext::RakeBuilder.build 'mkrf_conf.rb', nil, @dest_path, output
+ end
+ end
+
+ expected = [
+ "#{@@ruby} mkrf_conf.rb",
+ "",
+ "#{@@rake} RUBYARCHDIR=#{@dest_path} RUBYLIBDIR=#{@dest_path}",
+ "(in #{realdir})\n"
+ ]
+
+ assert_equal expected, output
+ end
+
+ def test_class_build_fail
+ File.open File.join(@ext, 'mkrf_conf.rb'), 'w' do |mkrf_conf|
+ mkrf_conf.puts <<-EO_MKRF
+ File.open("Rakefile","w") do |f|
+ f.puts "task :default do abort 'fail' end"
+ end
+ EO_MKRF
+ end
+
+ output = []
+
+ error = assert_raise Gem::InstallError do
+ build_rake_in do
+ Dir.chdir @ext do
+ Gem::Ext::RakeBuilder.build "mkrf_conf.rb", nil, @dest_path, output
+ end
+ end
+ end
+
+ expected = <<-EOF.strip
+rake failed:
+
+#{@@ruby} mkrf_conf.rb
+
+#{@@rake} RUBYARCHDIR=#{@dest_path} RUBYLIBDIR=#{@dest_path}
+ EOF
+
+ assert_equal expected, error.message.split("\n")[0..4].join("\n")
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_format.rb b/trunk/test/rubygems/test_gem_format.rb
new file mode 100644
index 0000000000..4014acfed9
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_format.rb
@@ -0,0 +1,69 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'simple_gem')
+require 'rubygems/format'
+
+class TestGemFormat < RubyGemTestCase
+
+ def setup
+ super
+
+ @simple_gem = SIMPLE_GEM
+ end
+
+ def test_from_file_by_path
+ util_make_gems
+
+ gems = Dir[File.join(@gemhome, 'cache', '*.gem')]
+
+ names = [@a1, @a2, @a_evil9, @b2, @c1_2, @pl1].map do |spec|
+ spec.original_name
+ end
+
+ gems_n_names = gems.sort.zip names
+
+ gems_n_names.each do |gemfile, name|
+ spec = Gem::Format.from_file_by_path(gemfile).spec
+
+ assert_equal name, spec.original_name
+ end
+ end
+
+ def test_from_file_by_path_nonexistent
+ assert_raise Gem::Exception do
+ Gem::Format.from_file_by_path '/nonexistent'
+ end
+ end
+
+ def test_from_io_garbled
+ e = assert_raise Gem::Package::FormatError do
+ # subtly bogus input
+ Gem::Format.from_io(StringIO.new(@simple_gem.upcase))
+ end
+
+ assert_equal 'No metadata found!', e.message
+
+ e = assert_raise Gem::Package::FormatError do
+ # Totally bogus input
+ Gem::Format.from_io(StringIO.new(@simple_gem.reverse))
+ end
+
+ assert_equal 'No metadata found!', e.message
+
+ e = assert_raise Gem::Package::FormatError do
+ # This was intentionally screws up YAML parsing.
+ Gem::Format.from_io(StringIO.new(@simple_gem.gsub(/:/, "boom")))
+ end
+
+ assert_equal 'No metadata found!', e.message
+ end
+
+end
+
+
diff --git a/trunk/test/rubygems/test_gem_gem_path_searcher.rb b/trunk/test/rubygems/test_gem_gem_path_searcher.rb
new file mode 100644
index 0000000000..c9da4d2b05
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_gem_path_searcher.rb
@@ -0,0 +1,60 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), '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 TestGemGemPathSearcher < 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'
+
+ @fetcher = Gem::FakeFetcher.new
+ Gem::RemoteFetcher.fetcher = @fetcher
+
+ Gem.source_index = util_setup_spec_fetcher @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
+
diff --git a/trunk/test/rubygems/test_gem_gem_runner.rb b/trunk/test/rubygems/test_gem_gem_runner.rb
new file mode 100644
index 0000000000..4e3239f015
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_gem_runner.rb
@@ -0,0 +1,35 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/gem_runner'
+
+class TestGemGemRunner < RubyGemTestCase
+
+ def test_do_configuration
+ Gem.clear_paths
+
+ temp_conf = File.join @tempdir, '.gemrc'
+
+ other_gem_path = File.join @tempdir, 'other_gem_path'
+ other_gem_home = File.join @tempdir, 'other_gem_home'
+
+ Gem.ensure_gem_subdirectories other_gem_path
+ Gem.ensure_gem_subdirectories other_gem_home
+
+ File.open temp_conf, 'w' do |fp|
+ fp.puts "gem: --commands"
+ fp.puts "gemhome: #{other_gem_home}"
+ fp.puts "gempath:"
+ fp.puts " - #{other_gem_path}"
+ fp.puts "rdoc: --all"
+ end
+
+ gr = Gem::GemRunner.new
+ gr.send :do_configuration, %W[--config-file #{temp_conf}]
+
+ assert_equal [other_gem_path, other_gem_home], Gem.path
+ assert_equal %w[--commands], Gem::Command.extra_args
+ assert_equal %w[--all], Gem::DocManager.configured_args
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_indexer.rb b/trunk/test/rubygems/test_gem_indexer.rb
new file mode 100644
index 0000000000..9d68a7f34d
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_indexer.rb
@@ -0,0 +1,263 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+
+require 'rubygems/indexer'
+
+unless ''.respond_to? :to_xs then
+ warn "Gem::Indexer tests are being skipped. Install builder gem."
+end
+
+class TestGemIndexer < RubyGemTestCase
+
+ def setup
+ super
+
+ util_make_gems
+
+ @d2_0 = quick_gem 'd', '2.0'
+ util_build_gem @d2_0
+
+ gems = File.join(@tempdir, 'gems')
+ FileUtils.mkdir_p gems
+ cache_gems = File.join @gemhome, 'cache', '*.gem'
+ FileUtils.mv Dir[cache_gems], gems
+
+ @indexer = Gem::Indexer.new @tempdir
+ end
+
+ def test_initialize
+ assert_equal @tempdir, @indexer.dest_directory
+ assert_equal File.join(Dir.tmpdir, "gem_generate_index_#{$$}"),
+ @indexer.directory
+ end
+
+ def test_build_indicies
+ spec = quick_gem 'd', '2.0'
+ spec.instance_variable_set :@original_platform, ''
+
+ @indexer.make_temp_directories
+
+ index = Gem::SourceIndex.new
+ index.add_spec spec
+
+ use_ui @ui do
+ @indexer.build_indicies index
+ end
+
+ specs_path = File.join @indexer.directory, "specs.#{@marshal_version}"
+ specs_dump = Gem.read_binary specs_path
+ specs = Marshal.load specs_dump
+
+ expected = [
+ ['d', Gem::Version.new('2.0'), 'ruby'],
+ ]
+
+ assert_equal expected, specs, 'specs'
+
+ latest_specs_path = File.join @indexer.directory,
+ "latest_specs.#{@marshal_version}"
+ latest_specs_dump = Gem.read_binary latest_specs_path
+ latest_specs = Marshal.load latest_specs_dump
+
+ expected = [
+ ['d', Gem::Version.new('2.0'), 'ruby'],
+ ]
+
+ assert_equal expected, latest_specs, 'latest_specs'
+ end
+
+ def test_generate_index
+ use_ui @ui do
+ @indexer.generate_index
+ end
+
+ assert_indexed @tempdir, 'yaml'
+ assert_indexed @tempdir, 'yaml.Z'
+ assert_indexed @tempdir, "Marshal.#{@marshal_version}"
+ assert_indexed @tempdir, "Marshal.#{@marshal_version}.Z"
+
+ quickdir = File.join @tempdir, 'quick'
+ marshal_quickdir = File.join quickdir, "Marshal.#{@marshal_version}"
+
+ assert File.directory?(quickdir)
+ assert File.directory?(marshal_quickdir)
+
+ assert_indexed quickdir, "index"
+ assert_indexed quickdir, "index.rz"
+
+ quick_index = File.read File.join(quickdir, 'index')
+ expected = <<-EOF
+a-1
+a-2
+a_evil-9
+b-2
+c-1.2
+d-2.0
+pl-1-i386-linux
+ EOF
+
+ assert_equal expected, quick_index
+
+ assert_indexed quickdir, "latest_index"
+ assert_indexed quickdir, "latest_index.rz"
+
+ latest_quick_index = File.read File.join(quickdir, 'latest_index')
+ expected = <<-EOF
+a-2
+a_evil-9
+b-2
+c-1.2
+d-2.0
+pl-1-i386-linux
+ EOF
+
+ assert_equal expected, latest_quick_index
+
+ assert_indexed quickdir, "#{@a1.full_name}.gemspec.rz"
+ assert_indexed quickdir, "#{@a2.full_name}.gemspec.rz"
+ assert_indexed quickdir, "#{@b2.full_name}.gemspec.rz"
+ assert_indexed quickdir, "#{@c1_2.full_name}.gemspec.rz"
+
+ assert_indexed quickdir, "#{@pl1.original_name}.gemspec.rz"
+ refute_indexed quickdir, "#{@pl1.full_name}.gemspec.rz"
+
+ assert_indexed marshal_quickdir, "#{@a1.full_name}.gemspec.rz"
+ assert_indexed marshal_quickdir, "#{@a2.full_name}.gemspec.rz"
+
+ refute_indexed quickdir, "#{@c1_2.full_name}.gemspec"
+ refute_indexed marshal_quickdir, "#{@c1_2.full_name}.gemspec"
+
+ assert_indexed @tempdir, "specs.#{@marshal_version}"
+ assert_indexed @tempdir, "specs.#{@marshal_version}.gz"
+
+ assert_indexed @tempdir, "latest_specs.#{@marshal_version}"
+ assert_indexed @tempdir, "latest_specs.#{@marshal_version}.gz"
+ end
+
+ def test_generate_index_ui
+ use_ui @ui do
+ @indexer.generate_index
+ end
+
+ expected = <<-EOF
+Loading 7 gems from #{@tempdir}
+.......
+Loaded all gems
+Generating quick index gemspecs for 7 gems
+.......
+Complete
+Generating specs index
+Generating latest specs index
+Generating quick index
+Generating latest index
+Generating Marshal master index
+Generating YAML master index for 7 gems (this may take a while)
+.......
+Complete
+Compressing indicies
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_generate_index_master
+ use_ui @ui do
+ @indexer.generate_index
+ end
+
+ yaml_path = File.join @tempdir, 'yaml'
+ dump_path = File.join @tempdir, "Marshal.#{@marshal_version}"
+
+ yaml_index = YAML.load_file yaml_path
+ dump_index = Marshal.load Gem.read_binary(dump_path)
+
+ dump_index.each do |_,gem|
+ gem.send :remove_instance_variable, :@loaded
+ end
+
+ assert_equal yaml_index, dump_index,
+ "expected YAML and Marshal to produce identical results"
+ end
+
+ def test_generate_index_specs
+ use_ui @ui do
+ @indexer.generate_index
+ end
+
+ specs_path = File.join @tempdir, "specs.#{@marshal_version}"
+
+ specs_dump = Gem.read_binary specs_path
+ specs = Marshal.load specs_dump
+
+ expected = [
+ ['a', Gem::Version.new(1), 'ruby'],
+ ['a', Gem::Version.new(2), 'ruby'],
+ ['a_evil', Gem::Version.new(9), 'ruby'],
+ ['b', Gem::Version.new(2), 'ruby'],
+ ['c', Gem::Version.new('1.2'), 'ruby'],
+ ['d', Gem::Version.new('2.0'), 'ruby'],
+ ['pl', Gem::Version.new(1), 'i386-linux'],
+ ]
+
+ assert_equal expected, specs
+
+ assert_same specs[0].first, specs[1].first,
+ 'identical names not identical'
+
+ assert_same specs[0][1], specs[-1][1],
+ 'identical versions not identical'
+
+ assert_same specs[0].last, specs[1].last,
+ 'identical platforms not identical'
+
+ assert_not_same specs[1][1], specs[5][1],
+ 'different versions not different'
+ end
+
+ def test_generate_index_latest_specs
+ use_ui @ui do
+ @indexer.generate_index
+ end
+
+ latest_specs_path = File.join @tempdir, "latest_specs.#{@marshal_version}"
+
+ latest_specs_dump = Gem.read_binary latest_specs_path
+ latest_specs = Marshal.load latest_specs_dump
+
+ expected = [
+ ['a', Gem::Version.new(2), 'ruby'],
+ ['a_evil', Gem::Version.new(9), 'ruby'],
+ ['b', Gem::Version.new(2), 'ruby'],
+ ['c', Gem::Version.new('1.2'), 'ruby'],
+ ['d', Gem::Version.new('2.0'), 'ruby'],
+ ['pl', Gem::Version.new(1), 'i386-linux'],
+ ]
+
+ assert_equal expected, latest_specs
+
+ assert_same latest_specs[0][1], latest_specs[2][1],
+ 'identical versions not identical'
+
+ assert_same latest_specs[0].last, latest_specs[1].last,
+ 'identical platforms not identical'
+ end
+
+ def assert_indexed(dir, name)
+ file = File.join dir, name
+ assert File.exist?(file), "#{file} does not exist"
+ end
+
+ def refute_indexed(dir, name)
+ file = File.join dir, name
+ assert !File.exist?(file), "#{file} exists"
+ end
+
+end if ''.respond_to? :to_xs
+
diff --git a/trunk/test/rubygems/test_gem_install_update_options.rb b/trunk/test/rubygems/test_gem_install_update_options.rb
new file mode 100644
index 0000000000..3638a0670a
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_install_update_options.rb
@@ -0,0 +1,61 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_installer_test_case')
+require 'rubygems/install_update_options'
+require 'rubygems/command'
+
+class TestGemInstallUpdateOptions < GemInstallerTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Command.new 'dummy', 'dummy'
+ @cmd.extend Gem::InstallUpdateOptions
+ @cmd.add_install_update_options
+ end
+
+ def test_add_install_update_options
+ args = %w[-i /install_to --rdoc --ri -E -f -t -w -P HighSecurity
+ --ignore-dependencies --format-exec --include-dependencies]
+
+ assert @cmd.handles?(args)
+ end
+
+ def test_security_policy
+ @cmd.handle_options %w[-P HighSecurity]
+
+ assert_equal Gem::Security::HighSecurity, @cmd.options[:security_policy]
+ end
+
+ def test_security_policy_unknown
+ @cmd.add_install_update_options
+
+ assert_raise OptionParser::InvalidArgument do
+ @cmd.handle_options %w[-P UnknownSecurity]
+ end
+ end
+
+ def test_user_install_enabled
+ @cmd.handle_options %w[--user-install]
+
+ @installer = Gem::Installer.new @gem, @cmd.options
+ @installer.install
+ assert File.exist?(File.join(@userhome, '.gem', 'gems'))
+ assert File.exist?(File.join(@userhome, '.gem', 'gems',
+ @spec.full_name))
+ end
+
+ def test_user_install_disabled_read_only
+ @cmd.handle_options %w[--no-user-install]
+
+ File.chmod 0755, @userhome
+ FileUtils.chmod 0000, @gemhome
+
+ assert_raises(Gem::FilePermissionError) do
+ @installer = Gem::Installer.new @gem, @cmd.options
+ end
+ ensure
+ FileUtils.chmod 0755, @gemhome
+ end
+end
diff --git a/trunk/test/rubygems/test_gem_installer.rb b/trunk/test/rubygems/test_gem_installer.rb
new file mode 100644
index 0000000000..eb9743dd42
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_installer.rb
@@ -0,0 +1,868 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_installer_test_case')
+
+class TestGemInstaller < GemInstallerTestCase
+
+ def test_app_script_text
+ util_make_exec '2', ''
+
+ expected = <<-EOF
+#!#{Gem.ruby}
+#
+# This file was generated by RubyGems.
+#
+# The application 'a' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require 'rubygems'
+
+version = \">= 0\"
+
+if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
+ version = $1
+ ARGV.shift
+end
+
+gem 'a', version
+load 'my_exec'
+ EOF
+
+ wrapper = @installer.app_script_text 'my_exec'
+ assert_equal expected, wrapper
+ end
+
+ def test_build_extensions_none
+ use_ui @ui do
+ @installer.build_extensions
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+
+ assert !File.exist?('gem_make.out')
+ end
+
+ def test_build_extensions_extconf_bad
+ @spec.extensions << 'extconf.rb'
+
+ e = assert_raise Gem::Installer::ExtensionBuildError do
+ use_ui @ui do
+ @installer.build_extensions
+ end
+ end
+
+ assert_match(/\AERROR: Failed to build gem native extension.$/, e.message)
+
+ assert_equal "Building native extensions. This could take a while...\n",
+ @ui.output
+ assert_equal '', @ui.error
+
+ gem_make_out = File.join @gemhome, 'gems', @spec.full_name, 'gem_make.out'
+ expected = <<-EOF
+#{Gem.ruby} extconf.rb
+#{Gem.ruby}: No such file or directory -- extconf.rb (LoadError)
+ EOF
+
+ assert_equal expected, File.read(gem_make_out)
+ end
+
+ def test_build_extensions_unsupported
+ @spec.extensions << nil
+
+ e = assert_raise Gem::Installer::ExtensionBuildError do
+ use_ui @ui do
+ @installer.build_extensions
+ end
+ end
+
+ assert_match(/^No builder for extension ''$/, e.message)
+
+ assert_equal "Building native extensions. This could take a while...\n",
+ @ui.output
+ assert_equal '', @ui.error
+
+ assert_equal "No builder for extension ''\n", File.read('gem_make.out')
+ ensure
+ FileUtils.rm_f 'gem_make.out'
+ end
+
+ def test_ensure_dependency
+ dep = Gem::Dependency.new 'a', '>= 2'
+ assert @installer.ensure_dependency(@spec, dep)
+
+ dep = Gem::Dependency.new 'b', '> 2'
+ e = assert_raise Gem::InstallError do
+ @installer.ensure_dependency @spec, dep
+ end
+
+ assert_equal 'a requires b (> 2, runtime)', e.message
+ end
+
+ def test_expand_and_validate_gem_dir
+ @installer.gem_dir = '/nonexistent'
+ expanded_gem_dir = @installer.send(:expand_and_validate_gem_dir)
+ if win_platform?
+ expected = File.expand_path('/nonexistent').downcase
+ expanded_gem_dir = expanded_gem_dir.downcase
+ else
+ expected = '/nonexistent'
+ end
+
+ assert_equal expected, expanded_gem_dir
+ end
+
+ def test_extract_files
+ format = Object.new
+ def format.file_entries
+ [[{'size' => 7, 'mode' => 0400, 'path' => 'thefile'}, 'thefile']]
+ end
+
+ @installer.format = format
+
+ @installer.extract_files
+
+ thefile_path = File.join(util_gem_dir, 'thefile')
+ assert_equal 'thefile', File.read(thefile_path)
+
+ unless Gem.win_platform? then
+ assert_equal 0400, File.stat(thefile_path).mode & 0777
+ end
+ end
+
+ def test_extract_files_bad_dest
+ @installer.gem_dir = 'somedir'
+ @installer.format = nil
+ e = assert_raise ArgumentError do
+ @installer.extract_files
+ end
+
+ assert_equal 'format required to extract from', e.message
+ end
+
+ def test_extract_files_relative
+ format = Object.new
+ def format.file_entries
+ [[{'size' => 10, 'mode' => 0644, 'path' => '../thefile'}, '../thefile']]
+ end
+
+ @installer.format = format
+
+ e = assert_raise Gem::InstallError do
+ @installer.extract_files
+ end
+
+ assert_equal "attempt to install file into \"../thefile\" under #{util_gem_dir.inspect}",
+ e.message
+ assert_equal false, File.file?(File.join(@tempdir, '../thefile')),
+ "You may need to remove this file if you broke the test once"
+ end
+
+ def test_extract_files_absolute
+ format = Object.new
+ def format.file_entries
+ [[{'size' => 8, 'mode' => 0644, 'path' => '/thefile'}, '/thefile']]
+ end
+
+ @installer.format = format
+
+ e = assert_raise Gem::InstallError do
+ @installer.extract_files
+ end
+
+ assert_equal 'attempt to install file into "/thefile"', e.message
+ assert_equal false, File.file?(File.join('/thefile')),
+ "You may need to remove this file if you broke the test once"
+ end
+
+ def test_generate_bin_bindir
+ @installer.wrappers = true
+
+ @spec.executables = ["my_exec"]
+ @spec.bindir = '.'
+
+ exec_file = @installer.formatted_program_filename "my_exec"
+ exec_path = File.join util_gem_dir(@spec.version), exec_file
+ File.open exec_path, 'w' do |f|
+ f.puts '#!/usr/bin/ruby'
+ end
+
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal true, File.exist?(installed_exec)
+ assert_equal(0100755, File.stat(installed_exec).mode) unless win_platform?
+
+ wrapper = File.read installed_exec
+ assert_match %r|generated by RubyGems|, wrapper
+ end
+
+ def test_generate_bin_script
+ @installer.wrappers = true
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal true, File.exist?(installed_exec)
+ assert_equal(0100755, File.stat(installed_exec).mode) unless win_platform?
+
+ wrapper = File.read installed_exec
+ assert_match %r|generated by RubyGems|, wrapper
+ end
+
+ def test_generate_bin_script_format
+ @installer.format_executable = true
+ @installer.wrappers = true
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ Gem::Installer.exec_format = 'foo-%s-bar'
+ @installer.generate_bin
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join util_inst_bindir, 'foo-my_exec-bar'
+ assert_equal true, File.exist?(installed_exec)
+ ensure
+ Gem::Installer.exec_format = nil
+ end
+
+ def test_generate_bin_script_format_disabled
+ @installer.wrappers = true
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ Gem::Installer.exec_format = 'foo-%s-bar'
+ @installer.generate_bin
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join util_inst_bindir, 'my_exec'
+ assert_equal true, File.exist?(installed_exec)
+ ensure
+ Gem::Installer.exec_format = nil
+ end
+
+ def test_generate_bin_script_install_dir
+ @installer.wrappers = true
+ @spec.executables = ["my_exec"]
+
+ gem_dir = File.join "#{@gemhome}2", 'gems', @spec.full_name
+ gem_bindir = File.join gem_dir, 'bin'
+ FileUtils.mkdir_p gem_bindir
+ File.open File.join(gem_bindir, "my_exec"), 'w' do |f|
+ f.puts "#!/bin/ruby"
+ end
+
+ @installer.gem_home = "#{@gemhome}2"
+ @installer.gem_dir = gem_dir
+
+ @installer.generate_bin
+
+ installed_exec = File.join("#{@gemhome}2", 'bin', 'my_exec')
+ assert_equal true, File.exist?(installed_exec)
+ assert_equal(0100755, File.stat(installed_exec).mode) unless win_platform?
+
+ wrapper = File.read installed_exec
+ assert_match %r|generated by RubyGems|, wrapper
+ end
+
+ def test_generate_bin_script_no_execs
+ @installer.wrappers = true
+ @installer.generate_bin
+ assert_equal false, File.exist?(util_inst_bindir)
+ end
+
+ def test_generate_bin_script_no_perms
+ @installer.wrappers = true
+ util_make_exec
+
+ Dir.mkdir util_inst_bindir
+ File.chmod 0000, util_inst_bindir
+
+ assert_raises Gem::FilePermissionError do
+ @installer.generate_bin
+ end
+
+ ensure
+ File.chmod 0700, util_inst_bindir unless $DEBUG
+ end
+
+ def test_generate_bin_script_no_shebang
+ @installer.wrappers = true
+ @spec.executables = ["my_exec"]
+
+ gem_dir = File.join @gemhome, 'gems', @spec.full_name
+ gem_bindir = File.join gem_dir, 'bin'
+ FileUtils.mkdir_p gem_bindir
+ File.open File.join(gem_bindir, "my_exec"), 'w' do |f|
+ f.puts "blah blah blah"
+ end
+
+ @installer.generate_bin
+
+ installed_exec = File.join @gemhome, 'bin', 'my_exec'
+ assert_equal true, File.exist?(installed_exec)
+ assert_equal 0100755, File.stat(installed_exec).mode unless win_platform?
+
+ wrapper = File.read installed_exec
+ assert_match %r|generated by RubyGems|, wrapper
+ # HACK some gems don't have #! in their executables, restore 2008/06
+ #assert_no_match %r|generated by RubyGems|, wrapper
+ end
+
+ def test_generate_bin_script_wrappers
+ @installer.wrappers = true
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+
+ real_exec = File.join util_gem_dir, 'bin', 'my_exec'
+
+ # fake --no-wrappers for previous install
+ unless Gem.win_platform? then
+ FileUtils.mkdir_p File.dirname(installed_exec)
+ FileUtils.ln_s real_exec, installed_exec
+ end
+
+ @installer.generate_bin
+ assert_equal true, File.directory?(util_inst_bindir)
+ assert_equal true, File.exist?(installed_exec)
+ assert_equal(0100755, File.stat(installed_exec).mode) unless win_platform?
+
+ assert_match %r|generated by RubyGems|, File.read(installed_exec)
+
+ assert_no_match %r|generated by RubyGems|, File.read(real_exec),
+ 'real executable overwritten'
+ end
+
+ def test_generate_bin_symlink
+ return if win_platform? #Windows FS do not support symlinks
+
+ @installer.wrappers = false
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal true, File.symlink?(installed_exec)
+ assert_equal(File.join(util_gem_dir, "bin", "my_exec"),
+ File.readlink(installed_exec))
+ end
+
+ def test_generate_bin_symlink_no_execs
+ @installer.wrappers = false
+ @installer.generate_bin
+ assert_equal false, File.exist?(util_inst_bindir)
+ end
+
+ def test_generate_bin_symlink_no_perms
+ @installer.wrappers = false
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ Dir.mkdir util_inst_bindir
+ File.chmod 0000, util_inst_bindir
+
+ assert_raises Gem::FilePermissionError do
+ @installer.generate_bin
+ end
+
+ ensure
+ File.chmod 0700, util_inst_bindir unless $DEBUG
+ end
+
+ def test_generate_bin_symlink_update_newer
+ return if win_platform? #Windows FS do not support symlinks
+
+ @installer.wrappers = false
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal(File.join(util_gem_dir, "bin", "my_exec"),
+ File.readlink(installed_exec))
+
+ @spec = Gem::Specification.new do |s|
+ s.files = ['lib/code.rb']
+ s.name = "a"
+ s.version = "3"
+ s.summary = "summary"
+ s.description = "desc"
+ s.require_path = 'lib'
+ end
+
+ util_make_exec '3'
+ @installer.gem_dir = File.join util_gem_dir('3')
+ @installer.generate_bin
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal(File.join(util_gem_bindir('3'), "my_exec"),
+ File.readlink(installed_exec),
+ "Ensure symlink moved to latest version")
+ end
+
+ def test_generate_bin_symlink_update_older
+ return if win_platform? #Windows FS do not support symlinks
+
+ @installer.wrappers = false
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal(File.join(util_gem_dir, "bin", "my_exec"),
+ File.readlink(installed_exec))
+
+ spec = Gem::Specification.new do |s|
+ s.files = ['lib/code.rb']
+ s.name = "a"
+ s.version = "1"
+ s.summary = "summary"
+ s.description = "desc"
+ s.require_path = 'lib'
+ end
+
+ util_make_exec '1'
+ @installer.gem_dir = util_gem_dir('1')
+ @installer.spec = spec
+
+ @installer.generate_bin
+
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal(File.join(util_gem_dir('2'), "bin", "my_exec"),
+ File.readlink(installed_exec),
+ "Ensure symlink not moved")
+ end
+
+ def test_generate_bin_symlink_update_remove_wrapper
+ return if win_platform? #Windows FS do not support symlinks
+
+ @installer.wrappers = true
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ @installer.generate_bin
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal true, File.exist?(installed_exec)
+
+ @spec = Gem::Specification.new do |s|
+ s.files = ['lib/code.rb']
+ s.name = "a"
+ s.version = "3"
+ s.summary = "summary"
+ s.description = "desc"
+ s.require_path = 'lib'
+ end
+
+ @installer.wrappers = false
+ util_make_exec '3'
+ @installer.gem_dir = util_gem_dir '3'
+ @installer.generate_bin
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal(File.join(util_gem_dir('3'), "bin", "my_exec"),
+ File.readlink(installed_exec),
+ "Ensure symlink moved to latest version")
+ end
+
+ def test_generate_bin_symlink_win32
+ old_win_platform = Gem.win_platform?
+ Gem.win_platform = true
+ @installer.wrappers = false
+ util_make_exec
+ @installer.gem_dir = util_gem_dir
+
+ use_ui @ui do
+ @installer.generate_bin
+ end
+
+ assert_equal true, File.directory?(util_inst_bindir)
+ installed_exec = File.join(util_inst_bindir, "my_exec")
+ assert_equal true, File.exist?(installed_exec)
+
+ assert_match(/Unable to use symlinks on Windows, installing wrapper/i,
+ @ui.error)
+
+ wrapper = File.read installed_exec
+ assert_match(/generated by RubyGems/, wrapper)
+ ensure
+ Gem.win_platform = old_win_platform
+ end
+
+ def test_generate_bin_uses_default_shebang
+ return if win_platform? #Windows FS do not support symlinks
+
+ @installer.wrappers = true
+ util_make_exec
+
+ @installer.generate_bin
+
+ default_shebang = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
+ shebang_line = open("#{@gemhome}/bin/my_exec") { |f| f.readlines.first }
+ assert_match(/\A#!/, shebang_line)
+ assert_match(/#{default_shebang}/, shebang_line)
+ end
+
+ def test_initialize
+ spec = quick_gem 'a' do |s| s.platform = Gem::Platform.new 'mswin32' end
+ gem = File.join @tempdir, "#{spec.full_name}.gem"
+
+ Dir.mkdir util_inst_bindir
+ util_build_gem spec
+ FileUtils.mv File.join(@gemhome, 'cache', "#{spec.full_name}.gem"),
+ @tempdir
+
+ installer = Gem::Installer.new gem
+
+ assert_equal File.join(@gemhome, 'gems', spec.full_name), installer.gem_dir
+ end
+
+ def test_install
+ Dir.mkdir util_inst_bindir
+ util_setup_gem
+
+ build_rake_in do
+ use_ui @ui do
+ assert_equal @spec, @installer.install
+ end
+ end
+
+ gemdir = File.join @gemhome, 'gems', @spec.full_name
+ assert File.exist?(gemdir)
+
+ exe = File.join(gemdir, 'bin', 'executable')
+ assert File.exist?(exe)
+ exe_mode = File.stat(exe).mode & 0111
+ assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?
+
+ assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
+
+ assert File.exist?(File.join(gemdir, 'ext', 'a', 'Rakefile'))
+
+ spec_file = File.join(@gemhome, 'specifications',
+ "#{@spec.full_name}.gemspec")
+
+ assert_equal spec_file, @spec.loaded_from
+ assert File.exist?(spec_file)
+ end
+
+ def test_install_bad_gem
+ gem = nil
+
+ use_ui @ui do
+ Dir.chdir @tempdir do Gem::Builder.new(@spec).build end
+ gem = File.join @tempdir, "#{@spec.full_name}.gem"
+ end
+
+ gem_data = File.open gem, 'rb' do |fp| fp.read 1024 end
+ File.open gem, 'wb' do |fp| fp.write gem_data end
+
+ e = assert_raise Gem::InstallError do
+ use_ui @ui do
+ @installer = Gem::Installer.new gem
+ @installer.install
+ end
+ end
+
+ assert_equal "invalid gem format for #{gem}", e.message
+ end
+
+ def test_install_check_dependencies
+ @spec.add_dependency 'b', '> 5'
+ util_setup_gem
+
+ use_ui @ui do
+ assert_raise Gem::InstallError do
+ @installer.install
+ end
+ end
+ end
+
+ def test_install_force
+ use_ui @ui do
+ installer = Gem::Installer.new old_ruby_required, :force => true
+ installer.install
+ end
+
+ gem_dir = File.join(@gemhome, 'gems', 'old_ruby_required-1')
+ assert File.exist?(gem_dir)
+ end
+
+ def test_install_ignore_dependencies
+ Dir.mkdir util_inst_bindir
+ @spec.add_dependency 'b', '> 5'
+ util_setup_gem
+ @installer.ignore_dependencies = true
+
+ build_rake_in do
+ use_ui @ui do
+ assert_equal @spec, @installer.install
+ end
+ end
+
+ gemdir = File.join @gemhome, 'gems', @spec.full_name
+ assert File.exist?(gemdir)
+
+ exe = File.join(gemdir, 'bin', 'executable')
+ assert File.exist?(exe)
+ exe_mode = File.stat(exe).mode & 0111
+ assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?
+ assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
+
+ assert File.exist?(File.join(@gemhome, 'specifications',
+ "#{@spec.full_name}.gemspec"))
+ end
+
+ def test_install_missing_dirs
+ FileUtils.rm_f File.join(Gem.dir, 'cache')
+ FileUtils.rm_f File.join(Gem.dir, 'docs')
+ FileUtils.rm_f File.join(Gem.dir, 'specifications')
+
+ use_ui @ui do
+ Dir.chdir @tempdir do Gem::Builder.new(@spec).build end
+ gem = File.join @tempdir, "#{@spec.full_name}.gem"
+
+ @installer.install
+ end
+
+ File.directory? File.join(Gem.dir, 'cache')
+ File.directory? File.join(Gem.dir, 'docs')
+ File.directory? File.join(Gem.dir, 'specifications')
+
+ assert File.exist?(File.join(@gemhome, 'cache', "#{@spec.full_name}.gem"))
+ assert File.exist?(File.join(@gemhome, 'specifications',
+ "#{@spec.full_name}.gemspec"))
+ end
+ unless win_platform? # File.chmod doesn't work
+ def test_install_user_local_fallback
+ Dir.mkdir util_inst_bindir
+ File.chmod 0755, @userhome
+ File.chmod 0000, util_inst_bindir
+ File.chmod 0000, Gem.dir
+ install_dir = File.join @userhome, '.gem', 'gems', @spec.full_name
+ @spec.executables = ["executable"]
+
+ build_rake_in do
+ use_ui @ui do
+ util_setup_gem
+ @installer.install
+ end
+ end
+
+ assert File.exist?(File.join(install_dir, 'lib', 'code.rb'))
+ assert File.exist?(File.join(@userhome, '.gem', 'bin', 'executable'))
+ ensure
+ File.chmod 0755, Gem.dir
+ File.chmod 0755, util_inst_bindir
+ end
+
+ def test_install_bindir_read_only
+ Dir.mkdir util_inst_bindir
+ File.chmod 0755, @userhome
+ File.chmod 0000, util_inst_bindir
+
+ build_rake_in do
+ use_ui @ui do
+ setup
+ util_setup_gem
+ @installer.install
+ end
+ end
+
+ assert File.exist?(File.join(@userhome, '.gem', 'bin', 'executable'))
+ ensure
+ File.chmod 0755, util_inst_bindir
+ end
+ end
+
+ def test_install_with_message
+ @spec.post_install_message = 'I am a shiny gem!'
+
+ use_ui @ui do
+ Dir.chdir @tempdir do Gem::Builder.new(@spec).build end
+
+ @installer.install
+ end
+
+ assert_match %r|I am a shiny gem!|, @ui.output
+ end
+
+ def test_install_wrong_ruby_version
+ use_ui @ui do
+ installer = Gem::Installer.new old_ruby_required
+ e = assert_raise Gem::InstallError do
+ installer.install
+ end
+ assert_equal 'old_ruby_required requires Ruby version = 1.4.6',
+ e.message
+ end
+ end
+
+ def test_install_wrong_rubygems_version
+ spec = quick_gem 'old_rubygems_required', '1' do |s|
+ s.required_rubygems_version = '< 0'
+ end
+
+ util_build_gem spec
+
+ gem = File.join @gemhome, 'cache', "#{spec.full_name}.gem"
+
+ use_ui @ui do
+ @installer = Gem::Installer.new gem
+ e = assert_raise Gem::InstallError do
+ @installer.install
+ end
+ assert_equal 'old_rubygems_required requires RubyGems version < 0',
+ e.message
+ end
+ end
+
+ def test_installation_satisfies_dependency_eh
+ dep = Gem::Dependency.new 'a', '>= 2'
+ assert @installer.installation_satisfies_dependency?(dep)
+
+ dep = Gem::Dependency.new 'a', '> 2'
+ assert ! @installer.installation_satisfies_dependency?(dep)
+ end
+
+ def test_shebang
+ util_make_exec '2', "#!/usr/bin/ruby"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_arguments
+ util_make_exec '2', "#!/usr/bin/ruby -ws"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby} -ws", shebang
+ end
+
+ def test_shebang_empty
+ util_make_exec '2', ''
+
+ shebang = @installer.shebang 'my_exec'
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_env
+ util_make_exec '2', "#!/usr/bin/env ruby"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_env_arguments
+ util_make_exec '2', "#!/usr/bin/env ruby -ws"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby} -ws", shebang
+ end
+
+ def test_shebang_env_shebang
+ util_make_exec '2', ''
+ @installer.env_shebang = true
+
+ shebang = @installer.shebang 'my_exec'
+ assert_equal "#!/usr/bin/env #{Gem::ConfigMap[:RUBY_INSTALL_NAME]}", shebang
+ end
+
+ def test_shebang_nested
+ util_make_exec '2', "#!/opt/local/ruby/bin/ruby"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_nested_arguments
+ util_make_exec '2', "#!/opt/local/ruby/bin/ruby -ws"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby} -ws", shebang
+ end
+
+ def test_shebang_version
+ util_make_exec '2', "#!/usr/bin/ruby18"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_version_arguments
+ util_make_exec '2', "#!/usr/bin/ruby18 -ws"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby} -ws", shebang
+ end
+
+ def test_shebang_version_env
+ util_make_exec '2', "#!/usr/bin/env ruby18"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby}", shebang
+ end
+
+ def test_shebang_version_env_arguments
+ util_make_exec '2', "#!/usr/bin/env ruby18 -ws"
+
+ shebang = @installer.shebang 'my_exec'
+
+ assert_equal "#!#{Gem.ruby} -ws", shebang
+ end
+
+ def test_unpack
+ util_setup_gem
+
+ dest = File.join @gemhome, 'gems', @spec.full_name
+
+ @installer.unpack dest
+
+ assert File.exist?(File.join(dest, 'lib', 'code.rb'))
+ assert File.exist?(File.join(dest, 'bin', 'executable'))
+ end
+
+ def test_write_spec
+ spec_dir = File.join @gemhome, 'specifications'
+ spec_file = File.join spec_dir, "#{@spec.full_name}.gemspec"
+ FileUtils.rm spec_file
+ assert !File.exist?(spec_file)
+
+ @installer.spec = @spec
+ @installer.gem_home = @gemhome
+
+ @installer.write_spec
+
+ assert File.exist?(spec_file)
+ assert_equal @spec, eval(File.read(spec_file))
+ end
+
+ def old_ruby_required
+ spec = quick_gem 'old_ruby_required', '1' do |s|
+ s.required_ruby_version = '= 1.4.6'
+ end
+
+ util_build_gem spec
+
+ File.join @gemhome, 'cache', "#{spec.full_name}.gem"
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_local_remote_options.rb b/trunk/test/rubygems/test_gem_local_remote_options.rb
new file mode 100644
index 0000000000..e676c94f21
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_local_remote_options.rb
@@ -0,0 +1,85 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/local_remote_options'
+require 'rubygems/command'
+
+class TestGemLocalRemoteOptions < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Command.new 'dummy', 'dummy'
+ @cmd.extend Gem::LocalRemoteOptions
+ end
+
+ def test_add_local_remote_options
+ @cmd.add_local_remote_options
+
+ args = %w[-l -r -b -B 10 --source http://gems.example.com -p --update-sources]
+ assert @cmd.handles?(args)
+ end
+
+ def test_local_eh
+ assert_equal false, @cmd.local?
+
+ @cmd.options[:domain] = :local
+
+ assert_equal true, @cmd.local?
+
+ @cmd.options[:domain] = :both
+
+ assert_equal true, @cmd.local?
+ end
+
+ def test_remote_eh
+ assert_equal false, @cmd.remote?
+
+ @cmd.options[:domain] = :remote
+
+ assert_equal true, @cmd.remote?
+
+ @cmd.options[:domain] = :both
+
+ assert_equal true, @cmd.remote?
+ end
+
+ def test_source_option
+ @cmd.add_source_option
+
+ s1 = URI.parse 'http://more-gems.example.com/'
+ s2 = URI.parse 'http://even-more-gems.example.com/'
+ s3 = URI.parse 'http://other-gems.example.com/some_subdir'
+
+ @cmd.handle_options %W[--source #{s1} --source #{s2} --source #{s3}]
+
+ assert_equal [s1.to_s, s2.to_s, "#{s3}/"], Gem.sources
+ end
+
+ def test_update_sources_option
+ @cmd.add_update_sources_option
+
+ Gem.configuration.update_sources = false
+
+ @cmd.handle_options %W[--update-sources]
+
+ assert_equal true, Gem.configuration.update_sources
+
+ @cmd.handle_options %W[--no-update-sources]
+
+ assert_equal false, Gem.configuration.update_sources
+ end
+
+ def test_source_option_bad
+ @cmd.add_source_option
+
+ s1 = 'htp://more-gems.example.com'
+
+ assert_raise OptionParser::InvalidArgument do
+ @cmd.handle_options %W[--source #{s1}]
+ end
+
+ assert_equal [@gem_repo], Gem.sources
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_header.rb b/trunk/test/rubygems/test_gem_package_tar_header.rb
new file mode 100644
index 0000000000..9b7708dca8
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_header.rb
@@ -0,0 +1,137 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package'
+
+class TestGemPackageTarHeader < TarTestCase
+
+ def setup
+ super
+
+ header = {
+ :name => 'x',
+ :mode => 0644,
+ :uid => 1000,
+ :gid => 10000,
+ :size => 100,
+ :mtime => 12345,
+ :typeflag => '0',
+ :linkname => 'link',
+ :uname => 'user',
+ :gname => 'group',
+ :devmajor => 1,
+ :devminor => 2,
+ :prefix => 'y',
+ }
+
+ @tar_header = Gem::Package::TarHeader.new header
+ end
+
+ def test_self_from
+ io = TempIO.new @tar_header.to_s
+
+ new_header = Gem::Package::TarHeader.from io
+
+ assert_headers_equal @tar_header, new_header
+ end
+
+ def test_initialize
+ assert_equal '', @tar_header.checksum, 'checksum'
+ assert_equal 1, @tar_header.devmajor, 'devmajor'
+ assert_equal 2, @tar_header.devminor, 'devminor'
+ assert_equal 10000, @tar_header.gid, 'gid'
+ assert_equal 'group', @tar_header.gname, 'gname'
+ assert_equal 'link', @tar_header.linkname, 'linkname'
+ assert_equal 'ustar', @tar_header.magic, 'magic'
+ assert_equal 0644, @tar_header.mode, 'mode'
+ assert_equal 12345, @tar_header.mtime, 'mtime'
+ assert_equal 'x', @tar_header.name, 'name'
+ assert_equal 'y', @tar_header.prefix, 'prefix'
+ assert_equal 100, @tar_header.size, 'size'
+ assert_equal '0', @tar_header.typeflag, 'typeflag'
+ assert_equal 1000, @tar_header.uid, 'uid'
+ assert_equal 'user', @tar_header.uname, 'uname'
+ assert_equal '00', @tar_header.version, 'version'
+
+ assert !@tar_header.empty?, 'empty'
+ end
+
+ def test_initialize_bad
+ assert_raises ArgumentError do
+ Gem::Package::TarHeader.new :name => '', :size => '', :mode => ''
+ end
+
+ assert_raises ArgumentError do
+ Gem::Package::TarHeader.new :name => '', :size => '', :prefix => ''
+ end
+
+ assert_raises ArgumentError do
+ Gem::Package::TarHeader.new :name => '', :prefix => '', :mode => ''
+ end
+
+ assert_raises ArgumentError do
+ Gem::Package::TarHeader.new :prefix => '', :size => '', :mode => ''
+ end
+ end
+
+ def test_empty_eh
+ assert !@tar_header.empty?
+
+ @tar_header = Gem::Package::TarHeader.new :name => 'x', :prefix => '',
+ :mode => 0, :size => 0,
+ :empty => true
+
+ assert @tar_header.empty?
+ end
+
+ def test_equals2
+ assert_equal @tar_header, @tar_header
+ assert_equal @tar_header, @tar_header.dup
+ end
+
+ def test_to_s
+ expected = <<-EOF.split("\n").join
+x\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\0000000644\0000001750\0000023420\00000000000144\00000000030071
+\000012467\000 0link\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000ustar\00000user\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+group\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\0000000001\0000000002\000y\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
+\000\000\000\000\000\000\000\000\000\000
+ EOF
+
+ assert_headers_equal expected, @tar_header
+ end
+
+ def test_update_checksum
+ assert_equal '', @tar_header.checksum
+
+ @tar_header.update_checksum
+
+ assert_equal '012467', @tar_header.checksum
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_input.rb b/trunk/test/rubygems/test_gem_package_tar_input.rb
new file mode 100644
index 0000000000..279026b149
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_input.rb
@@ -0,0 +1,119 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package/tar_input'
+
+class TestGemPackageTarInput < TarTestCase
+
+ # Sometimes the setgid bit doesn't take. Don't know if this is a problem on
+ # all systems, or just some. But for now, we will ignore it in the tests.
+ SETGID_BIT = 02000
+
+ def setup
+ super
+
+ inner_tar = tar_file_header("bla", "", 0612, 10)
+ inner_tar += "0123456789" + "\0" * 502
+ inner_tar += tar_file_header("foo", "", 0636, 5)
+ inner_tar += "01234" + "\0" * 507
+ inner_tar += tar_dir_header("__dir__", "", 0600)
+ inner_tar += "\0" * 1024
+ str = TempIO.new
+
+ begin
+ os = Zlib::GzipWriter.new str
+ os.write inner_tar
+ ensure
+ os.finish
+ end
+
+ str.rewind
+
+ @file = File.join @tempdir, 'bla.tar'
+
+ File.open @file, 'wb' do |f|
+ f.write tar_file_header("data.tar.gz", "", 0644, str.string.size)
+ f.write str.string
+ f.write "\0" * ((512 - (str.string.size % 512)) % 512 )
+
+ @spec = Gem::Specification.new do |spec|
+ spec.author = "Mauricio :)"
+ end
+
+ meta = @spec.to_yaml
+
+ f.write tar_file_header("metadata", "", 0644, meta.size)
+ f.write meta + "\0" * (1024 - meta.size)
+ f.write "\0" * 1024
+ end
+
+ @entry_names = %w{bla foo __dir__}
+ @entry_sizes = [10, 5, 0]
+ #FIXME: are these modes system dependent?
+ @entry_modes = [0100612, 0100636, 040600]
+ @entry_files = %W[#{@tempdir}/bla #{@tempdir}/foo]
+ @entry_contents = %w[0123456789 01234]
+ end
+
+ def test_each_works
+ open @file, 'rb' do |io|
+ Gem::Package::TarInput.open io do |tar_input|
+ count = 0
+
+ tar_input.each_with_index do |entry, i|
+ count = i
+
+ assert_kind_of Gem::Package::TarReader::Entry, entry
+ assert_equal @entry_names[i], entry.header.name
+ assert_equal @entry_sizes[i], entry.header.size
+ end
+
+ assert_equal 2, count
+
+ assert_equal @spec, tar_input.metadata
+ end
+ end
+ end
+
+ def test_extract_entry_works
+ open @file, 'rb' do |io|
+ Gem::Package::TarInput.open io do |tar_input|
+ assert_equal @spec, tar_input.metadata
+
+ count = 0
+
+ tar_input.each_with_index do |entry, i|
+ count = i
+ tar_input.extract_entry @tempdir, entry
+ name = File.join @tempdir, entry.header.name
+
+ if entry.directory?
+ assert File.dir?(name)
+ else
+ assert File.file?(name)
+ assert_equal @entry_sizes[i], File.stat(name).size
+ #FIXME: win32? !!
+ end
+
+ unless Gem.win_platform? then
+ assert_equal @entry_modes[i], File.stat(name).mode & (~SETGID_BIT)
+ end
+ end
+
+ assert_equal 2, count
+ end
+ end
+
+ @entry_files.each_with_index do |x, i|
+ assert File.file?(x)
+ assert_equal @entry_contents[i], File.read_b(x)
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_output.rb b/trunk/test/rubygems/test_gem_package_tar_output.rb
new file mode 100644
index 0000000000..06dbb1a4da
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_output.rb
@@ -0,0 +1,104 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package/tar_output'
+
+class TestGemPackageTarOutput < TarTestCase
+
+ def setup
+ super
+
+ @file = File.join @tempdir, 'bla2.tar'
+ end
+
+ def test_self_open
+ open @file, 'wb' do |tar_io|
+ Gem::Package::TarOutput.open tar_io do |tar_writer|
+ tar_writer.add_file_simple 'README', 0, 17 do |io|
+ io.write "This is a README\n"
+ end
+
+ tar_writer.metadata = "This is some metadata\n"
+ end
+ end
+
+ files = util_extract
+
+ name, data = files.shift
+ assert_equal 'data.tar.gz', name
+
+ gz = Zlib::GzipReader.new StringIO.new(data)
+
+ Gem::Package::TarReader.new gz do |tar_reader|
+ tar_reader.each do |entry|
+ assert_equal 'README', entry.full_name
+ assert_equal "This is a README\n", entry.read
+ end
+ end
+
+ gz.close
+
+ name, data = files.shift
+ assert_equal 'metadata.gz', name
+
+ gz = Zlib::GzipReader.new StringIO.new(data)
+ assert_equal "This is some metadata\n", gz.read
+
+ assert files.empty?
+ ensure
+ gz.close if gz
+ end
+
+ if defined? OpenSSL then
+ def test_self_open_signed
+ signer = Gem::Security::Signer.new @private_key, [@public_cert]
+
+ open @file, 'wb' do |tar_io|
+ Gem::Package::TarOutput.open tar_io, signer do |tar_writer|
+ tar_writer.add_file_simple 'README', 0, 17 do |io|
+ io.write "This is a README\n"
+ end
+
+ tar_writer.metadata = "This is some metadata\n"
+ end
+ end
+
+ files = util_extract
+
+ name, data = files.shift
+ assert_equal 'data.tar.gz', name
+
+ name, data = files.shift
+ assert_equal 'metadata.gz', name
+
+ name, data = files.shift
+ assert_equal 'data.tar.gz.sig', name
+
+ name, data = files.shift
+ assert_equal 'metadata.gz.sig', name
+
+ assert files.empty?
+ end
+ end
+
+ def util_extract
+ files = []
+
+ open @file, 'rb' do |io|
+ Gem::Package::TarReader.new io do |tar_reader|
+ tar_reader.each do |entry|
+ files << [entry.full_name, entry.read]
+ end
+ end
+ end
+
+ files
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_reader.rb b/trunk/test/rubygems/test_gem_package_tar_reader.rb
new file mode 100644
index 0000000000..6962088878
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_reader.rb
@@ -0,0 +1,53 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package'
+
+class TestGemPackageTarReader < TarTestCase
+
+ def test_each_entry
+ tar = tar_dir_header "foo", "bar", 0
+ tar << tar_file_header("bar", "baz", 0, 0)
+
+ io = TempIO.new tar
+
+ entries = 0
+
+ Gem::Package::TarReader.new io do |tar_reader|
+ tar_reader.each_entry do |entry|
+ assert_kind_of Gem::Package::TarReader::Entry, entry
+
+ entries += 1
+ end
+ end
+
+ assert_equal 2, entries
+ end
+
+ def test_rewind
+ content = ('a'..'z').to_a.join(" ")
+
+ str = tar_file_header("lib/foo", "", 010644, content.size) + content +
+ "\0" * (512 - content.size)
+ str << "\0" * 1024
+
+ Gem::Package::TarReader.new(TempIO.new(str)) do |tar_reader|
+ 3.times do
+ tar_reader.rewind
+ i = 0
+ tar_reader.each_entry do |entry|
+ assert_equal(content, entry.read)
+ i += 1
+ end
+ assert_equal(1, i)
+ end
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_reader_entry.rb b/trunk/test/rubygems/test_gem_package_tar_reader_entry.rb
new file mode 100644
index 0000000000..7e25143a85
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_reader_entry.rb
@@ -0,0 +1,116 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package'
+
+class TestGemPackageTarReaderEntry < TarTestCase
+
+ def setup
+ super
+
+ @contents = ('a'..'z').to_a.join * 100
+
+ @tar = ''
+ @tar << tar_file_header("lib/foo", "", 0, @contents.size)
+ @tar << @contents
+ @tar << "\0" * (512 - (@tar.size % 512))
+
+ @entry = util_entry @tar
+ end
+
+ def test_bytes_read
+ assert_equal 0, @entry.bytes_read
+
+ @entry.getc
+
+ assert_equal 1, @entry.bytes_read
+ end
+
+ def test_close
+ @entry.close
+
+ assert @entry.bytes_read
+
+ e = assert_raise IOError do @entry.eof? end
+ assert_equal 'closed Gem::Package::TarReader::Entry', e.message
+
+ e = assert_raise IOError do @entry.getc end
+ assert_equal 'closed Gem::Package::TarReader::Entry', e.message
+
+ e = assert_raise IOError do @entry.pos end
+ assert_equal 'closed Gem::Package::TarReader::Entry', e.message
+
+ e = assert_raise IOError do @entry.read end
+ assert_equal 'closed Gem::Package::TarReader::Entry', e.message
+
+ e = assert_raise IOError do @entry.rewind end
+ assert_equal 'closed Gem::Package::TarReader::Entry', e.message
+ end
+
+ def test_closed_eh
+ @entry.close
+
+ assert @entry.closed?
+ end
+
+ def test_eof_eh
+ @entry.read
+
+ assert @entry.eof?
+ end
+
+ def test_full_name
+ assert_equal 'lib/foo', @entry.full_name
+ end
+
+ def test_getc
+ assert_equal ?a, @entry.getc
+ end
+
+ def test_directory_eh
+ assert_equal false, @entry.directory?
+ assert_equal true, util_dir_entry.directory?
+ end
+
+ def test_file_eh
+ assert_equal true, @entry.file?
+ assert_equal false, util_dir_entry.file?
+ end
+
+ def test_pos
+ assert_equal 0, @entry.pos
+
+ @entry.getc
+
+ assert_equal 1, @entry.pos
+ end
+
+ def test_read
+ assert_equal @contents, @entry.read
+ end
+
+ def test_read_big
+ assert_equal @contents, @entry.read(@contents.size * 2)
+ end
+
+ def test_read_small
+ assert_equal @contents[0...100], @entry.read(100)
+ end
+
+ def test_rewind
+ char = @entry.getc
+
+ @entry.rewind
+
+ assert_equal 0, @entry.pos
+
+ assert_equal char, @entry.getc
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_package_tar_writer.rb b/trunk/test/rubygems/test_gem_package_tar_writer.rb
new file mode 100644
index 0000000000..e066c2967f
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_package_tar_writer.rb
@@ -0,0 +1,151 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_package_tar_test_case')
+require 'rubygems/package/tar_writer'
+
+class TestTarWriter < TarTestCase
+
+ def setup
+ super
+
+ @data = 'abcde12345'
+ @io = TempIO.new
+ @tar_writer = Gem::Package::TarWriter.new @io
+ end
+
+ def teardown
+ @tar_writer.close unless @tar_writer.closed?
+
+ super
+ end
+
+ def test_add_file
+ @tar_writer.add_file 'x', 0644 do |f| f.write 'a' * 10 end
+
+ assert_headers_equal(tar_file_header('x', '', 0644, 10),
+ @io.string[0, 512])
+ assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
+ assert_equal 1024, @io.pos
+ end
+
+ def test_add_file_simple
+ @tar_writer.add_file_simple 'x', 0644, 10 do |io| io.write "a" * 10 end
+
+ assert_headers_equal(tar_file_header('x', '', 0644, 10),
+ @io.string[0, 512])
+
+ assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
+ assert_equal 1024, @io.pos
+ end
+
+ def test_add_file_simple_padding
+ @tar_writer.add_file_simple 'x', 0, 100
+
+ assert_headers_equal tar_file_header('x', '', 0, 100),
+ @io.string[0, 512]
+
+ assert_equal "\0" * 512, @io.string[512, 512]
+ end
+
+ def test_add_file_simple_data
+ @tar_writer.add_file_simple("lib/foo/bar", 0, 10) { |f| f.write @data }
+ @tar_writer.flush
+
+ assert_equal @data + ("\0" * (512-@data.size)),
+ @io.string[512, 512]
+ end
+
+ def test_add_file_simple_size
+ assert_raise Gem::Package::TarWriter::FileOverflow do
+ @tar_writer.add_file_simple("lib/foo/bar", 0, 10) do |io|
+ io.write "1" * 11
+ end
+ end
+ end
+
+ def test_add_file_unseekable
+ assert_raise Gem::Package::NonSeekableIO do
+ Gem::Package::TarWriter.new(Object.new).add_file 'x', 0
+ end
+ end
+
+ def test_close
+ @tar_writer.close
+
+ assert_equal "\0" * 1024, @io.string
+
+ e = assert_raise IOError do
+ @tar_writer.close
+ end
+ assert_equal 'closed Gem::Package::TarWriter', e.message
+
+ e = assert_raise IOError do
+ @tar_writer.flush
+ end
+ assert_equal 'closed Gem::Package::TarWriter', e.message
+
+ e = assert_raise IOError do
+ @tar_writer.add_file 'x', 0
+ end
+ assert_equal 'closed Gem::Package::TarWriter', e.message
+
+ e = assert_raise IOError do
+ @tar_writer.add_file_simple 'x', 0, 0
+ end
+ assert_equal 'closed Gem::Package::TarWriter', e.message
+
+ e = assert_raise IOError do
+ @tar_writer.mkdir 'x', 0
+ end
+ assert_equal 'closed Gem::Package::TarWriter', e.message
+ end
+
+ def test_mkdir
+ @tar_writer.mkdir 'foo', 0644
+
+ assert_headers_equal tar_dir_header('foo', '', 0644),
+ @io.string[0, 512]
+ assert_equal 512, @io.pos
+ end
+
+ def test_split_name
+ assert_equal ['b' * 100, 'a' * 155],
+ @tar_writer.split_name("#{'a' * 155}/#{'b' * 100}")
+
+ assert_equal ["#{'qwer/' * 19}bla", 'a' * 151],
+ @tar_writer.split_name("#{'a' * 151}/#{'qwer/' * 19}bla")
+ end
+
+ def test_split_name_too_long_name
+ name = File.join 'a', 'b' * 100
+ assert_equal ['b' * 100, 'a'], @tar_writer.split_name(name)
+
+ assert_raise Gem::Package::TooLongFileName do
+ name = File.join 'a', 'b' * 101
+ @tar_writer.split_name name
+ end
+ end
+
+ def test_split_name_too_long_prefix
+ name = File.join 'a' * 155, 'b'
+ assert_equal ['b', 'a' * 155], @tar_writer.split_name(name)
+
+ assert_raise Gem::Package::TooLongFileName do
+ name = File.join 'a' * 156, 'b'
+ @tar_writer.split_name name
+ end
+ end
+
+ def test_split_name_too_long_total
+ assert_raise Gem::Package::TooLongFileName do
+ @tar_writer.split_name 'a' * 257
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_platform.rb b/trunk/test/rubygems/test_gem_platform.rb
new file mode 100644
index 0000000000..648c2a9f36
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_platform.rb
@@ -0,0 +1,259 @@
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'test/unit'
+require 'rubygems/platform'
+require 'rbconfig'
+
+class TestGemPlatform < RubyGemTestCase
+
+ def test_self_const_missing
+ consts = [:DARWIN, :LINUX_586, :MSWIN32, :PPC_DARWIN, :WIN32, :X86_LINUX]
+
+ consts.each do |const|
+ e = assert_raise NameError do
+ Gem::Platform.const_missing const
+ end
+
+ assert_equal "#{const} has been removed, use CURRENT instead",
+ e.message
+ end
+ end
+
+ def test_self_local
+ util_set_arch 'i686-darwin8.10.1'
+
+ assert_equal Gem::Platform.new(%w[x86 darwin 8]), Gem::Platform.local
+ end
+
+ def test_self_match
+ assert Gem::Platform.match(nil), 'nil == ruby'
+ assert Gem::Platform.match(Gem::Platform.local), 'exact match'
+ assert Gem::Platform.match(Gem::Platform.local.to_s), '=~ match'
+ assert Gem::Platform.match(Gem::Platform::RUBY), 'ruby'
+ end
+
+ def test_self_new
+ assert_equal Gem::Platform.local, Gem::Platform.new(Gem::Platform::CURRENT)
+ assert_equal Gem::Platform::RUBY, Gem::Platform.new(Gem::Platform::RUBY)
+ assert_equal Gem::Platform::RUBY, Gem::Platform.new(nil)
+ assert_equal Gem::Platform::RUBY, Gem::Platform.new('')
+ end
+
+ def test_initialize
+ test_cases = {
+ 'amd64-freebsd6' => ['amd64', 'freebsd', '6'],
+ 'hppa2.0w-hpux11.31' => ['hppa2.0w', 'hpux', '11'],
+ 'java' => [nil, 'java', nil],
+ 'jruby' => [nil, 'java', nil],
+ 'powerpc-aix5.3.0.0' => ['powerpc', 'aix', '5'],
+ 'powerpc-darwin7' => ['powerpc', 'darwin', '7'],
+ 'powerpc-darwin8' => ['powerpc', 'darwin', '8'],
+ 'powerpc-linux' => ['powerpc', 'linux', nil],
+ 'powerpc64-linux' => ['powerpc64', 'linux', nil],
+ 'sparc-solaris2.10' => ['sparc', 'solaris', '2.10'],
+ 'sparc-solaris2.8' => ['sparc', 'solaris', '2.8'],
+ 'sparc-solaris2.9' => ['sparc', 'solaris', '2.9'],
+ 'universal-darwin8' => ['universal', 'darwin', '8'],
+ 'universal-darwin9' => ['universal', 'darwin', '9'],
+ 'i386-cygwin' => ['x86', 'cygwin', nil],
+ 'i686-darwin' => ['x86', 'darwin', nil],
+ 'i686-darwin8.4.1' => ['x86', 'darwin', '8'],
+ 'i386-freebsd4.11' => ['x86', 'freebsd', '4'],
+ 'i386-freebsd5' => ['x86', 'freebsd', '5'],
+ 'i386-freebsd6' => ['x86', 'freebsd', '6'],
+ 'i386-freebsd7' => ['x86', 'freebsd', '7'],
+ 'i386-java1.5' => ['x86', 'java', '1.5'],
+ 'x86-java1.6' => ['x86', 'java', '1.6'],
+ 'i386-java1.6' => ['x86', 'java', '1.6'],
+ 'i686-linux' => ['x86', 'linux', nil],
+ 'i586-linux' => ['x86', 'linux', nil],
+ 'i486-linux' => ['x86', 'linux', nil],
+ 'i386-linux' => ['x86', 'linux', nil],
+ 'i586-linux-gnu' => ['x86', 'linux', nil],
+ 'i386-linux-gnu' => ['x86', 'linux', nil],
+ 'i386-mingw32' => ['x86', 'mingw32', nil],
+ 'i386-mswin32' => ['x86', 'mswin32', nil],
+ 'i386-mswin32_80' => ['x86', 'mswin32', '80'],
+ 'i386-mswin32-80' => ['x86', 'mswin32', '80'],
+ 'x86-mswin32' => ['x86', 'mswin32', nil],
+ 'x86-mswin32_60' => ['x86', 'mswin32', '60'],
+ 'x86-mswin32-60' => ['x86', 'mswin32', '60'],
+ 'i386-netbsdelf' => ['x86', 'netbsdelf', nil],
+ 'i386-openbsd4.0' => ['x86', 'openbsd', '4.0'],
+ 'i386-solaris2.10' => ['x86', 'solaris', '2.10'],
+ 'i386-solaris2.8' => ['x86', 'solaris', '2.8'],
+ 'mswin32' => ['x86', 'mswin32', nil],
+ 'x86_64-linux' => ['x86_64', 'linux', nil],
+ 'x86_64-openbsd3.9' => ['x86_64', 'openbsd', '3.9'],
+ 'x86_64-openbsd4.0' => ['x86_64', 'openbsd', '4.0'],
+ }
+
+ test_cases.each do |arch, expected|
+ platform = Gem::Platform.new arch
+ assert_equal expected, platform.to_a, arch.inspect
+ end
+ end
+
+ def test_initialize_command_line
+ expected = ['x86', 'mswin32', nil]
+
+ platform = Gem::Platform.new 'i386-mswin32'
+
+ assert_equal expected, platform.to_a, 'i386-mswin32'
+
+ expected = ['x86', 'mswin32', '80']
+
+ platform = Gem::Platform.new 'i386-mswin32-80'
+
+ assert_equal expected, platform.to_a, 'i386-mswin32-80'
+ end
+
+ def test_initialize_mswin32_vc6
+ orig_RUBY_SO_NAME = Config::CONFIG['RUBY_SO_NAME']
+ Config::CONFIG['RUBY_SO_NAME'] = 'msvcrt-ruby18'
+
+ expected = ['x86', 'mswin32', nil]
+
+ platform = Gem::Platform.new 'i386-mswin32'
+
+ assert_equal expected, platform.to_a, 'i386-mswin32 VC6'
+ ensure
+ Config::CONFIG['RUBY_SO_NAME'] = orig_RUBY_SO_NAME
+ end
+
+ def test_initialize_platform
+ platform = Gem::Platform.new 'cpu-my_platform1'
+ expected = Gem::Platform.new platform
+
+ assert_equal 'cpu', platform.cpu
+ assert_equal 'my_platform', platform.os
+ assert_equal '1', platform.version
+ end
+
+ def test_initialize_test
+ platform = Gem::Platform.new 'cpu-my_platform1'
+ assert_equal 'cpu', platform.cpu
+ assert_equal 'my_platform', platform.os
+ assert_equal '1', platform.version
+
+ platform = Gem::Platform.new 'cpu-other_platform1'
+ assert_equal 'cpu', platform.cpu
+ assert_equal 'other_platform', platform.os
+ assert_equal '1', platform.version
+ end
+
+ def test_to_s
+ if win_platform? then
+ assert_equal 'x86-mswin32-60', Gem::Platform.local.to_s
+ else
+ assert_equal 'x86-darwin-8', Gem::Platform.local.to_s
+ end
+ end
+
+ def test_equals2
+ my = Gem::Platform.new %w[cpu my_platform 1]
+ other = Gem::Platform.new %w[cpu other_platform 1]
+
+ assert_equal my, my
+ assert_not_equal my, other
+ assert_not_equal other, my
+ end
+
+ def test_equals3
+ my = Gem::Platform.new %w[cpu my_platform 1]
+ other = Gem::Platform.new %w[cpu other_platform 1]
+
+ assert(my === my)
+ assert !(other === my)
+ assert !(my === other)
+ end
+
+ def test_equals3_cpu
+ ppc_darwin8 = Gem::Platform.new 'powerpc-darwin8.0'
+ uni_darwin8 = Gem::Platform.new 'universal-darwin8.0'
+ x86_darwin8 = Gem::Platform.new 'i686-darwin8.0'
+
+ util_set_arch 'powerpc-darwin8'
+ assert((ppc_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
+ assert((uni_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
+ assert !(x86_darwin8 === Gem::Platform.local), 'powerpc =~ universal'
+
+ util_set_arch 'i686-darwin8'
+ assert !(ppc_darwin8 === Gem::Platform.local), 'powerpc =~ universal'
+ assert((uni_darwin8 === Gem::Platform.local), 'x86 =~ universal')
+ assert((x86_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
+
+ util_set_arch 'universal-darwin8'
+ assert((ppc_darwin8 === Gem::Platform.local), 'universal =~ ppc')
+ assert((uni_darwin8 === Gem::Platform.local), 'universal =~ universal')
+ assert((x86_darwin8 === Gem::Platform.local), 'universal =~ x86')
+ end
+
+ def test_equals3_version
+ util_set_arch 'i686-darwin8'
+
+ x86_darwin = Gem::Platform.new ['x86', 'darwin', nil]
+ x86_darwin7 = Gem::Platform.new ['x86', 'darwin', '7']
+ x86_darwin8 = Gem::Platform.new ['x86', 'darwin', '8']
+ x86_darwin9 = Gem::Platform.new ['x86', 'darwin', '9']
+
+ assert((x86_darwin === Gem::Platform.local), 'x86_darwin === x86_darwin8')
+ assert((x86_darwin8 === Gem::Platform.local), 'x86_darwin8 === x86_darwin8')
+
+ assert !(x86_darwin7 === Gem::Platform.local), 'x86_darwin7 === x86_darwin8'
+ assert !(x86_darwin9 === Gem::Platform.local), 'x86_darwin9 === x86_darwin8'
+ end
+
+ def test_equals_tilde
+ util_set_arch 'i386-mswin32'
+
+ assert_match 'mswin32', Gem::Platform.local
+ assert_match 'i386-mswin32', Gem::Platform.local
+
+ # oddballs
+ assert_match 'i386-mswin32-mq5.3', Gem::Platform.local
+ assert_match 'i386-mswin32-mq6', Gem::Platform.local
+ deny_match 'win32-1.8.2-VC7', Gem::Platform.local
+ deny_match 'win32-1.8.4-VC6', Gem::Platform.local
+ deny_match 'win32-source', Gem::Platform.local
+ deny_match 'windows', Gem::Platform.local
+
+ util_set_arch 'i686-linux'
+ assert_match 'i486-linux', Gem::Platform.local
+ assert_match 'i586-linux', Gem::Platform.local
+ assert_match 'i686-linux', Gem::Platform.local
+
+ util_set_arch 'i686-darwin8'
+ assert_match 'i686-darwin8.4.1', Gem::Platform.local
+ assert_match 'i686-darwin8.8.2', Gem::Platform.local
+
+ util_set_arch 'java'
+ assert_match 'java', Gem::Platform.local
+ assert_match 'jruby', Gem::Platform.local
+
+ util_set_arch 'powerpc-darwin'
+ assert_match 'powerpc-darwin', Gem::Platform.local
+
+ util_set_arch 'powerpc-darwin7'
+ assert_match 'powerpc-darwin7.9.0', Gem::Platform.local
+
+ util_set_arch 'powerpc-darwin8'
+ assert_match 'powerpc-darwin8.10.0', Gem::Platform.local
+
+ util_set_arch 'sparc-solaris2.8'
+ assert_match 'sparc-solaris2.8-mq5.3', Gem::Platform.local
+ end
+
+ def assert_match(pattern, platform, message = '')
+ full_message = build_message message, "<?> expected to be =~\n<?>.",
+ platform, pattern
+ assert_block(full_message) { platform =~ pattern }
+ end
+
+ def deny_match(pattern, platform, message = '')
+ full_message = build_message message, "<?> expected to be !~\n<?>.",
+ platform, pattern
+ assert_block(full_message) { platform !~ pattern }
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_remote_fetcher.rb b/trunk/test/rubygems/test_gem_remote_fetcher.rb
new file mode 100644
index 0000000000..484104dd8f
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_remote_fetcher.rb
@@ -0,0 +1,692 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'webrick'
+require 'zlib'
+require 'rubygems/remote_fetcher'
+require 'ostruct'
+
+# = Testing Proxy Settings
+#
+# These tests check the proper proxy server settings by running two
+# web servers. The web server at http://localhost:#{SERVER_PORT}
+# represents the normal gem server and returns a gemspec with a rake
+# version of 0.4.11. The web server at http://localhost:#{PROXY_PORT}
+# represents the proxy server and returns a different dataset where
+# rake has version 0.4.2. This allows us to detect which server is
+# returning the data.
+#
+# Note that the proxy server is not a *real* proxy server. But our
+# software doesn't really care, as long as we hit the proxy URL when a
+# proxy is configured.
+
+class TestGemRemoteFetcher < RubyGemTestCase
+
+ include Gem::DefaultUserInteraction
+
+ SERVER_DATA = <<-EOY
+--- !ruby/object:Gem::Cache
+gems:
+ rake-0.4.11: !ruby/object:Gem::Specification
+ rubygems_version: "0.7"
+ specification_version: 1
+ name: rake
+ version: !ruby/object:Gem::Version
+ version: 0.4.11
+ date: 2004-11-12
+ summary: Ruby based make-like utility.
+ require_paths:
+ - lib
+ author: Jim Weirich
+ email: jim@weirichhouse.org
+ homepage: http://rake.rubyforge.org
+ rubyforge_project: rake
+ description: Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.
+ autorequire:
+ default_executable: rake
+ bindir: bin
+ has_rdoc: true
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
+ requirements:
+ -
+ - ">"
+ - !ruby/object:Gem::Version
+ version: 0.0.0
+ version:
+ platform: ruby
+ files:
+ - README
+ test_files: []
+ library_stubs:
+ rdoc_options:
+ extra_rdoc_files:
+ executables:
+ - rake
+ extensions: []
+ requirements: []
+ dependencies: []
+ EOY
+
+ PROXY_DATA = SERVER_DATA.gsub(/0.4.11/, '0.4.2')
+
+ # don't let 1.8 and 1.9 autotest collide
+ RUBY_VERSION =~ /(\d+)\.(\d+)\.(\d+)/
+ # don't let parallel runners collide
+ PROXY_PORT = process_based_port + 100 + $1.to_i * 100 + $2.to_i * 10 + $3.to_i
+ SERVER_PORT = process_based_port + 200 + $1.to_i * 100 + $2.to_i * 10 + $3.to_i
+
+ def setup
+ super
+ self.class.start_servers
+ self.class.enable_yaml = true
+ self.class.enable_zip = false
+ ENV.delete 'http_proxy'
+ ENV.delete 'HTTP_PROXY'
+ ENV.delete 'http_proxy_user'
+ ENV.delete 'HTTP_PROXY_USER'
+ ENV.delete 'http_proxy_pass'
+ ENV.delete 'HTTP_PROXY_PASS'
+
+ base_server_uri = "http://localhost:#{SERVER_PORT}"
+ @proxy_uri = "http://localhost:#{PROXY_PORT}"
+
+ @server_uri = base_server_uri + "/yaml"
+ @server_z_uri = base_server_uri + "/yaml.Z"
+
+ # REFACTOR: copied from test_gem_dependency_installer.rb
+ @gems_dir = File.join @tempdir, 'gems'
+ @cache_dir = File.join @gemhome, 'cache'
+ FileUtils.mkdir @gems_dir
+
+ @a1, @a1_gem = util_gem 'a', '1' do |s| s.executables << 'a_bin' end
+
+ Gem::RemoteFetcher.fetcher = nil
+
+ @fetcher = Gem::RemoteFetcher.fetcher
+ end
+
+ def test_self_fetcher
+ fetcher = Gem::RemoteFetcher.fetcher
+ assert_not_nil fetcher
+ assert_kind_of Gem::RemoteFetcher, fetcher
+ end
+
+ def test_self_fetcher_with_proxy
+ proxy_uri = 'http://proxy.example.com'
+ Gem.configuration[:http_proxy] = proxy_uri
+ Gem::RemoteFetcher.fetcher = nil
+
+ fetcher = Gem::RemoteFetcher.fetcher
+
+ assert_not_nil fetcher
+ assert_kind_of Gem::RemoteFetcher, fetcher
+ assert_equal proxy_uri, fetcher.instance_variable_get(:@proxy_uri).to_s
+ end
+
+ def test_self_fetcher_with_proxy_URI
+ proxy_uri = URI.parse 'http://proxy.example.com'
+ Gem.configuration[:http_proxy] = proxy_uri
+ fetcher = Gem::RemoteFetcher.fetcher
+ assert_not_nil fetcher
+ assert_kind_of Gem::RemoteFetcher, fetcher
+ assert_equal proxy_uri, fetcher.instance_variable_get(:@proxy_uri)
+ end
+
+ def test_fetch_size_bad_uri
+ fetcher = Gem::RemoteFetcher.new nil
+
+ e = assert_raise ArgumentError do
+ fetcher.fetch_size 'gems.example.com/yaml'
+ end
+
+ assert_equal 'uri is not an HTTP URI', e.message
+ end
+
+ def test_fetch_size_socket_error
+ fetcher = Gem::RemoteFetcher.new nil
+ def fetcher.connection_for(uri)
+ raise SocketError, "tarded"
+ end
+
+ uri = 'http://gems.example.com/yaml'
+ e = assert_raise Gem::RemoteFetcher::FetchError do
+ fetcher.fetch_size uri
+ end
+
+ assert_equal "SocketError: tarded (#{uri})", e.message
+ end
+
+ def test_no_proxy
+ use_ui @ui do
+ assert_data_from_server @fetcher.fetch_path(@server_uri)
+ assert_equal SERVER_DATA.size, @fetcher.fetch_size(@server_uri)
+ end
+ end
+
+ def util_fuck_with_fetcher data, blow = false
+ fetcher = Gem::RemoteFetcher.fetcher
+ fetcher.instance_variable_set :@test_data, data
+
+ unless blow then
+ def fetcher.fetch_path arg
+ @test_arg = arg
+ @test_data
+ end
+ else
+ def fetcher.fetch_path arg
+ # OMG I'm such an ass
+ class << self; remove_method :fetch_path; end
+ def self.fetch_path arg
+ @test_arg = arg
+ @test_data
+ end
+
+ raise Gem::RemoteFetcher::FetchError.new("haha!", nil)
+ end
+ end
+
+ fetcher
+ end
+
+ def test_download
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ fetcher = util_fuck_with_fetcher a1_data
+
+ a1_cache_gem = File.join(@gemhome, 'cache', "#{@a1.full_name}.gem")
+ assert_equal a1_cache_gem, fetcher.download(@a1, 'http://gems.example.com')
+ assert_equal("http://gems.example.com/gems/a-1.gem",
+ fetcher.instance_variable_get(:@test_arg).to_s)
+ assert File.exist?(a1_cache_gem)
+ end
+
+ def test_download_cached
+ FileUtils.mv @a1_gem, @cache_dir
+
+ inst = Gem::RemoteFetcher.fetcher
+
+ assert_equal File.join(@gemhome, 'cache', "#{@a1.full_name}.gem"),
+ inst.download(@a1, 'http://gems.example.com')
+ end
+
+ def test_download_local
+ FileUtils.mv @a1_gem, @tempdir
+ local_path = File.join @tempdir, "#{@a1.full_name}.gem"
+ inst = nil
+
+ Dir.chdir @tempdir do
+ inst = Gem::RemoteFetcher.fetcher
+ end
+
+ assert_equal File.join(@gemhome, 'cache', "#{@a1.full_name}.gem"),
+ inst.download(@a1, local_path)
+ end
+
+ def test_download_install_dir
+ a1_data = nil
+ File.open @a1_gem, 'rb' do |fp|
+ a1_data = fp.read
+ end
+
+ fetcher = util_fuck_with_fetcher a1_data
+
+ install_dir = File.join @tempdir, 'more_gems'
+
+ a1_cache_gem = File.join install_dir, 'cache', "#{@a1.full_name}.gem"
+ FileUtils.mkdir_p(File.dirname(a1_cache_gem))
+ actual = fetcher.download(@a1, 'http://gems.example.com', install_dir)
+
+ assert_equal a1_cache_gem, actual
+ assert_equal("http://gems.example.com/gems/a-1.gem",
+ fetcher.instance_variable_get(:@test_arg).to_s)
+
+ assert File.exist?(a1_cache_gem)
+ end
+
+ unless win_platform? # File.chmod doesn't work
+ def test_download_local_read_only
+ FileUtils.mv @a1_gem, @tempdir
+ local_path = File.join @tempdir, "#{@a1.full_name}.gem"
+ inst = nil
+ File.chmod 0555, File.join(@gemhome, 'cache')
+
+ Dir.chdir @tempdir do
+ inst = Gem::RemoteFetcher.fetcher
+ end
+
+ assert_equal File.join(@tempdir, "#{@a1.full_name}.gem"),
+ inst.download(@a1, local_path)
+ ensure
+ File.chmod 0755, File.join(@gemhome, 'cache')
+ end
+
+ def test_download_read_only
+ File.chmod 0555, File.join(@gemhome, 'cache')
+ File.chmod 0555, File.join(@gemhome)
+
+ fetcher = util_fuck_with_fetcher File.read(@a1_gem)
+ fetcher.download(@a1, 'http://gems.example.com')
+ assert File.exist?(File.join(@userhome, '.gem',
+ 'cache', "#{@a1.full_name}.gem"))
+ ensure
+ File.chmod 0755, File.join(@gemhome)
+ File.chmod 0755, File.join(@gemhome, 'cache')
+ end
+ end
+
+ def test_download_platform_legacy
+ original_platform = 'old-platform'
+
+ e1, e1_gem = util_gem 'e', '1' do |s|
+ s.platform = Gem::Platform::CURRENT
+ s.instance_variable_set :@original_platform, original_platform
+ end
+
+ e1_data = nil
+ File.open e1_gem, 'rb' do |fp|
+ e1_data = fp.read
+ end
+
+ fetcher = util_fuck_with_fetcher e1_data, :blow_chunks
+
+ e1_cache_gem = File.join(@gemhome, 'cache', "#{e1.full_name}.gem")
+
+ assert_equal e1_cache_gem, fetcher.download(e1, 'http://gems.example.com')
+
+ assert_equal("http://gems.example.com/gems/#{e1.original_name}.gem",
+ fetcher.instance_variable_get(:@test_arg).to_s)
+ assert File.exist?(e1_cache_gem)
+ end
+
+ def test_download_unsupported
+ inst = Gem::RemoteFetcher.fetcher
+
+ e = assert_raise Gem::InstallError do
+ inst.download @a1, 'ftp://gems.rubyforge.org'
+ end
+
+ assert_equal 'unsupported URI scheme ftp', e.message
+ end
+
+ def test_explicit_proxy
+ use_ui @ui do
+ fetcher = Gem::RemoteFetcher.new @proxy_uri
+ assert_equal PROXY_DATA.size, fetcher.fetch_size(@server_uri)
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_explicit_proxy_with_user_auth
+ use_ui @ui do
+ uri = URI.parse @proxy_uri
+ uri.user, uri.password = 'foo', 'bar'
+ fetcher = Gem::RemoteFetcher.new uri.to_s
+ proxy = fetcher.instance_variable_get("@proxy_uri")
+ assert_equal 'foo', proxy.user
+ assert_equal 'bar', proxy.password
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+
+ use_ui @ui do
+ uri = URI.parse @proxy_uri
+ uri.user, uri.password = 'domain%5Cuser', 'bar'
+ fetcher = Gem::RemoteFetcher.new uri.to_s
+ proxy = fetcher.instance_variable_get("@proxy_uri")
+ assert_equal 'domain\user', URI.unescape(proxy.user)
+ assert_equal 'bar', proxy.password
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+
+ use_ui @ui do
+ uri = URI.parse @proxy_uri
+ uri.user, uri.password = 'user', 'my%20pass'
+ fetcher = Gem::RemoteFetcher.new uri.to_s
+ proxy = fetcher.instance_variable_get("@proxy_uri")
+ assert_equal 'user', proxy.user
+ assert_equal 'my pass', URI.unescape(proxy.password)
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_explicit_proxy_with_user_auth_in_env
+ use_ui @ui do
+ ENV['http_proxy'] = @proxy_uri
+ ENV['http_proxy_user'] = 'foo'
+ ENV['http_proxy_pass'] = 'bar'
+ fetcher = Gem::RemoteFetcher.new nil
+ proxy = fetcher.instance_variable_get("@proxy_uri")
+ assert_equal 'foo', proxy.user
+ assert_equal 'bar', proxy.password
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+
+ use_ui @ui do
+ ENV['http_proxy'] = @proxy_uri
+ ENV['http_proxy_user'] = 'foo\user'
+ ENV['http_proxy_pass'] = 'my bar'
+ fetcher = Gem::RemoteFetcher.new nil
+ proxy = fetcher.instance_variable_get("@proxy_uri")
+ assert_equal 'foo\user', URI.unescape(proxy.user)
+ assert_equal 'my bar', URI.unescape(proxy.password)
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_fetch_path_gzip
+ fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ Gem.gzip 'foo'
+ end
+
+ assert_equal 'foo', fetcher.fetch_path(@uri + 'foo.gz')
+ end
+
+ def test_fetch_path_io_error
+ fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ raise EOFError
+ end
+
+ e = assert_raise Gem::RemoteFetcher::FetchError do
+ fetcher.fetch_path 'uri'
+ end
+
+ assert_equal 'EOFError: EOFError (uri)', e.message
+ assert_equal 'uri', e.uri
+ end
+
+ def test_fetch_path_socket_error
+ fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ raise SocketError
+ end
+
+ e = assert_raise Gem::RemoteFetcher::FetchError do
+ fetcher.fetch_path 'uri'
+ end
+
+ assert_equal 'SocketError: SocketError (uri)', e.message
+ assert_equal 'uri', e.uri
+ end
+
+ def test_fetch_path_system_call_error
+ fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime = nil, head = nil)
+ raise Errno::ECONNREFUSED, 'connect(2)'
+ end
+
+ e = assert_raise Gem::RemoteFetcher::FetchError do
+ fetcher.fetch_path 'uri'
+ end
+
+ assert_match %r|ECONNREFUSED:.*connect\(2\) \(uri\)\z|,
+ e.message
+ assert_equal 'uri', e.uri
+ end
+
+ def test_fetch_path_unmodified
+ fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ ''
+ end
+
+ assert_equal '', fetcher.fetch_path(URI.parse(@gem_repo), Time.at(0))
+ end
+
+ def test_get_proxy_from_env_empty
+ orig_env_HTTP_PROXY = ENV['HTTP_PROXY']
+ orig_env_http_proxy = ENV['http_proxy']
+
+ ENV['HTTP_PROXY'] = ''
+ ENV.delete 'http_proxy'
+
+ fetcher = Gem::RemoteFetcher.new nil
+
+ assert_equal nil, fetcher.send(:get_proxy_from_env)
+
+ ensure
+ orig_env_HTTP_PROXY.nil? ? ENV.delete('HTTP_PROXY') :
+ ENV['HTTP_PROXY'] = orig_env_HTTP_PROXY
+ orig_env_http_proxy.nil? ? ENV.delete('http_proxy') :
+ ENV['http_proxy'] = orig_env_http_proxy
+ end
+
+ def test_implicit_no_proxy
+ use_ui @ui do
+ ENV['http_proxy'] = 'http://fakeurl:12345'
+ fetcher = Gem::RemoteFetcher.new :no_proxy
+ assert_data_from_server fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_implicit_proxy
+ use_ui @ui do
+ ENV['http_proxy'] = @proxy_uri
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_implicit_upper_case_proxy
+ use_ui @ui do
+ ENV['HTTP_PROXY'] = @proxy_uri
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_data_from_proxy fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_implicit_proxy_no_env
+ use_ui @ui do
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_data_from_server fetcher.fetch_path(@server_uri)
+ end
+ end
+
+ def test_open_uri_or_path
+ fetcher = Gem::RemoteFetcher.new nil
+
+ conn = Object.new
+ def conn.started?() true end
+ def conn.request(req)
+ unless defined? @requested then
+ @requested = true
+ res = Net::HTTPRedirection.new nil, 301, nil
+ res.add_field 'Location', 'http://gems.example.com/real_path'
+ res
+ else
+ res = Net::HTTPOK.new nil, 200, nil
+ def res.body() 'real_path' end
+ res
+ end
+ end
+
+ conn = { 'gems.example.com:80' => conn }
+ fetcher.instance_variable_set :@connections, conn
+
+ data = fetcher.open_uri_or_path 'http://gems.example.com/redirect'
+
+ assert_equal 'real_path', data
+ end
+
+ def test_open_uri_or_path_limited_redirects
+ fetcher = Gem::RemoteFetcher.new nil
+
+ conn = Object.new
+ def conn.started?() true end
+ def conn.request(req)
+ res = Net::HTTPRedirection.new nil, 301, nil
+ res.add_field 'Location', 'http://gems.example.com/redirect'
+ res
+ end
+
+ conn = { 'gems.example.com:80' => conn }
+ fetcher.instance_variable_set :@connections, conn
+
+ e = assert_raise Gem::RemoteFetcher::FetchError do
+ fetcher.open_uri_or_path 'http://gems.example.com/redirect'
+ end
+
+ assert_equal 'too many redirects (http://gems.example.com/redirect)',
+ e.message
+ end
+
+ def test_request
+ uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
+ util_stub_connection_for :body => :junk, :code => 200
+
+ response = @fetcher.request uri, Net::HTTP::Get
+
+ assert_equal 200, response.code
+ assert_equal :junk, response.body
+ end
+
+ def test_request_head
+ uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
+ util_stub_connection_for :body => '', :code => 200
+ response = @fetcher.request uri, Net::HTTP::Head
+
+ assert_equal 200, response.code
+ assert_equal '', response.body
+ end
+
+ def test_request_unmodifed
+ uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
+ conn = util_stub_connection_for :body => '', :code => 304
+
+ t = Time.now
+ response = @fetcher.request uri, Net::HTTP::Head, t
+
+ assert_equal 304, response.code
+ assert_equal '', response.body
+
+ assert_equal t.rfc2822, conn.payload['if-modified-since']
+ end
+
+ def test_yaml_error_on_size
+ use_ui @ui do
+ self.class.enable_yaml = false
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_error { fetcher.size }
+ end
+ end
+
+ def util_stub_connection_for hash
+ def @fetcher.connection= conn
+ @conn = conn
+ end
+
+ def @fetcher.connection_for uri
+ @conn
+ end
+
+ @fetcher.connection = Conn.new OpenStruct.new(hash)
+ end
+
+ def assert_error(exception_class=Exception)
+ got_exception = false
+ begin
+ yield
+ rescue exception_class => ex
+ got_exception = true
+ end
+ assert got_exception, "Expected exception conforming to #{exception_class}"
+ end
+
+ def assert_data_from_server(data)
+ assert_block("Data is not from server") { data =~ /0\.4\.11/ }
+ end
+
+ def assert_data_from_proxy(data)
+ assert_block("Data is not from proxy") { data =~ /0\.4\.2/ }
+ end
+
+ class Conn
+ attr_accessor :payload
+
+ def initialize(response)
+ @response = response
+ self.payload = nil
+ end
+
+ def request(req)
+ self.payload = req
+ @response
+ end
+ end
+
+ class NilLog < WEBrick::Log
+ def log(level, data) #Do nothing
+ end
+ end
+
+ class << self
+ attr_reader :normal_server, :proxy_server
+ attr_accessor :enable_zip, :enable_yaml
+
+ def start_servers
+ @normal_server ||= start_server(SERVER_PORT, SERVER_DATA)
+ @proxy_server ||= start_server(PROXY_PORT, PROXY_DATA)
+ @enable_yaml = true
+ @enable_zip = false
+ end
+
+ private
+
+ def start_server(port, data)
+ Thread.new do
+ begin
+ null_logger = NilLog.new
+ s = WEBrick::HTTPServer.new(
+ :Port => port,
+ :DocumentRoot => nil,
+ :Logger => null_logger,
+ :AccessLog => null_logger
+ )
+ s.mount_proc("/kill") { |req, res| s.shutdown }
+ s.mount_proc("/yaml") { |req, res|
+ if @enable_yaml
+ res.body = data
+ res['Content-Type'] = 'text/plain'
+ res['content-length'] = data.size
+ else
+ res.status = "404"
+ res.body = "<h1>NOT FOUND</h1>"
+ res['Content-Type'] = 'text/html'
+ end
+ }
+ s.mount_proc("/yaml.Z") { |req, res|
+ if @enable_zip
+ res.body = Zlib::Deflate.deflate(data)
+ res['Content-Type'] = 'text/plain'
+ else
+ res.status = "404"
+ res.body = "<h1>NOT FOUND</h1>"
+ res['Content-Type'] = 'text/html'
+ end
+ }
+ s.start
+ rescue Exception => ex
+ abort ex.message
+ puts "ERROR during server thread: #{ex.message}"
+ end
+ end
+ sleep 0.2 # Give the servers time to startup
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_requirement.rb b/trunk/test/rubygems/test_gem_requirement.rb
new file mode 100644
index 0000000000..c441bd16df
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_requirement.rb
@@ -0,0 +1,223 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/version'
+
+class TestGemRequirement < RubyGemTestCase
+
+ def setup
+ super
+
+ @r1_2 = Gem::Requirement.new '= 1.2'
+ @r1_3 = Gem::Requirement.new '= 1.3'
+ end
+
+ def test_initialize
+ r = Gem::Requirement.new '2'
+ assert_equal '= 2', r.to_s, 'String'
+
+ r = Gem::Requirement.new %w[2]
+ assert_equal '= 2', r.to_s, 'Array of Strings'
+
+ r = Gem::Requirement.new Gem::Version.new('2')
+ assert_equal '= 2', r.to_s, 'Gem::Version'
+ end
+
+ def test_equals2
+ assert_equal @r1_2, @r1_2.dup
+ assert_equal @r1_2.dup, @r1_2
+
+ assert_not_equal @r1_3, @r1_2
+ assert_not_equal @r1_2, @r1_3
+
+ assert_not_equal Object.new, @r1_2
+ assert_not_equal @r1_2, Object.new
+ end
+
+ def test_hash
+ assert_equal @r1_2.hash, @r1_2.dup.hash
+ assert_equal @r1_2.dup.hash, @r1_2.hash
+
+ assert_not_equal @r1_2.hash, @r1_3.hash
+ assert_not_equal @r1_3.hash, @r1_2.hash
+ end
+
+ # We may get some old gems that have requirements in old formats.
+ # We need to be able to handle those old requirements by normalizing
+ # them to the latest format.
+ def test_normalization
+ require 'yaml'
+ yamldep = %{--- !ruby/object:Gem::Requirement
+ nums:
+ - 1
+ - 0
+ - 4
+ op: ">="
+ version: ">= 1.0.4"}
+ dep = YAML.load(yamldep)
+ dep.normalize
+ assert_equal ">= 1.0.4", dep.to_s
+ end
+
+ def test_parse
+ assert_equal ['=', Gem::Version.new(1)], @r1_2.parse(' 1')
+
+ assert_equal ['=', Gem::Version.new(1)], @r1_2.parse('= 1')
+ assert_equal ['>', Gem::Version.new(1)], @r1_2.parse('> 1')
+
+ assert_equal ['=', Gem::Version.new(0)], @r1_2.parse('=')
+ assert_equal ['>', Gem::Version.new(0)], @r1_2.parse('>')
+
+ assert_equal ['=', Gem::Version.new(1)], @r1_2.parse("=\n1")
+ assert_equal ['=', Gem::Version.new(0)], @r1_2.parse("=\njunk")
+
+ assert_equal ['=', Gem::Version.new(2)], @r1_2.parse(Gem::Version.new('2'))
+ end
+
+ def test_parse_illformed
+ e = assert_raise ArgumentError do
+ @r1_2.parse(nil)
+ end
+
+ assert_equal 'Illformed requirement [nil]', e.message
+
+ e = assert_raise ArgumentError do
+ @r1_2.parse('')
+ end
+
+ assert_equal 'Illformed requirement [""]', e.message
+ end
+
+ def test_satisfied_by_eh_bang_equal
+ r1_2 = Gem::Requirement.new '!= 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal true, r1_2.satisfied_by?(nil)
+ assert_equal true, r1_2.satisfied_by?(v1_1)
+ assert_equal false, r1_2.satisfied_by?(v1_2)
+ assert_equal true, r1_2.satisfied_by?(v1_3)
+ end
+
+ def test_satisfied_by_eh_blank
+ r1_2 = Gem::Requirement.new '1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r1_2.satisfied_by?(nil)
+ assert_equal false, r1_2.satisfied_by?(v1_1)
+ assert_equal true, r1_2.satisfied_by?(v1_2)
+ assert_equal false, r1_2.satisfied_by?(v1_3)
+ end
+
+ def test_satisfied_by_eh_equal
+ r1_2 = @r1_2
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r1_2.satisfied_by?(nil)
+ assert_equal false, r1_2.satisfied_by?(v1_1)
+ assert_equal true, r1_2.satisfied_by?(v1_2)
+ assert_equal false, r1_2.satisfied_by?(v1_3)
+ end
+
+ def test_satisfied_by_eh_gt
+ r1_2 = Gem::Requirement.new '> 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r1_2.satisfied_by?(v1_1)
+ assert_equal false, r1_2.satisfied_by?(v1_2)
+ assert_equal true, r1_2.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r1_2.satisfied_by?(nil)
+ end
+ end
+
+ def test_satisfied_by_eh_gte
+ r1_2 = Gem::Requirement.new '>= 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r1_2.satisfied_by?(v1_1)
+ assert_equal true, r1_2.satisfied_by?(v1_2)
+ assert_equal true, r1_2.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r1_2.satisfied_by?(nil)
+ end
+ end
+
+ def test_satisfied_by_eh_list
+ r = Gem::Requirement.create(['> 1.1', '< 1.3'])
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r.satisfied_by?(v1_1)
+ assert_equal true, r.satisfied_by?(v1_2)
+ assert_equal false, r.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r.satisfied_by?(nil)
+ end
+ end
+
+ def test_satisfied_by_eh_lt
+ r1_2 = Gem::Requirement.new '< 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal true, r1_2.satisfied_by?(v1_1)
+ assert_equal false, r1_2.satisfied_by?(v1_2)
+ assert_equal false, r1_2.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r1_2.satisfied_by?(nil)
+ end
+ end
+
+ def test_satisfied_by_eh_lte
+ r1_2 = Gem::Requirement.new '<= 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal true, r1_2.satisfied_by?(v1_1)
+ assert_equal true, r1_2.satisfied_by?(v1_2)
+ assert_equal false, r1_2.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r1_2.satisfied_by?(nil)
+ end
+ end
+
+ def test_satisfied_by_eh_tilde_gt
+ r1_2 = Gem::Requirement.new '~> 1.2'
+ v1_1 = Gem::Version.new '1.1'
+ v1_2 = Gem::Version.new '1.2'
+ v1_3 = Gem::Version.new '1.3'
+
+ assert_equal false, r1_2.satisfied_by?(v1_1)
+ assert_equal true, r1_2.satisfied_by?(v1_2)
+ assert_equal true, r1_2.satisfied_by?(v1_3)
+
+ assert_raise NoMethodError do
+ assert_equal true, r1_2.satisfied_by?(nil)
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_server.rb b/trunk/test/rubygems/test_gem_server.rb
new file mode 100644
index 0000000000..dcdc766f0f
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_server.rb
@@ -0,0 +1,301 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/server'
+require 'stringio'
+
+class Gem::Server
+ attr_accessor :source_index
+ attr_reader :server
+end
+
+class TestGemServer < RubyGemTestCase
+
+ def setup
+ super
+
+ @a1 = quick_gem 'a', '1'
+ @a2 = quick_gem 'a', '2'
+
+ @server = Gem::Server.new Gem.dir, process_based_port, false
+ @req = WEBrick::HTTPRequest.new :Logger => nil
+ @res = WEBrick::HTTPResponse.new :HTTPVersion => '1.0'
+ end
+
+ def test_Marshal
+ data = StringIO.new "GET /Marshal.#{Gem.marshal_version} HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.Marshal @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/octet-stream', @res['content-type']
+
+ si = Gem::SourceIndex.new
+ si.add_specs @a1, @a2
+
+ assert_equal si, Marshal.load(@res.body)
+ end
+
+ def test_Marshal_Z
+ data = StringIO.new "GET /Marshal.#{Gem.marshal_version}.Z HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.Marshal @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ si = Gem::SourceIndex.new
+ si.add_specs @a1, @a2
+
+ assert_equal si, Marshal.load(Gem.inflate(@res.body))
+ end
+
+ def test_latest_specs
+ data = StringIO.new "GET /latest_specs.#{Gem.marshal_version} HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.latest_specs @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/octet-stream', @res['content-type']
+ assert_equal [['a', Gem::Version.new(2), Gem::Platform::RUBY]],
+ Marshal.load(@res.body)
+ end
+
+ def test_latest_specs_gz
+ data = StringIO.new "GET /latest_specs.#{Gem.marshal_version}.gz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.latest_specs @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-gzip', @res['content-type']
+ assert_equal [['a', Gem::Version.new(2), Gem::Platform::RUBY]],
+ Marshal.load(Gem.gunzip(@res.body))
+ end
+
+ def test_quick_a_1_gemspec_rz
+ data = StringIO.new "GET /quick/a-1.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ spec = YAML.load Gem.inflate(@res.body)
+ assert_equal 'a', spec.name
+ assert_equal Gem::Version.new(1), spec.version
+ end
+
+ def test_quick_a_1_mswin32_gemspec_rz
+ a1_p = quick_gem 'a', '1' do |s| s.platform = Gem::Platform.local end
+
+ data = StringIO.new "GET /quick/a-1-#{Gem::Platform.local}.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ spec = YAML.load Gem.inflate(@res.body)
+ assert_equal 'a', spec.name
+ assert_equal Gem::Version.new(1), spec.version
+ assert_equal Gem::Platform.local, spec.platform
+ end
+
+ def test_quick_common_substrings
+ ab1 = quick_gem 'ab', '1'
+
+ data = StringIO.new "GET /quick/a-1.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ spec = YAML.load Gem.inflate(@res.body)
+ assert_equal 'a', spec.name
+ assert_equal Gem::Version.new(1), spec.version
+ end
+
+ def test_quick_index
+ data = StringIO.new "GET /quick/index HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'text/plain', @res['content-type']
+ assert_equal "a-1\na-2", @res.body
+ end
+
+ def test_quick_index_rz
+ data = StringIO.new "GET /quick/index.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+ assert_equal "a-1\na-2", Gem.inflate(@res.body)
+ end
+
+ def test_quick_latest_index
+ data = StringIO.new "GET /quick/latest_index HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'text/plain', @res['content-type']
+ assert_equal 'a-2', @res.body
+ end
+
+ def test_quick_latest_index_rz
+ data = StringIO.new "GET /quick/latest_index.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+ assert_equal 'a-2', Gem.inflate(@res.body)
+ end
+
+ def test_quick_missing
+ data = StringIO.new "GET /quick/z-9.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 404, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'text/plain', @res['content-type']
+ assert_equal 'No gems found matching "z" "9" nil', @res.body
+ assert_equal 404, @res.status
+ end
+
+ def test_quick_marshal_a_1_gemspec_rz
+ data = StringIO.new "GET /quick/Marshal.#{Gem.marshal_version}/a-1.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ spec = Marshal.load Gem.inflate(@res.body)
+ assert_equal 'a', spec.name
+ assert_equal Gem::Version.new(1), spec.version
+ end
+
+ def test_quick_marshal_a_1_mswin32_gemspec_rz
+ a1_p = quick_gem 'a', '1' do |s| s.platform = Gem::Platform.local end
+
+ data = StringIO.new "GET /quick/Marshal.#{Gem.marshal_version}/a-1-#{Gem::Platform.local}.gemspec.rz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.quick @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ spec = Marshal.load Gem.inflate(@res.body)
+ assert_equal 'a', spec.name
+ assert_equal Gem::Version.new(1), spec.version
+ assert_equal Gem::Platform.local, spec.platform
+ end
+
+
+ def test_root
+ data = StringIO.new "GET / HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.root @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'text/html', @res['content-type']
+ end
+
+ def test_specs
+ data = StringIO.new "GET /specs.#{Gem.marshal_version} HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.specs @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/octet-stream', @res['content-type']
+
+ assert_equal [['a', Gem::Version.new(1), Gem::Platform::RUBY],
+ ['a', Gem::Version.new(2), Gem::Platform::RUBY]],
+ Marshal.load(@res.body)
+ end
+
+ def test_specs_gz
+ data = StringIO.new "GET /specs.#{Gem.marshal_version}.gz HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.specs @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-gzip', @res['content-type']
+
+ assert_equal [['a', Gem::Version.new(1), Gem::Platform::RUBY],
+ ['a', Gem::Version.new(2), Gem::Platform::RUBY]],
+ Marshal.load(Gem.gunzip(@res.body))
+ end
+
+ def test_yaml
+ data = StringIO.new "GET /yaml.#{Gem.marshal_version} HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.yaml @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'text/plain', @res['content-type']
+
+ si = Gem::SourceIndex.new
+ si.add_specs @a1, @a2
+
+ assert_equal si, YAML.load(@res.body)
+ end
+
+ def test_yaml_Z
+ data = StringIO.new "GET /yaml.#{Gem.marshal_version}.Z HTTP/1.0\r\n\r\n"
+ @req.parse data
+
+ @server.yaml @req, @res
+
+ assert_equal 200, @res.status, @res.body
+ assert_match %r| \d\d:\d\d:\d\d |, @res['date']
+ assert_equal 'application/x-deflate', @res['content-type']
+
+ si = Gem::SourceIndex.new
+ si.add_specs @a1, @a2
+
+ assert_equal si, YAML.load(Gem.inflate(@res.body))
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_source_index.rb b/trunk/test/rubygems/test_gem_source_index.rb
new file mode 100644
index 0000000000..263826759a
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_source_index.rb
@@ -0,0 +1,834 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/source_index'
+require 'rubygems/config_file'
+
+class Gem::SourceIndex
+ public :fetcher, :fetch_bulk_index, :fetch_quick_index,
+ :find_missing, :gems, :remove_extra,
+ :update_with_missing, :unzip
+end
+
+class TestGemSourceIndex < RubyGemTestCase
+
+ def setup
+ super
+
+ util_setup_fake_fetcher
+ end
+
+ def test_self_from_gems_in
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.rm_r spec_dir
+
+ FileUtils.mkdir_p spec_dir
+
+ a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
+
+ spec_file = File.join spec_dir, "#{a1.full_name}.gemspec"
+
+ File.open spec_file, 'w' do |fp|
+ fp.write a1.to_ruby
+ end
+
+ si = Gem::SourceIndex.from_gems_in spec_dir
+
+ assert_equal [spec_dir], si.spec_dirs
+ assert_equal [a1.full_name], si.gems.keys
+ end
+
+ def test_self_load_specification
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.rm_r spec_dir
+
+ FileUtils.mkdir_p spec_dir
+
+ a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
+
+ spec_file = File.join spec_dir, "#{a1.full_name}.gemspec"
+
+ File.open spec_file, 'w' do |fp|
+ fp.write a1.to_ruby
+ end
+
+ spec = Gem::SourceIndex.load_specification spec_file
+
+ assert_equal a1.author, spec.author
+ end
+
+ def test_self_load_specification_exception
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.mkdir_p spec_dir
+
+ spec_file = File.join spec_dir, 'a-1.gemspec'
+
+ File.open spec_file, 'w' do |fp|
+ fp.write 'raise Exception, "epic fail"'
+ end
+
+ use_ui @ui do
+ assert_equal nil, Gem::SourceIndex.load_specification(spec_file)
+ end
+
+ assert_equal '', @ui.output
+
+ expected = <<-EOF
+WARNING: #<Exception: epic fail>
+raise Exception, "epic fail"
+WARNING: Invalid .gemspec format in '#{spec_file}'
+ EOF
+
+ assert_equal expected, @ui.error
+ end
+
+ def test_self_load_specification_interrupt
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.mkdir_p spec_dir
+
+ spec_file = File.join spec_dir, 'a-1.gemspec'
+
+ File.open spec_file, 'w' do |fp|
+ fp.write 'raise Interrupt, "^C"'
+ end
+
+ use_ui @ui do
+ assert_raise Interrupt do
+ Gem::SourceIndex.load_specification(spec_file)
+ end
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_self_load_specification_syntax_error
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.mkdir_p spec_dir
+
+ spec_file = File.join spec_dir, 'a-1.gemspec'
+
+ File.open spec_file, 'w' do |fp|
+ fp.write '1 +'
+ end
+
+ use_ui @ui do
+ assert_equal nil, Gem::SourceIndex.load_specification(spec_file)
+ end
+
+ assert_equal '', @ui.output
+
+ assert_match(/syntax error/, @ui.error)
+ assert_match(/1 \+/, @ui.error)
+ end
+
+ def test_self_load_specification_system_exit
+ spec_dir = File.join @gemhome, 'specifications'
+
+ FileUtils.mkdir_p spec_dir
+
+ spec_file = File.join spec_dir, 'a-1.gemspec'
+
+ File.open spec_file, 'w' do |fp|
+ fp.write 'raise SystemExit, "bye-bye"'
+ end
+
+ use_ui @ui do
+ assert_raise SystemExit do
+ Gem::SourceIndex.load_specification(spec_file)
+ end
+ end
+
+ assert_equal '', @ui.output
+ assert_equal '', @ui.error
+ end
+
+ def test_create_from_directory
+ # TODO
+ end
+
+ def test_fetcher
+ assert_equal @fetcher, @source_index.fetcher
+ end
+
+ def test_fetch_bulk_index_compressed
+ util_setup_bulk_fetch true
+
+ use_ui @ui do
+ fetched_index = @source_index.fetch_bulk_index @uri
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name].sort,
+ fetched_index.gems.map { |n,s| n }.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_bulk_index_error
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] = proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}yaml.Z"] = proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}yaml"] = proc { raise SocketError }
+
+ e = assert_raise Gem::RemoteSourceException do
+ use_ui @ui do
+ @source_index.fetch_bulk_index @uri
+ end
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
+ assert_equal "#{@gem_repo}yaml.Z", paths.shift
+ assert_equal "#{@gem_repo}yaml", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+
+ assert_equal 'Error fetching remote gem cache: SocketError',
+ e.message
+ end
+
+ def test_fetch_bulk_index_fallback
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] =
+ proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] =
+ proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}yaml.Z"] = proc { raise SocketError }
+ @fetcher.data["#{@gem_repo}yaml"] = @source_index.to_yaml
+
+ use_ui @ui do
+ fetched_index = @source_index.fetch_bulk_index @uri
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name].sort,
+ fetched_index.gems.map { |n,s| n }.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
+ assert_equal "#{@gem_repo}yaml.Z", paths.shift
+ assert_equal "#{@gem_repo}yaml", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_bulk_index_marshal_mismatch
+ marshal = @source_index.dump
+ marshal[0] = (Marshal::MAJOR_VERSION - 1).chr
+
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = marshal
+ @fetcher.data["#{@gem_repo}yaml"] = @source_index.to_yaml
+
+ use_ui @ui do
+ fetched_index = @source_index.fetch_bulk_index @uri
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name].sort,
+ fetched_index.gems.map { |n,s| n }.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
+ assert_equal "#{@gem_repo}yaml.Z", paths.shift
+ assert_equal "#{@gem_repo}yaml", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_bulk_index_uncompressed
+ util_setup_bulk_fetch false
+ use_ui @ui do
+ fetched_index = @source_index.fetch_bulk_index @uri
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name].sort,
+ fetched_index.gems.map { |n,s| n }.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_quick_index
+ index = util_zip @gem_names
+ latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
+
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = index
+ @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
+
+ quick_index = @source_index.fetch_quick_index @uri, false
+ assert_equal [@a2.full_name, @b2.full_name].sort,
+ quick_index.sort
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_quick_index_all
+ index = util_zip @gem_names
+ latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
+
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = index
+ @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
+
+ quick_index = @source_index.fetch_quick_index @uri, true
+ assert_equal [@a1.full_name, @a2.full_name, @b2.full_name].sort,
+ quick_index.sort
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_quick_index_error
+ @fetcher.data["#{@gem_repo}quick/index.rz"] =
+ proc { raise Exception }
+
+ e = assert_raise Gem::OperationNotSupportedError do
+ @source_index.fetch_quick_index @uri, true
+ end
+
+ assert_equal 'No quick index found: Exception', e.message
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_quick_index_fallback
+ index = util_zip @gem_names
+
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = index
+
+ quick_index = @source_index.fetch_quick_index @uri, false
+ assert_equal @gem_names.split, quick_index.sort
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_quick_index_subdir
+ latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
+ repo = URI.parse "#{@gem_repo}~nobody/mirror/"
+
+ @fetcher.data["#{repo}quick/latest_index.rz"] = latest_index
+
+ quick_index = @source_index.fetch_quick_index repo, false
+ assert_equal [@a2.full_name, @b2.full_name].sort,
+ quick_index.sort
+
+ paths = @fetcher.paths
+
+ assert_equal "#{repo}quick/latest_index.rz", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_single_spec
+ a1_spec_url = "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz"
+ @fetcher.data[a1_spec_url] = util_zip Marshal.dump(@a1)
+
+ spec = @source_index.send :fetch_single_spec, URI.parse(@gem_repo),
+ @a1.full_name
+
+ assert_equal @a1.full_name, spec.full_name
+
+ paths = @fetcher.paths
+
+ assert_equal a1_spec_url, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_single_spec_subdir
+ repo = URI.parse "#{@gem_repo}~nobody/mirror/"
+
+ a1_spec_url = "#{repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz"
+ @fetcher.data[a1_spec_url] = util_zip Marshal.dump(@a1)
+
+ spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
+
+ assert_equal @a1.full_name, spec.full_name
+
+ paths = @fetcher.paths
+
+ assert_equal a1_spec_url, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_single_spec_yaml
+ a1_spec_url = "#{@gem_repo}quick/#{@a1.full_name}.gemspec.rz"
+ @fetcher.data[a1_spec_url] = util_zip @a1.to_yaml
+
+ repo = URI.parse @gem_repo
+
+ spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
+
+ assert_equal @a1.full_name, spec.full_name
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz", paths.shift
+ assert_equal a1_spec_url, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_fetch_single_spec_yaml_subdir
+ repo = URI.parse "#{@gem_repo}~nobody/mirror/"
+
+ a1_spec_url = "#{repo}quick/#{@a1.full_name}.gemspec.rz"
+ @fetcher.data[a1_spec_url] = util_zip @a1.to_yaml
+
+ spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
+
+ assert_equal @a1.full_name, spec.full_name
+
+ paths = @fetcher.paths
+
+ assert_equal "#{repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz", paths.shift
+ assert_equal a1_spec_url, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_find_missing
+ missing = @source_index.find_missing [@b2.full_name]
+ assert_equal [@b2.full_name], missing
+ end
+
+ def test_find_missing_none_missing
+ missing = @source_index.find_missing [
+ @a1.full_name, @a2.full_name, @c1_2.full_name
+ ]
+
+ assert_equal [], missing
+ end
+
+ def test_latest_specs
+ p1_ruby = quick_gem 'p', '1'
+ p1_platform = quick_gem 'p', '1' do |spec|
+ spec.platform = Gem::Platform::CURRENT
+ end
+
+ a1_platform = quick_gem @a1.name, (@a1.version) do |s|
+ s.platform = Gem::Platform.new 'x86-my_platform1'
+ end
+
+ a2_platform = quick_gem @a2.name, (@a2.version) do |s|
+ s.platform = Gem::Platform.new 'x86-my_platform1'
+ end
+
+ a2_platform_other = quick_gem @a2.name, (@a2.version) do |s|
+ s.platform = Gem::Platform.new 'x86-other_platform1'
+ end
+
+ a3_platform_other = quick_gem @a2.name, (@a2.version.bump) do |s|
+ s.platform = Gem::Platform.new 'x86-other_platform1'
+ end
+
+ @source_index.add_spec p1_ruby
+ @source_index.add_spec p1_platform
+ @source_index.add_spec a1_platform
+ @source_index.add_spec a2_platform
+ @source_index.add_spec a2_platform_other
+ @source_index.add_spec a3_platform_other
+
+ expected = [
+ @a2.full_name,
+ a2_platform.full_name,
+ a3_platform_other.full_name,
+ @c1_2.full_name,
+ @a_evil9.full_name,
+ p1_ruby.full_name,
+ p1_platform.full_name,
+ ].sort
+
+ latest_specs = @source_index.latest_specs.map { |s| s.full_name }.sort
+
+ assert_equal expected, latest_specs
+ end
+
+ def test_load_gems_in
+ spec_dir1 = File.join @gemhome, 'specifications'
+ spec_dir2 = File.join @tempdir, 'gemhome2', 'specifications'
+
+ FileUtils.rm_r spec_dir1
+
+ FileUtils.mkdir_p spec_dir1
+ FileUtils.mkdir_p spec_dir2
+
+ a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
+ a2 = quick_gem 'a', '1' do |spec| spec.author = 'author 2' end
+
+ File.open File.join(spec_dir1, "#{a1.full_name}.gemspec"), 'w' do |fp|
+ fp.write a1.to_ruby
+ end
+
+ File.open File.join(spec_dir2, "#{a2.full_name}.gemspec"), 'w' do |fp|
+ fp.write a2.to_ruby
+ end
+
+ @source_index.load_gems_in spec_dir1, spec_dir2
+
+ assert_equal a1.author, @source_index.specification(a1.full_name).author
+ end
+
+ def test_outdated
+ util_setup_spec_fetcher
+
+ assert_equal [], @source_index.outdated
+
+ updated = quick_gem @a2.name, (@a2.version.bump)
+ util_setup_spec_fetcher updated
+
+ assert_equal [updated.name], @source_index.outdated
+
+ updated_platform = quick_gem @a2.name, (updated.version.bump) do |s|
+ s.platform = Gem::Platform.new 'x86-other_platform1'
+ end
+
+ util_setup_spec_fetcher updated, updated_platform
+
+ assert_equal [updated_platform.name], @source_index.outdated
+ end
+
+ def test_refresh_bang
+ a1_spec = File.join @gemhome, "specifications", "#{@a1.full_name}.gemspec"
+
+ FileUtils.mv a1_spec, @tempdir
+
+ source_index = Gem::SourceIndex.from_installed_gems
+
+ assert !source_index.gems.include?(@a1.full_name)
+
+ FileUtils.mv File.join(@tempdir, "#{@a1.full_name}.gemspec"), a1_spec
+
+ source_index.refresh!
+
+ assert source_index.gems.include?(@a1.full_name)
+ end
+
+ def test_refresh_bang_not_from_dir
+ source_index = Gem::SourceIndex.new
+
+ e = assert_raise RuntimeError do
+ source_index.refresh!
+ end
+
+ assert_equal 'source index not created from disk', e.message
+ end
+
+ def test_remove_extra
+ @source_index.add_spec @a1
+ @source_index.add_spec @a2
+ @source_index.add_spec @pl1
+
+ @source_index.remove_extra [@a1.full_name, @pl1.full_name]
+
+ assert_equal [@a1.full_name],
+ @source_index.gems.map { |n,s| n }.sort
+ end
+
+ def test_remove_extra_no_changes
+ gems = [@a1.full_name, @a2.full_name]
+ @source_index.add_spec @a1
+ @source_index.add_spec @a2
+
+ @source_index.remove_extra gems
+
+ assert_equal gems, @source_index.gems.map { |n,s| n }.sort
+ end
+
+ def test_search
+ assert_equal [@a1, @a2, @a_evil9], @source_index.search('a')
+ assert_equal [@a2], @source_index.search('a', '= 2')
+
+ assert_equal [], @source_index.search('bogusstring')
+ assert_equal [], @source_index.search('a', '= 3')
+
+ source_index = Gem::SourceIndex.new
+ source_index.add_spec @a1
+ source_index.add_spec @a2
+
+ assert_equal [@a1], source_index.search(@a1.name, '= 1')
+
+ r1 = Gem::Requirement.create '= 1'
+ assert_equal [@a1], source_index.search(@a1.name, r1)
+
+ dep = Gem::Dependency.new @a1.name, r1
+ assert_equal [@a1], source_index.search(dep)
+ end
+
+ def test_search_empty_cache
+ empty_source_index = Gem::SourceIndex.new({})
+ assert_equal [], empty_source_index.search("foo")
+ end
+
+ def test_search_platform
+ util_set_arch 'x86-my_platform1'
+
+ a1 = quick_gem 'a', '1'
+ a1_mine = quick_gem 'a', '1' do |s|
+ s.platform = Gem::Platform.new 'x86-my_platform1'
+ end
+ a1_other = quick_gem 'a', '1' do |s|
+ s.platform = Gem::Platform.new 'x86-other_platform1'
+ end
+
+ si = Gem::SourceIndex.new(a1.full_name => a1, a1_mine.full_name => a1_mine,
+ a1_other.full_name => a1_other)
+
+ dep = Gem::Dependency.new 'a', Gem::Requirement.new('1')
+
+ gems = si.search dep, true
+
+ assert_equal [a1, a1_mine], gems.sort
+ end
+
+ def test_signature
+ sig = @source_index.gem_signature('foo-1.2.3')
+ assert_equal 64, sig.length
+ assert_match(/^[a-f0-9]{64}$/, sig)
+ end
+
+ def test_specification
+ assert_equal @a1, @source_index.specification(@a1.full_name)
+
+ assert_nil @source_index.specification("foo-1.2.4")
+ end
+
+ def test_index_signature
+ sig = @source_index.index_signature
+ assert_match(/^[a-f0-9]{64}$/, sig)
+ end
+
+ def test_unzip
+ input = "x\234+\316\317MU(I\255(\001\000\021\350\003\232"
+ assert_equal 'some text', @source_index.unzip(input)
+ end
+
+ def test_update_bulk
+ util_setup_bulk_fetch true
+
+ @source_index.gems.replace({})
+ assert_equal [], @source_index.gems.keys.sort
+
+ use_ui @ui do
+ @source_index.update @uri, true
+
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name],
+ @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_update_incremental
+ old_gem_conf = Gem.configuration
+ Gem.configuration = Gem::ConfigFile.new([])
+
+ latest_names = [@a2, @a_evil9, @b2, @c1_2].map { |s| s.full_name }
+ latest_index = util_zip latest_names.join("\n")
+ @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
+
+ marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
+ "#{@b2.full_name}.gemspec.rz"
+ @fetcher.data[marshal_uri] = util_zip Marshal.dump(@b2)
+
+ use_ui @ui do
+ @source_index.update @uri, false
+
+ assert_equal latest_names, @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+ assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
+ assert_equal marshal_uri, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ ensure
+ Gem.configuration = old_gem_conf
+ end
+
+ def test_update_incremental_all
+ old_gem_conf = Gem.configuration
+ Gem.configuration = Gem::ConfigFile.new([])
+
+ quick_index = util_zip @all_gem_names.join("\n")
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
+
+ marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
+ "#{@b2.full_name}.gemspec.rz"
+ @fetcher.data[marshal_uri] = util_zip Marshal.dump(@b2)
+
+ use_ui @ui do
+ @source_index.update @uri, true
+
+ assert_equal @all_gem_names, @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+ assert_equal marshal_uri, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ ensure
+ Gem.configuration = old_gem_conf
+ end
+
+ def test_update_incremental_fallback
+ old_gem_conf = Gem.configuration
+ Gem.configuration = Gem::ConfigFile.new([])
+
+ quick_index = util_zip @all_gem_names.join("\n")
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
+
+ marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
+ "#{@b2.full_name}.gemspec.rz"
+
+ yaml_uri = "#{@gem_repo}quick/#{@b2.full_name}.gemspec.rz"
+ @fetcher.data[yaml_uri] = util_zip @b2.to_yaml
+
+ use_ui @ui do
+ @source_index.update @uri, true
+
+ assert_equal @all_gem_names, @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+ assert_equal marshal_uri, paths.shift
+ assert_equal yaml_uri, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ ensure
+ Gem.configuration = old_gem_conf
+ end
+
+ def test_update_incremental_marshal_mismatch
+ old_gem_conf = Gem.configuration
+ Gem.configuration = Gem::ConfigFile.new([])
+
+ quick_index = util_zip @all_gem_names.join("\n")
+ @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
+
+ marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
+ "#{@b2.full_name}.gemspec.rz"
+ marshal_data = Marshal.dump(@b2)
+ marshal_data[0] = (Marshal::MAJOR_VERSION - 1).chr
+ @fetcher.data[marshal_uri] = util_zip marshal_data
+
+ yaml_uri = "#{@gem_repo}quick/#{@b2.full_name}.gemspec.rz"
+ @fetcher.data[yaml_uri] = util_zip @b2.to_yaml
+
+ use_ui @ui do
+ @source_index.update @uri, true
+
+ assert_equal @all_gem_names, @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+ assert_equal marshal_uri, paths.shift
+ assert_equal yaml_uri, paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ ensure
+ Gem.configuration = old_gem_conf
+ end
+
+ def test_update_subdir
+ @gem_repo = @gem_repo + 'subdir/'
+
+ util_setup_bulk_fetch true
+
+ @source_index.gems.replace({})
+ assert_equal [], @source_index.gems.keys.sort
+
+ uri = @uri.to_s + 'subdir/'
+
+ use_ui @ui do
+ @source_index.update uri, true
+
+ assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
+ @c1_2.full_name],
+ @source_index.gems.keys.sort
+ end
+
+ paths = @fetcher.paths
+
+ assert_equal "#{@gem_repo}quick/index.rz", paths.shift
+ assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
+
+ assert paths.empty?, paths.join(', ')
+ end
+
+ def test_update_with_missing
+ marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
+ "#{@c1_2.full_name}.gemspec.rz"
+ dumped = Marshal.dump @c1_2
+ @fetcher.data[marshal_uri] = util_zip(dumped)
+
+ use_ui @ui do
+ @source_index.update_with_missing @uri, [@c1_2.full_name]
+ end
+
+ spec = @source_index.specification(@c1_2.full_name)
+ # We don't care about the equality of undumped attributes
+ @c1_2.files = spec.files
+ @c1_2.loaded_from = spec.loaded_from
+
+ assert_equal @c1_2, spec
+ end
+
+ def util_setup_bulk_fetch(compressed)
+ source_index = @source_index.dump
+
+ if compressed then
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] = util_zip source_index
+ else
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = source_index
+ end
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_source_info_cache.rb b/trunk/test/rubygems/test_gem_source_info_cache.rb
new file mode 100644
index 0000000000..86866a8012
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_source_info_cache.rb
@@ -0,0 +1,448 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/source_info_cache'
+
+class Gem::SourceIndex
+ public :gems
+end
+
+class TestGemSourceInfoCache < RubyGemTestCase
+
+ def setup
+ @original_sources = Gem.sources
+
+ super
+
+ util_setup_fake_fetcher
+
+ @sic = Gem::SourceInfoCache.new
+ @sic.instance_variable_set :@fetcher, @fetcher
+
+ @si_new = Gem::SourceIndex.new
+ @sice_new = Gem::SourceInfoCacheEntry.new @si_new, 0
+
+ prep_cache_files @sic
+
+ @sic.reset_cache_data
+ end
+
+ def teardown
+ super
+ Gem.sources.replace @original_sources
+ Gem::SourceInfoCache.instance_variable_set :@cache, nil
+ end
+
+ def test_self_cache_refreshes
+ Gem.configuration.update_sources = true #true by default
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
+
+ Gem.sources.replace %W[#{@gem_repo}]
+
+ use_ui @ui do
+ assert_not_nil Gem::SourceInfoCache.cache
+ assert_kind_of Gem::SourceInfoCache, Gem::SourceInfoCache.cache
+ assert_equal Gem::SourceInfoCache.cache.object_id,
+ Gem::SourceInfoCache.cache.object_id
+ end
+
+ assert_match %r|Bulk updating|, @ui.output
+ end
+
+ def test_self_cache_skips_refresh_based_on_configuration
+ Gem.configuration.update_sources = false
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
+
+ Gem.sources.replace %w[#{@gem_repo}]
+
+ use_ui @ui do
+ assert_not_nil Gem::SourceInfoCache.cache
+ assert_kind_of Gem::SourceInfoCache, Gem::SourceInfoCache.cache
+ assert_equal Gem::SourceInfoCache.cache.object_id,
+ Gem::SourceInfoCache.cache.object_id
+ assert_no_match %r|Bulk updating|, @ui.output
+ end
+ end
+
+ def test_self_cache_data
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
+
+ Gem::SourceInfoCache.instance_variable_set :@cache, nil
+ sice = Gem::SourceInfoCacheEntry.new si, 0
+
+ use_ui @ui do
+ gems = Gem::SourceInfoCache.cache_data[@gem_repo].source_index.gems
+ gem_names = gems.map { |_, spec| spec.full_name }
+
+ assert_equal si.gems.map { |_,spec| spec.full_name }, gem_names
+ end
+ end
+
+ def test_cache_data
+ assert_equal [[@gem_repo, @usr_sice]], @sic.cache_data.to_a.sort
+ end
+
+ def test_cache_data_dirty
+ def @sic.dirty() @dirty; end
+ assert_equal false, @sic.dirty, 'clean on init'
+ @sic.cache_data
+ assert_equal false, @sic.dirty, 'clean on fetch'
+ @sic.update
+ @sic.cache_data
+ assert_equal true, @sic.dirty, 'still dirty'
+ end
+
+ def test_cache_data_irreparable
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = @source_index.dump
+
+ data = { @gem_repo => { 'totally' => 'borked' } }
+
+ cache_files = [
+ @sic.system_cache_file,
+ @sic.latest_system_cache_file,
+ @sic.user_cache_file,
+ @sic.latest_user_cache_file
+ ]
+
+ cache_files.each do |fn|
+ FileUtils.mkdir_p File.dirname(fn)
+ open(fn, "wb") { |f| f.write Marshal.dump(data) }
+ end
+
+ @sic.instance_eval { @cache_data = nil }
+
+ fetched = use_ui @ui do @sic.cache_data end
+
+ fetched_si = fetched["#{@gem_repo}"].source_index
+
+ assert_equal @source_index.index_signature, fetched_si.index_signature
+ end
+
+ def test_cache_data_none_readable
+ FileUtils.chmod 0222, @sic.system_cache_file
+ FileUtils.chmod 0222, @sic.latest_system_cache_file
+ FileUtils.chmod 0222, @sic.user_cache_file
+ FileUtils.chmod 0222, @sic.latest_user_cache_file
+ return if (File.stat(@sic.system_cache_file).mode & 0222) != 0222
+ return if (File.stat(@sic.user_cache_file).mode & 0222) != 0222
+ # HACK for systems that don't support chmod
+ assert_equal({}, @sic.cache_data)
+ end
+
+ def test_cache_data_none_writable
+ FileUtils.chmod 0444, @sic.system_cache_file
+ FileUtils.chmod 0444, @sic.user_cache_file
+ e = assert_raise RuntimeError do
+ @sic.cache_data
+ end
+ assert_equal 'unable to locate a writable cache file', e.message
+ end
+
+ def test_cache_data_nonexistent
+ FileUtils.rm @sic.system_cache_file
+ FileUtils.rm @sic.latest_system_cache_file
+ FileUtils.rm @sic.user_cache_file
+ FileUtils.rm @sic.latest_user_cache_file
+
+ # TODO test verbose output
+ assert_equal [], @sic.cache_data.to_a.sort
+ end
+
+ def test_cache_data_repair
+ data = {
+ @gem_repo => {
+ 'cache' => Gem::SourceIndex.new,
+ 'size' => 0,
+ }
+ }
+ [@sic.system_cache_file, @sic.user_cache_file].each do |fn|
+ FileUtils.mkdir_p File.dirname(fn)
+ open(fn, "wb") { |f| f.write Marshal.dump(data) }
+ end
+
+ @sic.instance_eval { @cache_data = nil }
+
+ expected = {
+ @gem_repo =>
+ Gem::SourceInfoCacheEntry.new(Gem::SourceIndex.new, 0)
+ }
+ assert_equal expected, @sic.cache_data
+ end
+
+ def test_cache_data_user_fallback
+ FileUtils.chmod 0444, @sic.system_cache_file
+
+ assert_equal [[@gem_repo, @usr_sice]], @sic.cache_data.to_a.sort
+ end
+
+ def test_cache_file
+ assert_equal @gemcache, @sic.cache_file
+ end
+
+ def test_cache_file_user_fallback
+ FileUtils.chmod 0444, @sic.system_cache_file
+ assert_equal @usrcache, @sic.cache_file
+ end
+
+ def test_cache_file_none_writable
+ FileUtils.chmod 0444, @sic.system_cache_file
+ FileUtils.chmod 0444, @sic.user_cache_file
+ e = assert_raise RuntimeError do
+ @sic.cache_file
+ end
+ assert_equal 'unable to locate a writable cache file', e.message
+ end
+
+ def test_flush
+ @sic.cache_data[@gem_repo] = @sice_new
+ @sic.update
+ @sic.flush
+
+ assert_equal [[@gem_repo, @sice_new]],
+ read_cache(@sic.system_cache_file).to_a.sort
+ end
+
+ def test_latest_cache_data
+ util_make_gems
+
+ sice = Gem::SourceInfoCacheEntry.new @source_index, 0
+
+ @sic.set_cache_data @gem_repo => sice
+ latest = @sic.latest_cache_data
+ gems = latest[@gem_repo].source_index.search('a').map { |s| s.full_name }
+
+ assert_equal %w[a-2 a_evil-9], gems
+ end
+
+ def test_latest_cache_file
+ latest_cache_file = File.join File.dirname(@gemcache),
+ "latest_#{File.basename @gemcache}"
+ assert_equal latest_cache_file, @sic.latest_cache_file
+ end
+
+ def test_latest_system_cache_file
+ assert_equal File.join(Gem.dir, "latest_source_cache"),
+ @sic.latest_system_cache_file
+ end
+
+ def test_latest_user_cache_file
+ assert_equal @latest_usrcache, @sic.latest_user_cache_file
+ end
+
+ def test_read_system_cache
+ assert_equal [[@gem_repo, @sys_sice]], @sic.cache_data.to_a.sort
+ end
+
+ def test_read_user_cache
+ FileUtils.chmod 0444, @sic.user_cache_file
+ FileUtils.chmod 0444, @sic.latest_user_cache_file
+
+ @si = Gem::SourceIndex.new
+ @si.add_specs @a1, @a2
+
+ @sice = Gem::SourceInfoCacheEntry.new @si, 0
+
+ @sic.set_cache_data({ @gem_repo => @sice })
+ @sic.update
+ @sic.write_cache
+ @sic.reset_cache_data
+
+ user_cache_data = @sic.cache_data.to_a.sort
+
+ assert_equal 1, user_cache_data.length
+ user_cache_data = user_cache_data.first
+
+ assert_equal @gem_repo, user_cache_data.first
+
+ gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
+ assert_equal [@a2.full_name], gems
+ end
+
+ def test_search
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
+ @sic.instance_variable_set :@cache_data, cache_data
+
+ assert_equal [@a1], @sic.search(//)
+ end
+
+ def test_search_all
+ util_make_gems
+
+ sice = Gem::SourceInfoCacheEntry.new @source_index, 0
+
+ @sic.set_cache_data @gem_repo => sice
+ @sic.update
+ @sic.instance_variable_set :@only_latest, false
+ @sic.write_cache
+ @sic.reset_cache_data
+
+ gem_names = @sic.search(//, false, true).map { |spec| spec.full_name }
+
+ assert_equal %w[a-1 a-2 a_evil-9 c-1.2], gem_names
+ end
+
+ def test_search_dependency
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
+ @sic.instance_variable_set :@cache_data, cache_data
+
+ dep = Gem::Dependency.new @a1.name, @a1.version
+
+ assert_equal [@a1], @sic.search(dep)
+ end
+
+ def test_search_no_matches
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
+ @sic.instance_variable_set :@cache_data, cache_data
+
+ assert_equal [], @sic.search(/nonexistent/)
+ end
+
+ def test_search_no_matches_in_source
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
+ @sic.instance_variable_set :@cache_data, cache_data
+ Gem.sources.replace %w[more-gems.example.com]
+
+ assert_equal [], @sic.search(/nonexistent/)
+ end
+
+ def test_search_with_source
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
+ @sic.instance_variable_set :@cache_data, cache_data
+
+ assert_equal [[@a1, @gem_repo]],
+ @sic.search_with_source(//)
+ end
+
+ def test_system_cache_file
+ assert_equal File.join(Gem.dir, "source_cache"), @sic.system_cache_file
+ end
+
+ def test_user_cache_file
+ assert_equal @usrcache, @sic.user_cache_file
+ end
+
+ def test_write_cache
+ @sic.cache_data[@gem_repo] = @sice_new
+ @sic.write_cache
+
+ assert_equal [[@gem_repo, @sice_new]],
+ read_cache(@sic.system_cache_file).to_a.sort
+ assert_equal [[@gem_repo, @usr_sice]],
+ read_cache(@sic.user_cache_file).to_a.sort
+ end
+
+ def test_write_cache_user
+ FileUtils.chmod 0444, @sic.system_cache_file
+ @sic.set_cache_data({@gem_repo => @sice_new})
+ @sic.update
+ @sic.write_cache
+ @sic.instance_variable_set :@only_latest, false
+
+ assert File.exist?(@sic.user_cache_file), 'user_cache_file'
+ assert File.exist?(@sic.latest_user_cache_file),
+ 'latest_user_cache_file exists'
+
+ assert_equal [[@gem_repo, @sys_sice]],
+ read_cache(@sic.system_cache_file).to_a.sort
+ assert_equal [[@gem_repo, @sice_new]],
+ read_cache(@sic.user_cache_file).to_a.sort
+ end
+
+ def test_write_cache_user_from_scratch
+ FileUtils.rm_rf @sic.user_cache_file
+ FileUtils.rm_rf @sic.latest_user_cache_file
+
+ FileUtils.chmod 0444, @sic.system_cache_file
+ FileUtils.chmod 0444, @sic.latest_system_cache_file
+
+ @si = Gem::SourceIndex.new
+ @si.add_specs @a1, @a2
+
+ @sice = Gem::SourceInfoCacheEntry.new @si, 0
+
+ @sic.set_cache_data({ @gem_repo => @sice })
+ @sic.update
+
+ @sic.write_cache
+
+ assert File.exist?(@sic.user_cache_file), 'system_cache_file'
+ assert File.exist?(@sic.latest_user_cache_file),
+ 'latest_system_cache_file'
+
+ user_cache_data = read_cache(@sic.user_cache_file).to_a.sort
+ assert_equal 1, user_cache_data.length, 'user_cache_data length'
+ user_cache_data = user_cache_data.first
+
+ assert_equal @gem_repo, user_cache_data.first
+
+ gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
+ assert_equal [@a1.full_name, @a2.full_name], gems.sort
+
+ user_cache_data = read_cache(@sic.latest_user_cache_file).to_a.sort
+ assert_equal 1, user_cache_data.length
+ user_cache_data = user_cache_data.first
+
+ assert_equal @gem_repo, user_cache_data.first
+
+ gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
+ assert_equal [@a2.full_name], gems
+ end
+
+ def test_write_cache_user_no_directory
+ FileUtils.rm_rf File.dirname(@sic.user_cache_file)
+ FileUtils.chmod 0444, @sic.system_cache_file
+ @sic.set_cache_data({ @gem_repo => @sice_new })
+ @sic.update
+ @sic.write_cache
+
+ assert_equal [[@gem_repo, @sys_sice]],
+ read_cache(@sic.system_cache_file).to_a.sort
+ assert_equal [[@gem_repo, @sys_sice]],
+ read_cache(@sic.user_cache_file).to_a.sort
+ assert_equal [[@gem_repo, @sice_new]],
+ read_cache(@sic.latest_user_cache_file).to_a.sort
+ end
+
+ def test_write_cache_user_only_latest
+ FileUtils.chmod 0444, @sic.system_cache_file
+ @sic.set_cache_data({@gem_repo => @sice_new})
+ @sic.update
+ @sic.write_cache
+
+ assert File.exist?(@sic.user_cache_file), 'user_cache_file'
+ assert File.exist?(@sic.latest_user_cache_file),
+ 'latest_user_cache_file exists'
+
+ assert_equal [[@gem_repo, @sys_sice]],
+ read_cache(@sic.system_cache_file).to_a.sort
+ assert_equal [[@gem_repo, @sice_new]],
+ read_cache(@sic.user_cache_file).to_a.sort
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_source_info_cache_entry.rb b/trunk/test/rubygems/test_gem_source_info_cache_entry.rb
new file mode 100644
index 0000000000..6986c9cd7f
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_source_info_cache_entry.rb
@@ -0,0 +1,79 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/source_info_cache_entry'
+
+class TestGemSourceInfoCacheEntry < RubyGemTestCase
+
+ def setup
+ super
+
+ util_setup_fake_fetcher
+
+ @si = Gem::SourceIndex.new
+ @si.add_spec @a1
+ @sic_e = Gem::SourceInfoCacheEntry.new @si, @si.dump.size
+ end
+
+ def test_refresh
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] =
+ proc { raise }
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = @si.dump
+
+ use_ui @ui do
+ @sic_e.refresh @gem_repo, true
+ end
+ end
+
+ def test_refresh_all
+ @si.add_spec @a2
+
+ a1_name = @a1.full_name
+ a2_name = @a2.full_name
+
+ @fetcher.data["#{@gem_repo}quick/index.rz"] =
+ util_zip [a1_name, a2_name].join("\n")
+ @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = util_zip a2_name
+ @fetcher.data["#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{a1_name}.gemspec.rz"] = util_zip Marshal.dump(@a1)
+ @fetcher.data["#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{a2_name}.gemspec.rz"] = util_zip Marshal.dump(@a2)
+ @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] =
+ Marshal.dump @si
+
+ sic_e = Gem::SourceInfoCacheEntry.new Gem::SourceIndex.new, 0
+
+ assert_equal [], sic_e.source_index.map { |n,| n }
+
+ use_ui @ui do
+ assert sic_e.refresh(@gem_repo, false)
+ end
+
+ assert_equal [a2_name], sic_e.source_index.map { |n,| n }.sort
+
+ use_ui @ui do
+ sic_e.refresh @gem_repo, true
+ end
+
+ assert_equal [a1_name, a2_name], sic_e.source_index.map { |n,| n }.sort
+ end
+
+ def test_refresh_bad_uri
+ assert_raise URI::BadURIError do
+ @sic_e.refresh 'gems.example.com', true
+ end
+ end
+
+ def test_refresh_update
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+ si.add_spec @b2
+ @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
+
+ use_ui @ui do
+ @sic_e.refresh @gem_repo, true
+ end
+
+ new_gem = @sic_e.source_index.specification(@b2.full_name)
+ assert_equal @b2.full_name, new_gem.full_name
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_spec_fetcher.rb b/trunk/test/rubygems/test_gem_spec_fetcher.rb
new file mode 100644
index 0000000000..2e9ce1b7eb
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_spec_fetcher.rb
@@ -0,0 +1,303 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/spec_fetcher'
+
+class TestGemSpecFetcher < RubyGemTestCase
+
+ def setup
+ super
+
+ @uri = URI.parse @gem_repo
+
+ util_setup_fake_fetcher
+
+ @source_index.add_spec @pl1
+
+ @specs = @source_index.gems.sort.map do |name, spec|
+ [spec.name, spec.version, spec.original_platform]
+ end.sort
+
+ @fetcher.data["#{@gem_repo}specs.#{Gem.marshal_version}.gz"] =
+ util_gzip(Marshal.dump(@specs))
+
+ @latest_specs = @source_index.latest_specs.sort.map do |spec|
+ [spec.name, spec.version, spec.original_platform]
+ end
+
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] =
+ util_gzip(Marshal.dump(@latest_specs))
+
+ @sf = Gem::SpecFetcher.new
+ end
+
+ def test_fetch_all
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@a1))
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a2.full_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@a2))
+
+ dep = Gem::Dependency.new 'a', 1
+ specs_and_sources = @sf.fetch dep, true
+
+ spec_names = specs_and_sources.map do |spec, source_uri|
+ [spec.full_name, source_uri]
+ end
+
+ expected = [[@a1.full_name, @gem_repo], [@a2.full_name, @gem_repo]]
+
+ assert_equal expected, spec_names
+
+ assert_same specs_and_sources.first.last, specs_and_sources.last.last
+ end
+
+ def test_fetch_latest
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@a1))
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a2.full_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@a2))
+
+ dep = Gem::Dependency.new 'a', 1
+ specs_and_sources = @sf.fetch dep
+
+ spec_names = specs_and_sources.map do |spec, source_uri|
+ [spec.full_name, source_uri]
+ end
+
+ assert_equal [[@a2.full_name, @gem_repo]], spec_names
+ end
+
+ def test_fetch_legacy_repo
+ @fetcher.data["#{@gem_repo}specs.#{Gem.marshal_version}.gz"] = nil
+ @fetcher.data["#{@gem_repo}yaml"] = ''
+ util_setup_source_info_cache @a1, @a2
+
+ dep = Gem::Dependency.new 'a', 1
+ specs = nil
+
+ use_ui @ui do
+ specs = @sf.fetch dep, true
+ end
+
+ expected = <<-EOF
+WARNING: RubyGems 1.2+ index not found for:
+\thttp://gems.example.com/
+
+RubyGems will revert to legacy indexes degrading performance.
+ EOF
+
+ assert_equal expected, @ui.error
+
+ specs = specs.map { |spec, source_uri| [spec.full_name, source_uri] }
+
+ expected = [
+ [@a1.full_name, @gem_repo],
+ [@a2.full_name, @gem_repo],
+ ]
+
+ assert_equal expected, specs
+ end
+
+ def test_fetch_platform
+ util_set_arch 'i386-linux'
+
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@pl1.original_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@pl1))
+
+ dep = Gem::Dependency.new 'pl', 1
+ specs_and_sources = @sf.fetch dep
+
+ spec_names = specs_and_sources.map do |spec, source_uri|
+ [spec.full_name, source_uri]
+ end
+
+ assert_equal [[@pl1.full_name, @gem_repo]], spec_names
+ end
+
+ def test_fetch_spec
+ spec_uri = "#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec"
+ @fetcher.data["#{spec_uri}.rz"] = util_zip(Marshal.dump(@a1))
+
+ spec = @sf.fetch_spec ['a', Gem::Version.new(1), 'ruby'], @uri
+ assert_equal @a1.full_name, spec.full_name
+
+ cache_dir = @sf.cache_dir URI.parse(spec_uri)
+
+ cache_file = File.join cache_dir, "#{@a1.full_name}.gemspec"
+
+ assert File.exist?(cache_file)
+ end
+
+ def test_fetch_spec_cached
+ spec_uri = "#{@gem_repo}/#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec"
+ @fetcher.data["#{spec_uri}.rz"] = nil
+
+ cache_dir = @sf.cache_dir URI.parse(spec_uri)
+ FileUtils.mkdir_p cache_dir
+
+ cache_file = File.join cache_dir, "#{@a1.full_name}.gemspec"
+
+ open cache_file, 'wb' do |io|
+ Marshal.dump @a1, io
+ end
+
+ spec = @sf.fetch_spec ['a', Gem::Version.new(1), 'ruby'], @uri
+ assert_equal @a1.full_name, spec.full_name
+ end
+
+ def test_fetch_spec_platform
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@pl1.original_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@pl1))
+
+ spec = @sf.fetch_spec ['pl', Gem::Version.new(1), 'i386-linux'], @uri
+
+ assert_equal @pl1.full_name, spec.full_name
+ end
+
+ def test_fetch_spec_platform_ruby
+ @fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
+ util_zip(Marshal.dump(@a1))
+
+ spec = @sf.fetch_spec ['a', Gem::Version.new(1), nil], @uri
+ assert_equal @a1.full_name, spec.full_name
+
+ spec = @sf.fetch_spec ['a', Gem::Version.new(1), ''], @uri
+ assert_equal @a1.full_name, spec.full_name
+ end
+
+ def test_find_matching_all
+ dep = Gem::Dependency.new 'a', 1
+ specs = @sf.find_matching dep, true
+
+ expected = [
+ [['a', Gem::Version.new(1), Gem::Platform::RUBY], @gem_repo],
+ [['a', Gem::Version.new(2), Gem::Platform::RUBY], @gem_repo],
+ ]
+
+ assert_equal expected, specs
+ end
+
+ def test_find_matching_latest
+ dep = Gem::Dependency.new 'a', 1
+ specs = @sf.find_matching dep
+
+ expected = [
+ [['a', Gem::Version.new(2), Gem::Platform::RUBY], @gem_repo],
+ ]
+
+ assert_equal expected, specs
+ end
+
+ def test_find_matching_platform
+ util_set_arch 'i386-linux'
+
+ dep = Gem::Dependency.new 'pl', 1
+ specs = @sf.find_matching dep
+
+ expected = [
+ [['pl', Gem::Version.new(1), 'i386-linux'], @gem_repo],
+ ]
+
+ assert_equal expected, specs
+
+ util_set_arch 'i386-freebsd6'
+
+ dep = Gem::Dependency.new 'pl', 1
+ specs = @sf.find_matching dep
+
+ assert_equal [], specs
+ end
+
+ def test_find_all_platforms
+ util_set_arch 'i386-freebsd6'
+
+ dep = Gem::Dependency.new 'pl', 1
+ specs = @sf.find_matching dep, false, false
+
+ expected = [
+ [['pl', Gem::Version.new(1), 'i386-linux'], @gem_repo],
+ ]
+
+ assert_equal expected, specs
+ end
+
+ def test_list
+ specs = @sf.list
+
+ assert_equal [@uri], specs.keys
+ assert_equal @latest_specs, specs[@uri].sort
+ end
+
+ def test_list_all
+ specs = @sf.list true
+
+ assert_equal [@uri], specs.keys
+
+ assert_equal @specs, specs[@uri].sort
+ end
+
+ def test_list_cache
+ specs = @sf.list
+
+ assert !specs[@uri].empty?
+
+ @fetcher.data["#{@gem_repo}/latest_specs.#{Gem.marshal_version}.gz"] = nil
+
+ cached_specs = @sf.list
+
+ assert_equal specs, cached_specs
+ end
+
+ def test_list_cache_all
+ specs = @sf.list true
+
+ assert !specs[@uri].empty?
+
+ @fetcher.data["#{@gem_repo}/specs.#{Gem.marshal_version}.gz"] = nil
+
+ cached_specs = @sf.list true
+
+ assert_equal specs, cached_specs
+ end
+
+ def test_load_specs
+ specs = @sf.load_specs @uri, 'specs'
+
+ expected = [
+ ['a', Gem::Version.new(1), Gem::Platform::RUBY],
+ ['a', Gem::Version.new(2), Gem::Platform::RUBY],
+ ['a_evil', Gem::Version.new(9), Gem::Platform::RUBY],
+ ['c', Gem::Version.new('1.2'), Gem::Platform::RUBY],
+ ['pl', Gem::Version.new(1), 'i386-linux'],
+ ]
+
+ assert_equal expected, specs
+
+ cache_dir = File.join Gem.user_home, '.gem', 'specs', 'gems.example.com%80'
+ assert File.exist?(cache_dir), "#{cache_dir} does not exist"
+
+ cache_file = File.join cache_dir, "specs.#{Gem.marshal_version}"
+ assert File.exist?(cache_file)
+ end
+
+ def test_load_specs_cached
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] = ''
+ @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}"] =
+ ' ' * Marshal.dump(@latest_specs).length
+
+ cache_dir = File.join Gem.user_home, '.gem', 'specs', 'gems.example.com%80'
+
+ FileUtils.mkdir_p cache_dir
+
+ cache_file = File.join cache_dir, "latest_specs.#{Gem.marshal_version}"
+
+ open cache_file, 'wb' do |io|
+ Marshal.dump @latest_specs, io
+ end
+
+ latest_specs = @sf.load_specs @uri, 'latest_specs'
+
+ assert_equal @latest_specs, latest_specs
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_specification.rb b/trunk/test/rubygems/test_gem_specification.rb
new file mode 100644
index 0000000000..57c3fdc158
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_specification.rb
@@ -0,0 +1,954 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'stringio'
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/specification'
+
+class TestGemSpecification < RubyGemTestCase
+
+ LEGACY_YAML_SPEC = <<-EOF
+--- !ruby/object:Gem::Specification
+rubygems_version: "1.0"
+name: keyedlist
+version: !ruby/object:Gem::Version
+ version: 0.4.0
+date: 2004-03-28 15:37:49.828000 +02:00
+platform:
+summary: A Hash which automatically computes keys.
+require_paths:
+ - lib
+files:
+ - lib/keyedlist.rb
+autorequire: keyedlist
+author: Florian Gross
+email: flgr@ccan.de
+has_rdoc: true
+ EOF
+
+ LEGACY_RUBY_SPEC = <<-EOF
+Gem::Specification.new do |s|
+ s.name = %q{keyedlist}
+ s.version = %q{0.4.0}
+ s.has_rdoc = true
+ s.summary = %q{A Hash which automatically computes keys.}
+ s.files = ["lib/keyedlist.rb"]
+ s.require_paths = ["lib"]
+ s.autorequire = %q{keyedlist}
+ s.author = %q{Florian Gross}
+ s.email = %q{flgr@ccan.de}
+end
+ EOF
+
+ def setup
+ super
+
+ @a1 = quick_gem 'a', '1' do |s|
+ s.executable = 'exec'
+ s.extensions << 'ext/a/extconf.rb'
+ s.has_rdoc = 'true'
+ s.test_file = 'test/suite.rb'
+ s.requirements << 'A working computer'
+ s.rubyforge_project = 'example'
+
+ s.add_dependency 'rake', '> 0.4'
+ s.add_dependency 'jabber4r', '> 0.0.0'
+ s.add_dependency 'pqa', ['> 0.4', '<= 0.6']
+
+ s.mark_version
+ s.files = %w[lib/code.rb]
+ end
+
+ @a2 = quick_gem 'a', '2' do |s|
+ s.files = %w[lib/code.rb]
+ end
+
+ FileUtils.mkdir_p File.join(@tempdir, 'bin')
+ File.open File.join(@tempdir, 'bin', 'exec'), 'w' do |fp|
+ fp.puts "#!#{Gem.ruby}"
+ end
+
+ @current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ end
+
+ def test_self_attribute_names
+ expected_value = %w[
+ authors
+ autorequire
+ bindir
+ cert_chain
+ date
+ default_executable
+ dependencies
+ description
+ email
+ executables
+ extensions
+ extra_rdoc_files
+ files
+ has_rdoc
+ homepage
+ name
+ platform
+ post_install_message
+ rdoc_options
+ require_paths
+ required_ruby_version
+ required_rubygems_version
+ requirements
+ rubyforge_project
+ rubygems_version
+ signing_key
+ specification_version
+ summary
+ test_files
+ version
+ ]
+
+ actual_value = Gem::Specification.attribute_names.map { |a| a.to_s }.sort
+
+ assert_equal expected_value, actual_value
+ end
+
+ def test_self__load_future
+ spec = Gem::Specification.new
+ spec.name = 'a'
+ spec.version = '1'
+ spec.specification_version = @current_version + 1
+
+ new_spec = Marshal.load Marshal.dump(spec)
+
+ assert_equal 'a', new_spec.name
+ assert_equal Gem::Version.new(1), new_spec.version
+ assert_equal @current_version, new_spec.specification_version
+ end
+
+ def test_self_load
+ spec = File.join @gemhome, 'specifications', "#{@a2.full_name}.gemspec"
+ gs = Gem::Specification.load spec
+
+ assert_equal @a2, gs
+ end
+
+ def test_self_load_legacy_ruby
+ spec = eval LEGACY_RUBY_SPEC
+ assert_equal 'keyedlist', spec.name
+ assert_equal '0.4.0', spec.version.to_s
+ assert_equal true, spec.has_rdoc?
+ assert_equal Gem::Specification::TODAY, spec.date
+ assert spec.required_ruby_version.satisfied_by?(Gem::Version.new('1'))
+ assert_equal false, spec.has_unit_tests?
+ end
+
+ def test_self_load_legacy_yaml
+ s = YAML.load StringIO.new(LEGACY_YAML_SPEC)
+ assert_equal 'keyedlist', s.name
+ assert_equal '0.4.0', s.version.to_s
+ assert_equal true, s.has_rdoc?
+ #assert_equal Date.today, s.date
+ #assert s.required_ruby_version.satisfied_by?(Gem::Version.new('1'))
+ assert_equal false, s.has_unit_tests?
+ end
+
+ def test_self_normalize_yaml_input_with_183_yaml
+ input = "!ruby/object:Gem::Specification "
+ assert_equal "--- #{input}", Gem::Specification.normalize_yaml_input(input)
+ end
+
+ def test_self_normalize_yaml_input_with_non_183_yaml
+ input = "--- !ruby/object:Gem::Specification "
+ assert_equal input, Gem::Specification.normalize_yaml_input(input)
+ end
+
+ def test_self_normalize_yaml_input_with_183_io
+ input = "!ruby/object:Gem::Specification "
+ assert_equal "--- #{input}",
+ Gem::Specification.normalize_yaml_input(StringIO.new(input))
+ end
+
+ def test_self_normalize_yaml_input_with_non_183_io
+ input = "--- !ruby/object:Gem::Specification "
+ assert_equal input,
+ Gem::Specification.normalize_yaml_input(StringIO.new(input))
+ end
+
+ def test_initialize
+ spec = Gem::Specification.new do |s|
+ s.name = "blah"
+ s.version = "1.3.5"
+ end
+
+ assert_equal "blah", spec.name
+ assert_equal "1.3.5", spec.version.to_s
+ assert_equal Gem::Platform::RUBY, spec.platform
+ assert_equal nil, spec.summary
+ assert_equal [], spec.files
+
+ assert_equal [], spec.test_files
+ assert_equal [], spec.rdoc_options
+ assert_equal [], spec.extra_rdoc_files
+ assert_equal [], spec.executables
+ assert_equal [], spec.extensions
+ assert_equal [], spec.requirements
+ assert_equal [], spec.dependencies
+ assert_equal 'bin', spec.bindir
+ assert_equal false, spec.has_rdoc
+ assert_equal false, spec.has_rdoc?
+ assert_equal '>= 0', spec.required_ruby_version.to_s
+ assert_equal '>= 0', spec.required_rubygems_version.to_s
+ end
+
+ def test_initialize_future
+ version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + 1
+ spec = Gem::Specification.new do |s|
+ s.name = "blah"
+ s.version = "1.3.5"
+
+ s.specification_version = version
+
+ s.new_unknown_attribute = "a value"
+ end
+
+ assert_equal "blah", spec.name
+ assert_equal "1.3.5", spec.version.to_s
+ end
+
+ def test__dump
+ @a2.platform = Gem::Platform.local
+ @a2.instance_variable_set :@original_platform, 'old_platform'
+
+ data = Marshal.dump @a2
+
+ same_spec = Marshal.load data
+
+ assert_equal 'old_platform', same_spec.original_platform
+ end
+
+ def test_add_dependency_with_explicit_type
+ gem = quick_gem "awesome", "1.0" do |awesome|
+ awesome.add_development_dependency "monkey"
+ end
+
+ monkey = gem.dependencies.detect { |d| d.name == "monkey" }
+ assert_equal(:development, monkey.type)
+ end
+
+ def test_author
+ assert_equal 'A User', @a1.author
+ end
+
+ def test_authors
+ assert_equal ['A User'], @a1.authors
+ end
+
+ def test_bindir_equals
+ @a1.bindir = 'apps'
+
+ assert_equal 'apps', @a1.bindir
+ end
+
+ def test_bindir_equals_nil
+ @a2.bindir = nil
+ @a2.executable = 'app'
+
+ assert_equal nil, @a2.bindir
+ assert_equal %w[lib/code.rb app], @a2.files
+ end
+
+ def test_date
+ assert_equal Gem::Specification::TODAY, @a1.date
+ end
+
+ def test_date_equals_date
+ @a1.date = Date.new(2003, 9, 17)
+ assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
+ end
+
+ def test_date_equals_string
+ @a1.date = '2003-09-17'
+ assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
+ end
+
+ def test_date_equals_time
+ @a1.date = Time.local(2003, 9, 17, 0,0,0)
+ assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
+ end
+
+ def test_date_equals_time_local
+ # HACK PDT
+ @a1.date = Time.local(2003, 9, 17, 19,50,0)
+ assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
+ end
+
+ def test_date_equals_time_utc
+ # HACK PDT
+ @a1.date = Time.local(2003, 9, 17, 19,50,0)
+ assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
+ end
+
+ def test_default_executable
+ assert_equal 'exec', @a1.default_executable
+
+ @a1.default_executable = nil
+ @a1.instance_variable_set :@executables, nil
+ assert_equal nil, @a1.default_executable
+ end
+
+ def test_dependencies
+ rake = Gem::Dependency.new 'rake', '> 0.4'
+ jabber = Gem::Dependency.new 'jabber4r', '> 0.0.0'
+ pqa = Gem::Dependency.new 'pqa', ['> 0.4', '<= 0.6']
+
+ assert_equal [rake, jabber, pqa], @a1.dependencies
+ end
+
+ def test_dependencies_scoped_by_type
+ gem = quick_gem "awesome", "1.0" do |awesome|
+ awesome.add_runtime_dependency "bonobo", []
+ awesome.add_development_dependency "monkey", []
+ end
+
+ bonobo = Gem::Dependency.new("bonobo", [])
+ monkey = Gem::Dependency.new("monkey", [], :development)
+
+ assert_equal([bonobo, monkey], gem.dependencies)
+ assert_equal([bonobo], gem.runtime_dependencies)
+ assert_equal([monkey], gem.development_dependencies)
+ end
+
+ def test_description
+ assert_equal 'This is a test description', @a1.description
+ end
+
+ def test_eql_eh
+ g1 = quick_gem 'gem'
+ g2 = quick_gem 'gem'
+
+ assert_equal g1, g2
+ assert_equal g1.hash, g2.hash
+ assert_equal true, g1.eql?(g2)
+ end
+
+ def test_equals2
+ assert_equal @a1, @a1
+ assert_equal @a1, @a1.dup
+ assert_not_equal @a1, @a2
+ assert_not_equal @a1, Object.new
+ end
+
+ # The cgikit specification was reported to be causing trouble in at least
+ # one version of RubyGems, so we test explicitly for it.
+ def test_equals2_cgikit
+ cgikit = Gem::Specification.new do |s|
+ s.name = %q{cgikit}
+ s.version = "1.1.0"
+ s.date = %q{2004-03-13}
+ s.summary = %q{CGIKit is a componented-oriented web application } +
+ %q{framework like Apple Computers WebObjects. } +
+ %{This framework services Model-View-Controller architecture } +
+ %q{programming by components based on a HTML file, a definition } +
+ %q{file and a Ruby source. }
+ s.email = %q{info@spice-of-life.net}
+ s.homepage = %q{http://www.spice-of-life.net/download/cgikit/}
+ s.autorequire = %q{cgikit}
+ s.bindir = nil
+ s.has_rdoc = nil
+ s.required_ruby_version = nil
+ s.platform = nil
+ s.files = ["lib/cgikit", "lib/cgikit.rb", "lib/cgikit/components", "..."]
+ end
+
+ assert_equal cgikit, cgikit
+ end
+
+ def test_equals2_default_executable
+ spec = @a1.dup
+ spec.default_executable = 'xx'
+
+ assert_not_equal @a1, spec
+ assert_not_equal spec, @a1
+ end
+
+ def test_equals2_extensions
+ spec = @a1.dup
+ spec.extensions = 'xx'
+
+ assert_not_equal @a1, spec
+ assert_not_equal spec, @a1
+ end
+
+ def test_executables
+ @a1.executable = 'app'
+ assert_equal %w[app], @a1.executables
+ end
+
+ def test_executable_equals
+ @a2.executable = 'app'
+ assert_equal 'app', @a2.executable
+ assert_equal %w[lib/code.rb bin/app], @a2.files
+ end
+
+ def test_extensions
+ assert_equal ['ext/a/extconf.rb'], @a1.extensions
+ end
+
+ def test_files
+ @a1.files = %w(files bin/common)
+ @a1.test_files = %w(test_files bin/common)
+ @a1.executables = %w(executables common)
+ @a1.extra_rdoc_files = %w(extra_rdoc_files bin/common)
+ @a1.extensions = %w(extensions bin/common)
+
+ expected = %w[
+ bin/common
+ bin/executables
+ extensions
+ extra_rdoc_files
+ files
+ test_files
+ ]
+ assert_equal expected, @a1.files.sort
+ end
+
+ def test_files_duplicate
+ @a2.files = %w[a b c d b]
+ @a2.extra_rdoc_files = %w[x y z x]
+ @a2.normalize
+
+ assert_equal %w[a b c d x y z], @a2.files
+ assert_equal %w[x y z], @a2.extra_rdoc_files
+ end
+
+ def test_files_extra_rdoc_files
+ @a2.files = %w[a b c d]
+ @a2.extra_rdoc_files = %w[x y z]
+ @a2.normalize
+ assert_equal %w[a b c d x y z], @a2.files
+ end
+
+ def test_files_non_array
+ @a1.files = "F"
+ @a1.test_files = "TF"
+ @a1.executables = "X"
+ @a1.extra_rdoc_files = "ERF"
+ @a1.extensions = "E"
+
+ assert_equal %w[E ERF F TF bin/X], @a1.files.sort
+ end
+
+ def test_files_non_array_pathological
+ @a1.instance_variable_set :@files, "F"
+ @a1.instance_variable_set :@test_files, "TF"
+ @a1.instance_variable_set :@extra_rdoc_files, "ERF"
+ @a1.instance_variable_set :@extensions, "E"
+ @a1.instance_variable_set :@executables, "X"
+
+ assert_equal %w[E ERF F TF bin/X], @a1.files.sort
+ assert_kind_of Integer, @a1.hash
+ end
+
+ def test_full_gem_path
+ assert_equal File.join(@gemhome, 'gems', @a1.full_name),
+ @a1.full_gem_path
+
+ @a1.original_platform = 'mswin32'
+
+ assert_equal File.join(@gemhome, 'gems', @a1.original_name),
+ @a1.full_gem_path
+ end
+
+ def test_full_gem_path_double_slash
+ gemhome = @gemhome.sub(/\w\//, '\&/')
+ @a1.loaded_from = File.join gemhome, 'specifications',
+ "#{@a1.full_name}.gemspec"
+
+ assert_equal File.join(@gemhome, 'gems', @a1.full_name),
+ @a1.full_gem_path
+ end
+
+ def test_full_name
+ assert_equal 'a-1', @a1.full_name
+
+ @a1.platform = Gem::Platform.new ['universal', 'darwin', nil]
+ assert_equal 'a-1-universal-darwin', @a1.full_name
+
+ @a1.instance_variable_set :@new_platform, 'mswin32'
+ assert_equal 'a-1-mswin32', @a1.full_name, 'legacy'
+
+ return if win_platform?
+
+ @a1.platform = 'current'
+ assert_equal 'a-1-x86-darwin-8', @a1.full_name
+ end
+
+ def test_full_name_windows
+ test_cases = {
+ 'i386-mswin32' => 'a-1-x86-mswin32-60',
+ 'i386-mswin32_80' => 'a-1-x86-mswin32-80',
+ 'i386-mingw32' => 'a-1-x86-mingw32'
+ }
+
+ test_cases.each do |arch, expected|
+ util_set_arch arch
+ @a1.platform = 'current'
+ assert_equal expected, @a1.full_name
+ end
+ end
+
+ def test_has_rdoc_eh
+ assert_equal true, @a1.has_rdoc?
+ end
+
+ def test_hash
+ assert_equal @a1.hash, @a1.hash
+ assert_equal @a1.hash, @a1.dup.hash
+ assert_not_equal @a1.hash, @a2.hash
+ end
+
+ def test_lib_files
+ @a1.files = %w[lib/foo.rb Rakefile]
+
+ assert_equal %w[lib/foo.rb], @a1.lib_files
+ end
+
+ def test_name
+ assert_equal 'a', @a1.name
+ end
+
+ def test_original_name
+ assert_equal 'a-1', @a1.full_name
+
+ @a1.platform = 'i386-linux'
+ @a1.instance_variable_set :@original_platform, 'i386-linux'
+ assert_equal 'a-1-i386-linux', @a1.original_name
+ end
+
+ def test_platform
+ assert_equal Gem::Platform::RUBY, @a1.platform
+ end
+
+ def test_platform_equals
+ @a1.platform = nil
+ assert_equal Gem::Platform::RUBY, @a1.platform
+
+ @a1.platform = Gem::Platform::RUBY
+ assert_equal Gem::Platform::RUBY, @a1.platform
+
+ test_cases = {
+ 'i386-mswin32' => ['x86', 'mswin32', '60'],
+ 'i386-mswin32_80' => ['x86', 'mswin32', '80'],
+ 'i386-mingw32' => ['x86', 'mingw32', nil ],
+ 'x86-darwin8' => ['x86', 'darwin', '8' ],
+ }
+
+ test_cases.each do |arch, expected|
+ util_set_arch arch
+ @a1.platform = Gem::Platform::CURRENT
+ assert_equal Gem::Platform.new(expected), @a1.platform
+ end
+ end
+
+ def test_platform_equals_current
+ @a1.platform = Gem::Platform::CURRENT
+ assert_equal Gem::Platform.local, @a1.platform
+ assert_equal Gem::Platform.local.to_s, @a1.original_platform
+ end
+
+ def test_platform_equals_legacy
+ @a1.platform = 'mswin32'
+ assert_equal Gem::Platform.new('x86-mswin32'), @a1.platform
+
+ @a1.platform = 'i586-linux'
+ assert_equal Gem::Platform.new('x86-linux'), @a1.platform
+
+ @a1.platform = 'powerpc-darwin'
+ assert_equal Gem::Platform.new('ppc-darwin'), @a1.platform
+ end
+
+ def test_require_paths
+ @a1.require_path = 'lib'
+ assert_equal %w[lib], @a1.require_paths
+ end
+
+ def test_requirements
+ assert_equal ['A working computer'], @a1.requirements
+ end
+
+ def test_runtime_dependencies_legacy
+ # legacy gems don't have a type
+ @a1.runtime_dependencies.each do |dep|
+ dep.instance_variable_set :@type, nil
+ end
+
+ expected = %w[rake jabber4r pqa]
+
+ assert_equal expected, @a1.runtime_dependencies.map { |d| d.name }
+ end
+
+ def test_spaceship_name
+ s1 = quick_gem 'a', '1'
+ s2 = quick_gem 'b', '1'
+
+ assert_equal(-1, (s1 <=> s2))
+ assert_equal( 0, (s1 <=> s1))
+ assert_equal( 1, (s2 <=> s1))
+ end
+
+ def test_spaceship_platform
+ s1 = quick_gem 'a', '1'
+ s2 = quick_gem 'a', '1' do |s|
+ s.platform = Gem::Platform.new 'x86-my_platform1'
+ end
+
+ assert_equal( -1, (s1 <=> s2))
+ assert_equal( 0, (s1 <=> s1))
+ assert_equal( 1, (s2 <=> s1))
+ end
+
+ def test_spaceship_version
+ s1 = quick_gem 'a', '1'
+ s2 = quick_gem 'a', '2'
+
+ assert_equal( -1, (s1 <=> s2))
+ assert_equal( 0, (s1 <=> s1))
+ assert_equal( 1, (s2 <=> s1))
+ end
+
+ def test_summary
+ assert_equal 'this is a summary', @a1.summary
+ end
+
+ def test_test_files
+ @a1.test_file = 'test/suite.rb'
+ assert_equal ['test/suite.rb'], @a1.test_files
+ end
+
+ def test_to_ruby
+ @a2.add_runtime_dependency 'b', '1'
+ @a2.dependencies.first.instance_variable_set :@type, nil
+ @a2.required_rubygems_version = Gem::Requirement.new '> 0'
+
+ ruby_code = @a2.to_ruby
+
+ expected = "Gem::Specification.new do |s|
+ s.name = %q{a}
+ s.version = \"2\"
+
+ s.required_rubygems_version = Gem::Requirement.new(\"> 0\") if s.respond_to? :required_rubygems_version=
+ s.authors = [\"A User\"]
+ s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
+ s.description = %q{This is a test description}
+ s.email = %q{example@example.com}
+ s.files = [\"lib/code.rb\"]
+ s.has_rdoc = true
+ s.homepage = %q{http://example.com}
+ s.require_paths = [\"lib\"]
+ s.rubygems_version = %q{#{Gem::RubyGemsVersion}}
+ s.summary = %q{this is a summary}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = #{Gem::Specification::CURRENT_SPECIFICATION_VERSION}
+
+ if current_version >= 3 then
+ s.add_runtime_dependency(%q<b>, [\"= 1\"])
+ else
+ s.add_dependency(%q<b>, [\"= 1\"])
+ end
+ else
+ s.add_dependency(%q<b>, [\"= 1\"])
+ end
+end
+"
+
+ assert_equal expected, ruby_code
+
+ same_spec = eval ruby_code
+
+ assert_equal @a2, same_spec
+ end
+
+ def test_to_ruby_fancy
+ @a1.platform = Gem::Platform.local
+ ruby_code = @a1.to_ruby
+
+ local = Gem::Platform.local
+ expected_platform = "[#{local.cpu.inspect}, #{local.os.inspect}, #{local.version.inspect}]"
+
+ expected = "Gem::Specification.new do |s|
+ s.name = %q{a}
+ s.version = \"1\"
+ s.platform = Gem::Platform.new(#{expected_platform})
+
+ s.required_rubygems_version = Gem::Requirement.new(\">= 0\") if s.respond_to? :required_rubygems_version=
+ s.authors = [\"A User\"]
+ s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
+ s.default_executable = %q{exec}
+ s.description = %q{This is a test description}
+ s.email = %q{example@example.com}
+ s.executables = [\"exec\"]
+ s.extensions = [\"ext/a/extconf.rb\"]
+ s.files = [\"lib/code.rb\", \"test/suite.rb\", \"bin/exec\", \"ext/a/extconf.rb\"]
+ s.has_rdoc = %q{true}
+ s.homepage = %q{http://example.com}
+ s.require_paths = [\"lib\"]
+ s.requirements = [\"A working computer\"]
+ s.rubyforge_project = %q{example}
+ s.rubygems_version = %q{#{Gem::RubyGemsVersion}}
+ s.summary = %q{this is a summary}
+ s.test_files = [\"test/suite.rb\"]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if current_version >= 3 then
+ s.add_runtime_dependency(%q<rake>, [\"> 0.4\"])
+ s.add_runtime_dependency(%q<jabber4r>, [\"> 0.0.0\"])
+ s.add_runtime_dependency(%q<pqa>, [\"> 0.4\", \"<= 0.6\"])
+ else
+ s.add_dependency(%q<rake>, [\"> 0.4\"])
+ s.add_dependency(%q<jabber4r>, [\"> 0.0.0\"])
+ s.add_dependency(%q<pqa>, [\"> 0.4\", \"<= 0.6\"])
+ end
+ else
+ s.add_dependency(%q<rake>, [\"> 0.4\"])
+ s.add_dependency(%q<jabber4r>, [\"> 0.0.0\"])
+ s.add_dependency(%q<pqa>, [\"> 0.4\", \"<= 0.6\"])
+ end
+end
+"
+
+ assert_equal expected, ruby_code
+
+ same_spec = eval ruby_code
+
+ assert_equal @a1, same_spec
+ end
+
+ def test_to_ruby_legacy
+ gemspec1 = eval LEGACY_RUBY_SPEC
+ ruby_code = gemspec1.to_ruby
+ gemspec2 = eval ruby_code
+
+ assert_equal gemspec1, gemspec2
+ end
+
+ def test_to_ruby_platform
+ @a2.platform = Gem::Platform.local
+ @a2.instance_variable_set :@original_platform, 'old_platform'
+
+ ruby_code = @a2.to_ruby
+
+ same_spec = eval ruby_code
+
+ assert_equal 'old_platform', same_spec.original_platform
+ end
+
+ def test_to_yaml
+ yaml_str = @a1.to_yaml
+ same_spec = YAML.load(yaml_str)
+
+ assert_equal @a1, same_spec
+ end
+
+ def test_to_yaml_fancy
+ @a1.platform = Gem::Platform.local
+ yaml_str = @a1.to_yaml
+
+ same_spec = YAML.load(yaml_str)
+
+ assert_equal Gem::Platform.local, same_spec.platform
+
+ assert_equal @a1, same_spec
+ end
+
+ def test_to_yaml_platform_empty_string
+ @a1.instance_variable_set :@original_platform, ''
+
+ assert_match %r|^platform: ruby$|, @a1.to_yaml
+ end
+
+ def test_to_yaml_platform_legacy
+ @a1.platform = 'powerpc-darwin7.9.0'
+ @a1.instance_variable_set :@original_platform, 'powerpc-darwin7.9.0'
+
+ yaml_str = @a1.to_yaml
+
+ same_spec = YAML.load(yaml_str)
+
+ assert_equal Gem::Platform.new('powerpc-darwin7'), same_spec.platform
+ assert_equal 'powerpc-darwin7.9.0', same_spec.original_platform
+ end
+
+ def test_to_yaml_platform_nil
+ @a1.instance_variable_set :@original_platform, nil
+
+ assert_match %r|^platform: ruby$|, @a1.to_yaml
+ end
+
+ def test_validate
+ Dir.chdir @tempdir do
+ assert @a1.validate
+ end
+ end
+
+ def test_validate_authors
+ Dir.chdir @tempdir do
+ @a1.authors = []
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: no author specified\n", @ui.error, 'error'
+
+ @a1.authors = [Object.new]
+
+ e = assert_raise Gem::InvalidSpecificationException do
+ @a1.validate
+ end
+
+ assert_equal 'authors must be Array of Strings', e.message
+ end
+ end
+
+ def test_validate_autorequire
+ Dir.chdir @tempdir do
+ @a1.autorequire = 'code'
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: deprecated autorequire specified\n",
+ @ui.error, 'error'
+ end
+ end
+
+ def test_validate_email
+ Dir.chdir @tempdir do
+ @a1.email = ''
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: no email specified\n", @ui.error, 'error'
+ end
+ end
+
+ def test_validate_empty
+ e = assert_raise Gem::InvalidSpecificationException do
+ Gem::Specification.new.validate
+ end
+
+ assert_equal 'missing value for attribute name', e.message
+ end
+
+ def test_validate_executables
+ FileUtils.mkdir_p File.join(@tempdir, 'bin')
+ File.open File.join(@tempdir, 'bin', 'exec'), 'w' do end
+
+ use_ui @ui do
+ Dir.chdir @tempdir do
+ assert @a1.validate
+ end
+ end
+
+ assert_equal '', @ui.output, 'output'
+ assert_equal "WARNING: bin/exec is missing #! line\n", @ui.error, 'error'
+ end
+
+ def test_validate_empty_require_paths
+ @a1.require_paths = []
+ e = assert_raise Gem::InvalidSpecificationException do
+ @a1.validate
+ end
+
+ assert_equal 'specification must have at least one require_path', e.message
+ end
+
+ def test_validate_homepage
+ Dir.chdir @tempdir do
+ @a1.homepage = ''
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: no homepage specified\n", @ui.error, 'error'
+ end
+ end
+
+ def test_validate_has_rdoc
+ Dir.chdir @tempdir do
+ @a1.has_rdoc = false
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: RDoc will not be generated (has_rdoc == false)\n",
+ @ui.error, 'error'
+ end
+ end
+
+ def test_validate_platform_legacy
+ Dir.chdir @tempdir do
+ @a1.platform = 'mswin32'
+ assert @a1.validate
+
+ @a1.platform = 'i586-linux'
+ assert @a1.validate
+
+ @a1.platform = 'powerpc-darwin'
+ assert @a1.validate
+ end
+ end
+
+ def test_validate_rubyforge_project
+ Dir.chdir @tempdir do
+ @a1.rubyforge_project = ''
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: no rubyforge_project specified\n",
+ @ui.error, 'error'
+ end
+ end
+
+ def test_validate_rubygems_version
+ @a1.rubygems_version = "3"
+ e = assert_raise Gem::InvalidSpecificationException do
+ @a1.validate
+ end
+
+ assert_equal "expected RubyGems version #{Gem::RubyGemsVersion}, was 3",
+ e.message
+ end
+
+ def test_validate_summary
+ Dir.chdir @tempdir do
+ @a1.summary = ''
+
+ use_ui @ui do
+ @a1.validate
+ end
+
+ assert_equal "WARNING: no summary specified\n", @ui.error, 'error'
+ end
+ end
+
+ def test_version
+ assert_equal Gem::Version.new('1'), @a1.version
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_stream_ui.rb b/trunk/test/rubygems/test_gem_stream_ui.rb
new file mode 100644
index 0000000000..a8564ba94f
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_stream_ui.rb
@@ -0,0 +1,117 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/user_interaction'
+
+class TestGemStreamUI < RubyGemTestCase
+
+ module IsTty
+ attr_accessor :tty
+
+ def tty?
+ @tty = true unless defined? @tty
+ return @tty
+ end
+
+ alias_method :isatty, :tty?
+ end
+
+ def setup
+ super
+
+ @cfg = Gem.configuration
+
+ @in = StringIO.new
+ @out = StringIO.new
+ @err = StringIO.new
+
+ @in.extend IsTty
+
+ @sui = Gem::StreamUI.new @in, @out, @err
+ end
+
+ def test_ask
+ timeout(1) do
+ expected_answer = "Arthur, King of the Britons"
+ @in.string = "#{expected_answer}\n"
+ actual_answer = @sui.ask("What is your name?")
+ assert_equal expected_answer, actual_answer
+ end
+ end
+
+ def test_ask_no_tty
+ @in.tty = false
+
+ timeout(0.1) do
+ answer = @sui.ask("what is your favorite color?")
+ assert_equal nil, answer
+ end
+ end
+
+ def test_ask_yes_no_no_tty_with_default
+ @in.tty = false
+
+ timeout(0.1) do
+ answer = @sui.ask_yes_no("do coconuts migrate?", false)
+ assert_equal false, answer
+
+ answer = @sui.ask_yes_no("do coconuts migrate?", true)
+ assert_equal true, answer
+ end
+ end
+
+ def test_ask_yes_no_no_tty_without_default
+ @in.tty = false
+
+ timeout(0.1) do
+ assert_raises(Gem::OperationNotSupportedError) do
+ @sui.ask_yes_no("do coconuts migrate?")
+ end
+ end
+ end
+
+ def test_choose_from_list
+ @in.puts "1"
+ @in.rewind
+
+ result = @sui.choose_from_list 'which one?', %w[foo bar]
+
+ assert_equal ['foo', 0], result
+ assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
+ end
+
+ def test_choose_from_list_EOF
+ result = @sui.choose_from_list 'which one?', %w[foo bar]
+
+ assert_equal [nil, nil], result
+ assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
+ end
+
+ def test_proress_reporter_silent_nil
+ @cfg.verbose = nil
+ reporter = @sui.progress_reporter 10, 'hi'
+ assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
+ end
+
+ def test_proress_reporter_silent_false
+ @cfg.verbose = false
+ reporter = @sui.progress_reporter 10, 'hi'
+ assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
+ assert_equal "", @out.string
+ end
+
+ def test_proress_reporter_simple
+ @cfg.verbose = true
+ reporter = @sui.progress_reporter 10, 'hi'
+ assert_kind_of Gem::StreamUI::SimpleProgressReporter, reporter
+ assert_equal "hi\n", @out.string
+ end
+
+ def test_proress_reporter_verbose
+ @cfg.verbose = 0
+ reporter = @sui.progress_reporter 10, 'hi'
+ assert_kind_of Gem::StreamUI::VerboseProgressReporter, reporter
+ assert_equal "hi\n", @out.string
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_uninstaller.rb b/trunk/test/rubygems/test_gem_uninstaller.rb
new file mode 100644
index 0000000000..7d7890c3ea
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_uninstaller.rb
@@ -0,0 +1,66 @@
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_installer_test_case')
+require 'rubygems/uninstaller'
+
+class TestGemUninstaller < GemInstallerTestCase
+
+ def setup
+ super
+
+ ui = MockGemUi.new
+ util_setup_gem ui
+
+ build_rake_in do
+ use_ui ui do
+ @installer.install
+ end
+ end
+ end
+
+ def test_initialize_expand_path
+ uninstaller = Gem::Uninstaller.new nil, :install_dir => '/foo//bar'
+
+ assert_match %r|/foo/bar$|, uninstaller.instance_variable_get(:@gem_home)
+ end
+
+ def test_remove_executables_force_keep
+ uninstaller = Gem::Uninstaller.new nil, :executables => false
+
+ use_ui @ui do
+ uninstaller.remove_executables @spec
+ end
+
+ assert_equal true, File.exist?(File.join(@gemhome, 'bin', 'executable'))
+
+ assert_equal "Executables and scripts will remain installed.\n", @ui.output
+ end
+
+ def test_remove_executables_force_remove
+ uninstaller = Gem::Uninstaller.new nil, :executables => true
+
+ use_ui @ui do
+ uninstaller.remove_executables @spec
+ end
+
+ assert_equal "Removing executable\n", @ui.output
+
+ assert_equal false, File.exist?(File.join(@gemhome, 'bin', 'executable'))
+ end
+
+ def test_path_ok_eh
+ uninstaller = Gem::Uninstaller.new nil
+
+ assert_equal true, uninstaller.path_ok?(@spec)
+ end
+
+ def test_path_ok_eh_legacy
+ uninstaller = Gem::Uninstaller.new nil
+
+ @spec.loaded_from.gsub! @spec.full_name, '\&-legacy'
+ @spec.platform = 'legacy'
+
+ assert_equal true, uninstaller.path_ok?(@spec)
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_validator.rb b/trunk/test/rubygems/test_gem_validator.rb
new file mode 100644
index 0000000000..c910cfdf11
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_validator.rb
@@ -0,0 +1,70 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'simple_gem')
+require 'rubygems/validator'
+
+class TestGemValidator < RubyGemTestCase
+
+ def setup
+ super
+
+ @simple_gem = SIMPLE_GEM
+ @validator = Gem::Validator.new
+ end
+
+ def test_verify_gem_file
+ gem_file = File.join @tempdir, 'simple_gem.gem'
+ File.open gem_file, 'wb' do |fp| fp.write @simple_gem end
+
+ assert_equal nil, @validator.verify_gem_file(gem_file)
+ end
+
+ def test_verify_gem_file_empty
+ e = assert_raise Gem::VerificationError do
+ @validator.verify_gem_file ''
+ end
+
+ assert_equal 'missing gem file ', e.message
+ end
+
+ def test_verify_gem_file_nonexistent
+ file = '/nonexistent/nonexistent.gem'
+ e = assert_raise Gem::VerificationError do
+ @validator.verify_gem_file file
+ end
+
+ assert_equal "missing gem file #{file}", e.message
+ end
+
+ def test_verify_gem
+ assert_equal nil, @validator.verify_gem(@simple_gem)
+ end
+
+ def test_verify_gem_empty
+ e = assert_raise Gem::VerificationError do
+ @validator.verify_gem ''
+ end
+
+ assert_equal 'empty gem file', e.message
+ end
+
+ def test_verify_gem_invalid_checksum
+ e = assert_raise Gem::VerificationError do
+ @validator.verify_gem @simple_gem.upcase
+ end
+
+ assert_equal 'invalid checksum for gem file', e.message
+ end
+
+ def test_verify_gem_no_sum
+ assert_equal nil, @validator.verify_gem('words')
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_version.rb b/trunk/test/rubygems/test_gem_version.rb
new file mode 100644
index 0000000000..8d10700490
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_version.rb
@@ -0,0 +1,215 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/version'
+
+class TestGemVersion < RubyGemTestCase
+
+ def setup
+ super
+
+ @v1_0 = Gem::Version.new '1.0'
+ @v1_2 = Gem::Version.new '1.2'
+ @v1_3 = Gem::Version.new '1.3'
+ end
+
+ def test_class_create
+ assert_version Gem::Version.create('1.0')
+ assert_version Gem::Version.create("1.0 ")
+ assert_version Gem::Version.create(" 1.0 ")
+ assert_version Gem::Version.create("1.0\n")
+ assert_version Gem::Version.create("\n1.0\n")
+
+ assert_equal Gem::Version.create('1'), Gem::Version.create(1)
+ end
+
+ def test_class_create_malformed
+ e = assert_raise ArgumentError do Gem::Version.create("junk") end
+ assert_equal "Malformed version number string junk", e.message
+
+ e = assert_raise ArgumentError do Gem::Version.create("1.0\n2.0") end
+ assert_equal "Malformed version number string 1.0\n2.0", e.message
+ end
+
+ def test_bad
+ assert_inadequate( "", "> 0.1")
+ assert_inadequate( "1.2.3", "!= 1.2.3")
+ assert_inadequate( "1.2.003.0.0", "!= 1.02.3")
+ assert_inadequate( "4.5.6", "< 1.2.3")
+ assert_inadequate( "1.0", "> 1.1")
+ assert_inadequate( "0", ">")
+ assert_inadequate( "0", "<")
+ assert_inadequate( "", "= 0.1")
+ assert_inadequate( "1.1.1", "> 1.1.1")
+ assert_inadequate( "1.2", "= 1.1")
+ assert_inadequate( "1.40", "= 1.1")
+ assert_inadequate( "1.3", "= 1.40")
+ assert_inadequate( "9.3.3", "<= 9.3.2")
+ assert_inadequate( "9.3.1", ">= 9.3.2")
+ assert_inadequate( "9.3.03", "<= 9.3.2")
+ assert_inadequate( "1.0.0.1", "= 1.0")
+ end
+
+ def test_bump_trailing_zeros
+ v = Gem::Version.new("5.0.0")
+ assert_equal "5.1", v.bump.to_s
+ end
+
+ def test_bump
+ v = Gem::Version.new("5.2.4")
+ assert_equal "5.3", v.bump.to_s
+ end
+
+ def test_bump_one_level
+ v = Gem::Version.new("5")
+ assert_equal "6", v.bump.to_s
+ end
+
+ def test_eql_eh
+ v1_2 = Gem::Version.new '1.2'
+ v1_2_0 = Gem::Version.new '1.2.0'
+
+ assert_equal true, v1_2.eql?(@v1_2)
+ assert_equal true, @v1_2.eql?(v1_2)
+
+ assert_equal false, v1_2_0.eql?(@v1_2)
+ assert_equal false, @v1_2.eql?(v1_2_0)
+
+ assert_equal false, @v1_2.eql?(@v1_3)
+ assert_equal false, @v1_3.eql?(@v1_2)
+ end
+
+ def test_equals2
+ v = Gem::Version.new("1.2")
+
+ assert_equal v, @v1_2
+ assert_equal @v1_2, v
+
+ assert_not_equal @v1_2, @v1_3
+ assert_not_equal @v1_3, @v1_2
+ end
+
+ def test_hash
+ v1_2 = Gem::Version.new "1.2"
+ v1_2_0 = Gem::Version.new "1.2.0"
+
+ assert_equal v1_2.hash, @v1_2.hash
+
+ assert_not_equal v1_2_0.hash, @v1_2.hash
+
+ assert_not_equal @v1_2.hash, @v1_3.hash
+ end
+
+ def test_illformed_requirements
+ [ ">>> 1.3.5", "> blah" ].each do |rq|
+ assert_raises(ArgumentError, "req [#{rq}] should fail") {
+ Gem::Version::Requirement.new(rq)
+ }
+ end
+ end
+
+ def test_normalize
+ assert_equal [1], Gem::Version.new("1").to_ints
+ assert_equal [1], Gem::Version.new("1.0").to_ints
+ assert_equal [1, 1], Gem::Version.new("1.1").to_ints
+ end
+
+ def test_ok
+ assert_adequate( "0.2.33", "= 0.2.33")
+ assert_adequate( "0.2.34", "> 0.2.33")
+ assert_adequate( "1.0", "= 1.0")
+ assert_adequate( "1.0", "1.0")
+ assert_adequate( "1.8.2", "> 1.8.0")
+ assert_adequate( "1.112", "> 1.111")
+ assert_adequate( "0.2", "> 0.0.0")
+ assert_adequate( "0.0.0.0.0.2", "> 0.0.0")
+ assert_adequate( "0.0.1.0", "> 0.0.0.1")
+ assert_adequate( "10.3.2", "> 9.3.2")
+ assert_adequate( "1.0.0.0", "= 1.0")
+ assert_adequate( "10.3.2", "!= 9.3.4")
+ assert_adequate( "10.3.2", "> 9.3.2")
+ assert_adequate( "10.3.2", "> 9.3.2")
+ assert_adequate( " 9.3.2", ">= 9.3.2")
+ assert_adequate( "9.3.2 ", ">= 9.3.2")
+ assert_adequate( "", "= 0")
+ assert_adequate( "", "< 0.1")
+ assert_adequate( " ", "< 0.1 ")
+ assert_adequate( "", " < 0.1")
+ assert_adequate( "0", "=")
+ assert_adequate( "0", ">=")
+ assert_adequate( "0", "<=")
+ end
+
+ def test_satisfied_by_eh_boxed
+ assert_inadequate("1.3", "~> 1.4")
+ assert_adequate( "1.4", "~> 1.4")
+ assert_adequate( "1.5", "~> 1.4")
+ assert_inadequate("2.0", "~> 1.4")
+
+ assert_inadequate("1.3", "~> 1.4.4")
+ assert_inadequate("1.4", "~> 1.4.4")
+ assert_adequate( "1.4.4", "~> 1.4.4")
+ assert_adequate( "1.4.5", "~> 1.4.4")
+ assert_inadequate("1.5", "~> 1.4.4")
+ assert_inadequate("2.0", "~> 1.4.4")
+ end
+
+ def test_satisfied_by_eh_multiple
+ req = [">= 1.4", "<= 1.6", "!= 1.5"]
+ assert_inadequate("1.3", req)
+ assert_adequate( "1.4", req)
+ assert_inadequate("1.5", req)
+ assert_adequate( "1.6", req)
+ assert_inadequate("1.7", req)
+ assert_inadequate("2.0", req)
+ end
+
+ def test_spaceship
+ assert_equal 1, Gem::Version.new('1.8.2') <=> Gem::Version.new('0.0.0')
+ end
+
+ def test_boxed
+ assert_inadequate("1.3", "~> 1.4")
+ assert_adequate( "1.4", "~> 1.4")
+ assert_adequate( "1.5", "~> 1.4")
+ assert_inadequate("2.0", "~> 1.4")
+
+ assert_inadequate("1.3", "~> 1.4.4")
+ assert_inadequate("1.4", "~> 1.4.4")
+ assert_adequate( "1.4.4", "~> 1.4.4")
+ assert_adequate( "1.4.5", "~> 1.4.4")
+ assert_inadequate("1.5", "~> 1.4.4")
+ assert_inadequate("2.0", "~> 1.4.4")
+ end
+
+ def test_to_s
+ v = Gem::Version.new("5.2.4")
+ assert_equal "5.2.4", v.to_s
+ end
+
+ def assert_adequate(version, requirement)
+ ver = Gem::Version.new(version)
+ req = Gem::Requirement.new(requirement)
+ assert req.satisfied_by?(ver),
+ "Version #{version} should be adequate for Requirement #{requirement}"
+ end
+
+ def assert_inadequate(version, requirement)
+ ver = Gem::Version.new(version)
+ req = Gem::Version::Requirement.new(requirement)
+ assert ! req.satisfied_by?(ver),
+ "Version #{version} should not be adequate for Requirement #{requirement}"
+ end
+
+ def assert_version(actual)
+ assert_equal @v1_0, actual
+ assert_equal @v1_0.version, actual.version
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_gem_version_option.rb b/trunk/test/rubygems/test_gem_version_option.rb
new file mode 100644
index 0000000000..30c73390fe
--- /dev/null
+++ b/trunk/test/rubygems/test_gem_version_option.rb
@@ -0,0 +1,77 @@
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/command'
+require 'rubygems/version_option'
+
+class TestGemVersionOption < RubyGemTestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Command.new 'dummy', 'dummy'
+ @cmd.extend Gem::VersionOption
+ end
+
+ def test_add_platform_option
+ @cmd.add_platform_option
+
+ assert @cmd.handles?(%w[--platform x86-darwin])
+ end
+
+ def test_add_version_option
+ @cmd.add_version_option
+
+ assert @cmd.handles?(%w[--version >1])
+ end
+
+ def test_platform_option
+ @cmd.add_platform_option
+
+ @cmd.handle_options %w[--platform x86-freebsd6 --platform x86-freebsd7]
+
+ expected = [
+ Gem::Platform::RUBY,
+ Gem::Platform.new('x86-freebsd6'),
+ Gem::Platform.new('x86-freebsd7'),
+ ]
+
+ assert_equal expected, Gem.platforms
+ end
+
+ def test_platform_option_ruby
+ @cmd.add_platform_option
+
+ @cmd.handle_options %w[--platform ruby]
+
+ expected = [
+ Gem::Platform::RUBY
+ ]
+
+ assert_equal expected, Gem.platforms
+ end
+
+ def test_platform_option_twice
+ @cmd.add_platform_option
+
+ @cmd.handle_options %w[--platform x86-freebsd6 --platform x86-freebsd-6]
+
+ expected = [
+ Gem::Platform::RUBY,
+ Gem::Platform.new('x86-freebsd6'),
+ ]
+
+ assert_equal expected, Gem.platforms
+ end
+
+ def test_version_option
+ @cmd.add_version_option
+
+ @cmd.handle_options %w[--version >1]
+
+ expected = { :version => Gem::Requirement.new('> 1'), :args => [] }
+
+ assert_equal expected, @cmd.options
+ end
+
+end
+
diff --git a/trunk/test/rubygems/test_kernel.rb b/trunk/test/rubygems/test_kernel.rb
new file mode 100644
index 0000000000..da31d772eb
--- /dev/null
+++ b/trunk/test/rubygems/test_kernel.rb
@@ -0,0 +1,65 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'test/unit'
+require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require 'rubygems/package'
+
+class TestKernel < RubyGemTestCase
+
+ def setup
+ super
+
+ @old_path = $:.dup
+
+ util_make_gems
+ end
+
+ def teardown
+ super
+
+ $:.replace @old_path
+ end
+
+ def test_gem
+ assert gem('a', '= 1'), "Should load"
+ assert $:.any? { |p| %r{a-1/lib} =~ p }
+ assert $:.any? { |p| %r{a-1/bin} =~ p }
+ end
+
+ def test_gem_redundent
+ assert gem('a', '= 1'), "Should load"
+ assert ! gem('a', '= 1'), "Should not load"
+ assert_equal 1, $:.select { |p| %r{a-1/lib} =~ p }.size
+ assert_equal 1, $:.select { |p| %r{a-1/bin} =~ p }.size
+ end
+
+ def test_gem_overlapping
+ assert gem('a', '= 1'), "Should load"
+ assert ! gem('a', '>= 1'), "Should not load"
+ assert_equal 1, $:.select { |p| %r{a-1/lib} =~ p }.size
+ assert_equal 1, $:.select { |p| %r{a-1/bin} =~ p }.size
+ end
+
+ def test_gem_conflicting
+ assert gem('a', '= 1'), "Should load"
+
+ ex = assert_raise Gem::Exception do
+ gem 'a', '= 2'
+ end
+
+ assert_match(/activate a \(= 2, runtime\)/, ex.message)
+ assert_match(/activated a-1/, ex.message)
+
+ assert $:.any? { |p| %r{a-1/lib} =~ p }
+ assert $:.any? { |p| %r{a-1/bin} =~ p }
+ assert ! $:.any? { |p| %r{a-2/lib} =~ p }
+ assert ! $:.any? { |p| %r{a-2/bin} =~ p }
+ end
+
+end
+