@@ -19,7 +19,7 @@ def initialize(fixture_path)
1919 File . open ( fixture_path ) do |fixture |
2020 JSON . load ( fixture ) . tap do |test_case |
2121 self . name = test_case [ 'name' ]
22- self . index = TestIndex . new ( test_case [ 'index' ] || 'awesome' )
22+ self . index = TestIndex . from_fixture ( test_case [ 'index' ] || 'awesome' )
2323 self . requested = test_case [ 'requested' ] . map do |( name , reqs ) |
2424 VersionKit ::Dependency . new name , reqs . split ( ',' ) . map ( &:chomp )
2525 end
@@ -89,8 +89,9 @@ def initialize(fixture_path)
8989 end
9090
9191 describe 'in general' do
92+ let ( :index ) { TestIndex . from_fixture ( 'awesome' ) }
9293 before do
93- @resolver = described_class . new ( TestIndex . new ( 'awesome' ) , TestUI . new )
94+ @resolver = described_class . new ( index , TestUI . new )
9495 end
9596
9697 it 'can resolve a list of 0 requirements' do
@@ -121,6 +122,61 @@ def initialize(fixture_path)
121122 expect ( resolved . map ( &:payload ) . map ( &:to_s ) ) . to eq ( [ 'actionpack (1.2.3)' ] )
122123 end
123124
125+ describe 'optional dependencies' do
126+ let ( :index ) do
127+ TestIndex . new (
128+ 'no_deps' => [
129+ TestSpecification . new ( 'name' => 'no_deps' , 'version' => '1.0' ) ,
130+ TestSpecification . new ( 'name' => 'no_deps' , 'version' => '2.0' ) ,
131+ TestSpecification . new ( 'name' => 'no_deps' , 'version' => '3.0' ) ,
132+ ] ,
133+ 'strong_dep' => [
134+ TestSpecification . new ( 'name' => 'strong_dep' , 'version' => '1.0' ,
135+ 'dependencies' => { 'no_deps' => '< 3' } ) ,
136+ ] ,
137+ 'weak_dep' => [
138+ TestSpecification . new ( 'name' => 'weak_dep' , 'version' => '1.0' ,
139+ 'dependencies' => { 'no_deps' => '< 2 optional' } ) ,
140+ ]
141+ )
142+ end
143+
144+ let ( :no_deps ) { VersionKit ::Dependency . new ( 'no_deps' , '> 1' ) }
145+ let ( :weak_dep ) { VersionKit ::Dependency . new ( 'weak_dep' , '>= 0' ) }
146+ let ( :strong_dep ) { VersionKit ::Dependency . new ( 'strong_dep' , '>= 0' ) }
147+
148+ it 'ignores optional explicit dependencies' do
149+ no_deps . optional = true
150+ expect ( @resolver . resolve ( [ no_deps ] , DependencyGraph . new ) . map ( &:payload ) . compact ) . to be_empty
151+ end
152+
153+ it 'ignores nested optional dependencies' do
154+ expect ( @resolver . resolve ( [ weak_dep ] , DependencyGraph . new ) . map ( &:payload ) . compact . map ( &:to_s ) ) . to eq [
155+ 'weak_dep (1.0.0)' ,
156+ 'no_deps (1.0.0)' ,
157+ ]
158+ end
159+
160+ it 'uses nested optional dependencies' do
161+ expect ( @resolver . resolve ( [ weak_dep , strong_dep ] , DependencyGraph . new ) . map ( &:payload ) . compact . map ( &:to_s ) ) .
162+ to eq [
163+ 'weak_dep (1.0.0)' ,
164+ 'strong_dep (1.0.0)' ,
165+ 'no_deps (1.0.0)' ,
166+ ]
167+ end
168+
169+ it 'raises when an optional dependency conflicts' do
170+ resolve = proc { @resolver . resolve ( [ weak_dep , no_deps ] , DependencyGraph . new ) }
171+ expect ( &resolve ) . to raise_error ( Molinillo ::VersionConflict , <<-EOS . strip )
172+ Unable to satisfy the following requirements:
173+
174+ - `no_deps (> 1.0.0)` required by `user-specified dependency`
175+ - `no_deps (< 2.0.0)` required by `weak_dep (1.0.0)`
176+ EOS
177+ end
178+ end
179+
124180 # Regression test. See: https://github.com/CocoaPods/Molinillo/pull/38
125181 it 'can resolve when swapping children with successors' do
126182 swap_child_with_successors_index = Class . new ( TestIndex ) do
@@ -144,7 +200,7 @@ def versions_of(dependency_name)
144200 end
145201 end
146202
147- index = swap_child_with_successors_index . new ( 'swap_child_with_successors' )
203+ index = swap_child_with_successors_index . from_fixture ( 'swap_child_with_successors' )
148204 @resolver = described_class . new ( index , TestUI . new )
149205 demands = [
150206 VersionKit ::Dependency . new ( 'build-essential' , '>= 0.0.0' ) ,
0 commit comments