Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge #7464
Browse files Browse the repository at this point in the history
7464: Improve rubygems switcher r=deivid-rodriguez a=deivid-rodriguez

### What was the end-user problem that led to this PR?

The problem was we have some code in our tests to make sure that the `ENV["RGV"]` variable we use to set the rubygems version we want to test bundler against is always respected, including every subprocess our tests start. However:

* The code was overly complicated.
* It didn't support custom branches, for example, `RGV=lazily_load_uri`. This is a feature I find very useful when a PR you're working on also need some changes in the rubygems side, like it happened to me at #7460.

### What was your diagnosis of the problem?

My diagnosis was that all the code needs to do is:

* Set up the location of the rubygems code we'll test bundler against, be it a path, a branch, or a released version.
* Modify `RUBYOPT` to include that location in the LOAD_PATH, so that `rubygems` is always loaded from there instead of the system's version.

### What is your fix for the problem, implemented in this PR?

My fix is to do just that, remove all the stuff that wasn't needed, and do a bit of renaming to improve readability.

Co-authored-by: David Rodríguez <deivid.rodriguez@riseup.net>
(cherry picked from commit 0cb5192)
  • Loading branch information
bundlerbot authored and deivid-rodriguez committed Dec 13, 2019
1 parent 4e4ea8f commit 144b55c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
9 changes: 0 additions & 9 deletions spec/rubygems/rubygems.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -82,7 +82,7 @@ def self.ruby=(ruby)
config.before :suite do
require_relative "support/rubygems_ext"
Spec::Rubygems.setup
ENV["RUBYOPT"] = "#{ENV["RUBYOPT"]} -I#{Spec::Path.spec_dir}/rubygems -r#{Spec::Path.spec_dir}/support/hax.rb"
ENV["RUBYOPT"] = "#{ENV["RUBYOPT"]} -r#{Spec::Path.spec_dir}/support/hax.rb"
ENV["BUNDLE_SPEC_RUN"] = "true"
ENV["BUNDLE_USER_CONFIG"] = ENV["BUNDLE_USER_CACHE"] = ENV["BUNDLE_USER_PLUGIN"] = nil
ENV["GEMRC"] = nil
Expand Down
4 changes: 3 additions & 1 deletion spec/support/rubygems_ext.rb
Expand Up @@ -39,7 +39,9 @@ def dev_setup
end

def gem_load(gem_name, bin_container)
require_relative "../rubygems/rubygems"
require_relative "rubygems_version_manager"
RubygemsVersionManager.new(ENV["RGV"]).switch

gem_load_and_activate(gem_name, bin_container)
end

Expand Down
74 changes: 25 additions & 49 deletions spec/support/rubygems_version_manager.rb
Expand Up @@ -8,27 +8,25 @@ class RubygemsVersionManager
include Spec::Helpers
include Spec::Path

def initialize(env_version)
@env_version = env_version
def initialize(source)
@source = source
end

def switch
return if use_system?

unrequire_rubygems_if_needed

switch_local_copy_if_needed

prepare_environment
reexec_if_needed
end

private

def use_system?
@env_version.nil?
@source.nil?
end

def unrequire_rubygems_if_needed
def reexec_if_needed
return unless rubygems_unrequire_needed?

require "rbconfig"
Expand All @@ -37,7 +35,8 @@ def unrequire_rubygems_if_needed
ruby << RbConfig::CONFIG["EXEEXT"]

cmd = [ruby, $0, *ARGV].compact
cmd[1, 0] = "--disable-gems"

ENV["RUBYOPT"] = "-I#{local_copy_path.join("lib")} #{ENV["RUBYOPT"]}"

exec(ENV, *cmd)
end
Expand All @@ -47,43 +46,36 @@ def switch_local_copy_if_needed

Dir.chdir(local_copy_path) do
sys_exec!("git remote update")
sys_exec!("git checkout #{target_tag_version} --quiet")
sys_exec!("git checkout #{target_tag} --quiet")
end
end

def prepare_environment
$:.unshift File.expand_path("lib", local_copy_path)
ENV["RGV"] = local_copy_path.to_s
end

def rubygems_unrequire_needed?
defined?(Gem::VERSION) && Gem::VERSION != target_gem_version
!$LOADED_FEATURES.include?(local_copy_path.join("lib/rubygems.rb").to_s)
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
!source_is_path? && target_tag != local_copy_tag
end

def target_tag_version
@target_tag_version ||= resolve_target_tag_version
def target_tag
@target_tag ||= resolve_target_tag
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]
def local_copy_tag
Dir.chdir(local_copy_path) do
sys_exec!("git rev-parse --abbrev-ref HEAD")
end
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?
return expanded_source if source_is_path?

rubygems_path = root.join("tmp/rubygems")

Expand All @@ -95,33 +87,17 @@ def resolve_local_copy_path
rubygems_path
end

def env_version_is_path?
expanded_env_version.directory?
def source_is_path?
expanded_source.directory?
end

def expanded_env_version
@expanded_env_version ||= Pathname.new(@env_version).expand_path(root)
def expanded_source
@expanded_source ||= Pathname.new(@source).expand_path(root)
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 local_copy_version if env_version_is_path?

return @env_version[1..-1] if @env_version.match(/^v/)

return master_gem_version if @env_version == "master"

@env_version
end
def resolve_target_tag
return "v#{@source}" if @source.match(/^\d/)

def master_gem_version
"3.1.0.pre1"
@source
end
end

0 comments on commit 144b55c

Please sign in to comment.