2013-02-12 16:06:36 -05:00
# coding: US-ASCII
2011-01-28 18:46:47 -05:00
require 'rubygems/test_case'
2007-11-10 02:48:56 -05:00
require 'rubygems'
2013-10-16 18:37:39 -04:00
require 'rubygems/command'
2007-12-20 03:39:12 -05:00
require 'rubygems/installer'
2007-11-10 02:48:56 -05:00
require 'pathname'
2008-06-28 00:13:08 -04:00
require 'tmpdir'
2018-12-19 02:20:17 -05:00
require 'rbconfig'
2007-11-10 02:48:56 -05:00
2011-01-28 18:46:47 -05:00
class TestGem < Gem :: TestCase
2018-10-21 20:27:02 -04:00
PLUGINS_LOADED = [ ] # rubocop:disable Style/MutableConstant
2013-09-14 04:59:02 -04:00
2019-11-11 01:03:57 -05:00
PROJECT_DIR = File . expand_path ( '../../..' , __FILE__ ) . tap ( & Gem :: UNTAINT )
2019-07-24 00:24:18 -04:00
2007-11-10 02:48:56 -05:00
def setup
super
2013-07-22 18:46:50 -04:00
2013-09-14 04:59:02 -04:00
PLUGINS_LOADED . clear
common_installer_setup
2012-11-29 01:52:18 -05:00
ENV . delete 'RUBYGEMS_GEMDEPS'
2020-06-10 13:46:05 -04:00
@additional = %w[ a b ] . map { | d | File . join @tempdir , d }
2010-04-22 04:24:42 -04:00
util_remove_interrupt_command
2007-11-10 02:48:56 -05:00
end
2012-11-29 01:52:18 -05:00
def test_self_finish_resolve
save_loaded_features do
2018-05-30 09:01:35 -04:00
a1 = util_spec " a " , " 1 " , " b " = > " > 0 "
b1 = util_spec " b " , " 1 " , " c " = > " >= 1 "
b2 = util_spec " b " , " 2 " , " c " = > " >= 2 "
c1 = util_spec " c " , " 1 "
c2 = util_spec " c " , " 2 "
2012-11-29 01:52:18 -05:00
2015-07-01 17:50:14 -04:00
install_specs c1 , c2 , b1 , b2 , a1
2012-11-29 01:52:18 -05:00
a1 . activate
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 ] , loaded_spec_names
2012-11-29 01:52:18 -05:00
assert_equal [ " b (> 0) " ] , unresolved_names
Gem . finish_resolve
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-2 c-2 ] , loaded_spec_names
2012-11-29 01:52:18 -05:00
assert_equal [ ] , unresolved_names
end
end
def test_self_finish_resolve_wtf
save_loaded_features do
2018-05-30 09:01:35 -04:00
a1 = util_spec " a " , " 1 " , " b " = > " > 0 " , " d " = > " > 0 " # this
b1 = util_spec " b " , " 1 " , { " c " = > " >= 1 " } , " lib/b.rb " # this
b2 = util_spec " b " , " 2 " , { " c " = > " >= 2 " } , " lib/b.rb "
c1 = util_spec " c " , " 1 " # this
c2 = util_spec " c " , " 2 "
d1 = util_spec " d " , " 1 " , { " c " = > " < 2 " } , " lib/d.rb "
d2 = util_spec " d " , " 2 " , { " c " = > " < 2 " } , " lib/d.rb " # this
2012-11-29 01:52:18 -05:00
2015-07-01 17:50:14 -04:00
install_specs c1 , c2 , b1 , b2 , d1 , d2 , a1
2012-11-29 01:52:18 -05:00
a1 . activate
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 ] , loaded_spec_names
2012-11-29 01:52:18 -05:00
assert_equal [ " b (> 0) " , " d (> 0) " ] , unresolved_names
Gem . finish_resolve
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-1 c-1 d-2 ] , loaded_spec_names
2012-11-29 01:52:18 -05:00
assert_equal [ ] , unresolved_names
end
end
2017-05-01 07:40:46 -04:00
def test_self_finish_resolve_respects_loaded_specs
save_loaded_features do
2018-05-30 09:01:35 -04:00
a1 = util_spec " a " , " 1 " , " b " = > " > 0 "
b1 = util_spec " b " , " 1 " , " c " = > " >= 1 "
b2 = util_spec " b " , " 2 " , " c " = > " >= 2 "
c1 = util_spec " c " , " 1 "
c2 = util_spec " c " , " 2 "
2017-05-01 07:40:46 -04:00
install_specs c1 , c2 , b1 , b2 , a1
a1 . activate
c1 . activate
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 c-1 ] , loaded_spec_names
2017-05-01 07:40:46 -04:00
assert_equal [ " b (> 0) " ] , unresolved_names
Gem . finish_resolve
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-1 c-1 ] , loaded_spec_names
2017-05-01 07:40:46 -04:00
assert_equal [ ] , unresolved_names
end
end
2014-09-13 23:30:02 -04:00
def test_self_install
spec_fetcher do | f |
f . gem 'a' , 1
f . spec 'a' , 2
end
gemhome2 = " #{ @gemhome } 2 "
installed = Gem . install 'a' , '= 1' , :install_dir = > gemhome2
2020-06-10 13:46:05 -04:00
assert_equal %w[ a-1 ] , installed . map { | spec | spec . full_name }
2014-09-13 23:30:02 -04:00
assert_path_exists File . join ( gemhome2 , 'gems' , 'a-1' )
end
2015-07-01 17:50:14 -04:00
def test_self_install_in_rescue
spec_fetcher do | f |
f . gem 'a' , 1
f . spec 'a' , 2
end
gemhome2 = " #{ @gemhome } 2 "
installed =
begin
raise 'Error'
rescue StandardError
Gem . install 'a' , '= 1' , :install_dir = > gemhome2
end
2020-06-10 13:46:05 -04:00
assert_equal %w[ a-1 ] , installed . map { | spec | spec . full_name }
2015-07-01 17:50:14 -04:00
end
2018-05-30 09:01:35 -04:00
def test_self_install_permissions
assert_self_install_permissions
end
def test_self_install_permissions_umask_0
umask = File . umask ( 0 )
assert_self_install_permissions
ensure
File . umask ( umask )
end
def test_self_install_permissions_umask_077
umask = File . umask ( 077 )
assert_self_install_permissions
ensure
File . umask ( umask )
end
2018-12-31 23:53:52 -05:00
def test_self_install_permissions_with_format_executable
2019-02-14 07:59:03 -05:00
assert_self_install_permissions ( format_executable : true )
end
def test_self_install_permissions_with_format_executable_and_non_standard_ruby_install_name
Gem :: Installer . exec_format = nil
2020-01-31 21:14:04 -05:00
ruby_install_name 'ruby27' do
assert_self_install_permissions ( format_executable : true )
2019-02-14 07:59:03 -05:00
end
ensure
Gem :: Installer . exec_format = nil
2018-12-31 23:53:52 -05:00
end
2019-02-14 07:59:03 -05:00
def assert_self_install_permissions ( format_executable : false )
2019-01-22 01:28:04 -05:00
mask = win_platform? ? 0700 : 0777
2018-05-30 09:01:35 -04:00
options = {
:dir_mode = > 0500 ,
2019-02-14 07:59:03 -05:00
:prog_mode = > win_platform? ? 0410 : 0510 ,
2018-05-30 09:01:35 -04:00
:data_mode = > 0640 ,
:wrappers = > true ,
2020-12-08 02:33:39 -05:00
:format_executable = > format_executable ,
2018-05-30 09:01:35 -04:00
}
Dir . chdir @tempdir do
Dir . mkdir 'bin'
Dir . mkdir 'data'
2019-02-14 07:59:03 -05:00
File . write 'bin/foo' , " # !/usr/bin/env ruby \n "
File . chmod 0755 , 'bin/foo'
File . write 'data/foo.txt' , " blah \n "
2018-05-30 09:01:35 -04:00
spec_fetcher do | f |
f . gem 'foo' , 1 do | s |
2019-02-14 07:59:03 -05:00
s . executables = [ 'foo' ]
s . files = %w[ bin/foo data/foo.txt ]
2018-05-30 09:01:35 -04:00
end
end
Gem . install 'foo' , Gem :: Requirement . default , options
end
prog_mode = ( options [ :prog_mode ] & mask ) . to_s ( 8 )
dir_mode = ( options [ :dir_mode ] & mask ) . to_s ( 8 )
data_mode = ( options [ :data_mode ] & mask ) . to_s ( 8 )
2019-02-14 07:59:03 -05:00
prog_name = 'foo'
prog_name = RbConfig :: CONFIG [ 'ruby_install_name' ] . sub ( 'ruby' , 'foo' ) if options [ :format_executable ]
2018-05-30 09:01:35 -04:00
expected = {
2018-12-31 23:53:52 -05:00
" bin/ #{ prog_name } " = > prog_mode ,
2018-05-30 09:01:35 -04:00
'gems/foo-1' = > dir_mode ,
'gems/foo-1/bin' = > dir_mode ,
'gems/foo-1/data' = > dir_mode ,
2019-02-14 07:59:03 -05:00
'gems/foo-1/bin/foo' = > prog_mode ,
2018-05-30 09:01:35 -04:00
'gems/foo-1/data/foo.txt' = > data_mode ,
}
2019-02-14 07:59:03 -05:00
# add Windows script
expected [ " bin/ #{ prog_name } .bat " ] = mask . to_s ( 8 ) if win_platform?
2018-05-30 09:01:35 -04:00
result = { }
Dir . chdir @gemhome do
expected . each_key do | n |
result [ n ] = ( File . stat ( n ) . mode & mask ) . to_s ( 8 )
end
end
assert_equal ( expected , result )
ensure
2020-06-10 13:46:05 -04:00
File . chmod ( 0755 , * Dir . glob ( @gemhome + '/gems/**/' ) . map { | path | path . tap ( & Gem :: UNTAINT ) } )
2018-05-30 09:01:35 -04:00
end
2011-03-07 03:44:45 -05:00
def test_require_missing
save_loaded_features do
assert_raises :: LoadError do
2019-12-10 09:01:23 -05:00
require " test_require_missing "
2011-03-07 03:44:45 -05:00
end
end
end
2011-05-31 23:45:05 -04:00
def test_require_does_not_glob
save_loaded_features do
2018-05-30 09:01:35 -04:00
a1 = util_spec " a " , " 1 " , nil , " lib/a1.rb "
2011-05-31 23:45:05 -04:00
install_specs a1
assert_raises :: LoadError do
require " a* "
end
assert_equal [ ] , loaded_spec_names
end
end
2014-12-06 19:53:01 -05:00
def test_self_bin_path_active
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
end
util_spec 'a' , '2' do | s |
s . executables = [ 'exec' ]
end
a1 . activate
assert_match 'a-1/bin/exec' , Gem . bin_path ( 'a' , 'exec' , '>= 0' )
end
2016-03-27 22:30:28 -04:00
def test_self_bin_path_picking_newest
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
end
a2 = util_spec 'a' , '2' do | s |
s . executables = [ 'exec' ]
end
install_specs a1 , a2
assert_match 'a-2/bin/exec' , Gem . bin_path ( 'a' , 'exec' , '>= 0' )
end
2019-04-26 07:26:21 -04:00
def test_self_activate_bin_path_no_exec_name
e = assert_raises ArgumentError do
Gem . activate_bin_path 'a'
end
assert_equal 'you must supply exec_name' , e . message
end
2017-10-07 21:32:18 -04:00
def test_activate_bin_path_resolves_eagerly
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
s . add_dependency 'b'
end
b1 = util_spec 'b' , '1' do | s |
s . add_dependency 'c' , '2'
end
b2 = util_spec 'b' , '2' do | s |
s . add_dependency 'c' , '1'
end
c1 = util_spec 'c' , '1'
c2 = util_spec 'c' , '2'
install_specs c1 , c2 , b1 , b2 , a1
Gem . activate_bin_path ( " a " , " exec " , " >= 0 " )
# If we didn't eagerly resolve, this would activate c-2 and then the
# finish_resolve would cause a conflict
gem 'c'
Gem . finish_resolve
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-2 c-1 ] , loaded_spec_names
2017-10-07 21:32:18 -04:00
end
2021-02-01 10:17:16 -05:00
def test_activate_bin_path_does_not_error_if_a_gem_thats_not_finally_activated_has_orphaned_dependencies
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
s . add_dependency 'b'
end
b1 = util_spec 'b' , '1' do | s |
s . add_dependency 'c' , '1'
end
b2 = util_spec 'b' , '2' do | s |
s . add_dependency 'c' , '2'
end
c2 = util_spec 'c' , '2'
install_specs c2 , b1 , b2 , a1
# c1 is missing, but not needed for activation, so we should not get any errors here
Gem . activate_bin_path ( " a " , " exec " , " >= 0 " )
assert_equal %w[ a-1 b-2 c-2 ] , loaded_spec_names
end
def test_activate_bin_path_raises_a_meaningful_error_if_a_gem_thats_finally_activated_has_orphaned_dependencies
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
s . add_dependency 'b'
end
b1 = util_spec 'b' , '1' do | s |
s . add_dependency 'c' , '1'
end
b2 = util_spec 'b' , '2' do | s |
s . add_dependency 'c' , '2'
end
c1 = util_spec 'c' , '1'
install_specs c1 , b1 , b2 , a1
# c2 is missing, and b2 which has it as a dependency will be activated, so we should get an error about the orphaned dependency
e = assert_raises Gem :: UnsatisfiableDependencyError do
load Gem . activate_bin_path ( " a " , " exec " , " >= 0 " )
end
assert_equal " Unable to resolve dependency: 'b (>= 0)' requires 'c (= 2)' " , e . message
end
2020-03-16 02:36:41 -04:00
def test_activate_bin_path_in_debug_mode
a1 = util_spec 'a' , '1' do | s |
s . executables = [ 'exec' ]
end
install_specs a1
2020-03-29 16:05:40 -04:00
require " open3 "
2020-03-16 02:36:41 -04:00
output , status = Open3 . capture2e (
{ " GEM_HOME " = > Gem . paths . home , " DEBUG_RESOLVER " = > " 1 " } ,
2020-04-08 15:14:27 -04:00
* ruby_with_rubygems_in_load_path , " -e " , " \" Gem.activate_bin_path('a', 'exec', '>= 0') \" "
2020-03-16 02:36:41 -04:00
)
assert status . success? , output
end
2018-11-21 05:20:47 -05:00
def test_activate_bin_path_gives_proper_error_for_bundler
bundler = util_spec 'bundler' , '2' do | s |
s . executables = [ 'bundle' ]
end
install_specs bundler
File . open ( " Gemfile.lock " , " w " ) do | f |
f . write <<-L.gsub(/ {8}/, "")
GEM
remote : https : / / rubygems . org /
specs :
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
9999
L
end
2020-06-10 13:46:05 -04:00
File . open ( " Gemfile " , " w " ) { | f | f . puts ( 'source "https://rubygems.org"' ) }
2018-11-21 05:20:47 -05:00
e = assert_raises Gem :: GemNotFoundException do
load Gem . activate_bin_path ( " bundler " , " bundle " , " >= 0.a " )
end
assert_includes e . message , " Could not find 'bundler' (9999) required by your #{ File . expand_path ( " Gemfile.lock " ) } . "
assert_includes e . message , " To update to the latest version installed on your system, run `bundle update --bundler`. "
assert_includes e . message , " To install the missing version, run `gem install bundler:9999` "
refute_includes e . message , " can't find gem bundler (>= 0.a) with executable bundle "
end
2019-01-07 06:00:35 -05:00
def test_activate_bin_path_selects_exact_bundler_version_if_present
bundler_latest = util_spec 'bundler' , '2.0.1' do | s |
s . executables = [ 'bundle' ]
end
bundler_previous = util_spec 'bundler' , '2.0.0' do | s |
s . executables = [ 'bundle' ]
end
install_specs bundler_latest , bundler_previous
File . open ( " Gemfile.lock " , " w " ) do | f |
f . write <<-L.gsub(/ {8}/, "")
GEM
remote : https : / / rubygems . org /
specs :
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2 . 0 . 0
L
end
2020-06-10 13:46:05 -04:00
File . open ( " Gemfile " , " w " ) { | f | f . puts ( 'source "https://rubygems.org"' ) }
2019-01-07 06:00:35 -05:00
load Gem . activate_bin_path ( " bundler " , " bundle " , " >= 0.a " )
2020-03-24 14:51:43 -04:00
assert_equal %w[ bundler-2.0.0 ] , loaded_spec_names
2019-01-07 06:00:35 -05:00
end
2019-09-10 05:07:03 -04:00
def test_activate_bin_path_respects_underscore_selection_if_given
bundler_latest = util_spec 'bundler' , '2.0.1' do | s |
s . executables = [ 'bundle' ]
end
bundler_previous = util_spec 'bundler' , '1.17.3' do | s |
s . executables = [ 'bundle' ]
end
install_specs bundler_latest , bundler_previous
File . open ( " Gemfile.lock " , " w " ) do | f |
f . write <<-L.gsub(/ {8}/, "")
GEM
remote : https : / / rubygems . org /
specs :
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2 . 0 . 1
L
end
2020-06-10 13:46:05 -04:00
File . open ( " Gemfile " , " w " ) { | f | f . puts ( 'source "https://rubygems.org"' ) }
2019-09-10 05:07:03 -04:00
load Gem . activate_bin_path ( " bundler " , " bundle " , " = 1.17.3 " )
2020-03-24 14:51:43 -04:00
assert_equal %w[ bundler-1.17.3 ] , loaded_spec_names
2019-09-10 05:07:03 -04:00
end
2021-02-01 10:17:16 -05:00
def test_activate_bin_path_gives_proper_error_for_bundler_when_underscore_selection_given
File . open ( " Gemfile.lock " , " w " ) do | f |
f . write <<-L.gsub(/ {8}/, "")
GEM
remote : https : / / rubygems . org /
specs :
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2 . 1 . 4
L
end
File . open ( " Gemfile " , " w " ) { | f | f . puts ( 'source "https://rubygems.org"' ) }
e = assert_raises Gem :: GemNotFoundException do
load Gem . activate_bin_path ( " bundler " , " bundle " , " = 2.2.8 " )
end
assert_equal " can't find gem bundler (= 2.2.8) with executable bundle " , e . message
end
2011-05-31 23:45:05 -04:00
def test_self_bin_path_no_exec_name
e = assert_raises ArgumentError do
Gem . bin_path 'a'
end
assert_equal 'you must supply exec_name' , e . message
2008-06-17 18:04:18 -04:00
end
2007-11-10 02:48:56 -05:00
2009-06-09 17:38:59 -04:00
def test_self_bin_path_bin_name
2015-07-01 17:50:14 -04:00
install_specs util_exec_gem
2009-06-09 17:38:59 -04:00
assert_equal @abin_path , Gem . bin_path ( 'a' , 'abin' )
end
def test_self_bin_path_bin_name_version
2015-07-01 17:50:14 -04:00
install_specs util_exec_gem
2009-06-09 17:38:59 -04:00
assert_equal @abin_path , Gem . bin_path ( 'a' , 'abin' , '4' )
end
2011-01-18 19:08:49 -05:00
def test_self_bin_path_nonexistent_binfile
2013-11-11 19:16:41 -05:00
util_spec 'a' , '2' do | s |
2009-06-09 17:38:59 -04:00
s . executables = [ 'exec' ]
end
2011-01-18 19:08:49 -05:00
assert_raises ( Gem :: GemNotFoundException ) do
Gem . bin_path ( 'a' , 'other' , '2' )
2009-06-09 17:38:59 -04:00
end
end
def test_self_bin_path_no_bin_file
2013-11-11 19:16:41 -05:00
util_spec 'a' , '1'
2011-05-31 23:45:05 -04:00
assert_raises ( ArgumentError ) do
2011-01-18 19:08:49 -05:00
Gem . bin_path ( 'a' , nil , '1' )
2009-06-09 17:38:59 -04:00
end
end
def test_self_bin_path_not_found
assert_raises ( Gem :: GemNotFoundException ) do
2011-05-31 23:45:05 -04:00
Gem . bin_path ( 'non-existent' , 'blah' )
2009-06-09 17:38:59 -04:00
end
end
2011-01-18 19:08:49 -05:00
def test_self_bin_path_bin_file_gone_in_latest
2015-07-01 17:50:14 -04:00
install_specs util_exec_gem
spec = util_spec 'a' , '10' do | s |
2011-01-18 19:08:49 -05:00
s . executables = [ ]
end
2015-07-01 17:50:14 -04:00
install_specs spec
2011-01-18 19:08:49 -05:00
# Should not find a-10's non-abin (bug)
assert_equal @abin_path , Gem . bin_path ( 'a' , 'abin' )
end
2007-11-10 02:48:56 -05:00
def test_self_bindir
assert_equal File . join ( @gemhome , 'bin' ) , Gem . bindir
assert_equal File . join ( @gemhome , 'bin' ) , Gem . bindir ( Gem . dir )
assert_equal File . join ( @gemhome , 'bin' ) , Gem . bindir ( Pathname . new ( Gem . dir ) )
end
def test_self_bindir_default_dir
default = Gem . default_dir
2008-09-25 06:13:50 -04:00
2012-11-29 01:52:18 -05:00
assert_equal Gem . default_bindir , Gem . bindir ( default )
2007-11-10 02:48:56 -05:00
end
def test_self_clear_paths
2011-05-31 23:45:05 -04:00
assert_match ( / gemhome$ / , Gem . dir )
assert_match ( / gemhome$ / , Gem . path . first )
2007-11-10 02:48:56 -05:00
Gem . clear_paths
2011-05-31 23:45:05 -04:00
assert_nil Gem :: Specification . send ( :class_variable_get , :@@all )
2007-11-10 02:48:56 -05:00
end
def test_self_configuration
expected = Gem :: ConfigFile . new [ ]
Gem . configuration = nil
assert_equal expected , Gem . configuration
end
def test_self_datadir
foo = nil
Dir . chdir @tempdir do
FileUtils . mkdir_p 'data'
File . open File . join ( 'data' , 'foo.txt' ) , 'w' do | fp |
fp . puts 'blah'
end
2019-02-14 07:59:03 -05:00
foo = util_spec 'foo' do | s |
s . files = %w[ data/foo.txt ]
end
2007-11-10 02:48:56 -05:00
install_gem foo
end
gem 'foo'
expected = File . join @gemhome , 'gems' , foo . full_name , 'data' , 'foo'
2017-10-07 21:32:18 -04:00
assert_equal expected , Gem :: Specification . find_by_name ( " foo " ) . datadir
2007-11-10 02:48:56 -05:00
end
def test_self_datadir_nonexistent_package
2017-10-07 21:32:18 -04:00
assert_raises ( Gem :: MissingSpecError ) do
Gem :: Specification . find_by_name ( " xyzzy " ) . datadir
end
2007-11-10 02:48:56 -05:00
end
2007-12-20 03:39:12 -05:00
def test_self_default_exec_format
2014-02-03 19:48:31 -05:00
ruby_install_name 'ruby' do
assert_equal '%s' , Gem . default_exec_format
end
2007-12-20 03:39:12 -05:00
end
def test_self_default_exec_format_18
2014-02-03 19:48:31 -05:00
ruby_install_name 'ruby18' do
assert_equal '%s18' , Gem . default_exec_format
end
2007-12-20 03:39:12 -05:00
end
def test_self_default_exec_format_jruby
2014-02-03 19:48:31 -05:00
ruby_install_name 'jruby' do
assert_equal 'j%s' , Gem . default_exec_format
end
2007-12-20 03:39:12 -05:00
end
2014-09-13 23:30:02 -04:00
def test_default_path
2019-06-01 05:45:11 -04:00
vendordir ( File . join ( @tempdir , 'vendor' ) ) do
FileUtils . rm_rf Gem . user_home
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
expected = [ Gem . default_dir ]
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , Gem . default_path
end
2014-09-13 23:30:02 -04:00
end
def test_default_path_missing_vendor
2019-06-01 05:45:11 -04:00
vendordir ( nil ) do
FileUtils . rm_rf Gem . user_home
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
expected = [ Gem . default_dir ]
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , Gem . default_path
end
2014-09-13 23:30:02 -04:00
end
def test_default_path_user_home
2019-06-01 05:45:11 -04:00
vendordir ( File . join ( @tempdir , 'vendor' ) ) do
expected = [ Gem . user_dir , Gem . default_dir ]
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , Gem . default_path
end
2014-09-13 23:30:02 -04:00
end
def test_default_path_vendor_dir
2019-06-01 05:45:11 -04:00
vendordir ( File . join ( @tempdir , 'vendor' ) ) do
FileUtils . mkdir_p Gem . vendor_dir
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
FileUtils . rm_rf Gem . user_home
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
expected = [ Gem . default_dir , Gem . vendor_dir ]
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , Gem . default_path
end
2014-09-13 23:30:02 -04:00
end
2007-11-10 02:48:56 -05:00
def test_self_default_sources
2013-02-28 17:25:55 -05:00
assert_equal %w[ https://rubygems.org/ ] , Gem . default_sources
2007-11-10 02:48:56 -05:00
end
2018-05-17 21:39:13 -04:00
def test_self_use_gemdeps
2013-02-04 21:37:35 -05:00
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , '-'
FileUtils . mkdir_p 'detect/a/b'
FileUtils . mkdir_p 'detect/a/Isolate'
FileUtils . touch 'detect/Isolate'
begin
Dir . chdir 'detect/a/b'
2018-05-17 21:39:13 -04:00
assert_equal add_bundler_full_name ( [ ] ) , Gem . use_gemdeps . map ( & :full_name )
2013-02-04 21:37:35 -05:00
ensure
Dir . chdir @tempdir
end
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2007-11-10 02:48:56 -05:00
def test_self_dir
assert_equal @gemhome , Gem . dir
end
def test_self_ensure_gem_directories
FileUtils . rm_r @gemhome
Gem . use_paths @gemhome
Gem . ensure_gem_subdirectories @gemhome
2013-10-15 20:14:16 -04:00
assert_path_exists File . join @gemhome , 'build_info'
assert_path_exists File . join @gemhome , 'cache'
assert_path_exists File . join @gemhome , 'doc'
assert_path_exists File . join @gemhome , 'extensions'
assert_path_exists File . join @gemhome , 'gems'
assert_path_exists File . join @gemhome , 'specifications'
2007-11-10 02:48:56 -05:00
end
2013-03-05 18:02:00 -05:00
def test_self_ensure_gem_directories_permissions
FileUtils . rm_r @gemhome
Gem . use_paths @gemhome
Gem . ensure_gem_subdirectories @gemhome , 0750
2019-03-04 22:32:58 -05:00
assert_directory_exists File . join ( @gemhome , " cache " )
2013-03-05 18:02:00 -05:00
assert_equal 0750 , File :: Stat . new ( @gemhome ) . mode & 0777
assert_equal 0750 , File :: Stat . new ( File . join ( @gemhome , " cache " ) ) . mode & 0777
end unless win_platform?
2011-08-04 21:00:01 -04:00
def test_self_ensure_gem_directories_safe_permissions
FileUtils . rm_r @gemhome
Gem . use_paths @gemhome
old_umask = File . umask
File . umask 0
Gem . ensure_gem_subdirectories @gemhome
2012-04-17 20:04:12 -04:00
assert_equal 0 , File :: Stat . new ( @gemhome ) . mode & 002
assert_equal 0 , File :: Stat . new ( File . join ( @gemhome , " cache " ) ) . mode & 002
2011-08-04 21:00:01 -04:00
ensure
File . umask old_umask
end unless win_platform?
2007-11-10 02:48:56 -05:00
def test_self_ensure_gem_directories_missing_parents
gemdir = File . join @tempdir , 'a/b/c/gemdir'
FileUtils . rm_rf File . join ( @tempdir , 'a' ) rescue nil
2009-06-09 17:38:59 -04:00
refute File . exist? ( File . join ( @tempdir , 'a' ) ) ,
2007-11-10 02:48:56 -05:00
" manually remove #{ File . join @tempdir , 'a' } , tests are broken "
Gem . use_paths gemdir
Gem . ensure_gem_subdirectories gemdir
2019-03-04 22:32:58 -05:00
assert_directory_exists util_cache_dir
2007-11-10 02:48:56 -05:00
end
2019-05-03 13:56:58 -04:00
unless win_platform? || Process . uid . zero? # only for FS that support write protection
2007-11-10 02:48:56 -05:00
def test_self_ensure_gem_directories_write_protected
gemdir = File . join @tempdir , " egd "
FileUtils . rm_r gemdir rescue nil
2009-06-09 17:38:59 -04:00
refute File . exist? ( gemdir ) , " manually remove #{ gemdir } , tests are broken "
2007-11-10 02:48:56 -05:00
FileUtils . mkdir_p gemdir
FileUtils . chmod 0400 , gemdir
Gem . use_paths gemdir
Gem . ensure_gem_subdirectories gemdir
2011-05-31 23:45:05 -04:00
refute File . exist? ( util_cache_dir )
2007-11-10 02:48:56 -05:00
ensure
FileUtils . chmod 0600 , gemdir
end
def test_self_ensure_gem_directories_write_protected_parents
parent = File . join ( @tempdir , " egd " )
gemdir = " #{ parent } /a/b/c "
FileUtils . rm_r parent rescue nil
2009-06-09 17:38:59 -04:00
refute File . exist? ( parent ) , " manually remove #{ parent } , tests are broken "
2007-11-10 02:48:56 -05:00
FileUtils . mkdir_p parent
FileUtils . chmod 0400 , parent
Gem . use_paths ( gemdir )
Gem . ensure_gem_subdirectories gemdir
2011-05-31 23:45:05 -04:00
refute File . exist? File . join ( gemdir , " gems " )
2007-11-10 02:48:56 -05:00
ensure
FileUtils . chmod 0600 , parent
end
2021-02-01 10:17:16 -05:00
def test_self_ensure_gem_directories_non_existent_paths
Gem . ensure_gem_subdirectories '/proc/0123456789/bogus' # should not raise
Gem . ensure_gem_subdirectories 'classpath:/bogus/x' # JRuby embed scenario
end
2007-11-10 02:48:56 -05:00
end
2013-12-10 14:54:19 -05:00
def test_self_extension_dir_shared
2014-02-03 19:48:31 -05:00
enable_shared 'yes' do
assert_equal Gem . ruby_api_version , Gem . extension_api_version
end
2013-10-17 17:03:49 -04:00
end
2013-12-10 14:54:19 -05:00
def test_self_extension_dir_static
2014-02-03 19:48:31 -05:00
enable_shared 'no' do
assert_equal " #{ Gem . ruby_api_version } -static " , Gem . extension_api_version
end
2013-10-17 17:03:49 -04:00
end
2008-09-25 06:13:50 -04:00
def test_self_find_files
2019-07-24 00:57:59 -04:00
cwd = File . expand_path ( " test/rubygems " , PROJECT_DIR )
2011-03-07 03:44:45 -05:00
$LOAD_PATH . unshift cwd
2009-06-09 17:38:59 -04:00
2011-05-31 23:45:05 -04:00
discover_path = File . join 'lib' , 'sff' , 'discover.rb'
2008-09-25 06:13:50 -04:00
2020-03-24 14:51:43 -04:00
foo1 , foo2 = %w[ 1 2 ] . map do | version |
2011-05-31 23:45:05 -04:00
spec = quick_gem 'sff' , version do | s |
s . files << discover_path
end
2008-09-25 06:13:50 -04:00
2011-05-31 23:45:05 -04:00
write_file ( File . join 'gems' , spec . full_name , discover_path ) do | fp |
fp . puts " # #{ spec . full_name } "
end
2008-09-25 06:13:50 -04:00
2011-05-31 23:45:05 -04:00
spec
2019-02-14 07:59:03 -05:00
end
2008-09-25 06:13:50 -04:00
2013-09-14 04:59:02 -04:00
Gem . refresh
2008-09-25 06:13:50 -04:00
expected = [
2019-07-24 00:57:59 -04:00
File . expand_path ( 'test/rubygems/sff/discover.rb' , PROJECT_DIR ) ,
2009-06-09 17:38:59 -04:00
File . join ( foo2 . full_gem_path , discover_path ) ,
File . join ( foo1 . full_gem_path , discover_path ) ,
2008-09-25 06:13:50 -04:00
]
2011-01-18 19:08:49 -05:00
assert_equal expected , Gem . find_files ( 'sff/discover' )
assert_equal expected , Gem . find_files ( 'sff/**.rb' ) , '[ruby-core:31730]'
2009-10-04 00:53:57 -04:00
ensure
2010-04-22 04:24:42 -04:00
assert_equal cwd , $LOAD_PATH . shift
2008-09-25 06:13:50 -04:00
end
2016-03-03 19:29:40 -05:00
def test_self_find_files_with_gemfile
2019-07-24 00:57:59 -04:00
cwd = File . expand_path ( " test/rubygems " , PROJECT_DIR )
2017-05-01 07:40:46 -04:00
actual_load_path = $LOAD_PATH . unshift ( cwd ) . dup
2016-03-03 19:29:40 -05:00
discover_path = File . join 'lib' , 'sff' , 'discover.rb'
2020-03-24 14:51:43 -04:00
foo1 , _ = %w[ 1 2 ] . map do | version |
2016-03-03 19:29:40 -05:00
spec = quick_gem 'sff' , version do | s |
s . files << discover_path
end
write_file ( File . join 'gems' , spec . full_name , discover_path ) do | fp |
fp . puts " # #{ spec . full_name } "
end
spec
2019-02-14 07:59:03 -05:00
end
2016-03-03 19:29:40 -05:00
Gem . refresh
write_file ( File . join Dir . pwd , 'Gemfile' ) do | fp |
fp . puts " source 'https://rubygems.org' "
fp . puts " gem ' #{ foo1 . name } ', ' #{ foo1 . version } ' "
end
Gem . use_gemdeps ( File . join Dir . pwd , 'Gemfile' )
expected = [
2019-07-24 00:57:59 -04:00
File . expand_path ( 'test/rubygems/sff/discover.rb' , PROJECT_DIR ) ,
2020-12-08 02:33:39 -05:00
File . join ( foo1 . full_gem_path , discover_path ) ,
2017-05-01 07:40:46 -04:00
] . sort
2016-03-03 19:29:40 -05:00
2017-05-01 07:40:46 -04:00
assert_equal expected , Gem . find_files ( 'sff/discover' ) . sort
assert_equal expected , Gem . find_files ( 'sff/**.rb' ) . sort , '[ruby-core:31730]'
2016-03-03 19:29:40 -05:00
ensure
2019-06-01 05:45:11 -04:00
assert_equal cwd , actual_load_path . shift unless Gem . java_platform?
2016-03-03 19:29:40 -05:00
end
2013-09-14 04:59:02 -04:00
def test_self_find_latest_files
2019-07-24 00:57:59 -04:00
cwd = File . expand_path ( " test/rubygems " , PROJECT_DIR )
2013-09-14 04:59:02 -04:00
$LOAD_PATH . unshift cwd
discover_path = File . join 'lib' , 'sff' , 'discover.rb'
2020-03-24 14:51:43 -04:00
_ , foo2 = %w[ 1 2 ] . map do | version |
2013-09-14 04:59:02 -04:00
spec = quick_gem 'sff' , version do | s |
s . files << discover_path
end
write_file ( File . join 'gems' , spec . full_name , discover_path ) do | fp |
fp . puts " # #{ spec . full_name } "
end
spec
2019-02-14 07:59:03 -05:00
end
2013-09-14 04:59:02 -04:00
Gem . refresh
expected = [
2019-07-24 00:57:59 -04:00
File . expand_path ( 'test/rubygems/sff/discover.rb' , PROJECT_DIR ) ,
2013-09-14 04:59:02 -04:00
File . join ( foo2 . full_gem_path , discover_path ) ,
]
assert_equal expected , Gem . find_latest_files ( 'sff/discover' )
assert_equal expected , Gem . find_latest_files ( 'sff/**.rb' ) , '[ruby-core:31730]'
ensure
assert_equal cwd , $LOAD_PATH . shift
end
2012-12-22 19:35:09 -05:00
def test_self_latest_spec_for
2013-11-11 19:16:41 -05:00
gems = spec_fetcher do | fetcher |
2013-11-10 12:51:40 -05:00
fetcher . spec 'a' , 1
fetcher . spec 'a' , '3.a'
2013-11-11 19:16:41 -05:00
fetcher . spec 'a' , 2
2013-11-10 12:51:40 -05:00
end
2012-12-22 19:35:09 -05:00
spec = Gem . latest_spec_for 'a'
2013-11-11 19:16:41 -05:00
assert_equal gems [ 'a-2' ] , spec
2012-12-22 19:35:09 -05:00
end
def test_self_latest_rubygems_version
2013-11-10 12:51:40 -05:00
spec_fetcher do | fetcher |
fetcher . spec 'rubygems-update' , '1.8.23'
fetcher . spec 'rubygems-update' , '1.8.24'
fetcher . spec 'rubygems-update' , '2.0.0.preview3'
end
2012-12-22 19:35:09 -05:00
version = Gem . latest_rubygems_version
assert_equal Gem :: Version . new ( '1.8.24' ) , version
end
def test_self_latest_version_for
2013-11-10 12:51:40 -05:00
spec_fetcher do | fetcher |
fetcher . spec 'a' , 1
fetcher . spec 'a' , 2
fetcher . spec 'a' , '3.a'
end
2012-12-22 19:35:09 -05:00
version = Gem . latest_version_for 'a'
assert_equal Gem :: Version . new ( 2 ) , version
end
2007-11-10 02:48:56 -05:00
def test_self_loaded_specs
2013-11-11 19:16:41 -05:00
foo = util_spec 'foo'
2007-11-10 02:48:56 -05:00
install_gem foo
2011-05-31 23:45:05 -04:00
foo . activate
2007-11-10 02:48:56 -05:00
assert_equal true , Gem . loaded_specs . keys . include? ( 'foo' )
end
2011-01-18 19:08:49 -05:00
def util_path
ENV . delete " GEM_HOME "
ENV . delete " GEM_PATH "
end
2007-11-10 02:48:56 -05:00
def test_self_path
assert_equal [ Gem . dir ] , Gem . path
end
2008-06-17 18:04:18 -04:00
def test_self_path_default
2011-01-18 19:08:49 -05:00
util_path
2011-07-26 21:40:07 -04:00
if defined? ( APPLE_GEM_HOME )
2008-06-17 18:04:18 -04:00
orig_APPLE_GEM_HOME = APPLE_GEM_HOME
Object . send :remove_const , :APPLE_GEM_HOME
end
2011-05-31 23:45:05 -04:00
Gem . instance_variable_set :@paths , nil
2012-11-29 01:52:18 -05:00
assert_equal [ Gem . default_path , Gem . dir ] . flatten . uniq , Gem . path
2008-06-17 18:04:18 -04:00
ensure
2011-07-26 21:40:07 -04:00
Object . const_set :APPLE_GEM_HOME , orig_APPLE_GEM_HOME if orig_APPLE_GEM_HOME
2008-06-17 18:04:18 -04:00
end
2008-04-11 16:57:02 -04:00
unless win_platform?
def test_self_path_APPLE_GEM_HOME
2011-01-18 19:08:49 -05:00
util_path
2008-04-11 16:57:02 -04:00
Gem . clear_paths
2008-09-25 06:13:50 -04:00
apple_gem_home = File . join @tempdir , 'apple_gem_home'
2011-07-26 21:40:07 -04:00
old , $- w = $- w , nil
Object . const_set :APPLE_GEM_HOME , apple_gem_home
$- w = old
2008-09-25 06:13:50 -04:00
2009-06-09 17:38:59 -04:00
assert_includes Gem . path , apple_gem_home
2008-04-11 16:57:02 -04:00
ensure
2011-07-26 21:40:07 -04:00
Object . send :remove_const , :APPLE_GEM_HOME
2008-04-11 16:57:02 -04:00
end
2008-09-25 06:13:50 -04:00
2008-04-11 16:57:02 -04:00
def test_self_path_APPLE_GEM_HOME_GEM_PATH
Gem . clear_paths
ENV [ 'GEM_PATH' ] = @gemhome
2008-09-25 06:13:50 -04:00
apple_gem_home = File . join @tempdir , 'apple_gem_home'
Gem . const_set :APPLE_GEM_HOME , apple_gem_home
2009-06-09 17:38:59 -04:00
refute Gem . path . include? ( apple_gem_home )
2008-04-11 16:57:02 -04:00
ensure
Gem . send :remove_const , :APPLE_GEM_HOME
end
2008-03-31 18:40:06 -04:00
end
2007-11-10 02:48:56 -05:00
def test_self_path_ENV_PATH
2007-11-20 00:56:43 -05:00
path_count = Gem . path . size
2007-11-10 02:48:56 -05:00
Gem . clear_paths
ENV [ 'GEM_PATH' ] = @additional . join ( File :: PATH_SEPARATOR )
2012-11-29 01:52:18 -05:00
assert_equal @additional , Gem . path [ 0 , 2 ]
2008-06-25 22:06:00 -04:00
assert_equal path_count + @additional . size , Gem . path . size ,
" extra path components: #{ Gem . path [ 2 .. - 1 ] . inspect } "
2012-11-29 01:52:18 -05:00
assert_equal Gem . dir , Gem . path . last
2007-11-10 02:48:56 -05:00
end
def test_self_path_duplicate
Gem . clear_paths
util_ensure_gem_dirs
dirs = @additional + [ @gemhome ] + [ File . join ( @tempdir , 'a' ) ]
ENV [ 'GEM_HOME' ] = @gemhome
ENV [ 'GEM_PATH' ] = dirs . join File :: PATH_SEPARATOR
assert_equal @gemhome , Gem . dir
2008-03-31 18:40:06 -04:00
2012-11-29 01:52:18 -05:00
paths = [ Gem . dir ]
assert_equal @additional + paths , Gem . path
2007-11-10 02:48:56 -05:00
end
def test_self_path_overlap
Gem . clear_paths
util_ensure_gem_dirs
ENV [ 'GEM_HOME' ] = @gemhome
ENV [ 'GEM_PATH' ] = @additional . join ( File :: PATH_SEPARATOR )
assert_equal @gemhome , Gem . dir
2008-03-31 18:40:06 -04:00
2012-11-29 01:52:18 -05:00
paths = [ Gem . dir ]
assert_equal @additional + paths , Gem . path
2007-11-10 02:48:56 -05:00
end
def test_self_platforms
assert_equal [ Gem :: Platform :: RUBY , Gem :: Platform . local ] , Gem . platforms
end
def test_self_prefix
2019-07-24 00:57:59 -04:00
assert_equal PROJECT_DIR , Gem . prefix
2008-04-11 16:57:02 -04:00
end
def test_self_prefix_libdir
2014-01-06 20:19:28 -05:00
orig_libdir = RbConfig :: CONFIG [ 'libdir' ]
2019-07-24 00:57:59 -04:00
RbConfig :: CONFIG [ 'libdir' ] = PROJECT_DIR
2008-04-11 16:57:02 -04:00
assert_nil Gem . prefix
ensure
2014-01-06 20:19:28 -05:00
RbConfig :: CONFIG [ 'libdir' ] = orig_libdir
2007-11-10 02:48:56 -05:00
end
2008-04-11 16:57:02 -04:00
def test_self_prefix_sitelibdir
2014-01-06 20:19:28 -05:00
orig_sitelibdir = RbConfig :: CONFIG [ 'sitelibdir' ]
2019-07-24 00:57:59 -04:00
RbConfig :: CONFIG [ 'sitelibdir' ] = PROJECT_DIR
2008-03-31 18:40:06 -04:00
assert_nil Gem . prefix
ensure
2014-01-06 20:19:28 -05:00
RbConfig :: CONFIG [ 'sitelibdir' ] = orig_sitelibdir
end
def test_self_read_binary
2018-02-05 21:58:35 -05:00
File . open 'test' , 'w' do | io |
2014-01-06 20:19:28 -05:00
io . write " \xCF \x80 "
end
assert_equal [ " \xCF " , " \x80 " ] , Gem . read_binary ( 'test' ) . chars . to_a
skip 'chmod not supported' if Gem . win_platform?
begin
File . chmod 0444 , 'test'
assert_equal [ " \xCF " , " \x80 " ] , Gem . read_binary ( 'test' ) . chars . to_a
ensure
File . chmod 0644 , 'test'
end
2008-03-31 18:40:06 -04:00
end
2008-04-11 16:57:02 -04:00
def test_self_refresh
util_make_gems
2011-05-31 23:45:05 -04:00
a1_spec = @a1 . spec_file
moved_path = File . join @tempdir , File . basename ( a1_spec )
2008-04-11 16:57:02 -04:00
2011-05-31 23:45:05 -04:00
FileUtils . mv a1_spec , moved_path
2008-04-11 16:57:02 -04:00
Gem . refresh
2011-05-31 23:45:05 -04:00
refute_includes Gem :: Specification . all_names , @a1 . full_name
2008-04-11 16:57:02 -04:00
2011-05-31 23:45:05 -04:00
FileUtils . mv moved_path , a1_spec
Gem . refresh
2007-11-10 02:48:56 -05:00
2011-05-31 23:45:05 -04:00
assert_includes Gem :: Specification . all_names , @a1 . full_name
2007-11-10 02:48:56 -05:00
end
2013-01-16 19:37:28 -05:00
def test_self_refresh_keeps_loaded_specs_activated
util_make_gems
a1_spec = @a1 . spec_file
moved_path = File . join @tempdir , File . basename ( a1_spec )
FileUtils . mv a1_spec , moved_path
Gem . refresh
s = Gem :: Specification . first
s . activate
Gem . refresh
2020-06-10 13:46:05 -04:00
Gem :: Specification . each { | spec | assert spec . activated? if spec == s }
2013-01-16 19:37:28 -05:00
Gem . loaded_specs . delete ( s )
Gem . refresh
end
2008-09-25 06:13:50 -04:00
def test_self_ruby_escaping_spaces_in_path
2020-03-24 02:39:24 -04:00
with_clean_path_to_ruby do
with_rb_config_ruby ( " C:/Ruby 1.8/bin/ruby.exe " ) do
2020-01-31 21:14:04 -05:00
assert_equal " \" C:/Ruby 1.8/bin/ruby.exe \" " , Gem . ruby
2019-03-04 22:32:58 -05:00
end
end
2008-09-25 06:13:50 -04:00
end
def test_self_ruby_path_without_spaces
2020-03-24 02:39:24 -04:00
with_clean_path_to_ruby do
with_rb_config_ruby ( " C:/Ruby18/bin/ruby.exe " ) do
2020-01-31 21:14:04 -05:00
assert_equal " C:/Ruby18/bin/ruby.exe " , Gem . ruby
2019-03-04 22:32:58 -05:00
end
end
2008-09-25 06:13:50 -04:00
end
2013-10-15 20:14:16 -04:00
def test_self_ruby_api_version
2014-01-06 20:19:28 -05:00
orig_ruby_version , RbConfig :: CONFIG [ 'ruby_version' ] = RbConfig :: CONFIG [ 'ruby_version' ] , '1.2.3'
2013-10-15 20:14:16 -04:00
Gem . instance_variable_set :@ruby_api_version , nil
assert_equal '1.2.3' , Gem . ruby_api_version
ensure
Gem . instance_variable_set :@ruby_api_version , nil
2014-01-06 20:19:28 -05:00
RbConfig :: CONFIG [ 'ruby_version' ] = orig_ruby_version
2013-10-15 20:14:16 -04:00
end
2016-03-27 22:26:39 -04:00
def test_self_env_requirement
ENV [ " GEM_REQUIREMENT_FOO " ] = '>= 1.2.3'
ENV [ " GEM_REQUIREMENT_BAR " ] = '1.2.3'
ENV [ " GEM_REQUIREMENT_BAZ " ] = 'abcd'
assert_equal Gem :: Requirement . create ( '>= 1.2.3' ) , Gem . env_requirement ( 'foo' )
assert_equal Gem :: Requirement . create ( '1.2.3' ) , Gem . env_requirement ( 'bAr' )
assert_raises ( Gem :: Requirement :: BadRequirementError ) { Gem . env_requirement ( 'baz' ) }
assert_equal Gem :: Requirement . default , Gem . env_requirement ( 'qux' )
end
2018-11-21 05:20:47 -05:00
def test_self_ruby_version_with_patchlevel_less_ancient_rubies
2009-06-09 17:38:59 -04:00
util_set_RUBY_VERSION '1.8.5'
2008-03-31 18:40:06 -04:00
2009-06-09 17:38:59 -04:00
assert_equal Gem :: Version . new ( '1.8.5' ) , Gem . ruby_version
ensure
util_restore_RUBY_VERSION
end
2018-11-21 05:20:47 -05:00
def test_self_ruby_version_with_release
2009-06-09 17:38:59 -04:00
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
2018-11-21 05:20:47 -05:00
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 ( '2.5.0' ) , Gem . ruby_version
ensure
util_restore_RUBY_VERSION
end
2020-01-31 21:14:04 -05:00
def test_self_ruby_version_with_svn_prerelease
2018-11-21 05:20:47 -05:00
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
2020-01-31 21:14:04 -05:00
def test_self_ruby_version_with_git_prerelease
util_set_RUBY_VERSION '2.7.0' , - 1 , 'b563439274a402e33541f5695b1bfd4ac1085638' , 'ruby 2.7.0preview3 (2019-11-23 master b563439274) [x86_64-linux]'
assert_equal Gem :: Version . new ( '2.7.0.preview3' ) , Gem . ruby_version
ensure
util_restore_RUBY_VERSION
end
2018-11-21 05:20:47 -05:00
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
2020-01-31 21:14:04 -05:00
def test_self_ruby_version_with_svn_trunk
2018-11-21 05:20:47 -05:00
util_set_RUBY_VERSION '1.9.2' , - 1 , 23493 , 'ruby 1.9.2dev (2009-05-20 trunk 23493) [x86_64-linux]'
2009-06-09 17:38:59 -04:00
2018-11-21 05:20:47 -05:00
assert_equal Gem :: Version . new ( '1.9.2.dev' ) , Gem . ruby_version
2009-06-09 17:38:59 -04:00
ensure
util_restore_RUBY_VERSION
2008-03-31 18:40:06 -04:00
end
2020-01-31 21:14:04 -05:00
def test_self_ruby_version_with_git_master
util_set_RUBY_VERSION '2.7.0' , - 1 , '5de284ec78220e75643f89b454ce999da0c1c195' , 'ruby 2.7.0dev (2019-12-23T01:37:30Z master 5de284ec78) [x86_64-linux]'
assert_equal Gem :: Version . new ( '2.7.0.dev' ) , Gem . ruby_version
ensure
util_restore_RUBY_VERSION
end
2012-11-29 01:52:18 -05:00
def test_self_rubygems_version
assert_equal Gem :: Version . new ( Gem :: VERSION ) , Gem . rubygems_version
end
2011-05-31 23:45:05 -04:00
def test_self_paths_eq
2008-10-31 18:27:35 -04:00
other = File . join @tempdir , 'other'
path = [ @userhome , other ] . join File :: PATH_SEPARATOR
2011-05-31 23:45:05 -04:00
#
# FIXME remove after fixing test_case
#
ENV [ " GEM_HOME " ] = @gemhome
Gem . paths = { " GEM_PATH " = > path }
2008-10-31 18:27:35 -04:00
2012-11-29 01:52:18 -05:00
assert_equal [ @userhome , other , @gemhome ] , Gem . path
2008-10-31 18:27:35 -04:00
end
2011-05-31 23:45:05 -04:00
def test_self_paths_eq_nonexistent_home
2009-06-09 17:38:59 -04:00
ENV [ 'GEM_HOME' ] = @gemhome
2008-10-31 18:27:35 -04:00
Gem . clear_paths
other = File . join @tempdir , 'other'
ENV [ 'HOME' ] = other
2011-05-31 23:45:05 -04:00
Gem . paths = { " GEM_PATH " = > other }
2008-10-31 18:27:35 -04:00
2012-11-29 01:52:18 -05:00
assert_equal [ other , @gemhome ] , Gem . path
2008-10-31 18:27:35 -04:00
end
2012-11-29 01:52:18 -05:00
def test_self_post_build
assert_equal 1 , Gem . post_build_hooks . length
2020-06-10 13:46:05 -04:00
Gem . post_build { | installer | }
2012-11-29 01:52:18 -05:00
assert_equal 2 , Gem . post_build_hooks . length
end
def test_self_post_install
assert_equal 1 , Gem . post_install_hooks . length
2020-06-10 13:46:05 -04:00
Gem . post_install { | installer | }
2012-11-29 01:52:18 -05:00
assert_equal 2 , Gem . post_install_hooks . length
end
def test_self_done_installing
assert_empty Gem . done_installing_hooks
2020-06-10 13:46:05 -04:00
Gem . done_installing { | gems | }
2012-11-29 01:52:18 -05:00
assert_equal 1 , Gem . done_installing_hooks . length
end
def test_self_post_reset
assert_empty Gem . post_reset_hooks
2020-06-10 13:46:05 -04:00
Gem . post_reset { }
2012-11-29 01:52:18 -05:00
assert_equal 1 , Gem . post_reset_hooks . length
end
def test_self_post_uninstall
assert_equal 1 , Gem . post_uninstall_hooks . length
2020-06-10 13:46:05 -04:00
Gem . post_uninstall { | installer | }
2012-11-29 01:52:18 -05:00
assert_equal 2 , Gem . post_uninstall_hooks . length
end
def test_self_pre_install
assert_equal 1 , Gem . pre_install_hooks . length
2020-06-10 13:46:05 -04:00
Gem . pre_install { | installer | }
2012-11-29 01:52:18 -05:00
assert_equal 2 , Gem . pre_install_hooks . length
end
def test_self_pre_reset
assert_empty Gem . pre_reset_hooks
2020-06-10 13:46:05 -04:00
Gem . pre_reset { }
2012-11-29 01:52:18 -05:00
assert_equal 1 , Gem . pre_reset_hooks . length
end
def test_self_pre_uninstall
assert_equal 1 , Gem . pre_uninstall_hooks . length
2020-06-10 13:46:05 -04:00
Gem . pre_uninstall { | installer | }
2012-11-29 01:52:18 -05:00
assert_equal 2 , Gem . pre_uninstall_hooks . length
2007-11-10 02:48:56 -05:00
end
def test_self_sources
2008-06-17 18:04:18 -04:00
assert_equal %w[ http://gems.example.com/ ] , Gem . sources
2016-09-27 20:57:53 -04:00
Gem . sources = nil
Gem . configuration . sources = %w[ http://test.example.com/ ]
assert_equal %w[ http://test.example.com/ ] , Gem . sources
2007-11-10 02:48:56 -05:00
end
2015-07-01 17:50:14 -04:00
def test_try_activate_returns_true_for_activated_specs
b = util_spec 'b' , '1.0' do | spec |
spec . files << 'lib/b.rb'
end
install_specs b
assert Gem . try_activate ( 'b' ) , 'try_activate should return true'
assert Gem . try_activate ( 'b' ) , 'try_activate should still return true'
end
2016-04-06 02:01:14 -04:00
def test_spec_order_is_consistent
b1 = util_spec 'b' , '1.0'
b2 = util_spec 'b' , '2.0'
b3 = util_spec 'b' , '3.0'
install_specs b1 , b2 , b3
2020-06-10 13:46:05 -04:00
specs1 = Gem :: Specification . stubs . find_all { | s | s . name == 'b' }
2016-04-06 02:01:14 -04:00
Gem :: Specification . reset
specs2 = Gem :: Specification . stubs_for ( 'b' )
assert_equal specs1 . map ( & :version ) , specs2 . map ( & :version )
end
2011-05-31 23:45:05 -04:00
def test_self_try_activate_missing_dep
2015-07-01 17:50:14 -04:00
b = util_spec 'b' , '1.0'
2011-05-31 23:45:05 -04:00
a = util_spec 'a' , '1.0' , 'b' = > '>= 1.0'
2015-07-01 17:50:14 -04:00
install_specs b , a
uninstall_gem b
2011-05-31 23:45:05 -04:00
a_file = File . join a . gem_dir , 'lib' , 'a_file.rb'
write_file a_file do | io |
io . puts '# a_file.rb'
end
2016-04-06 02:01:14 -04:00
e = assert_raises Gem :: MissingSpecError do
2011-05-31 23:45:05 -04:00
Gem . try_activate 'a_file'
end
2020-03-24 14:51:43 -04:00
assert_match %r{ Could not find 'b' } , e . message
2020-04-26 13:55:37 -04:00
assert_match %r{ at: #{ a . spec_file } } , e . message
2007-11-10 02:48:56 -05:00
end
2016-03-03 19:29:40 -05:00
def test_self_try_activate_missing_prerelease
b = util_spec 'b' , '1.0rc1'
a = util_spec 'a' , '1.0rc1' , 'b' = > '1.0rc1'
install_specs b , a
uninstall_gem b
a_file = File . join a . gem_dir , 'lib' , 'a_file.rb'
write_file a_file do | io |
io . puts '# a_file.rb'
end
2016-04-06 02:01:14 -04:00
e = assert_raises Gem :: MissingSpecError do
2016-03-03 19:29:40 -05:00
Gem . try_activate 'a_file'
end
2020-03-24 14:51:43 -04:00
assert_match %r{ Could not find 'b' \ (= 1.0rc1 \ ) } , e . message
2016-03-03 19:29:40 -05:00
end
2014-09-13 23:30:02 -04:00
def test_self_try_activate_missing_extensions
2015-07-01 17:50:14 -04:00
spec = util_spec 'ext' , '1' do | s |
2014-09-13 23:30:02 -04:00
s . extensions = %w[ ext/extconf.rb ]
s . mark_version
s . installed_by_version = v ( '2.2' )
end
2015-07-01 17:50:14 -04:00
# write the spec without install to simulate a failed install
write_file spec . spec_file do | io |
io . write spec . to_ruby_for_cache
end
2014-09-13 23:30:02 -04:00
_ , err = capture_io do
refute Gem . try_activate 'nonexistent'
end
2019-06-01 05:45:11 -04:00
unless Gem . java_platform?
expected = " Ignoring ext-1 because its extensions are not built. " +
" Try: gem pristine ext --version 1 \n "
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , err
end
2014-09-13 23:30:02 -04:00
end
2016-03-03 19:29:40 -05:00
def test_self_use_paths_with_nils
orig_home = ENV . delete 'GEM_HOME'
orig_path = ENV . delete 'GEM_PATH'
Gem . use_paths nil , nil
assert_equal Gem . default_dir , Gem . paths . home
2017-10-07 21:32:18 -04:00
path = ( Gem . default_path + [ Gem . paths . home ] ) . uniq
assert_equal path , Gem . paths . path
2016-03-03 19:29:40 -05:00
ensure
ENV [ 'GEM_HOME' ] = orig_home
ENV [ 'GEM_PATH' ] = orig_path
end
def test_setting_paths_does_not_warn_about_unknown_keys
stdout , stderr = capture_io do
Gem . paths = { 'foo' = > [ ] ,
'bar' = > Object . new ,
'GEM_HOME' = > Gem . paths . home ,
'GEM_PATH' = > 'foo' }
end
assert_equal [ 'foo' , Gem . paths . home ] , Gem . paths . path
assert_equal '' , stderr
assert_equal '' , stdout
end
def test_setting_paths_does_not_mutate_parameter_object
Gem . paths = { 'GEM_HOME' = > Gem . paths . home ,
'GEM_PATH' = > 'foo' } . freeze
assert_equal [ 'foo' , Gem . paths . home ] , Gem . paths . path
end
def test_deprecated_paths =
stdout , stderr = capture_io do
Gem . paths = { 'GEM_HOME' = > Gem . paths . home ,
'GEM_PATH' = > [ Gem . paths . home , 'foo' ] }
end
assert_equal [ Gem . paths . home , 'foo' ] , Gem . paths . path
2016-04-06 02:01:14 -04:00
assert_match ( / Array values in the parameter to `Gem.paths=` are deprecated. \ nPlease use a String or nil /m , stderr )
2016-03-03 19:29:40 -05:00
assert_equal '' , stdout
end
2007-11-10 02:48:56 -05:00
def test_self_use_paths
util_ensure_gem_dirs
Gem . use_paths @gemhome , @additional
assert_equal @gemhome , Gem . dir
2012-11-29 01:52:18 -05:00
assert_equal @additional + [ Gem . dir ] , Gem . path
2007-11-10 02:48:56 -05:00
end
2008-09-25 06:13:50 -04:00
def test_self_user_dir
2013-07-08 18:41:03 -04:00
parts = [ @userhome , '.gem' , Gem . ruby_engine ]
2014-01-06 20:19:28 -05:00
parts << RbConfig :: CONFIG [ 'ruby_version' ] unless RbConfig :: CONFIG [ 'ruby_version' ] . empty?
2013-07-08 18:41:03 -04:00
2020-04-23 06:16:06 -04:00
FileUtils . mkdir_p File . join ( parts )
2013-07-08 18:41:03 -04:00
assert_equal File . join ( parts ) , Gem . user_dir
2008-09-25 06:13:50 -04:00
end
2007-11-10 02:48:56 -05:00
def test_self_user_home
2018-11-21 05:20:47 -05:00
if ENV [ 'HOME' ]
2007-11-10 02:48:56 -05:00
assert_equal ENV [ 'HOME' ] , Gem . user_home
else
assert true , 'count this test'
end
end
2012-11-29 01:52:18 -05:00
def test_self_needs
a = util_spec " a " , " 1 "
b = util_spec " b " , " 1 " , " c " = > nil
c = util_spec " c " , " 2 "
2015-07-01 17:50:14 -04:00
install_specs a , c , b
2012-11-29 01:52:18 -05:00
Gem . needs do | r |
r . gem " a "
r . gem " b " , " = 1 "
end
2020-06-10 13:46:05 -04:00
activated = Gem :: Specification . map { | x | x . full_name }
2012-11-29 01:52:18 -05:00
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-1 c-2 ] , activated . sort
2012-11-29 01:52:18 -05:00
end
def test_self_needs_picks_up_unresolved_deps
save_loaded_features do
a = util_spec " a " , " 1 "
b = util_spec " b " , " 1 " , " c " = > nil
c = util_spec " c " , " 2 "
2019-12-10 04:31:01 -05:00
d = util_spec " d " , " 1 " , { 'e' = > '= 1' } , " lib/d #{ $$ } .rb "
2012-11-29 01:52:18 -05:00
e = util_spec " e " , " 1 "
2015-07-01 17:50:14 -04:00
install_specs a , c , b , e , d
2012-11-29 01:52:18 -05:00
Gem . needs do | r |
r . gem " a "
r . gem " b " , " = 1 "
2019-12-10 04:31:01 -05:00
require " d #{ $$ } "
2012-11-29 01:52:18 -05:00
end
2020-03-24 14:51:43 -04:00
assert_equal %w[ a-1 b-1 c-2 d-1 e-1 ] , loaded_spec_names
2012-11-29 01:52:18 -05:00
end
end
2013-02-12 16:06:36 -05:00
def test_self_gunzip
input = " \x1F \x8B \b \0 \xED \xA3 \x1A Q \0 \x03 \xCB H " +
" \xCD \xC9 \xC9 \a \0 \x86 \xA6 \x10 6 \x05 \0 \0 \0 "
2018-05-17 21:39:13 -04:00
output = Gem :: Util . gunzip input
2013-02-12 16:06:36 -05:00
assert_equal 'hello' , output
assert_equal Encoding :: BINARY , output . encoding
end
def test_self_gzip
input = 'hello'
2018-05-17 21:39:13 -04:00
output = Gem :: Util . gzip input
2013-02-12 16:06:36 -05:00
zipped = StringIO . new output
assert_equal 'hello' , Zlib :: GzipReader . new ( zipped ) . read
assert_equal Encoding :: BINARY , output . encoding
end
2014-09-13 23:30:02 -04:00
def test_self_vendor_dir
2019-06-01 05:45:11 -04:00
vendordir ( File . join ( @tempdir , 'vendor' ) ) do
expected =
File . join RbConfig :: CONFIG [ 'vendordir' ] , 'gems' ,
RbConfig :: CONFIG [ 'ruby_version' ]
2014-09-13 23:30:02 -04:00
2019-06-01 05:45:11 -04:00
assert_equal expected , Gem . vendor_dir
end
2014-09-13 23:30:02 -04:00
end
def test_self_vendor_dir_ENV_GEM_VENDOR
ENV [ 'GEM_VENDOR' ] = File . join @tempdir , 'vendor' , 'gems'
assert_equal ENV [ 'GEM_VENDOR' ] , Gem . vendor_dir
refute Gem . vendor_dir . frozen?
end
def test_self_vendor_dir_missing
2019-06-01 05:45:11 -04:00
vendordir ( nil ) do
assert_nil Gem . vendor_dir
end
2014-09-13 23:30:02 -04:00
end
2010-04-22 04:24:42 -04:00
def test_load_plugins
2011-01-18 19:08:49 -05:00
plugin_path = File . join " lib " , " rubygems_plugin.rb "
Dir . chdir @tempdir do
FileUtils . mkdir_p 'lib'
File . open plugin_path , " w " do | fp |
2013-09-14 04:59:02 -04:00
fp . puts " class TestGem; PLUGINS_LOADED << 'plugin'; end "
2011-01-18 19:08:49 -05:00
end
2013-11-11 19:16:41 -05:00
foo1 = util_spec 'foo' , '1' do | s |
2013-07-22 18:46:50 -04:00
s . files << plugin_path
end
2013-09-14 04:59:02 -04:00
install_gem foo1
2013-11-11 19:16:41 -05:00
foo2 = util_spec 'foo' , '2' do | s |
2013-09-14 04:59:02 -04:00
s . files << plugin_path
end
install_gem foo2
2011-01-18 19:08:49 -05:00
end
2011-05-31 23:45:05 -04:00
Gem :: Specification . reset
2011-01-18 19:08:49 -05:00
gem 'foo'
Gem . load_plugins
2013-09-14 04:59:02 -04:00
assert_equal %w[ plugin ] , PLUGINS_LOADED
2011-01-18 19:08:49 -05:00
end
def test_load_env_plugins
with_plugin ( 'load' ) { Gem . load_env_plugins }
2011-03-07 03:44:45 -05:00
assert_equal :loaded , TEST_PLUGIN_LOAD rescue nil
2010-04-22 04:24:42 -04:00
util_remove_interrupt_command
# Should attempt to cause a StandardError
2011-01-18 19:08:49 -05:00
with_plugin ( 'standarderror' ) { Gem . load_env_plugins }
2011-03-07 03:44:45 -05:00
assert_equal :loaded , TEST_PLUGIN_STANDARDERROR rescue nil
2010-04-22 04:24:42 -04:00
util_remove_interrupt_command
# Should attempt to cause an Exception
2011-01-18 19:08:49 -05:00
with_plugin ( 'exception' ) { Gem . load_env_plugins }
2011-03-07 03:44:45 -05:00
assert_equal :loaded , TEST_PLUGIN_EXCEPTION rescue nil
2010-04-22 04:24:42 -04:00
end
2012-04-17 20:04:12 -04:00
def test_gem_path_ordering
refute_equal Gem . dir , Gem . user_dir
2020-06-10 13:46:05 -04:00
write_file File . join ( @tempdir , 'lib' , " g.rb " ) { | fp | fp . puts " " }
write_file File . join ( @tempdir , 'lib' , 'm.rb' ) { | fp | fp . puts " " }
2012-04-17 20:04:12 -04:00
2018-05-30 09:01:35 -04:00
g = util_spec 'g' , '1' , nil , " lib/g.rb "
m = util_spec 'm' , '1' , nil , " lib/m.rb "
2012-04-17 20:04:12 -04:00
install_gem g , :install_dir = > Gem . dir
m0 = install_gem m , :install_dir = > Gem . dir
m1 = install_gem m , :install_dir = > Gem . user_dir
assert_equal m0 . gem_dir , File . join ( Gem . dir , " gems " , " m-1 " )
assert_equal m1 . gem_dir , File . join ( Gem . user_dir , " gems " , " m-1 " )
tests = [
[ :dir0 , [ Gem . dir , Gem . user_dir ] , m0 ] ,
2020-12-08 02:33:39 -05:00
[ :dir1 , [ Gem . user_dir , Gem . dir ] , m1 ] ,
2012-04-17 20:04:12 -04:00
]
tests . each do | _name , _paths , expected |
2015-01-09 09:20:10 -05:00
Gem . use_paths _paths . first , _paths
2012-04-17 20:04:12 -04:00
Gem :: Specification . reset
Gem . searcher = nil
assert_equal Gem :: Dependency . new ( 'm' , '1' ) . to_specs ,
Gem :: Dependency . new ( 'm' , '1' ) . to_specs . sort
assert_equal \
[ expected . gem_dir ] ,
Gem :: Dependency . new ( 'm' , '1' ) . to_specs . map ( & :gem_dir ) . sort ,
" Wrong specs for #{ _name } "
spec = Gem :: Dependency . new ( 'm' , '1' ) . to_spec
assert_equal \
File . join ( _paths . first , " gems " , " m-1 " ) ,
spec . gem_dir ,
" Wrong spec before require for #{ _name } "
refute spec . activated? , " dependency already activated for #{ _name } "
gem " m "
spec = Gem :: Dependency . new ( 'm' , '1' ) . to_spec
assert spec . activated? , " dependency not activated for #{ _name } "
assert_equal \
File . join ( _paths . first , " gems " , " m-1 " ) ,
spec . gem_dir ,
" Wrong spec after require for #{ _name } "
spec . instance_variable_set :@activated , false
Gem . loaded_specs . delete ( spec . name )
$: . delete ( File . join ( spec . gem_dir , " lib " ) )
end
end
def test_gem_path_ordering_short
2020-06-10 13:46:05 -04:00
write_file File . join ( @tempdir , 'lib' , " g.rb " ) { | fp | fp . puts " " }
write_file File . join ( @tempdir , 'lib' , 'm.rb' ) { | fp | fp . puts " " }
2012-04-17 20:04:12 -04:00
2018-05-30 09:01:35 -04:00
g = util_spec 'g' , '1' , nil , " lib/g.rb "
m = util_spec 'm' , '1' , nil , " lib/m.rb "
2012-04-17 20:04:12 -04:00
install_gem g , :install_dir = > Gem . dir
install_gem m , :install_dir = > Gem . dir
install_gem m , :install_dir = > Gem . user_dir
2015-01-09 09:20:10 -05:00
Gem . use_paths Gem . dir , [ Gem . dir , Gem . user_dir ]
2012-04-17 20:04:12 -04:00
assert_equal \
File . join ( Gem . dir , " gems " , " m-1 " ) ,
Gem :: Dependency . new ( 'm' , '1' ) . to_spec . gem_dir ,
" Wrong spec selected "
end
2012-11-29 01:52:18 -05:00
def test_auto_activation_of_specific_gemdeps_file
2018-05-30 09:01:35 -04:00
a = util_spec " a " , " 1 " , nil , " lib/a.rb "
b = util_spec " b " , " 1 " , nil , " lib/b.rb "
c = util_spec " c " , " 1 " , nil , " lib/c.rb "
2012-11-29 01:52:18 -05:00
install_specs a , b , c
path = File . join @tempdir , " gem.deps.rb "
File . open path , " w " do | f |
f . puts " gem 'a' "
f . puts " gem 'b' "
f . puts " gem 'c' "
end
ENV [ 'RUBYGEMS_GEMDEPS' ] = path
2018-05-17 21:39:13 -04:00
Gem . use_gemdeps
2012-11-29 01:52:18 -05:00
2020-03-24 14:51:43 -04:00
assert_equal add_bundler_full_name ( %W[ a-1 b-1 c-1 ] ) , loaded_spec_names
2012-11-29 01:52:18 -05:00
end
2018-05-17 21:39:13 -04:00
def test_auto_activation_of_used_gemdeps_file
2018-05-30 09:01:35 -04:00
a = util_spec " a " , " 1 " , nil , " lib/a.rb "
b = util_spec " b " , " 1 " , nil , " lib/b.rb "
c = util_spec " c " , " 1 " , nil , " lib/c.rb "
2012-11-29 01:52:18 -05:00
install_specs a , b , c
path = File . join @tempdir , " gem.deps.rb "
File . open path , " w " do | f |
f . puts " gem 'a' "
f . puts " gem 'b' "
f . puts " gem 'c' "
end
ENV [ 'RUBYGEMS_GEMDEPS' ] = " - "
2019-04-26 07:26:21 -04:00
expected_specs = [ a , b , util_spec ( " bundler " , Bundler :: VERSION ) , c ] . compact
2020-06-10 13:46:05 -04:00
assert_equal expected_specs , Gem . use_gemdeps . sort_by { | s | s . name }
2012-11-29 01:52:18 -05:00
end
2020-04-08 14:58:32 -04:00
BUNDLER_LIB_PATH = File . expand_path $LOAD_PATH . find { | lp | File . file? ( File . join ( lp , " bundler.rb " ) ) }
2019-04-26 07:26:21 -04:00
BUNDLER_FULL_NAME = " bundler- #{ Bundler :: VERSION } " . freeze
2017-10-07 21:32:18 -04:00
def add_bundler_full_name ( names )
names << BUNDLER_FULL_NAME
names . sort!
names
end
2012-11-29 01:52:18 -05:00
def test_looks_for_gemdeps_files_automatically_on_start
2019-06-01 05:45:11 -04:00
skip " Requiring bundler messes things up " if Gem . java_platform?
2012-11-29 01:52:18 -05:00
2018-05-30 09:01:35 -04:00
a = util_spec " a " , " 1 " , nil , " lib/a.rb "
b = util_spec " b " , " 1 " , nil , " lib/b.rb "
c = util_spec " c " , " 1 " , nil , " lib/c.rb "
2012-11-29 01:52:18 -05:00
install_specs a , b , c
path = File . join ( @tempdir , " gd-tmp " )
install_gem a , :install_dir = > path
install_gem b , :install_dir = > path
install_gem c , :install_dir = > path
ENV [ 'GEM_PATH' ] = path
ENV [ 'RUBYGEMS_GEMDEPS' ] = " - "
2017-02-03 20:04:14 -05:00
path = File . join @tempdir , " gem.deps.rb "
2020-04-08 15:14:27 -04:00
cmd = [ * ruby_with_rubygems_in_load_path ,
2020-04-08 14:59:24 -04:00
" -I #{ BUNDLER_LIB_PATH } " ]
2018-05-30 09:01:35 -04:00
cmd << " -eputs Gem.loaded_specs.values.map(&:full_name).sort "
2017-02-03 20:04:14 -05:00
File . open path , " w " do | f |
f . puts " gem 'a' "
end
2017-03-16 21:29:22 -04:00
out0 = IO . popen ( cmd , & :read ) . split ( / \ n / )
2017-02-03 20:04:14 -05:00
File . open path , " a " do | f |
f . puts " gem 'b' "
f . puts " gem 'c' "
end
2017-03-16 21:29:22 -04:00
out = IO . popen ( cmd , & :read ) . split ( / \ n / )
2012-11-29 01:52:18 -05:00
2017-02-03 20:04:14 -05:00
assert_equal [ " b-1 " , " c-1 " ] , out - out0
2019-04-26 07:26:21 -04:00
end
2012-11-29 01:52:18 -05:00
2012-11-30 19:42:36 -05:00
def test_looks_for_gemdeps_files_automatically_on_start_in_parent_dir
2019-06-01 05:45:11 -04:00
skip " Requiring bundler messes things up " if Gem . java_platform?
2012-11-30 19:42:36 -05:00
2018-05-30 09:01:35 -04:00
a = util_spec " a " , " 1 " , nil , " lib/a.rb "
b = util_spec " b " , " 1 " , nil , " lib/b.rb "
c = util_spec " c " , " 1 " , nil , " lib/c.rb "
2012-11-30 19:42:36 -05:00
install_specs a , b , c
path = File . join ( @tempdir , " gd-tmp " )
install_gem a , :install_dir = > path
install_gem b , :install_dir = > path
install_gem c , :install_dir = > path
ENV [ 'GEM_PATH' ] = path
ENV [ 'RUBYGEMS_GEMDEPS' ] = " - "
Dir . mkdir " sub1 "
2017-02-03 20:04:14 -05:00
path = File . join @tempdir , " gem.deps.rb "
2020-04-08 15:14:27 -04:00
cmd = [ * ruby_with_rubygems_in_load_path , " -Csub1 " ,
2020-04-08 14:59:24 -04:00
" -I #{ BUNDLER_LIB_PATH } " ]
2018-05-30 09:01:35 -04:00
cmd << " -eputs Gem.loaded_specs.values.map(&:full_name).sort "
2017-02-03 20:04:14 -05:00
File . open path , " w " do | f |
f . puts " gem 'a' "
end
2017-03-16 21:29:22 -04:00
out0 = IO . popen ( cmd , & :read ) . split ( / \ n / )
2017-02-03 20:04:14 -05:00
File . open path , " a " do | f |
f . puts " gem 'b' "
f . puts " gem 'c' "
2012-11-30 19:42:36 -05:00
end
2017-03-16 21:29:22 -04:00
out = IO . popen ( cmd , & :read ) . split ( / \ n / )
2012-11-30 19:42:36 -05:00
Dir . rmdir " sub1 "
2017-02-03 20:04:14 -05:00
assert_equal [ " b-1 " , " c-1 " ] , out - out0
2019-04-26 07:26:21 -04:00
end
2013-07-09 19:41:44 -04:00
2013-09-14 04:59:02 -04:00
def test_register_default_spec
Gem . clear_default_specs
old_style = Gem :: Specification . new do | spec |
spec . files = [ " foo.rb " , " bar.rb " ]
end
Gem . register_default_spec old_style
assert_equal old_style , Gem . find_unresolved_default_spec ( " foo.rb " )
assert_equal old_style , Gem . find_unresolved_default_spec ( " bar.rb " )
2018-05-30 09:01:35 -04:00
assert_nil Gem . find_unresolved_default_spec ( " baz.rb " )
2013-09-14 04:59:02 -04:00
Gem . clear_default_specs
new_style = Gem :: Specification . new do | spec |
spec . files = [ " lib/foo.rb " , " ext/bar.rb " , " bin/exec " , " README " ]
spec . require_paths = [ " lib " , " ext " ]
end
Gem . register_default_spec new_style
assert_equal new_style , Gem . find_unresolved_default_spec ( " foo.rb " )
assert_equal new_style , Gem . find_unresolved_default_spec ( " bar.rb " )
2018-05-30 09:01:35 -04:00
assert_nil Gem . find_unresolved_default_spec ( " exec " )
assert_nil Gem . find_unresolved_default_spec ( " README " )
2013-09-14 04:59:02 -04:00
end
[rubygems/rubygems] Fix incorrect bundler version being required
In ruby 2.7.0, there's a slight change in bundler's default gemspec file
where the executable folder of the bundler gem is `libexec` instead of
`exe`. I made that change in https://github.com/ruby/ruby/pull/2380 to
try to simplify the integration of the `bundler` gem with upstream,
minimizing the number of changes that need to be made to the gemspec to
adapt to the structure of ruby-core.
That worked ok, expected for this issue. The new name of the folder
including the executable files uncovered a bug in rubygems, which is the
following:
* In order to be able to use newer versions of default gems, `rubygems`
ships with a customized `require` that has knowledge about which files
belong to which default gem. If one of these files is required,
`rubygems` will detect that and activate its gem mechanism to choose the
newest version of the corresponding default gem present in the system
(unless a different version has already been activated). It's this part
of the custom require:
https://github.com/rubygems/rubygems/blob/ea3e6f194df500b028b52b3b64decbd3df1d5ab0/lib/rubygems/core_ext/kernel_require.rb#L77-L85
* In order to do that, `rubygems` registers a map of default gems and
their files when it is first required:
https://github.com/rubygems/rubygems/blob/ea3e6f194df500b028b52b3b64decbd3df1d5ab0/lib/rubygems.rb#L1247-L1276
As explained in the method's header, two types of default gem
specifications are supported. One of the formats is the style used by
some ruby-core gemspec files, where paths inside the `spec.files` array
don't include the `spec.require_paths` part. So in this "old style", if
a gem ships with a `lib/bundler.rb` file, it will be registered in this
array as `spec.files = ["bundler.rb"]`, not as `spec.files =
["lib/bundler.rb"]`. The `Gem.register_default_spec` method "detects"
this style by making sure that none of the files in the `spec.files`
array start with any of the `spec.require_paths`.
* Since in ruby 2.7 the default specification file of the `bundler` gem
includes a `libexec/bundle` file, this check would no longer work
correctly, because even though the specification file is still "old
style", it has one registered file which starts with "lib", one of the
"require paths" of the gem.
* This means that the gem is incorrectly detected as "new style", but
since none of the paths start with "lib/", no files are actually
registered, so the gem is not being considered a default gem, and thus
the default version is always used with no possibility of being
"upgraded".
The fix of the problem is simple: check that no files start with `lib/`
(or any other require paths), instead of with "lib" which doesn't
exclude other potential "non requirable folder" starting with lib, like
in the `bundler` case.
https://github.com/rubygems/rubygems/commit/94df740c2b
2020-03-29 15:45:39 -04:00
def test_register_default_spec_old_style_with_folder_starting_with_lib
Gem . clear_default_specs
old_style = Gem :: Specification . new do | spec |
spec . files = [ " libexec/bundle " , " foo.rb " , " bar.rb " ]
end
Gem . register_default_spec old_style
assert_equal old_style , Gem . find_unresolved_default_spec ( " foo.rb " )
end
2013-11-21 18:27:30 -05:00
def test_use_gemdeps
2019-11-11 01:03:57 -05:00
gem_deps_file = 'gem.deps.rb' . tap ( & Gem :: UNTAINT )
2014-09-13 23:30:02 -04:00
spec = util_spec 'a' , 1
2015-07-01 17:50:14 -04:00
install_specs spec
2014-09-13 23:30:02 -04:00
2020-06-10 13:46:05 -04:00
spec = Gem :: Specification . find { | s | s == spec }
2014-09-13 23:30:02 -04:00
refute spec . activated?
2018-02-05 21:58:35 -05:00
File . open gem_deps_file , 'w' do | io |
2014-09-13 23:30:02 -04:00
io . write 'gem "a"'
end
2015-07-01 17:50:14 -04:00
assert_nil Gem . gemdeps
2014-09-13 23:30:02 -04:00
Gem . use_gemdeps gem_deps_file
2020-03-24 14:51:43 -04:00
assert_equal add_bundler_full_name ( %W[ a-1 ] ) , loaded_spec_names
2015-07-01 17:50:14 -04:00
refute_nil Gem . gemdeps
2014-09-13 23:30:02 -04:00
end
def test_use_gemdeps_ENV
2013-11-21 18:27:30 -05:00
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , nil
spec = util_spec 'a' , 1
refute spec . activated?
2018-02-05 21:58:35 -05:00
File . open 'gem.deps.rb' , 'w' do | io |
2013-11-21 18:27:30 -05:00
io . write 'gem "a"'
end
Gem . use_gemdeps
2013-11-22 13:53:21 -05:00
refute spec . activated?
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2014-09-13 23:30:02 -04:00
def test_use_gemdeps_argument_missing
e = assert_raises ArgumentError do
Gem . use_gemdeps 'gem.deps.rb'
end
assert_equal 'Unable to find gem dependencies file at gem.deps.rb' ,
e . message
end
def test_use_gemdeps_argument_missing_match_ENV
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] =
ENV [ 'RUBYGEMS_GEMDEPS' ] , 'gem.deps.rb'
e = assert_raises ArgumentError do
Gem . use_gemdeps 'gem.deps.rb'
end
assert_equal 'Unable to find gem dependencies file at gem.deps.rb' ,
e . message
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2013-11-22 13:53:21 -05:00
def test_use_gemdeps_automatic
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , '-'
spec = util_spec 'a' , 1
2015-07-01 17:50:14 -04:00
install_specs spec
2020-06-10 13:46:05 -04:00
spec = Gem :: Specification . find { | s | s == spec }
2013-11-22 13:53:21 -05:00
refute spec . activated?
2018-02-05 21:58:35 -05:00
File . open 'Gemfile' , 'w' do | io |
2013-11-22 13:53:21 -05:00
io . write 'gem "a"'
end
Gem . use_gemdeps
2020-03-24 14:51:43 -04:00
assert_equal add_bundler_full_name ( %W[ a-1 ] ) , loaded_spec_names
2013-11-21 18:27:30 -05:00
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2014-09-13 23:30:02 -04:00
def test_use_gemdeps_automatic_missing
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , '-'
Gem . use_gemdeps
assert true # count
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2013-11-21 18:27:30 -05:00
def test_use_gemdeps_disabled
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , ''
spec = util_spec 'a' , 1
refute spec . activated?
2018-02-05 21:58:35 -05:00
File . open 'gem.deps.rb' , 'w' do | io |
2013-11-21 18:27:30 -05:00
io . write 'gem "a"'
end
Gem . use_gemdeps
refute spec . activated?
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2014-09-13 23:30:02 -04:00
def test_use_gemdeps_missing_gem
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , 'x'
2018-02-05 21:58:35 -05:00
File . open 'x' , 'w' do | io |
2014-09-13 23:30:02 -04:00
io . write 'gem "a"'
end
2019-04-26 07:26:21 -04:00
expected = <<-EXPECTED
2021-02-01 10:17:16 -05:00
Could not find gem 'a' in locally installed gems .
The source does not contain any versions of 'a'
2014-09-13 23:30:02 -04:00
You may need to ` gem install -g ` to install missing gems
2019-04-26 07:26:21 -04:00
EXPECTED
2014-09-13 23:30:02 -04:00
2019-06-20 07:10:06 -04:00
Gem :: Deprecate . skip_during do
assert_output nil , expected do
Gem . use_gemdeps
end
2014-09-13 23:30:02 -04:00
end
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
2019-04-26 07:26:21 -04:00
end
2014-09-13 23:30:02 -04:00
2013-11-21 18:27:30 -05:00
def test_use_gemdeps_specific
rubygems_gemdeps , ENV [ 'RUBYGEMS_GEMDEPS' ] = ENV [ 'RUBYGEMS_GEMDEPS' ] , 'x'
spec = util_spec 'a' , 1
2015-07-01 17:50:14 -04:00
install_specs spec
2013-11-21 18:27:30 -05:00
2020-06-10 13:46:05 -04:00
spec = Gem :: Specification . find { | s | s == spec }
2013-11-21 18:27:30 -05:00
refute spec . activated?
2018-02-05 21:58:35 -05:00
File . open 'x' , 'w' do | io |
2013-11-21 18:27:30 -05:00
io . write 'gem "a"'
end
Gem . use_gemdeps
2020-03-24 14:51:43 -04:00
assert_equal add_bundler_full_name ( %W[ a-1 ] ) , loaded_spec_names
2013-11-21 18:27:30 -05:00
ensure
ENV [ 'RUBYGEMS_GEMDEPS' ] = rubygems_gemdeps
end
2018-05-30 09:01:35 -04:00
def test_operating_system_defaults
operating_system_defaults = Gem . operating_system_defaults
assert operating_system_defaults != nil
assert operating_system_defaults . is_a? Hash
end
2016-06-24 00:13:11 -04:00
def test_platform_defaults
platform_defaults = Gem . platform_defaults
assert platform_defaults != nil
assert platform_defaults . is_a? Hash
end
2020-03-24 02:39:24 -04:00
# Ensure that `Gem.source_date_epoch` is consistent even if
# $SOURCE_DATE_EPOCH has not been set.
def test_default_source_date_epoch_doesnt_change
old_epoch = ENV [ 'SOURCE_DATE_EPOCH' ]
ENV [ 'SOURCE_DATE_EPOCH' ] = nil
# Unfortunately, there is no real way to test this aside from waiting
# enough for `Time.now.to_i` to change -- which is a whole second.
#
# Fortunately, we only need to do this once.
a = Gem . source_date_epoch
sleep 1
b = Gem . source_date_epoch
assert_equal a , b
ensure
ENV [ 'SOURCE_DATE_EPOCH' ] = old_epoch
end
2018-11-21 05:20:47 -05:00
def ruby_install_name ( name )
2020-01-31 21:14:04 -05:00
with_clean_path_to_ruby do
orig_RUBY_INSTALL_NAME = RbConfig :: CONFIG [ 'ruby_install_name' ]
RbConfig :: CONFIG [ 'ruby_install_name' ] = name
2014-02-03 19:48:31 -05:00
2020-01-31 21:14:04 -05:00
begin
yield
ensure
if orig_RUBY_INSTALL_NAME
RbConfig :: CONFIG [ 'ruby_install_name' ] = orig_RUBY_INSTALL_NAME
else
RbConfig :: CONFIG . delete 'ruby_install_name'
end
end
2014-02-03 19:48:31 -05:00
end
end
2020-03-24 02:39:24 -04:00
def with_rb_config_ruby ( path )
rb_config_singleton_class = class << RbConfig ; self ; end
orig_path = RbConfig . ruby
redefine_method ( rb_config_singleton_class , :ruby , path )
yield
ensure
redefine_method ( rb_config_singleton_class , :ruby , orig_path )
end
def redefine_method ( base , method , new_result )
if RUBY_VERSION > = " 2.5 "
base . alias_method ( method , method )
base . define_method ( method ) { new_result }
else
base . send ( :alias_method , method , method )
base . send ( :define_method , method ) { new_result }
2019-12-19 21:50:32 -05:00
end
2019-04-02 07:48:18 -04:00
end
2010-04-22 04:24:42 -04:00
def with_plugin ( path )
2011-03-07 03:44:45 -05:00
test_plugin_path = File . expand_path ( " test/rubygems/plugin/ #{ path } " ,
2019-07-24 00:57:59 -04:00
PROJECT_DIR )
2010-04-22 04:24:42 -04:00
# A single test plugin should get loaded once only, in order to preserve
# sane test semantics.
refute_includes $LOAD_PATH , test_plugin_path
$LOAD_PATH . unshift test_plugin_path
capture_io do
yield
end
ensure
$LOAD_PATH . delete test_plugin_path
end
2007-11-10 02:48:56 -05:00
def util_ensure_gem_dirs
Gem . ensure_gem_subdirectories @gemhome
2011-05-31 23:45:05 -04:00
#
# FIXME what does this solve precisely? -ebh
#
2007-11-10 02:48:56 -05:00
@additional . each do | dir |
Gem . ensure_gem_subdirectories @gemhome
end
end
2009-06-09 17:38:59 -04:00
def util_exec_gem
2013-11-11 19:16:41 -05:00
spec , _ = util_spec 'a' , '4' do | s |
2009-06-09 17:38:59 -04:00
s . executables = [ 'exec' , 'abin' ]
end
@exec_path = File . join spec . full_gem_path , spec . bindir , 'exec'
@abin_path = File . join spec . full_gem_path , spec . bindir , 'abin'
2015-07-01 17:50:14 -04:00
spec
2009-06-09 17:38:59 -04:00
end
2010-04-22 04:24:42 -04:00
def util_remove_interrupt_command
Gem :: Commands . send :remove_const , :InterruptCommand if
Gem :: Commands . const_defined? :InterruptCommand
end
2011-05-31 23:45:05 -04:00
def util_cache_dir
File . join Gem . dir , " cache "
end
2007-11-10 02:48:56 -05:00
end