diff options
Diffstat (limited to 'spec/bundler/support/rubygems_ext.rb')
-rw-r--r-- | spec/bundler/support/rubygems_ext.rb | 140 |
1 files changed, 82 insertions, 58 deletions
diff --git a/spec/bundler/support/rubygems_ext.rb b/spec/bundler/support/rubygems_ext.rb index ee9c750a52..03c47ce57c 100644 --- a/spec/bundler/support/rubygems_ext.rb +++ b/spec/bundler/support/rubygems_ext.rb @@ -2,45 +2,18 @@ require_relative "path" +$LOAD_PATH.unshift(Spec::Path.lib_dir.to_s) + module Spec module Rubygems - DEV_DEPS = { - "automatiek" => "~> 0.3.0", - "parallel_tests" => "~> 2.29", - "rake" => "~> 12.0", - "ronn" => "~> 0.7.3", - "rspec" => "~> 3.8", - "rubocop" => "= 0.77.0", - "rubocop-performance" => "= 1.5.1", - }.freeze - - DEPS = { - "rack" => "~> 2.0", - "rack-test" => "~> 1.1", - "artifice" => "~> 0.6.0", - "compact_index" => "~> 0.11.0", - "sinatra" => "~> 2.0", - # Rake version has to be consistent for tests to pass - "rake" => "12.3.2", - "builder" => "~> 3.2", - # ruby-graphviz is used by the viz tests - "ruby-graphviz" => ">= 0.a", - }.freeze - extend self def dev_setup - deps = DEV_DEPS - - # JRuby can't build ronn, so we skip that - deps.delete("ronn") if RUBY_ENGINE == "jruby" - - install_gems(deps) + install_gems(dev_gemfile, dev_lockfile) end def gem_load(gem_name, bin_container) - require_relative "rubygems_version_manager" - RubygemsVersionManager.new(ENV["RGV"]).switch + require_relative "switch_rubygems" gem_load_and_activate(gem_name, bin_container) end @@ -50,25 +23,10 @@ module Spec require gem_name end - def setup - require "fileutils" - - Gem.clear_paths - - ENV["BUNDLE_PATH"] = nil - ENV["GEM_HOME"] = ENV["GEM_PATH"] = Path.base_system_gems.to_s - ENV["PATH"] = [Path.bindir, Path.system_gem_path.join("bin"), ENV["PATH"]].join(File::PATH_SEPARATOR) + def test_setup + setup_test_paths - manifest = DEPS.to_a.sort_by(&:first).map {|k, v| "#{k} => #{v}\n" } - manifest_path = Path.base_system_gems.join("manifest.txt") - # it's OK if there are extra gems - if !manifest_path.file? || !(manifest - manifest_path.readlines).empty? - FileUtils.rm_rf(Path.base_system_gems) - FileUtils.mkdir_p(Path.base_system_gems) - puts "installing gems for the tests to use..." - install_gems(DEPS) - manifest_path.open("wb") {|f| f << manifest.join } - end + require "fileutils" FileUtils.mkdir_p(Path.home) FileUtils.mkdir_p(Path.tmpdir) @@ -80,8 +38,52 @@ module Spec Gem::DefaultUserInteraction.ui = Gem::SilentUI.new end + def install_parallel_test_deps + require "parallel" + + prev_env_test_number = ENV["TEST_ENV_NUMBER"] + + begin + Parallel.processor_count.times do |n| + ENV["TEST_ENV_NUMBER"] = (n + 1).to_s + + install_test_deps + end + ensure + ENV["TEST_ENV_NUMBER"] = prev_env_test_number + end + end + + def setup_test_paths + Gem.clear_paths + + ENV["BUNDLE_PATH"] = nil + ENV["GEM_HOME"] = ENV["GEM_PATH"] = Path.base_system_gems.to_s + ENV["PATH"] = [Path.bindir, Path.system_gem_path.join("bin"), ENV["PATH"]].join(File::PATH_SEPARATOR) + end + + def install_test_deps + setup_test_paths + + workaround_loaded_specs_issue + + install_gems(test_gemfile, test_lockfile) + end + private + # Some rubygems versions include loaded specs when loading gemspec stubs + # from the file system. In this situation, that makes bundler incorrectly + # assume that `rake` is already installed at `tmp/` because it's installed + # globally, and makes it skip installing it to the proper location for our + # tests. To workaround, we remove `rake` from the loaded specs when running + # under those versions, so that `bundler` does the right thing. + def workaround_loaded_specs_issue + current_rubygems_version = Gem::Version.new(Gem::VERSION) + + Gem.loaded_specs.delete("rake") if current_rubygems_version >= Gem::Version.new("3.0.0.beta2") && current_rubygems_version < Gem::Version.new("3.2.0") + end + def gem_load_and_activate(gem_name, bin_container) gem_activate(gem_name) load Gem.bin_path(gem_name, bin_container) @@ -90,18 +92,40 @@ module Spec end def gem_activate(gem_name) - gem_requirement = DEV_DEPS[gem_name] + require "bundler" + gem_requirement = Bundler::LockfileParser.new(File.read(dev_lockfile)).dependencies[gem_name]&.requirement gem gem_name, gem_requirement end - def install_gems(gems) - reqs, no_reqs = gems.partition {|_, req| !req.nil? && !req.split(" ").empty? } - no_reqs.map!(&:first) - reqs.map! {|name, req| "'#{name}:#{req}'" } - deps = reqs.concat(no_reqs).join(" ") - gem = ENV["GEM_COMMAND"] || "#{Gem.ruby} -S gem --backtrace" - cmd = "#{gem} install #{deps} --no-document --conservative" - system(cmd) || raise("Installing gems #{deps} for the tests to use failed!") + def install_gems(gemfile, lockfile) + old_gemfile = ENV["BUNDLE_GEMFILE"] + ENV["BUNDLE_GEMFILE"] = gemfile.to_s + require "bundler" + definition = Bundler::Definition.build(gemfile, lockfile, nil) + definition.validate_runtime! + Bundler::Installer.install(Path.root, definition, :path => ENV["GEM_HOME"]) + ensure + ENV["BUNDLE_GEMFILE"] = old_gemfile + end + + def test_gemfile + Path.root.join("test_gems.rb") + end + + def test_lockfile + lockfile_for(test_gemfile) + end + + def dev_gemfile + Path.root.join("dev_gems.rb") + end + + def dev_lockfile + lockfile_for(dev_gemfile) + end + + def lockfile_for(gemfile) + Pathname.new("#{gemfile.expand_path}.lock") end end end |