2016-02-01 07:43:26 -05:00
# frozen_string_literal: true
2011-01-28 18:46:47 -05:00
require 'rubygems/test_case'
2010-02-21 21:52:35 -05:00
require 'rubygems/dependency'
2007-11-10 02:48:56 -05:00
2011-01-28 18:46:47 -05:00
class TestGemDependency < Gem :: TestCase
2014-09-13 23:30:02 -04:00
2007-11-10 02:48:56 -05:00
def test_initialize
2010-02-21 21:52:35 -05:00
d = dep " pkg " , " > 1.0 "
assert_equal " pkg " , d . name
assert_equal req ( " > 1.0 " ) , d . requirement
2007-11-10 02:48:56 -05:00
end
2012-11-29 01:52:18 -05:00
def test_initialize_type_bad
e = assert_raises ArgumentError do
Gem :: Dependency . new 'monkey' = > '1.0'
end
assert_equal 'dependency name must be a String, was {"monkey"=>"1.0"}' ,
e . message
end
2007-11-10 02:48:56 -05:00
def test_initialize_double
2010-02-21 21:52:35 -05:00
d = dep " pkg " , " > 1.0 " , " < 2.0 "
assert_equal req ( " > 1.0 " , " < 2.0 " ) , d . requirement
2007-11-10 02:48:56 -05:00
end
def test_initialize_empty
2010-02-21 21:52:35 -05:00
d = dep " pkg "
assert_equal req ( " >= 0 " ) , d . requirement
2007-11-10 02:48:56 -05:00
end
2014-09-13 23:30:02 -04:00
def test_initialize_prerelease
d = dep 'd' , '1.a'
assert d . prerelease?
d = dep 'd' , '= 1.a'
assert d . prerelease?
end
2010-02-21 21:52:35 -05:00
def test_initialize_type
assert_equal :runtime , dep ( " pkg " ) . type
assert_equal :development , dep ( " pkg " , [ ] , :development ) . type
2008-06-17 18:04:18 -04:00
2008-10-25 18:58:43 -04:00
assert_raises ArgumentError do
2010-02-21 21:52:35 -05:00
dep " pkg " , :sometimes
2008-06-17 18:04:18 -04:00
end
end
2010-02-21 21:52:35 -05:00
def test_initialize_version
d = dep " pkg " , v ( " 2 " )
assert_equal req ( " = 2 " ) , d . requirement
end
2007-11-10 02:48:56 -05:00
def test_equals2
2010-02-21 21:52:35 -05:00
o = dep " other "
d = dep " pkg " , " > 1.0 "
d1 = dep " pkg " , " > 1.1 "
assert_equal d , d . dup
assert_equal d . dup , d
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
refute_equal d , d1
refute_equal d1 , d
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
refute_equal d , o
refute_equal o , d
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
refute_equal d , Object . new
refute_equal Object . new , d
2007-11-10 02:48:56 -05:00
end
2008-06-17 18:04:18 -04:00
def test_equals2_type
2010-02-21 21:52:35 -05:00
refute_equal dep ( " pkg " , :runtime ) , dep ( " pkg " , :development )
2008-06-17 18:04:18 -04:00
end
def test_equals_tilde
2010-02-21 21:52:35 -05:00
d = dep " a " , " 0 "
2009-06-09 17:38:59 -04:00
2011-01-28 18:46:47 -05:00
assert_match d , d , " match self "
2010-02-21 21:52:35 -05:00
assert_match dep ( " a " , " >= 0 " ) , d , " match version exact "
assert_match dep ( " a " , " >= 0 " ) , dep ( " a " , " 1 " ) , " match version "
refute_match dep ( " a " ) , Object . new
2011-05-31 23:45:05 -04:00
2011-10-03 18:15:47 -04:00
Gem :: Deprecate . skip_during do
2011-05-31 23:45:05 -04:00
assert_match dep ( / a / , " >= 0 " ) , d , " match simple regexp "
assert_match dep ( / a|b / , " >= 0 " ) , d , " match scary regexp "
refute_match dep ( / a / ) , dep ( " b " )
end
2010-02-21 21:52:35 -05:00
end
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
def test_equals_tilde_escape
refute_match dep ( " a|b " ) , dep ( " a " , " 1 " )
2011-10-03 18:15:47 -04:00
Gem :: Deprecate . skip_during do
2011-05-31 23:45:05 -04:00
assert_match dep ( / a|b / ) , dep ( " a " , " 1 " )
end
2009-06-09 17:38:59 -04:00
end
def test_equals_tilde_object
2010-02-21 21:52:35 -05:00
o = Object . new
def o . name ; 'a' end
def o . version ; '0' end
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
assert_match dep ( " a " ) , o
2009-06-09 17:38:59 -04:00
end
def test_equals_tilde_spec
2010-02-21 21:52:35 -05:00
assert_match dep ( " a " , " >= 0 " ) , spec ( " a " , " 0 " )
assert_match dep ( " a " , " 1 " ) , spec ( " a " , " 1 " )
2011-10-03 18:15:47 -04:00
Gem :: Deprecate . skip_during do
2011-05-31 23:45:05 -04:00
assert_match dep ( / a / , " >= 0 " ) , spec ( " a " , " 0 " )
assert_match dep ( / a|b / , " >= 0 " ) , spec ( " b " , " 0 " )
refute_match dep ( / a / , " >= 0 " ) , spec ( " b " , " 0 " )
end
2010-02-21 21:52:35 -05:00
end
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
def test_hash
d = dep " pkg " , " 1.0 "
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
assert_equal d . hash , d . dup . hash
assert_equal d . dup . hash , d . hash
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
refute_equal dep ( " pkg " , " 1.0 " ) . hash , dep ( " pkg " , " 2.0 " ) . hash , " requirement "
refute_equal dep ( " pkg " , " 1.0 " ) . hash , dep ( " abc " , " 1.0 " ) . hash , " name "
refute_equal dep ( " pkg " , :development ) , dep ( " pkg " , :runtime ) , " type "
end
2008-06-17 18:04:18 -04:00
2014-09-13 23:30:02 -04:00
def test_match_eh_name_tuple
a_dep = dep 'a'
a_tup = Gem :: NameTuple . new 'a' , 1
b_tup = Gem :: NameTuple . new 'b' , 2
c_tup = Gem :: NameTuple . new 'c' , '2.a'
assert a_dep . match? a_tup
refute a_dep . match? b_tup
b_dep = dep 'b' , '>= 3'
refute b_dep . match? b_tup
c_dep = dep 'c' , '>= 1'
refute c_dep . match? c_tup
c_dep = dep 'c'
refute c_dep . match? c_tup
c_dep = dep 'c' , '2.a'
assert c_dep . match? c_tup
end
def test_match_eh_allow_prerelease
a_dep = dep 'a'
a_tup = Gem :: NameTuple . new 'a' , 1
b_tup = Gem :: NameTuple . new 'b' , 2
c_tup = Gem :: NameTuple . new 'c' , '2.a'
assert a_dep . match? a_tup , nil , true
refute a_dep . match? b_tup , nil , true
b_dep = dep 'b' , '>= 3'
refute b_dep . match? b_tup , nil , true
c_dep = dep 'c' , '>= 1'
assert c_dep . match? c_tup , nil , true
c_dep = dep 'c'
assert c_dep . match? c_tup , nil , true
c_dep = dep 'c' , '2.a'
assert c_dep . match? c_tup , nil , true
end
def test_match_eh_specification
a_dep = dep 'a'
a_spec = util_spec 'a' , 1
b_spec = util_spec 'b' , 2
c_spec = util_spec 'c' , '2.a'
assert a_dep . match? a_spec
refute a_dep . match? b_spec
b_dep = dep 'b' , '>= 3'
refute b_dep . match? b_spec
c_dep = dep 'c' , '>= 1'
refute c_dep . match? c_spec
c_dep = dep 'c'
refute c_dep . match? c_spec
c_dep = dep 'c' , '2.a'
assert c_dep . match? c_spec
end
def test_matches_spec_eh
spec = util_spec 'b' , 2
refute dep ( 'a' ) . matches_spec? ( spec ) , 'name mismatch'
assert dep ( 'b' ) . matches_spec? ( spec ) , 'name match'
refute dep ( 'b' , '= 1' ) . matches_spec? ( spec ) , 'requirement mismatch'
assert dep ( 'b' , '~> 2' ) . matches_spec? ( spec ) , 'requirement match'
end
def test_matches_spec_eh_prerelease
spec = util_spec 'b' , '2.1.a'
refute dep ( 'a' ) . matches_spec? ( spec ) , 'name mismatch'
assert dep ( 'b' ) . matches_spec? ( spec ) , 'name match'
refute dep ( 'b' , '= 1' ) . matches_spec? ( spec ) , 'requirement mismatch'
assert dep ( 'b' , '~> 2' ) . matches_spec? ( spec ) , 'requirement match'
assert dep ( 'b' , '~> 2.a' ) . matches_spec? ( spec ) , 'prerelease requirement'
end
2011-03-07 03:44:45 -05:00
def test_merge
a1 = dep 'a' , '~> 1.0'
a2 = dep 'a' , '= 1.0'
a3 = a1 . merge a2
assert_equal dep ( 'a' , '~> 1.0' , '= 1.0' ) , a3
end
def test_merge_default
a1 = dep 'a'
a2 = dep 'a' , '1'
a3 = a1 . merge a2
assert_equal dep ( 'a' , '1' ) , a3
end
def test_merge_name_mismatch
a = dep 'a'
b = dep 'b'
e = assert_raises ArgumentError do
a . merge b
end
assert_equal 'a (>= 0) and b (>= 0) have different names' ,
e . message
end
def test_merge_other_default
a1 = dep 'a' , '1'
a2 = dep 'a'
a3 = a1 . merge a2
assert_equal dep ( 'a' , '1' ) , a3
end
2010-02-21 21:52:35 -05:00
def test_prerelease_eh
d = dep " pkg " , " = 1 "
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
refute d . prerelease?
2009-06-09 17:38:59 -04:00
2010-02-21 21:52:35 -05:00
d . prerelease = true
2008-06-17 18:04:18 -04:00
2010-02-21 21:52:35 -05:00
assert d . prerelease?
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
d = dep " pkg " , " = 1.a "
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
assert d . prerelease?
d . prerelease = false
assert d . prerelease?
2007-11-10 02:48:56 -05:00
2010-02-21 21:52:35 -05:00
d = dep " pkg " , " > 1.a " , " > 2 "
2008-06-17 18:04:18 -04:00
2010-02-21 21:52:35 -05:00
assert d . prerelease?
2008-06-17 18:04:18 -04:00
end
2011-05-31 23:45:05 -04:00
def test_specific
refute dep ( 'a' , '> 1' ) . specific?
assert dep ( 'a' , '= 1' ) . specific?
end
2014-09-13 23:30:02 -04:00
def test_to_spec
2015-07-01 17:50:14 -04:00
a_1 = util_spec 'a' , '1'
2014-09-13 23:30:02 -04:00
a_2 = util_spec 'a' , '2'
a_dep = dep 'a' , '>= 0'
2015-07-01 17:50:14 -04:00
install_specs a_1 , a_2
2014-09-13 23:30:02 -04:00
assert_equal a_2 , a_dep . to_spec
end
def test_to_spec_prerelease
a_1 = util_spec 'a' , '1'
a_1_1_a = util_spec 'a' , '1.1.a'
a_dep = dep 'a' , '>= 0'
2015-07-01 17:50:14 -04:00
install_specs a_1 , a_1_1_a
2014-09-13 23:30:02 -04:00
assert_equal a_1 , a_dep . to_spec
a_pre_dep = dep 'a' , '>= 0'
a_pre_dep . prerelease = true
assert_equal a_1_1_a , a_pre_dep . to_spec
end
2012-11-29 01:52:18 -05:00
def test_to_specs_suggests_other_versions
2015-07-01 17:50:14 -04:00
a = util_spec 'a' , '1.0'
install_specs a
2012-11-29 01:52:18 -05:00
a_file = File . join a . gem_dir , 'lib' , 'a_file.rb'
write_file a_file do | io |
io . puts '# a_file.rb'
end
dep = Gem :: Dependency . new " a " , " = 2.0 "
2016-04-06 02:01:14 -04:00
e = assert_raises Gem :: MissingSpecVersionError do
2012-11-29 01:52:18 -05:00
dep . to_specs
end
2014-09-13 23:30:02 -04:00
assert_match " Could not find 'a' (= 2.0) - did find: [a-1.0] " , e . message
2012-11-29 01:52:18 -05:00
end
2017-10-07 21:32:18 -04:00
def test_to_specs_respects_bundler_version
b = util_spec 'bundler' , '2.0.0.pre.1'
b_1 = util_spec 'bundler' , '1'
install_specs b , b_1
b_file = File . join b . gem_dir , 'lib' , 'bundler' , 'setup.rb'
write_file b_file do | io |
io . puts '# setup.rb'
end
dep = Gem :: Dependency . new " bundler " , " >= 0.a "
assert_equal [ b , b_1 ] , dep . to_specs
Gem :: BundlerVersionFinder . stub ( :bundler_version_with_reason , [ " 3.5 " , " reason " ] ) do
e = assert_raises Gem :: MissingSpecVersionError do
dep . to_specs
end
2018-11-21 05:20:47 -05:00
assert_match " Could not find 'bundler' (3.5) required by reason. \n To update to the latest version installed on your system, run `bundle update --bundler`. \n To install the missing version, run `gem install bundler:3.5` \n " , e . message
2017-10-07 21:32:18 -04:00
end
Gem :: BundlerVersionFinder . stub ( :bundler_version_with_reason , [ " 2.0.0.pre.1 " , " reason " ] ) do
assert_equal [ b ] , dep . to_specs
end
end
2012-11-29 01:52:18 -05:00
def test_to_specs_indicates_total_gem_set_size
2015-07-01 17:50:14 -04:00
a = util_spec 'a' , '1.0'
install_specs a
2012-11-29 01:52:18 -05:00
a_file = File . join a . gem_dir , 'lib' , 'a_file.rb'
write_file a_file do | io |
io . puts '# a_file.rb'
end
dep = Gem :: Dependency . new " b " , " = 2.0 "
2016-04-06 02:01:14 -04:00
e = assert_raises Gem :: MissingSpecError do
2012-11-29 01:52:18 -05:00
dep . to_specs
end
2014-09-13 23:30:02 -04:00
assert_match " Could not find 'b' (= 2.0) among 1 total gem(s) " , e . message
2012-11-29 01:52:18 -05:00
end
2007-11-10 02:48:56 -05:00
end