diff options
author | Bundlerbot <bot@bundler.io> | 2019-09-22 12:18:12 +0000 |
---|---|---|
committer | David RodrÃguez <deivid.rodriguez@riseup.net> | 2019-11-07 16:41:29 +0100 |
commit | 19b7ce26ff1751f50c6e7f3ece1ca6a3468bfaf2 (patch) | |
tree | f37dbd062c36c14c97107625aa7f6370a545bcc5 /spec/support | |
parent | 03d4fe2b1898261c598a6f20f15645c6d3a55372 (diff) | |
download | bundler-19b7ce26ff1751f50c6e7f3ece1ca6a3468bfaf2.tar.gz |
Merge #7296
7296: Improve exec'ing to the proper rubygems version r=deivid-rodriguez a=deivid-rodriguez
### What was the end-user problem that led to this PR?
The problem was that the `with_rubygems` script is hard to work with, because it does complicated ARGV manipulation before exec'ing.
### What was your diagnosis of the problem?
My diagnosis was that we don't need it.
### What is your fix for the problem, implemented in this PR?
My fix is to implement the same thing in a simpler way.
### Why did you choose this fix out of the possible options?
I chose this fix because it simplifies things.
Co-authored-by: David RodrÃguez <deivid.rodriguez@riseup.net>
(cherry picked from commit 3260d5994ec33bfaf81162c61d0d20bfff023375)
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/helpers.rb | 6 | ||||
-rw-r--r-- | spec/support/path.rb | 2 | ||||
-rw-r--r-- | spec/support/rubygems.rb | 7 | ||||
-rw-r--r-- | spec/support/rubygems_ext.rb | 1 | ||||
-rw-r--r-- | spec/support/rubygems_version_manager.rb | 123 |
5 files changed, 137 insertions, 2 deletions
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index b5617d399f..ffcde39df7 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -2,6 +2,9 @@ require "open3" +require_relative "command_execution" +require_relative "the_bundle" + module Spec module Helpers def reset! @@ -113,6 +116,7 @@ module Spec env["PATH"].gsub!("#{Path.root}/exe", "") if env["PATH"] && system_bundler requires = options.delete(:requires) || [] + requires << "support/rubygems" requires << "support/hax" artifice = options.delete(:artifice) do @@ -146,7 +150,7 @@ module Spec end end.join - cmd = "#{sudo} #{Gem.ruby} #{load_path_str} #{requires_str} #{bundle_bin} #{cmd}#{args}" + cmd = "#{sudo} #{Gem.ruby} --disable-gems #{load_path_str} #{requires_str} #{bundle_bin} #{cmd}#{args}" sys_exec(cmd, env) {|i, o, thr| yield i, o, thr if block_given? } end bang :bundle diff --git a/spec/support/path.rb b/spec/support/path.rb index db28454792..79b55dd42a 100644 --- a/spec/support/path.rb +++ b/spec/support/path.rb @@ -22,7 +22,7 @@ module Spec end def gem_bin - @gem_bin ||= ruby_core? ? ENV["GEM_COMMAND"] : "#{Gem.ruby} -S gem --backtrace" + @gem_bin ||= ruby_core? ? ENV["GEM_COMMAND"] : "#{Gem.ruby} --disable-gems -r#{spec_dir}/support/rubygems -S gem --backtrace" end def spec_dir diff --git a/spec/support/rubygems.rb b/spec/support/rubygems.rb new file mode 100644 index 0000000000..e60f9a928e --- /dev/null +++ b/spec/support/rubygems.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require_relative "rubygems_version_manager" + +RubygemsVersionManager.new(ENV["RGV"]).switch + +require "rubygems" diff --git a/spec/support/rubygems_ext.rb b/spec/support/rubygems_ext.rb index 1a9549d2b5..da95837b1b 100644 --- a/spec/support/rubygems_ext.rb +++ b/spec/support/rubygems_ext.rb @@ -40,6 +40,7 @@ module Spec end def gem_load(gem_name, bin_container) + require_relative "rubygems" gem_load_and_activate(gem_name, bin_container) end diff --git a/spec/support/rubygems_version_manager.rb b/spec/support/rubygems_version_manager.rb new file mode 100644 index 0000000000..4be90f7679 --- /dev/null +++ b/spec/support/rubygems_version_manager.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +require "pathname" +require_relative "helpers" + +class RubygemsVersionManager + include Spec::Helpers + + def initialize(env_version) + @env_version = env_version + end + + def switch + return if use_system? + + unrequire_rubygems_if_needed + + switch_local_copy_if_needed + + prepare_environment + end + +private + + def use_system? + @env_version.nil? + end + + def unrequire_rubygems_if_needed + return unless rubygems_unrequire_needed? + + require "rbconfig" + + ruby = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"]) + ruby << RbConfig::CONFIG["EXEEXT"] + + cmd = [ruby, $0, *ARGV].compact + cmd[1, 0] = "--disable-gems" + + exec(ENV, *cmd) + end + + def switch_local_copy_if_needed + return unless local_copy_switch_needed? + + Dir.chdir(local_copy_path) do + sys_exec!("git remote update") + sys_exec!("git checkout #{target_tag_version} --quiet") + end + end + + def prepare_environment + $:.unshift File.expand_path("lib", local_copy_path) + end + + def rubygems_unrequire_needed? + defined?(Gem) && Gem::VERSION != target_gem_version + end + + def local_copy_switch_needed? + !env_version_is_path? && target_gem_version != local_copy_version + end + + def target_gem_version + @target_gem_version ||= resolve_target_gem_version + end + + def target_tag_version + @target_tag_version ||= resolve_target_tag_version + end + + def local_copy_version + gemspec_contents = File.read(local_copy_path.join("lib/rubygems.rb")) + version_regexp = /VERSION = ["'](.*)["']/ + + version_regexp.match(gemspec_contents)[1] + end + + def local_copy_path + @local_copy_path ||= resolve_local_copy_path + end + + def resolve_local_copy_path + return expanded_env_version if env_version_is_path? + + rubygems_path = Pathname.new("../../tmp/rubygems").expand_path(__dir__) + + unless rubygems_path.directory? + rubygems_path.parent.mkpath + sys_exec!("git clone https://github.com/rubygems/rubygems.git #{rubygems_path}") + end + + rubygems_path + end + + def env_version_is_path? + expanded_env_version.directory? + end + + def expanded_env_version + @expanded_env_version ||= Pathname.new(@env_version).expand_path + end + + def resolve_target_tag_version + return "v#{@env_version}" if @env_version.match(/^\d/) + + return "master" if @env_version == master_gem_version + + @env_version + end + + def resolve_target_gem_version + return @env_version[1..-1] if @env_version.match(/^v/) + + return master_gem_version if @env_version == "master" + + @env_version + end + + def master_gem_version + "3.1.0.pre1" + end +end |