diff options
author | David RodrÃguez <deivid.rodriguez@riseup.net> | 2020-03-22 12:50:56 +0100 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2020-03-30 12:54:58 +0900 |
commit | 9d5e7d6c67b084649a6966c69032dd35b1e16b79 (patch) | |
tree | 7f16f5c47582ccf198aa3530db528afd7c18255b | |
parent | 7f8f33ebb9a8eba3d6721e24eac248ff75e2c615 (diff) | |
download | ruby-9d5e7d6c67b084649a6966c69032dd35b1e16b79.tar.gz |
[rubygems/rubygems] Revert "Remove Gem::DependencyInstaller#find_gems_with_sources"
This reverts commit 04c79d3eb9d9803d9fae78575b125b325b97206e.
Final removal is postponed until next year until we find a better way to
manage deprecations.
https://github.com/rubygems/rubygems/commit/3e1cf918a5
-rw-r--r-- | lib/rubygems/dependency_installer.rb | 77 | ||||
-rw-r--r-- | test/rubygems/test_gem_dependency_installer.rb | 111 |
2 files changed, 188 insertions, 0 deletions
diff --git a/lib/rubygems/dependency_installer.rb b/lib/rubygems/dependency_installer.rb index 14ff8beab2..fe89b0063d 100644 --- a/lib/rubygems/dependency_installer.rb +++ b/lib/rubygems/dependency_installer.rb @@ -7,6 +7,7 @@ require 'rubygems/spec_fetcher' require 'rubygems/user_interaction' require 'rubygems/source' require 'rubygems/available_set' +require 'rubygems/deprecate' ## # Installs a gem along with all its dependencies from local and remote gems. @@ -14,6 +15,7 @@ require 'rubygems/available_set' class Gem::DependencyInstaller include Gem::UserInteraction + extend Gem::Deprecate DEFAULT_OPTIONS = { # :nodoc: :env_shebang => false, @@ -120,6 +122,81 @@ class Gem::DependencyInstaller @domain == :both or @domain == :remote end + ## + # Returns a list of pairs of gemspecs and source_uris that match + # Gem::Dependency +dep+ from both local (Dir.pwd) and remote (Gem.sources) + # sources. Gems are sorted with newer gems preferred over older gems, and + # local gems preferred over remote gems. + + def find_gems_with_sources(dep, best_only=false) # :nodoc: + set = Gem::AvailableSet.new + + if consider_local? + sl = Gem::Source::Local.new + + if spec = sl.find_gem(dep.name) + if dep.matches_spec? spec + set.add spec, sl + end + end + end + + if consider_remote? + begin + # This is pulled from #spec_for_dependency to allow + # us to filter tuples before fetching specs. + tuples, errors = Gem::SpecFetcher.fetcher.search_for_dependency dep + + if best_only && !tuples.empty? + tuples.sort! do |a,b| + if b[0].version == a[0].version + if b[0].platform != Gem::Platform::RUBY + 1 + else + -1 + end + else + b[0].version <=> a[0].version + end + end + tuples = [tuples.first] + end + + specs = [] + tuples.each do |tup, source| + begin + spec = source.fetch_spec(tup) + rescue Gem::RemoteFetcher::FetchError => e + errors << Gem::SourceFetchProblem.new(source, e) + else + specs << [spec, source] + end + end + + if @errors + @errors += errors + else + @errors = errors + end + + set << specs + + rescue Gem::RemoteFetcher::FetchError => e + # FIX if there is a problem talking to the network, we either need to always tell + # the user (no really_verbose) or fail hard, not silently tell them that we just + # couldn't find their requested gem. + verbose do + "Error fetching remote data:\t\t#{e.message}\n" \ + "Falling back to local-only install" + end + @domain = :local + end + end + + set + end + deprecate :find_gems_with_sources, :none, 2020, 12 + def in_background(what) # :nodoc: fork_happened = false if @build_docs_in_background and Process.respond_to?(:fork) diff --git a/test/rubygems/test_gem_dependency_installer.rb b/test/rubygems/test_gem_dependency_installer.rb index 50ae3f049b..e7a72c1ef8 100644 --- a/test/rubygems/test_gem_dependency_installer.rb +++ b/test/rubygems/test_gem_dependency_installer.rb @@ -911,6 +911,117 @@ class TestGemDependencyInstaller < Gem::TestCase assert_equal %w[d-2], inst.installed_gems.map { |s| s.full_name } end + def test_find_gems_gems_with_sources + util_setup_gems + + inst = Gem::DependencyInstaller.new + dep = Gem::Dependency.new 'b', '>= 0' + + Gem::Specification.reset + + set = Gem::Deprecate.skip_during do + inst.find_gems_with_sources(dep) + end + + assert_kind_of Gem::AvailableSet, set + + s = set.set.first + + assert_equal @b1, s.spec + assert_equal Gem::Source.new(@gem_repo), s.source + end + + def test_find_gems_with_sources_local + util_setup_gems + + FileUtils.mv @a1_gem, @tempdir + inst = Gem::DependencyInstaller.new + dep = Gem::Dependency.new 'a', '>= 0' + set = nil + + Dir.chdir @tempdir do + set = Gem::Deprecate.skip_during do + inst.find_gems_with_sources dep + end + end + + gems = set.sorted + + assert_equal 2, gems.length + + remote, local = gems + + assert_equal 'a-1', local.spec.full_name, 'local spec' + assert_equal File.join(@tempdir, @a1.file_name), + local.source.download(local.spec), 'local path' + + assert_equal 'a-1', remote.spec.full_name, 'remote spec' + assert_equal Gem::Source.new(@gem_repo), remote.source, 'remote path' + end + + def test_find_gems_with_sources_prerelease + util_setup_gems + + installer = Gem::DependencyInstaller.new + + dependency = Gem::Dependency.new('a', Gem::Requirement.default) + + set = Gem::Deprecate.skip_during do + installer.find_gems_with_sources(dependency) + end + + releases = set.all_specs + + assert releases.any? { |s| s.name == 'a' and s.version.to_s == '1' } + refute releases.any? { |s| s.name == 'a' and s.version.to_s == '1.a' } + + dependency.prerelease = true + + set = Gem::Deprecate.skip_during do + installer.find_gems_with_sources(dependency) + end + + prereleases = set.all_specs + + assert_equal [@a1_pre, @a1], prereleases + end + + def test_find_gems_with_sources_with_best_only_and_platform + util_setup_gems + a1_x86_mingw32, = util_gem 'a', '1' do |s| + s.platform = 'x86-mingw32' + end + util_setup_spec_fetcher @a1, a1_x86_mingw32 + Gem.platforms << Gem::Platform.new('x86-mingw32') + + installer = Gem::DependencyInstaller.new + + dependency = Gem::Dependency.new('a', Gem::Requirement.default) + + set = Gem::Deprecate.skip_during do + installer.find_gems_with_sources(dependency, true) + end + + releases = set.all_specs + + assert_equal [a1_x86_mingw32], releases + end + + def test_find_gems_with_sources_with_bad_source + Gem.sources.replace ["http://not-there.nothing"] + + installer = Gem::DependencyInstaller.new + + dep = Gem::Dependency.new('a') + + out = Gem::Deprecate.skip_during do + installer.find_gems_with_sources(dep) + end + + assert out.empty? + assert_kind_of Gem::SourceFetchProblem, installer.errors.first + end + def test_resolve_dependencies util_setup_gems |