[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
This commit is contained in:
David Rodríguez 2020-03-22 12:50:56 +01:00 коммит произвёл Hiroshi SHIBATA
Родитель 7f8f33ebb9
Коммит 9d5e7d6c67
2 изменённых файлов: 188 добавлений и 0 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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