summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/provider/package/rubygems.rb21
-rw-r--r--spec/unit/provider/package/rubygems_spec.rb12
2 files changed, 27 insertions, 6 deletions
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb
index 2a778fdb14..5b47532597 100644
--- a/lib/chef/provider/package/rubygems.rb
+++ b/lib/chef/provider/package/rubygems.rb
@@ -165,14 +165,23 @@ class Chef
# Find the newest gem version available from Gem.sources that satisfies
# the constraints of +gem_dependency+
def find_newest_remote_version(gem_dependency, *sources)
- # DependencyInstaller sorts the results such that the last one is
- # always the one it considers best.
- spec_with_source = dependency_installer.find_gems_with_sources(gem_dependency).last
+ available_gems = dependency_installer.find_gems_with_sources(gem_dependency)
+ spec, source = if available_gems.respond_to?(:last)
+ # DependencyInstaller sorts the results such that the last one is
+ # always the one it considers best.
+ spec_with_source = available_gems.last
+ spec_with_source && spec_with_source
+ else
+ # Rubygems 2.0 returns a Gem::Available set, which is a
+ # collection of AvailableSet::Tuple structs
+ available_gems.pick_best!
+ best_gem = available_gems.set.first
+ best_gem && [best_gem.spec, best_gem.source]
+ end
- spec = spec_with_source && spec_with_source[0]
- version = spec && spec_with_source[0].version
+ version = spec && spec.version
if version
- logger.debug { "#{@new_resource} found gem #{spec.name} version #{version} for platform #{spec.platform} from #{spec_with_source[1]}" }
+ logger.debug { "#{@new_resource} found gem #{spec.name} version #{version} for platform #{spec.platform} from #{source}" }
version
else
source_list = sources.compact.empty? ? "[#{Gem.sources.to_a.join(', ')}]" : "[#{sources.join(', ')}]"
diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb
index b3b7a030db..d7941af76d 100644
--- a/spec/unit/provider/package/rubygems_spec.rb
+++ b/spec/unit/provider/package/rubygems_spec.rb
@@ -95,6 +95,18 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do
@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0')).should == Gem::Version.new('1.3.0')
end
+ it "finds a matching gem candidate version on rubygems 2.0.0+" do
+ dep = Gem::Dependency.new('rspec', '>= 0')
+ dep_installer = Gem::DependencyInstaller.new
+ @gem_env.stub!(:dependency_installer).and_return(dep_installer)
+ best_gem = mock("best gem match", :spec => gemspec("rspec", Gem::Version.new("1.3.0")), :source => "https://rubygems.org")
+ available_set = mock("Gem::AvailableSet test double")
+ available_set.should_receive(:pick_best!)
+ available_set.should_receive(:set).and_return([best_gem])
+ dep_installer.should_receive(:find_gems_with_sources).with(dep).and_return(available_set)
+ @gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0')).should == Gem::Version.new('1.3.0')
+ end
+
it "gives the candidate version as nil if none is found" do
dep = Gem::Dependency.new('rspec', '>= 0')
latest = []