1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
require 'spec_helper'
require 'semantic_puppet/dependency'
describe SemanticPuppet::Dependency do
def create_release(source, name, version, deps = {})
SemanticPuppet::Dependency::ModuleRelease.new(
source,
name,
SemanticPuppet::Version.parse(version),
Hash[deps.map { |k, v| [k, SemanticPuppet::VersionRange.parse(v) ] }]
)
end
describe '.sources' do
it 'defaults to an empty list' do
expect(subject.sources).to be_empty
end
it 'is frozen' do
expect(subject.sources).to be_frozen
end
it 'can be modified by using #add_source' do
subject.add_source(SemanticPuppet::Dependency::Source.new)
expect(subject.sources).to_not be_empty
end
it 'can be emptied by using #clear_sources' do
subject.add_source(SemanticPuppet::Dependency::Source.new)
subject.clear_sources
expect(subject.sources).to be_empty
end
end
describe '.query' do
context 'without sources' do
it 'returns an unsatisfied ModuleRelease' do
expect(subject.query('module_name' => '1.0.0')).to_not be_satisfied
end
end
context 'with one source' do
let(:source) { double('Source', :priority => 0) }
before do
SemanticPuppet::Dependency.clear_sources
SemanticPuppet::Dependency.add_source(source)
end
it 'queries the source for release information' do
expect(source).to receive(:fetch).with('module_name').and_return([])
SemanticPuppet::Dependency.query('module_name' => '1.0.0')
end
it 'queries the source for each dependency' do
expect(source).to receive(:fetch).with('module_name').and_return([
create_release(source, 'module_name', '1.0.0', 'bar' => '1.0.0')
])
expect(source).to receive(:fetch).with('bar').and_return([])
SemanticPuppet::Dependency.query('module_name' => '1.0.0')
end
it 'queries the source for each dependency only once' do
expect(source).to receive(:fetch).with('module_name').and_return([
create_release(
source,
'module_name',
'1.0.0',
'bar' => '1.0.0', 'baz' => '0.0.2'
)
])
expect(source).to receive(:fetch).with('bar').and_return([
create_release(source, 'bar', '1.0.0', 'baz' => '0.0.3')
])
expect(source).to receive(:fetch).with('baz').once.and_return([])
SemanticPuppet::Dependency.query('module_name' => '1.0.0')
end
it 'returns a ModuleRelease with the requested dependencies' do
allow(source).to receive(:fetch).and_return([])
result = SemanticPuppet::Dependency.query('foo' => '1.0.0', 'bar' => '1.0.0')
expect(result.dependency_names).to match_array %w[ foo bar ]
end
it 'populates the returned ModuleRelease with related dependencies' do
allow(source).to receive(:fetch).and_return(
[ foo = create_release(source, 'foo', '1.0.0', 'bar' => '1.0.0') ],
[ bar = create_release(source, 'bar', '1.0.0') ]
)
result = SemanticPuppet::Dependency.query('foo' => '1.0.0', 'bar' => '1.0.0')
expect(result.dependencies['foo']).to eql [foo]
expect(result.dependencies['bar']).to eql [bar]
end
it 'populates all returned ModuleReleases with related dependencies' do
allow(source).to receive(:fetch).and_return(
[ foo = create_release(source, 'foo', '1.0.0', 'bar' => '1.0.0') ],
[ bar = create_release(source, 'bar', '1.0.0', 'baz' => '0.1.0') ],
[ baz = create_release(source, 'baz', '0.1.0', 'baz' => '1.0.0') ]
)
result = SemanticPuppet::Dependency.query('foo' => '1.0.0')
expect(result.dependencies['foo']).to eql [foo]
expect(foo.dependencies['bar']).to eql [bar]
expect(bar.dependencies['baz']).to eql [baz]
end
end
context 'with multiple sources' do
let(:source1) { double('SourceOne', :priority => 0) }
let(:source2) { double('SourceTwo', :priority => 0) }
let(:source3) { double('SourceThree', :priority => 0) }
before do
SemanticPuppet::Dependency.add_source(source1)
SemanticPuppet::Dependency.add_source(source2)
SemanticPuppet::Dependency.add_source(source3)
end
it 'queries each source in turn' do
expect(source1).to receive(:fetch).with('module_name').and_return([])
expect(source2).to receive(:fetch).with('module_name').and_return([])
expect(source3).to receive(:fetch).with('module_name').and_return([])
SemanticPuppet::Dependency.query('module_name' => '1.0.0')
end
it 'resolves all dependencies against all sources' do
expect(source1).to receive(:fetch).with('module_name').and_return([
create_release(source1, 'module_name', '1.0.0', 'bar' => '1.0.0')
])
expect(source2).to receive(:fetch).with('module_name').and_return([])
expect(source3).to receive(:fetch).with('module_name').and_return([])
expect(source1).to receive(:fetch).with('bar').and_return([])
expect(source2).to receive(:fetch).with('bar').and_return([])
expect(source3).to receive(:fetch).with('bar').and_return([])
SemanticPuppet::Dependency.query('module_name' => '1.0.0')
end
end
end
describe '.resolve' do
def add_source_modules(name, versions, deps = {})
versions = Array(versions)
releases = versions.map { |ver| create_release(source, name, ver, deps) }
allow(source).to receive(:fetch).with(name).and_return(modules[name].concat(releases))
end
def subject(specs)
graph = SemanticPuppet::Dependency.query(specs)
yield graph if block_given?
expect(graph.dependencies).to_not be_empty
result = SemanticPuppet::Dependency.resolve(graph)
expect(graph.dependencies).to_not be_empty
result.map { |rel| [ rel.name, rel.version.to_s ] }
end
let(:modules) { Hash.new { |h,k| h[k] = [] }}
let(:source) { double('Source', :priority => 0) }
before { SemanticPuppet::Dependency.add_source(source) }
context 'for a module without dependencies' do
def foo(range)
subject('foo' => range).map { |x| x.last }
end
it 'returns the greatest release matching the version range' do
add_source_modules('foo', %w[ 0.9.0 1.0.0 1.1.0 2.0.0 ])
expect(foo('1.x')).to eql %w[ 1.1.0 ]
end
context 'when the query includes both stable and prerelease versions' do
it 'returns the greatest stable release matching the range' do
add_source_modules('foo', %w[ 0.9.0 1.0.0 1.1.0 1.2.0-pre 2.0.0 ])
expect(foo('1.x')).to eql %w[ 1.1.0 ]
end
end
context 'when the query omits all stable versions' do
it 'returns the greatest prerelease version matching the range' do
add_source_modules('foo', %w[ 1.0.0 1.1.0-a 1.1.0-b 2.0.0 ])
expect(foo('>1.1.0-a <2.0.0')).to eql %w[ 1.1.0-b ]
expect(foo('1.1.0-a')).to eql %w[ 1.1.0-a ]
end
end
context 'when the query omits all versions' do
it 'fails with an appropriate message' do
add_source_modules('foo', %w[ 1.0.0 1.1.0-a 1.1.0 ])
with_message = /Could not find satisfying releases/
expect { foo('2.x') }.to raise_exception with_message
expect { foo('2.x') }.to raise_exception /\bfoo\b/
end
end
end
context 'for a module with dependencies' do
def foo(range)
subject('foo' => range)
end
it 'returns the greatest releases matching the dependency range' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('bar', %w[ 0.9.0 1.0.0 1.1.0 1.2.0 2.0.0 ])
expect(foo('1.1.0')).to include %w[ foo 1.1.0 ], %w[ bar 1.2.0 ]
end
context 'when the dependency has both stable and prerelease versions' do
it 'returns the greatest stable release matching the range' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('bar', %w[ 0.9.0 1.0.0 1.1.0 1.2.0-pre 2.0.0 ])
expect(foo('1.1.0')).to include %w[ foo 1.1.0 ], %w[ bar 1.1.0 ]
end
end
context 'when the dependency has no stable versions' do
it 'returns the greatest prerelease version matching the range' do
add_source_modules('foo', '1.1.0', 'bar' => '>=1.1.0-0 <1.2.0')
add_source_modules('foo', '1.1.1', 'bar' => '1.1.0-a')
add_source_modules('bar', %w[ 1.0.0 1.1.0-a 1.1.0-b 2.0.0 ])
expect(foo('1.1.0')).to include %w[ foo 1.1.0 ], %w[ bar 1.1.0-b ]
expect(foo('1.1.1')).to include %w[ foo 1.1.1 ], %w[ bar 1.1.0-a ]
end
end
context 'when the dependency cannot be satisfied' do
it 'fails with an appropriate message' do
add_source_modules('foo', %w[ 1.1.0 ], 'bar' => '1.x')
add_source_modules('bar', %w[ 0.0.1 0.1.0-a 0.1.0 ])
with_message = /Could not find satisfying releases/
expect { foo('1.1.0') }.to raise_exception with_message
expect { foo('1.1.0') }.to raise_exception /\bfoo\b/
end
it 'sets unsatisfiable' do
add_source_modules('foo', %w[ 1.1.0 ], 'bar' => '1.x')
add_source_modules('bar', %w[ 0.0.1 0.1.0-a 0.1.0 ])
with_message = /Could not find satisfying releases/
expect { foo('1.1.0') }.to raise_exception with_message
expect(SemanticPuppet::Dependency.unsatisfiable).to eq('foo')
end
end
end
context 'for a module with competing dependencies' do
def foo(range)
subject('foo' => range)
end
context 'that overlap' do
it 'returns the greatest release satisfying all dependencies' do
add_source_modules('foo', '1.1.0', 'bar' => '1.0.0', 'baz' => '1.0.0')
add_source_modules('bar', '1.0.0', 'quxx' => '1.x')
add_source_modules('baz', '1.0.0', 'quxx' => '1.1.x')
add_source_modules('quxx', %w[ 0.9.0 1.0.0 1.1.0 1.1.1 1.2.0 2.0.0 ])
expect(foo('1.1.0')).to_not include %w[ quxx 1.2.0 ]
expect(foo('1.1.0')).to include %w[ quxx 1.1.1 ]
end
end
context 'that do not overlap' do
it 'fails with an appropriate message' do
add_source_modules('foo','1.1.0', 'bar' => '1.0.0', 'baz' => '1.0.0')
add_source_modules('bar','1.0.0', 'quxx' => '1.x')
add_source_modules('baz','1.0.0', 'quxx' => '2.x')
add_source_modules('quxx', %w[ 0.9.0 1.0.0 1.1.0 1.1.1 1.2.0 2.0.0 ])
with_message = /Could not find satisfying releases/
expect { foo('1.1.0') }.to raise_exception with_message
expect { foo('1.1.0') }.to raise_exception /\bfoo\b/
end
end
end
context 'for a module with circular dependencies' do
def foo(range)
subject('foo' => range)
end
context 'that can be resolved' do
it 'terminates' do
add_source_modules('foo', '1.1.0', 'foo' => '1.x')
expect(foo('1.1.0')).to include %w[ foo 1.1.0 ]
end
end
context 'that cannot be resolved' do
it 'fails with an appropriate message' do
add_source_modules('foo', '1.1.0', 'foo' => '1.0.0')
with_message = /Could not find satisfying releases/
expect { foo('1.1.0') }.to raise_exception with_message
expect { foo('1.1.0') }.to raise_exception /\bfoo\b/
end
end
end
context 'for a module with dependencies' do
context 'that violate module constraints on the graph' do
def foo(range)
subject('foo' => range) do |graph|
graph.add_constraint('no downgrade', 'bar', '> 3.0.0') do |node|
SemanticPuppet::VersionRange.parse('> 3.0.0') === node.version
end
end
end
context 'that can be resolved' do
it 'terminates' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('foo', '1.2.0', 'bar' => '>= 2.0.0')
add_source_modules('bar', '1.0.0')
add_source_modules('bar', '2.0.0', 'baz' => '>= 1.0.0')
add_source_modules('bar', '3.0.0')
add_source_modules('bar', '3.0.1')
add_source_modules('baz', '1.0.0')
expect(foo('1.x')).to include %w[ foo 1.2.0 ], %w[ bar 3.0.1 ]
end
end
context 'that cannot be resolved' do
it 'fails with an appropriate message' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('foo', '1.2.0', 'bar' => '2.x')
add_source_modules('bar', '1.0.0', 'baz' => '1.x')
add_source_modules('bar', '2.0.0', 'baz' => '1.x')
add_source_modules('baz', '1.0.0')
add_source_modules('baz', '3.0.0')
add_source_modules('baz', '3.0.1')
with_message = /Could not find satisfying releases/
expect { foo('1.x') }.to raise_exception with_message
expect { foo('1.x') }.to raise_exception /\bfoo\b/
end
end
end
end
context 'that violate graph constraints' do
def foo(range)
subject('foo' => range) do |graph|
graph.add_graph_constraint('uniqueness') do |nodes|
nodes.none? { |node| node.name =~ /z/ }
end
end
end
context 'that can be resolved' do
it 'terminates' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('foo', '1.2.0', 'bar' => '2.x')
add_source_modules('bar', '1.0.0')
add_source_modules('bar', '2.0.0', 'baz' => '1.0.0')
add_source_modules('baz', '1.0.0')
expect(foo('1.x')).to include %w[ foo 1.1.0 ], %w[ bar 1.0.0 ]
end
end
context 'that cannot be resolved' do
it 'fails with an appropriate message' do
add_source_modules('foo', '1.1.0', 'bar' => '1.x')
add_source_modules('foo', '1.2.0', 'bar' => '2.x')
add_source_modules('bar', '1.0.0', 'baz' => '1.0.0')
add_source_modules('bar', '2.0.0', 'baz' => '1.0.0')
add_source_modules('baz', '1.0.0')
with_message = /Could not find satisfying releases/
expect { foo('1.1.0') }.to raise_exception with_message
expect { foo('1.1.0') }.to raise_exception /\bfoo\b/
end
end
end
end
end
|