class Gem::TestCase

def setup

def setup
  super
  @orig_gem_home   = ENV['GEM_HOME']
  @orig_gem_path   = ENV['GEM_PATH']
  @orig_gem_vendor = ENV['GEM_VENDOR']
  @orig_gem_spec_cache = ENV['GEM_SPEC_CACHE']
  @orig_rubygems_gemdeps = ENV['RUBYGEMS_GEMDEPS']
  @orig_rubygems_host = ENV['RUBYGEMS_HOST']
  ENV.keys.find_all { |k| k.start_with?('GEM_REQUIREMENT_') }.each do |k|
    ENV.delete k
  end
  @orig_gem_env_requirements = ENV.to_hash
  ENV['GEM_VENDOR'] = nil
  @current_dir = Dir.pwd
  @fetcher     = nil
  @ui          = Gem::MockGemUi.new
  tmpdir = File.expand_path Dir.tmpdir
  tmpdir.untaint
  if ENV['KEEP_FILES'] then
    @tempdir = File.join(tmpdir, "test_rubygems_#{$$}.#{Time.now.to_i}")
  else
    @tempdir = File.join(tmpdir, "test_rubygems_#{$$}")
  end
  @tempdir.untaint
  FileUtils.mkdir_p @tempdir
  # This makes the tempdir consistent on OS X.
  # File.expand_path Dir.tmpdir                      #=> "/var/..."
  # Dir.chdir Dir.tmpdir do File.expand_path '.' end #=> "/private/var/..."
  # TODO use File#realpath above instead of #expand_path once 1.8 support is
  # dropped.
  Dir.chdir @tempdir do
    @tempdir = File.expand_path '.'
    @tempdir.untaint
  end
  # This makes the tempdir consistent on Windows.
  # Dir.tmpdir may return short path name, but Dir[Dir.tmpdir] returns long
  # path name. https://bugs.ruby-lang.org/issues/10819
  # File.expand_path or File.realpath doesn't convert path name to long path
  # name. Only Dir[] (= Dir.glob) works.
  # Short and long path name is specific to Windows filesystem.
  if win_platform?
    @tempdir = Dir[@tempdir][0]
    @tempdir.untaint
  end
  @gemhome  = File.join @tempdir, 'gemhome'
  @userhome = File.join @tempdir, 'userhome'
  ENV["GEM_SPEC_CACHE"] = File.join @tempdir, 'spec_cache'
  @orig_ruby = if ENV['RUBY'] then
                 ruby = Gem.ruby
                 Gem.ruby = ENV['RUBY']
                 ruby
               end
  @git = ENV['GIT'] || 'git'
  Gem.ensure_gem_subdirectories @gemhome
  @orig_LOAD_PATH = $LOAD_PATH.dup
  $LOAD_PATH.map! { |s|
    (expand_path = File.expand_path(s)) == s ? s : expand_path.untaint
  }
  Dir.chdir @tempdir
  @orig_ENV_HOME = ENV['HOME']
  ENV['HOME'] = @userhome
  Gem.instance_variable_set :@user_home, nil
  Gem.instance_variable_set :@gemdeps, nil
  Gem.instance_variable_set :@env_requirements_by_name, nil
  Gem.send :remove_instance_variable, :@ruby_version if
    Gem.instance_variables.include? :@ruby_version
  FileUtils.mkdir_p @gemhome
  FileUtils.mkdir_p @userhome
  @orig_gem_private_key_passphrase = ENV['GEM_PRIVATE_KEY_PASSPHRASE']
  ENV['GEM_PRIVATE_KEY_PASSPHRASE'] = PRIVATE_KEY_PASSPHRASE
  @default_dir = File.join @tempdir, 'default'
  @default_spec_dir = File.join @default_dir, "specifications", "default"
  Gem.instance_variable_set :@default_dir, @default_dir
  FileUtils.mkdir_p @default_spec_dir
  # We use Gem::Specification.reset the first time only so that if there
  # are unresolved deps that leak into the whole test suite, they're at least
  # reported once.
  if @@initial_reset
    Gem::Specification.unresolved_deps.clear # done to avoid cross-test warnings
  else
    @@initial_reset = true
    Gem::Specification.reset
  end
  Gem.use_paths(@gemhome)
  Gem::Security.reset
  Gem.loaded_specs.clear
  Gem.clear_default_specs
  Gem::Specification.unresolved_deps.clear
  Gem.configuration.verbose = true
  Gem.configuration.update_sources = true
  Gem::RemoteFetcher.fetcher = Gem::FakeFetcher.new
  @gem_repo = "http://gems.example.com/"
  @uri = URI.parse @gem_repo
  Gem.sources.replace [@gem_repo]
  Gem.searcher = nil
  Gem::SpecFetcher.fetcher = nil
  @orig_BASERUBY = RbConfig::CONFIG['BASERUBY']
  RbConfig::CONFIG['BASERUBY'] = RbConfig::CONFIG['ruby_install_name']
  @orig_arch = RbConfig::CONFIG['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}"
  @orig_LOADED_FEATURES = $LOADED_FEATURES.dup
end