From 56660de3c6df7a4ff8667ef4047d30d0de169935 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sat, 1 Jun 2019 12:45:11 +0300 Subject: Merge rubygems master from upstream. I picked the commit from 3c469e0da538428a0ddd94f99aa73c32da22e8ba --- lib/rubygems/test_case.rb | 243 +++++++++++----------------------------------- 1 file changed, 54 insertions(+), 189 deletions(-) (limited to 'lib/rubygems/test_case.rb') diff --git a/lib/rubygems/test_case.rb b/lib/rubygems/test_case.rb index 8e909e4afe..b466e7a4e0 100644 --- a/lib/rubygems/test_case.rb +++ b/lib/rubygems/test_case.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -# TODO: $SAFE = 1 require 'rubygems' @@ -27,13 +26,6 @@ begin rescue LoadError end -# We have to load these up front because otherwise we'll try to load -# them while we're testing rubygems, and thus we can't actually load them. -unless Gem::Dependency.new('rdoc', '>= 3.10').matching_specs.empty? - gem 'rdoc' - gem 'json' -end - require 'bundler' require 'minitest/autorun' @@ -91,8 +83,6 @@ end # gem-related behavior in a sandbox. Through RubyGemTestCase you can install # and uninstall gems, fetch remote gems through a stub fetcher and be assured # your normal set of gems is not affected. -# -# Tests are always run at a safe level of 1. class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase) @@ -152,6 +142,28 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni end end + ## + # Sets the vendordir entry in RbConfig::CONFIG to +value+ and restores the + # original value when the block ends + # + def vendordir(value) + vendordir = RbConfig::CONFIG['vendordir'] + + if value + RbConfig::CONFIG['vendordir'] = value + else + RbConfig::CONFIG.delete 'vendordir' + end + + yield + ensure + if vendordir + RbConfig::CONFIG['vendordir'] = vendordir + else + RbConfig::CONFIG.delete 'vendordir' + end + end + # TODO: move to minitest def refute_path_exists(path, msg = nil) msg = message(msg) { "Expected path '#{path}' to not exist" } @@ -222,8 +234,6 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni @@project_dir = Dir.pwd.untaint unless defined?(@@project_dir) - @@initial_reset = false - ## # #setup prepares a sandboxed location to install gems. All installs are # directed to a temporary directory. All install plugins are removed. @@ -231,24 +241,11 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni # If the +RUBY+ environment variable is set the given path is used for # Gem::ruby. The local platform is set to i386-mswin32 for Windows # or i686-darwin8.10.1 otherwise. - # - # If the +KEEP_FILES+ environment variable is set the files will not be - # removed from /tmp/test_rubygems_#{$$}.#{Time.now.to_i}. 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_bundle_gemfile = ENV['BUNDLE_GEMFILE'] - @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 + @orig_env = ENV.to_hash ENV['GEM_VENDOR'] = nil ENV['SOURCE_DATE_EPOCH'] = nil @@ -256,36 +253,20 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni @current_dir = Dir.pwd @fetcher = nil - Bundler.ui = Bundler::UI::Silent.new - @back_ui = Gem::DefaultUserInteraction.ui @ui = Gem::MockGemUi.new # This needs to be a new instance since we call use_ui(@ui) when we want to # capture output Gem::DefaultUserInteraction.ui = Gem::MockGemUi.new - tmpdir = File.expand_path Dir.tmpdir + tmpdir = File.realpath Dir.tmpdir tmpdir.untaint - if ENV['KEEP_FILES'] - @tempdir = File.join(tmpdir, "test_rubygems_#{$$}.#{Time.now.to_i}") - else - @tempdir = File.join(tmpdir, "test_rubygems_#{$$}") - end + @tempdir = File.join(tmpdir, "test_rubygems_#{$$}") @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 @@ -343,25 +324,21 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni @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 + if Gem.java_platform? + @orig_default_gem_home = RbConfig::CONFIG['default_gem_home'] + RbConfig::CONFIG['default_gem_home'] = @default_dir else - @@initial_reset = true - Gem::Specification.reset + Gem.instance_variable_set(:@default_dir, @default_dir) end + FileUtils.mkdir_p @default_spec_dir + + Gem::Specification.unresolved_deps.clear Gem.use_paths(@gemhome) Gem::Security.reset Gem.loaded_specs.clear Gem.clear_default_specs - Gem::Specification.unresolved_deps.clear Bundler.reset! Gem.configuration.verbose = true @@ -395,7 +372,7 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni ## # #teardown restores the process to its original state and removes the - # tempdir unless the +KEEP_FILES+ environment variable was set. + # tempdir def teardown $LOAD_PATH.replace @orig_LOAD_PATH if @orig_LOAD_PATH @@ -420,33 +397,18 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni Dir.chdir @current_dir - FileUtils.rm_rf @tempdir unless ENV['KEEP_FILES'] + FileUtils.rm_rf @tempdir - ENV.clear - @orig_gem_env_requirements.each do |k,v| - ENV[k] = v - end - - ENV['GEM_HOME'] = @orig_gem_home - ENV['GEM_PATH'] = @orig_gem_path - ENV['GEM_VENDOR'] = @orig_gem_vendor - ENV['GEM_SPEC_CACHE'] = @orig_gem_spec_cache - ENV['RUBYGEMS_GEMDEPS'] = @orig_rubygems_gemdeps - ENV['BUNDLE_GEMFILE'] = @orig_bundle_gemfile - ENV['RUBYGEMS_HOST'] = @orig_rubygems_host + ENV.replace(@orig_env) Gem.ruby = @orig_ruby if @orig_ruby - if @orig_ENV_HOME - ENV['HOME'] = @orig_ENV_HOME + if Gem.java_platform? + RbConfig::CONFIG['default_gem_home'] = @orig_default_gem_home else - ENV.delete 'HOME' + Gem.instance_variable_set :@default_dir, nil end - Gem.instance_variable_set :@default_dir, nil - - ENV['GEM_PRIVATE_KEY_PASSPHRASE'] = @orig_gem_private_key_passphrase - Gem::Specification._clear_load_cache Gem::Specification.unresolved_deps.clear Gem::refresh @@ -593,22 +555,6 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni end.new(spec.name, :executables => true, :user_install => true).uninstall end - ## - # creates a temporary directory with hax - # TODO: deprecate and remove - - def create_tmpdir - tmpdir = nil - - Dir.chdir Dir.tmpdir do - tmpdir = Dir.pwd - end # HACK OSX /private/tmp - - tmpdir = File.join tmpdir, "test_rubygems_#{$$}" - FileUtils.mkdir_p tmpdir - return tmpdir - end - ## # Enables pretty-print for all tests @@ -686,21 +632,13 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni io.write spec.to_ruby_for_cache end - spec.loaded_from = spec.loaded_from = written_path + spec.loaded_from = written_path Gem::Specification.reset return spec end - ## - # TODO: remove in RubyGems 4.0 - - def quick_spec(name, version = '2') # :nodoc: - util_spec name, version - end - deprecate :quick_spec, :util_spec, 2018, 12 - ## # Builds a gem from +spec+ and places it in File.join @gemhome, # 'cache'. Automatically creates files based on +spec.files+ @@ -744,11 +682,6 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni Gem::Specification.reset end - def util_clear_default_gems - FileUtils.rm_rf @default_spec_dir - FileUtils.mkdir @default_spec_dir - end - ## # Install the provided specs @@ -802,52 +735,6 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni $LOADED_FEATURES.replace old_loaded_features end - ## - # new_spec is deprecated as it is never used. - # - # TODO: remove in RubyGems 4.0 - - def new_spec(name, version, deps = nil, *files) # :nodoc: - require 'rubygems/specification' - - spec = Gem::Specification.new do |s| - s.platform = Gem::Platform::RUBY - s.name = name - s.version = version - s.author = 'A User' - s.email = 'example@example.com' - s.homepage = 'http://example.com' - s.summary = "this is a summary" - s.description = "This is a test description" - - Array(deps).each do |n, req| - s.add_dependency n, (req || '>= 0') - end - - s.files.push(*files) unless files.empty? - - yield s if block_given? - end - - spec.loaded_from = spec.spec_file - - unless files.empty? - write_file spec.spec_file do |io| - io.write spec.to_ruby_for_cache - end - - util_build_gem spec - - cache_file = File.join @tempdir, 'gems', "#{spec.full_name}.gem" - FileUtils.mkdir_p File.dirname cache_file - FileUtils.mv spec.cache_file, cache_file - FileUtils.rm spec.spec_file - end - - spec - end - deprecate :new_spec, :none, 2018, 12 - def new_default_spec(name, version, deps = nil, *files) spec = util_spec name, version, deps @@ -910,8 +797,6 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni FileUtils.rm spec.spec_file end - Gem::Specification.reset - return spec end @@ -1060,31 +945,6 @@ Also, a list: platform end - ## - # Sets up a fake fetcher using the gems from #util_make_gems. Optionally - # additional +prerelease+ gems may be included. - # - # Gems created by this method may be fetched using Gem::RemoteFetcher. - - def util_setup_fake_fetcher(prerelease = false) - require 'zlib' - require 'socket' - require 'rubygems/remote_fetcher' - - @fetcher = Gem::FakeFetcher.new - - util_make_gems(prerelease) - Gem::Specification.reset - - @all_gems = [@a1, @a2, @a3a, @a_evil9, @b2, @c1_2].sort - @all_gem_names = @all_gems.map { |gem| gem.full_name } - - gem_names = [@a1.full_name, @a2.full_name, @a3a.full_name, @b2.full_name] - @gem_names = gem_names.sort.join("\n") - - Gem::RemoteFetcher.fetcher = @fetcher - end - ## # Add +spec+ to +@fetcher+ serving the data in the file +path+. # +repo+ indicates which repo to make +spec+ appear to be in. @@ -1096,7 +956,6 @@ Also, a list: ## # Sets up Gem::SpecFetcher to return information from the gems in +specs+. - # Best used with +@all_gems+ from #util_setup_fake_fetcher. def util_setup_spec_fetcher(*specs) all_specs = Gem::Specification.to_a + specs @@ -1163,8 +1022,7 @@ Also, a list: end def util_set_RUBY_VERSION(version, patchlevel = nil, revision = nil, description = nil, engine = "ruby", engine_version = nil) - if Gem.instance_variables.include? :@ruby_version or - Gem.instance_variables.include? '@ruby_version' + if Gem.instance_variables.include? :@ruby_version Gem.send :remove_instance_variable, :@ruby_version end @@ -1223,6 +1081,20 @@ Also, a list: Gem.win_platform? end + ## + # Is this test being run on a Java platform? + + def self.java_platform? + Gem.java_platform? + end + + ## + # Is this test being run on a Java platform? + + def java_platform? + Gem.java_platform? + end + ## # Returns whether or not we're on a version of Ruby built with VC++ (or # Borland) versus Cygwin, Mingw, etc. @@ -1623,10 +1495,3 @@ rescue LoadError, Gem::LoadError end require 'rubygems/test_utilities' -tmpdirs = [] -tmpdirs << (ENV['GEM_HOME'] = Dir.mktmpdir("home")) -tmpdirs << (ENV['GEM_PATH'] = Dir.mktmpdir("path")) -pid = $$ -END {tmpdirs.each {|dir| Dir.rmdir(dir)} if $$ == pid} -Gem.clear_paths -Gem.loaded_specs.clear -- cgit v1.2.3