Skip to content
Merged
8 changes: 6 additions & 2 deletions lib/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,12 @@ def self.ruby_version

if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then
version << ".#{RUBY_PATCHLEVEL}"
elsif defined?(RUBY_REVISION) then
version << ".dev.#{RUBY_REVISION}"
elsif defined?(RUBY_DESCRIPTION) then
if RUBY_ENGINE == "ruby" then
version << ".#{RUBY_DESCRIPTION[/\Aruby #{Regexp.quote(RUBY_VERSION)}([^ ]+) /, 1]}"
else
version << ".#{RUBY_DESCRIPTION[/\A#{RUBY_ENGINE} #{Regexp.quote(RUBY_ENGINE_VERSION)} \(#{RUBY_VERSION}([^ ]+)\) /, 1]}"
end
end

@ruby_version = Gem::Version.new version
Expand Down
4 changes: 2 additions & 2 deletions lib/rubygems/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ def ensure_loadable_spec

def ensure_required_ruby_version_met # :nodoc:
if rrv = spec.required_ruby_version then
unless rrv.satisfied_by? Gem.ruby_version then
ruby_version = Gem.ruby_api_version
ruby_version = Gem.ruby_version
unless rrv.satisfied_by? ruby_version then
raise Gem::RuntimeRequirementNotMetError,
"#{spec.name} requires Ruby version #{rrv}. The current ruby version is #{ruby_version}."
end
Expand Down
8 changes: 3 additions & 5 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,7 @@ def rdoc_options
# ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
# #<Gem::Version "2.0.0.247">
#
# Because patch-level is taken into account, be very careful specifying using
# `<=`: `<= 2.2.2` will not match any patch-level of 2.2.2 after the `p0`
# release. It is much safer to specify `< 2.2.3` instead
# Prereleases can also be specified.
#
# Usage:
#
Expand All @@ -662,8 +660,8 @@ def rdoc_options
# # Only with ruby 2.0.x
# spec.required_ruby_version = '~> 2.0'
#
# # Only with ruby between 2.2.0 and 2.2.2
# spec.required_ruby_version = ['>= 2.2.0', '< 2.2.3']
# # Only prereleases or final releases after 2.6.0.preview2
# spec.required_ruby_version = '> 2.6.0.preview2'

def required_ruby_version= req
@required_ruby_version = Gem::Requirement.create req
Expand Down
48 changes: 32 additions & 16 deletions lib/rubygems/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1161,35 +1161,51 @@ def util_zip(data)
Zlib::Deflate.deflate data
end

def util_set_RUBY_VERSION(version, patchlevel = nil, revision = nil)
def util_set_RUBY_VERSION(version, patchlevel = nil, revision = nil, description = nil, engine = "ruby", engine_version = nil)
if Gem.instance_variables.include? :@ruby_version or
Gem.instance_variables.include? '@ruby_version' then
Gem.send :remove_instance_variable, :@ruby_version
end

@RUBY_VERSION = RUBY_VERSION
@RUBY_PATCHLEVEL = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
@RUBY_REVISION = RUBY_REVISION if defined?(RUBY_REVISION)
@RUBY_VERSION = RUBY_VERSION
@RUBY_PATCHLEVEL = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
@RUBY_REVISION = RUBY_REVISION if defined?(RUBY_REVISION)
@RUBY_DESCRIPTION = RUBY_DESCRIPTION if defined?(RUBY_DESCRIPTION)
@RUBY_ENGINE = RUBY_ENGINE
@RUBY_ENGINE_VERSION = RUBY_ENGINE_VERSION if defined?(RUBY_ENGINE_VERSION)

Object.send :remove_const, :RUBY_VERSION
Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
util_clear_RUBY_VERSION

Object.const_set :RUBY_VERSION, version
Object.const_set :RUBY_PATCHLEVEL, patchlevel if patchlevel
Object.const_set :RUBY_REVISION, revision if revision
Object.const_set :RUBY_VERSION, version
Object.const_set :RUBY_PATCHLEVEL, patchlevel if patchlevel
Object.const_set :RUBY_REVISION, revision if revision
Object.const_set :RUBY_DESCRIPTION, description if description
Object.const_set :RUBY_ENGINE, engine
Object.const_set :RUBY_ENGINE_VERSION, engine_version if engine_version
end

def util_restore_RUBY_VERSION
Object.send :remove_const, :RUBY_VERSION
Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
util_clear_RUBY_VERSION

Object.const_set :RUBY_VERSION, @RUBY_VERSION
Object.const_set :RUBY_PATCHLEVEL, @RUBY_PATCHLEVEL if
Object.const_set :RUBY_VERSION, @RUBY_VERSION
Object.const_set :RUBY_PATCHLEVEL, @RUBY_PATCHLEVEL if
defined?(@RUBY_PATCHLEVEL)
Object.const_set :RUBY_REVISION, @RUBY_REVISION if
Object.const_set :RUBY_REVISION, @RUBY_REVISION if
defined?(@RUBY_REVISION)
Object.const_set :RUBY_DESCRIPTION, @RUBY_DESCRIPTION if
defined?(@RUBY_DESCRIPTION)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the big diff in this method, this is the gotcha of this aligment style: keeping it obscures the point of commits.

