diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-12-09 12:33:10 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-12-14 13:27:00 -0800 |
commit | 07d90bfea9e3f78c1321010c95f7c6e0736f9198 (patch) | |
tree | 7209bcdfc8b01cd6e57e5a1ea0e67ccc160012ab | |
parent | 83762a7cdd0f98471ebccd3b1ed4b0955fe07317 (diff) | |
download | chef-07d90bfea9e3f78c1321010c95f7c6e0736f9198.tar.gz |
fix specs for new rubygems behavior
-rw-r--r-- | lib/chef/provider/package/rubygems.rb | 21 | ||||
-rw-r--r-- | spec/unit/provider/package/rubygems_spec.rb | 74 |
2 files changed, 51 insertions, 44 deletions
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb index 3ef51c81c1..a0ff13e2b6 100644 --- a/lib/chef/provider/package/rubygems.rb +++ b/lib/chef/provider/package/rubygems.rb @@ -160,18 +160,23 @@ class Chef # the constraints of +gem_dependency+ def find_newest_remote_version(gem_dependency, *sources) spec, source = - if !Chef::Config[:rubygems_cache_enabled] - # Use the API that 'gem install' calls which does not pull down the rubygems universe - rs = Gem::RequestSet.new - rs.import [ gem_dependency ] - # This returns the gem which satisfies the dependency along with all of its deps - resolved_spec = rs.resolve.select { |spec| spec.name == gem_dependency.name }.first.spec - resolved_spec && resolved_spec - else + if Chef::Config[:rubygems_cache_enabled] + # This code caches every gem on rubygems.org and uses lots of RAM available_gems = dependency_installer.find_gems_with_sources(gem_dependency) available_gems.pick_best! best_gem = available_gems.set.first best_gem && [best_gem.spec, best_gem.source] + else + # Use the API that 'gem install' calls which does not pull down the rubygems universe + rs = Gem::RequestSet.new + rs.import [ gem_dependency ] + begin + # rs.resolve returns the gem which satisfies the dependency along with all of its deps + resolved_spec = rs.resolve.select { |spec| spec.name == gem_dependency.name }.first.spec + resolved_spec && resolved_spec + rescue Gem::UnsatisfiableDependencyError + nil + end end version = spec && spec.version diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb index f790bdb1ce..1fd15a52a3 100644 --- a/spec/unit/provider/package/rubygems_spec.rb +++ b/spec/unit/provider/package/rubygems_spec.rb @@ -86,34 +86,46 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do expect(Gem.sources).to eq(normal_sources) end - it "finds a matching gem candidate version" do - dep = Gem::Dependency.new('rspec', '>= 0') - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - latest = [[gemspec("rspec", Gem::Version.new("1.3.0")), "https://rubygems.org/"]] - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0'))).to eq(Gem::Version.new('1.3.0')) - end + context "old rubygems caching behavior" do + before do + Chef::Config[:rubygems_cache_enabled] = true + 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 - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - best_gem = double("best gem match", :spec => gemspec("rspec", Gem::Version.new("1.3.0")), :source => "https://rubygems.org") - available_set = double("Gem::AvailableSet test double") - expect(available_set).to receive(:pick_best!) - expect(available_set).to receive(:set).and_return([best_gem]) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(available_set) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0'))).to eq(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 + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + best_gem = double("best gem match", :spec => gemspec("rspec", Gem::Version.new("1.3.0")), :source => "https://rubygems.org") + available_set = double("Gem::AvailableSet test double") + expect(available_set).to receive(:pick_best!) + expect(available_set).to receive(:set).and_return([best_gem]) + expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(available_set) + expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0'))).to eq(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 = [] - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0'))).to be_nil + it "gives the candidate version as nil if none is found" do + dep = Gem::Dependency.new('rspec', '>= 0') + dep_installer = Gem::DependencyInstaller.new + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + available_set = double("Gem::AvailableSet test double") + expect(available_set).to receive(:pick_best!) + expect(available_set).to receive(:set).and_return([]) + expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(available_set) + expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>= 0'))).to be_nil + end + + it "finds a matching gem from a specific gemserver when explicit sources are given" do + dep = Gem::Dependency.new('rspec', '>= 0') + expect(@gem_env).to receive(:with_gem_sources).with('http://gems.example.com').and_yield + dep_installer = Gem::DependencyInstaller.new + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + best_gem = double("best gem match", :spec => gemspec("rspec", Gem::Version.new("1.3.0")), :source => "https://rubygems.org") + available_set = double("Gem::AvailableSet test double") + expect(available_set).to receive(:pick_best!) + expect(available_set).to receive(:set).and_return([best_gem]) + expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(available_set) + expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>=0'), 'http://gems.example.com')).to eq(Gem::Version.new('1.3.0')) + end end it "finds a matching candidate version from a .gem file when the path to the gem is supplied" do @@ -122,16 +134,6 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do expect(@gem_env.candidate_version_from_file(Gem::Dependency.new('chef-integration-test', '>= 0.2.0'), location)).to be_nil end - it "finds a matching gem from a specific gemserver when explicit sources are given" do - dep = Gem::Dependency.new('rspec', '>= 0') - latest = [[gemspec("rspec", Gem::Version.new("1.3.0")), "https://rubygems.org/"]] - - expect(@gem_env).to receive(:with_gem_sources).with('http://gems.example.com').and_yield - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new('rspec', '>=0'), 'http://gems.example.com')).to eq(Gem::Version.new('1.3.0')) - end it "installs a gem with a hash of options for the dependency installer" do dep_installer = Gem::DependencyInstaller.new |