Object.const_set :RUBY_ENGINE, @RUBY_ENGINE
Object.const_set :RUBY_ENGINE_VERSION, @RUBY_ENGINE_VERSION if
defined?(@RUBY_ENGINE_VERSION)
end

def util_clear_RUBY_VERSION
Object.send :remove_const, :RUBY_VERSION
Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
Object.send :remove_const, :RUBY_DESCRIPTION if defined?(RUBY_DESCRIPTION)
Object.send :remove_const, :RUBY_ENGINE
Object.send :remove_const, :RUBY_ENGINE_VERSION if defined?(RUBY_ENGINE_VERSION)
end

##
Expand Down
34 changes: 29 additions & 5 deletions test/rubygems/test_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,26 +993,50 @@ def test_self_env_requirement
assert_equal Gem::Requirement.default, Gem.env_requirement('qux')
end

def test_self_ruby_version_1_8_5
def test_self_ruby_version_with_patchlevel_less_ancient_rubies
util_set_RUBY_VERSION '1.8.5'

assert_equal Gem::Version.new('1.8.5'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_1_8_6p287
def test_self_ruby_version_with_release
util_set_RUBY_VERSION '1.8.6', 287

assert_equal Gem::Version.new('1.8.6.287'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_1_9_2dev_r23493
util_set_RUBY_VERSION '1.9.2', -1, 23493
def test_self_ruby_version_with_non_mri_implementations
util_set_RUBY_VERSION '2.5.0', 0, 60928, 'jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.171-b11 on 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11 [linux-x86_64]'

assert_equal Gem::Version.new('1.9.2.dev.23493'), Gem.ruby_version
assert_equal Gem::Version.new('2.5.0'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_with_prerelease
util_set_RUBY_VERSION '2.6.0', -1, 63539, 'ruby 2.6.0preview2 (2018-05-31 trunk 63539) [x86_64-linux]'

assert_equal Gem::Version.new('2.6.0.preview2'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_with_non_mri_implementations_with_mri_prerelase_compatibility
util_set_RUBY_VERSION '2.6.0', -1, 63539, 'weirdjruby 9.2.0.0 (2.6.0preview2) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.171-b11 on 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11 [linux-x86_64]', 'weirdjruby', '9.2.0.0'

assert_equal Gem::Version.new('2.6.0.preview2'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_with_trunk
util_set_RUBY_VERSION '1.9.2', -1, 23493, 'ruby 1.9.2dev (2009-05-20 trunk 23493) [x86_64-linux]'

assert_equal Gem::Version.new('1.9.2.dev'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end
Expand Down
26 changes: 21 additions & 5 deletions test/rubygems/test_gem_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ def test_install_with_no_prior_files

def test_install_force
use_ui @ui do
installer = Gem::Installer.at old_ruby_required, :force => true
installer = Gem::Installer.at old_ruby_required('= 1.4.6'), :force => true
installer.install
end

Expand Down Expand Up @@ -1380,16 +1380,32 @@ def test_pre_install_checks_dependencies_install_dir

def test_pre_install_checks_ruby_version
use_ui @ui do
installer = Gem::Installer.at old_ruby_required
installer = Gem::Installer.at old_ruby_required('= 1.4.6')
e = assert_raises Gem::RuntimeRequirementNotMetError do
installer.pre_install_checks
end
rv = Gem.ruby_api_version
rv = Gem.ruby_version
assert_equal "old_ruby_required requires Ruby version = 1.4.6. The current ruby version is #{rv}.",
e.message
end
end

def test_pre_install_checks_ruby_version_with_prereleases
util_set_RUBY_VERSION '2.6.0', -1, '63539', 'ruby 2.6.0preview2 (2018-05-31 trunk 63539) [x86_64-linux]'

installer = Gem::Installer.at old_ruby_required('>= 2.6.0.preview2')
assert installer.pre_install_checks

installer = Gem::Installer.at old_ruby_required('> 2.6.0.preview2')
e = assert_raises Gem::RuntimeRequirementNotMetError do
assert installer.pre_install_checks
end
assert_equal "old_ruby_required requires Ruby version > 2.6.0.preview2. The current ruby version is 2.6.0.preview2.",
e.message
ensure
util_restore_RUBY_VERSION
end

def test_pre_install_checks_wrong_rubygems_version
spec = util_spec 'old_rubygems_required', '1' do |s|
s.required_rubygems_version = '< 0'
Expand Down Expand Up @@ -1720,9 +1736,9 @@ def test_default_gem
assert_equal ['bin/executable'], default_spec.files
end

def old_ruby_required
def old_ruby_required(requirement)
spec = util_spec 'old_ruby_required', '1' do |s|
s.required_ruby_version = '= 1.4.6'
s.required_ruby_version = requirement
end

util_build_gem spec
Expand Down