Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: utils/tests/pub/version_solver_test.dart

Issue 13095015: Use backtracking when solving dependency constraints. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revised. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library pub_update_test; 5 library pub_update_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
11 11
12 import '../../pub/lock_file.dart'; 12 import '../../pub/lock_file.dart';
13 import '../../pub/package.dart'; 13 import '../../pub/package.dart';
14 import '../../pub/pubspec.dart'; 14 import '../../pub/pubspec.dart';
15 import '../../pub/source.dart'; 15 import '../../pub/source.dart';
16 import '../../pub/source_registry.dart'; 16 import '../../pub/source_registry.dart';
17 import '../../pub/system_cache.dart'; 17 import '../../pub/system_cache.dart';
18 import '../../pub/utils.dart'; 18 import '../../pub/utils.dart';
19 import '../../pub/version.dart'; 19 import '../../pub/version.dart';
20 import '../../pub/version_solver.dart'; 20 import '../../pub/solver/version_solver.dart';
21 import 'test_pub.dart'; 21 import 'test_pub.dart';
22 22
23 Matcher noVersion(List<String> packages) {
24 return predicate((x) {
25 if (x is! NoVersionException) return false;
26
27 // Make sure the error string mentions the conflicting dependers.
28 var message = x.toString();
29 return packages.every((package) => message.contains(package));
30 }, "is a NoVersionException");
31 }
32
33 Matcher disjointConstraint(List<String> packages) {
34 return predicate((x) {
35 if (x is! DisjointConstraintException) return false;
36
37 // Make sure the error string mentions the conflicting dependers.
38 var message = x.toString();
39 return packages.every((package) => message.contains(package));
40 }, "is a DisjointConstraintException");
41 }
42
43 Matcher descriptionMismatch(String package1, String package2) {
44 return predicate((x) {
45 if (x is! DescriptionMismatchException) return false;
46
47 // Make sure the error string mentions the conflicting dependers.
48 if (!x.toString().contains(package1)) return false;
49 if (!x.toString().contains(package2)) return false;
50
51 return true;
52 }, "is a DescriptionMismatchException");
53 }
54
55 final couldNotSolve = predicate((x) => x is CouldNotSolveException,
56 "is a CouldNotSolveException");
57
58 Matcher sourceMismatch(String package1, String package2) {
59 return predicate((x) {
60 if (x is! SourceMismatchException) return false;
61
62 // Make sure the error string mentions the conflicting dependers.
63 if (!x.toString().contains(package1)) return false;
64 if (!x.toString().contains(package2)) return false;
65
66 return true;
67 }, "is a SourceMismatchException");
68 }
69
70 MockSource source1; 23 MockSource source1;
71 MockSource source2; 24 MockSource source2;
72 25
26 bool allowBacktracking;
27
73 main() { 28 main() {
74 initConfig(); 29 initConfig();
75 30
31 for (allowBacktracking in [false, true]) {
32 group(allowBacktracking ? 'BackTrackingSolver' : 'GreedySolver', () {
33 group('basic graph', basicGraph);
34 group('with lockfile', withLockFile);
35 group('root dependency', rootDependency);
36 group('dev dependency', devDependency);
37 group('unsolvable', unsolvable);
38 group('backtracking', backtracking);
39 });
40 }
41 }
42
43 void basicGraph() {
76 testResolve('no dependencies', { 44 testResolve('no dependencies', {
77 'myapp 0.0.0': {} 45 'myapp 0.0.0': {}
78 }, result: { 46 }, result: {
79 'myapp from root': '0.0.0' 47 'myapp from root': '0.0.0'
80 }); 48 });
81 49
82 testResolve('simple dependency tree', { 50 testResolve('simple dependency tree', {
83 'myapp 0.0.0': { 51 'myapp 0.0.0': {
84 'a': '1.0.0', 52 'a': '1.0.0',
85 'b': '1.0.0' 53 'b': '1.0.0'
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 'foo 1.0.3': { 'zoop': '1.0.0' }, 109 'foo 1.0.3': { 'zoop': '1.0.0' },
142 'bar 1.0.0': { 'foo': '<=1.0.1' }, 110 'bar 1.0.0': { 'foo': '<=1.0.1' },
143 'bang 1.0.0': {}, 111 'bang 1.0.0': {},
144 'whoop 1.0.0': {}, 112 'whoop 1.0.0': {},
145 'zoop 1.0.0': {} 113 'zoop 1.0.0': {}
146 }, result: { 114 }, result: {
147 'myapp from root': '0.0.0', 115 'myapp from root': '0.0.0',
148 'foo': '1.0.1', 116 'foo': '1.0.1',
149 'bar': '1.0.0', 117 'bar': '1.0.0',
150 'bang': '1.0.0' 118 'bang': '1.0.0'
119 }, maxTries: 3, hasGreedySolution: true);
120
121 testResolve('circular dependency', {
122 'myapp 1.0.0': {
123 'foo': '1.0.0'
124 },
125 'foo 1.0.0': {
126 'bar': '1.0.0'
127 },
128 'bar 1.0.0': {
129 'foo': '1.0.0'
130 }
131 }, result: {
132 'myapp from root': '1.0.0',
133 'foo': '1.0.0',
134 'bar': '1.0.0'
151 }); 135 });
136 }
152 137
138 withLockFile() {
153 testResolve('with compatible locked dependency', { 139 testResolve('with compatible locked dependency', {
154 'myapp 0.0.0': { 140 'myapp 0.0.0': {
155 'foo': 'any' 141 'foo': 'any'
156 }, 142 },
157 'foo 1.0.0': { 'bar': '1.0.0' }, 143 'foo 1.0.0': { 'bar': '1.0.0' },
158 'foo 1.0.1': { 'bar': '1.0.1' }, 144 'foo 1.0.1': { 'bar': '1.0.1' },
159 'foo 1.0.2': { 'bar': '1.0.2' }, 145 'foo 1.0.2': { 'bar': '1.0.2' },
160 'bar 1.0.0': {}, 146 'bar 1.0.0': {},
161 'bar 1.0.1': {}, 147 'bar 1.0.1': {},
162 'bar 1.0.2': {} 148 'bar 1.0.2': {}
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 'bar 1.0.2': {}, 184 'bar 1.0.2': {},
199 'baz 1.0.0': {} 185 'baz 1.0.0': {}
200 }, lockfile: { 186 }, lockfile: {
201 'baz': '1.0.0' 187 'baz': '1.0.0'
202 }, result: { 188 }, result: {
203 'myapp from root': '0.0.0', 189 'myapp from root': '0.0.0',
204 'foo': '1.0.2', 190 'foo': '1.0.2',
205 'bar': '1.0.2' 191 'bar': '1.0.2'
206 }); 192 });
207 193
208 testResolve('circular dependency', { 194 testResolve('unlocks dependencies if necessary to ensure that a new '
195 'dependency is satisfied', {
196 'myapp 0.0.0': {
197 'foo': 'any',
198 'newdep': 'any'
199 },
200 'foo 1.0.0': { 'bar': '<2.0.0' },
201 'bar 1.0.0': { 'baz': '<2.0.0' },
202 'baz 1.0.0': { 'qux': '<2.0.0' },
203 'qux 1.0.0': {},
204 'foo 2.0.0': { 'bar': '<3.0.0' },
205 'bar 2.0.0': { 'baz': '<3.0.0' },
206 'baz 2.0.0': { 'qux': '<3.0.0' },
207 'qux 2.0.0': {},
208 'newdep 2.0.0': { 'baz': '>=1.5.0' }
209 }, lockfile: {
210 'foo': '1.0.0',
211 'bar': '1.0.0',
212 'baz': '1.0.0',
213 'qux': '1.0.0'
214 }, result: {
215 'myapp from root': '0.0.0',
216 'foo': '2.0.0',
217 'bar': '2.0.0',
218 'baz': '2.0.0',
219 'qux': '1.0.0',
220 'newdep': '2.0.0'
221 }, maxTries: 5, hasGreedySolution: true);
222 }
223
224 rootDependency() {
225 testResolve('with root source', {
209 'myapp 1.0.0': { 226 'myapp 1.0.0': {
210 'foo': '1.0.0' 227 'foo': '1.0.0'
211 }, 228 },
212 'foo 1.0.0': {
213 'bar': '1.0.0'
214 },
215 'bar 1.0.0': {
216 'foo': '1.0.0'
217 }
218 }, result: {
219 'myapp from root': '1.0.0',
220 'foo': '1.0.0',
221 'bar': '1.0.0'
222 });
223
224 testResolve('dependency back onto root package', {
225 'myapp 1.0.0': {
226 'foo': '1.0.0'
227 },
228 'foo 1.0.0': { 229 'foo 1.0.0': {
229 'myapp from root': '>=1.0.0' 230 'myapp from root': '>=1.0.0'
230 } 231 }
231 }, result: { 232 }, result: {
232 'myapp from root': '1.0.0', 233 'myapp from root': '1.0.0',
233 'foo': '1.0.0' 234 'foo': '1.0.0'
234 }); 235 });
235 236
236 testResolve('dependency back onto root package with different source', { 237 testResolve('with different source', {
237 'myapp 1.0.0': { 238 'myapp 1.0.0': {
238 'foo': '1.0.0' 239 'foo': '1.0.0'
239 }, 240 },
240 'foo 1.0.0': { 241 'foo 1.0.0': {
241 'myapp': '>=1.0.0' 242 'myapp': '>=1.0.0'
242 } 243 }
243 }, result: { 244 }, result: {
244 'myapp from root': '1.0.0', 245 'myapp from root': '1.0.0',
245 'foo': '1.0.0' 246 'foo': '1.0.0'
246 }); 247 });
247 248
248 testResolve('mismatched dependencies back onto root package', { 249 testResolve('with mismatched sources', {
249 'myapp 1.0.0': { 250 'myapp 1.0.0': {
250 'foo': '1.0.0', 251 'foo': '1.0.0',
251 'bar': '1.0.0' 252 'bar': '1.0.0'
252 }, 253 },
253 'foo 1.0.0': { 254 'foo 1.0.0': {
254 'myapp': '>=1.0.0' 255 'myapp': '>=1.0.0'
255 }, 256 },
256 'bar 1.0.0': { 257 'bar 1.0.0': {
257 'myapp from mock2': '>=1.0.0' 258 'myapp from mock2': '>=1.0.0'
258 } 259 }
259 }, error: sourceMismatch('foo', 'bar')); 260 }, error: sourceMismatch('foo', 'bar'));
260 261
261 testResolve('dependency back onto root package with wrong version', { 262 testResolve('with wrong version', {
262 'myapp 1.0.0': { 263 'myapp 1.0.0': {
263 'foo': '1.0.0' 264 'foo': '1.0.0'
264 }, 265 },
265 'foo 1.0.0': { 266 'foo 1.0.0': {
266 'myapp': '<1.0.0' 267 'myapp': '<1.0.0'
267 } 268 }
268 }, error: disjointConstraint(['foo'])); 269 }, error: couldNotSolve);
270 }
269 271
272 devDependency() {
273 testResolve("includes root package's dev dependencies", {
274 'myapp 1.0.0': {
275 '(dev) foo': '1.0.0',
276 '(dev) bar': '1.0.0'
277 },
278 'foo 1.0.0': {},
279 'bar 1.0.0': {}
280 }, result: {
281 'myapp from root': '1.0.0',
282 'foo': '1.0.0',
283 'bar': '1.0.0'
284 });
285
286 testResolve("includes dev dependency's transitive dependencies", {
287 'myapp 1.0.0': {
288 '(dev) foo': '1.0.0'
289 },
290 'foo 1.0.0': {
291 'bar': '1.0.0'
292 },
293 'bar 1.0.0': {}
294 }, result: {
295 'myapp from root': '1.0.0',
296 'foo': '1.0.0',
297 'bar': '1.0.0'
298 });
299
300 testResolve("ignores transitive dependency's dev dependencies", {
301 'myapp 1.0.0': {
302 'foo': '1.0.0'
303 },
304 'foo 1.0.0': {
305 '(dev) bar': '1.0.0'
306 },
307 'bar 1.0.0': {}
308 }, result: {
309 'myapp from root': '1.0.0',
310 'foo': '1.0.0'
311 });
312 }
313
314 unsolvable() {
270 testResolve('no version that matches requirement', { 315 testResolve('no version that matches requirement', {
271 'myapp 0.0.0': { 316 'myapp 0.0.0': {
272 'foo': '>=1.0.0 <2.0.0' 317 'foo': '>=1.0.0 <2.0.0'
273 }, 318 },
274 'foo 2.0.0': {}, 319 'foo 2.0.0': {},
275 'foo 2.1.3': {} 320 'foo 2.1.3': {}
276 }, error: noVersion(['myapp'])); 321 }, error: noVersion(['myapp']));
277 322
278 testResolve('no version that matches combined constraint', { 323 testResolve('no version that matches combined constraint', {
279 'myapp 0.0.0': { 324 'myapp 0.0.0': {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 'foo 1.0.0': { 373 'foo 1.0.0': {
329 'shared': '1.0.0' 374 'shared': '1.0.0'
330 }, 375 },
331 'bar 1.0.0': { 376 'bar 1.0.0': {
332 'shared from mock2': '1.0.0' 377 'shared from mock2': '1.0.0'
333 }, 378 },
334 'shared 1.0.0': {}, 379 'shared 1.0.0': {},
335 'shared 1.0.0 from mock2': {} 380 'shared 1.0.0 from mock2': {}
336 }, error: sourceMismatch('foo', 'bar')); 381 }, error: sourceMismatch('foo', 'bar'));
337 382
338 testResolve('unstable dependency graph', { 383 testResolve('no valid solution', {
384 'myapp 0.0.0': {
385 'a': 'any',
386 'b': 'any'
387 },
388 'a 1.0.0': {
389 'b': '1.0.0'
390 },
391 'a 2.0.0': {
392 'b': '2.0.0'
393 },
394 'b 1.0.0': {
395 'a': '2.0.0'
396 },
397 'b 2.0.0': {
398 'a': '1.0.0'
399 }
400 }, error: couldNotSolve, maxTries: 4);
401 }
402
403 backtracking() {
404 testResolve('circular dependency on older version', {
339 'myapp 0.0.0': { 405 'myapp 0.0.0': {
340 'a': '>=1.0.0' 406 'a': '>=1.0.0'
341 }, 407 },
342 'a 1.0.0': {}, 408 'a 1.0.0': {},
343 'a 2.0.0': { 409 'a 2.0.0': {
344 'b': '1.0.0' 410 'b': '1.0.0'
345 }, 411 },
346 'b 1.0.0': { 412 'b 1.0.0': {
347 'a': '1.0.0' 413 'a': '1.0.0'
348 } 414 }
349 }, error: couldNotSolve); 415 }, result: {
416 'myapp from root': '0.0.0',
417 'a': '1.0.0'
418 }, maxTries: 3);
350 419
351 group('dev dependencies', () { 420 /// The latest versions of a and b disagree on c. An older version of either
352 testResolve("includes root package's dev dependencies", { 421 /// will resolve the problem. This test validates that b, which is farther
353 'myapp 1.0.0': { 422 /// in the dependency graph from myapp is downgraded first.
354 '(dev) foo': '1.0.0', 423 testResolve('rolls back leaf versions first', {
355 '(dev) bar': '1.0.0' 424 'myapp 0.0.0': {
356 }, 425 'a': 'any'
357 'foo 1.0.0': {}, 426 },
358 'bar 1.0.0': {} 427 'a 1.0.0': {
359 }, result: { 428 'b': 'any'
360 'myapp from root': '1.0.0', 429 },
361 'foo': '1.0.0', 430 'a 2.0.0': {
362 'bar': '1.0.0' 431 'b': 'any',
363 }); 432 'c': '2.0.0'
433 },
434 'b 1.0.0': {},
435 'b 2.0.0': {
436 'c': '1.0.0'
437 },
438 'c 1.0.0': {},
439 'c 2.0.0': {}
440 }, result: {
441 'myapp from root': '0.0.0',
442 'a': '2.0.0',
443 'b': '1.0.0',
444 'c': '2.0.0'
445 }, maxTries: 3);
364 446
365 testResolve("includes dev dependency's transitive dependencies", { 447 // Only one version of baz, so foo and bar will have to downgrade until they
366 'myapp 1.0.0': { 448 // reach it.
367 '(dev) foo': '1.0.0' 449 testResolve('simple transitive', {
368 }, 450 'myapp 0.0.0': {'foo': 'any'},
369 'foo 1.0.0': { 451 'foo 1.0.0': {'bar': '1.0.0'},
370 'bar': '1.0.0' 452 'foo 2.0.0': {'bar': '2.0.0'},
371 }, 453 'foo 3.0.0': {'bar': '3.0.0'},
372 'bar 1.0.0': {} 454 'bar 1.0.0': {'baz': 'any'},
373 }, result: { 455 'bar 2.0.0': {'baz': '2.0.0'},
374 'myapp from root': '1.0.0', 456 'bar 3.0.0': {'baz': '3.0.0'},
375 'foo': '1.0.0', 457 'baz 1.0.0': {}
376 'bar': '1.0.0' 458 }, result: {
377 }); 459 'myapp from root': '0.0.0',
460 'foo': '1.0.0',
461 'bar': '1.0.0',
462 'baz': '1.0.0'
463 }, maxTries: 5);
378 464
379 testResolve("ignores transitive dependency's dev dependencies", { 465 // This sets up a hundred versions of foo and bar, 0.0.0 through 9.9.0. Each
380 'myapp 1.0.0': { 466 // version of foo depends on a baz with the same major version. Each version
381 'foo': '1.0.0' 467 // of bar depends on a baz with the same minor version. There is only one
382 }, 468 // version of baz, 0.0.0, so only older versions of foo and bar will
383 'foo 1.0.0': { 469 // satisfy it.
384 '(dev) bar': '1.0.0' 470 var map = {
385 }, 471 'myapp 0.0.0': {
386 'bar 1.0.0': {} 472 'foo': 'any',
387 }, result: { 473 'bar': 'any'
388 'myapp from root': '1.0.0', 474 },
389 'foo': '1.0.0' 475 'baz 0.0.0': {}
390 }); 476 };
391 }); 477
478 for (var i = 0; i < 10; i++) {
479 for (var j = 0; j < 10; j++) {
480 map['foo $i.$j.0'] = {'baz': '$i.0.0'};
481 map['bar $i.$j.0'] = {'baz': '0.$j.0'};
482 }
483 }
484
485 testResolve('complex backtrack', map, result: {
486 'myapp from root': '0.0.0',
487 'foo': '0.9.0',
488 'bar': '9.0.0',
489 'baz': '0.0.0'
490 }, maxTries: 1090); // TODO(rnystrom): Is this acceptable?
491
492 // TODO(rnystrom): More tests. In particular:
493 // - Tests that demonstrate backtracking for every case that can cause a
494 // solution to fail (no versions, disjoint, etc.)
495 // - Tests where there are multiple valid solutions and "best" is possibly
496 // ambiguous to nail down which order the backtracker tries solutions.
392 } 497 }
393 498
394 // TODO(rnystrom): More stuff to test: 499 testResolve(description, packages,
395 // - Depending on a non-existent package. 500 {lockfile, result, FailMatcherBuilder error, int maxTries,
396 // - Test that only a certain number requests are sent to the mock source so we 501 bool hasGreedySolution}) {
397 // can keep track of server traffic. 502 // Close over the top-level variable since it will be mutated.
503 var allowBacktracking_ = allowBacktracking;
398 504
399 testResolve(description, packages, {lockfile, result, Matcher error}) { 505 if (maxTries == null) maxTries = 1;
506 if (hasGreedySolution == null) hasGreedySolution = maxTries == 1;
507
508 if (!allowBacktracking_) {
509 // The greedy solver should fail any graph that does expect multiple tries
510 // and isn't explicitly annotated to have a greedy solution.
511 if (!hasGreedySolution) {
512 result = null;
513 error = couldNotSolve;
514 }
515 }
516
400 test(description, () { 517 test(description, () {
401 var cache = new SystemCache('.'); 518 var cache = new SystemCache('.');
402 source1 = new MockSource('mock1'); 519 source1 = new MockSource('mock1');
403 source2 = new MockSource('mock2'); 520 source2 = new MockSource('mock2');
404 cache.register(source1); 521 cache.register(source1);
405 cache.register(source2); 522 cache.register(source2);
406 cache.sources.setDefault(source1.name); 523 cache.sources.setDefault(source1.name);
407 524
408 // Build the test package graph. 525 // Build the test package graph.
409 var root; 526 var root;
410 packages.forEach((nameVersion, dependencies) { 527 packages.forEach((nameVersion, dependencies) {
411 var parsed = parseSource(nameVersion, (isDev, nameVersion, source) { 528 var parsed = parseSource(nameVersion, (isDev, nameVersion, source) {
412 var parts = nameVersion.split(' '); 529 var parts = nameVersion.split(' ');
413 var name = parts[0]; 530 var name = parts[0];
414 var version = parts[1]; 531 var version = parts[1];
415 532
416 var package = source1.mockPackage(name, version, dependencies); 533 var package = mockPackage(name, version, dependencies);
417 if (name == 'myapp') { 534 if (name == 'myapp') {
418 // Don't add the root package to the server, so we can verify that Pub 535 // Don't add the root package to the server, so we can verify that Pub
419 // doesn't try to look up information about the local package on the 536 // doesn't try to look up information about the local package on the
420 // remote server. 537 // remote server.
421 root = package; 538 root = package;
422 } else { 539 } else {
423 source.addPackage(package); 540 source.addPackage(name, package);
424 } 541 }
425 }); 542 });
426 }); 543 });
427 544
428 // Clean up the expectation. 545 // Clean up the expectation.
429 if (result != null) { 546 if (result != null) {
430 var newResult = {}; 547 var newResult = {};
431 result.forEach((name, version) { 548 result.forEach((name, version) {
432 parseSource(name, (isDev, name, source) { 549 parseSource(name, (isDev, name, source) {
433 version = new Version.parse(version); 550 version = new Version.parse(version);
434 newResult[name] = new PackageId(name, source, version, name); 551 newResult[name] = new PackageId(name, source, version, name);
435 }); 552 });
436 }); 553 });
437 result = newResult; 554 result = newResult;
438 } 555 }
439 556
440 var realLockFile = new LockFile.empty(); 557 var realLockFile = new LockFile.empty();
441 if (lockfile != null) { 558 if (lockfile != null) {
442 lockfile.forEach((name, version) { 559 lockfile.forEach((name, version) {
443 version = new Version.parse(version); 560 version = new Version.parse(version);
444 realLockFile.packages[name] = 561 realLockFile.packages[name] =
445 new PackageId(name, source1, version, name); 562 new PackageId(name, source1, version, name);
446 }); 563 });
447 } 564 }
448 565
449 // Resolve the versions. 566 // Resolve the versions.
450 var future = resolveVersions(cache.sources, root, realLockFile); 567 var future = resolveVersions(cache.sources, root,
568 allowBacktracking: allowBacktracking_, lockFile: realLockFile);
451 569
570 var matcher;
452 if (result != null) { 571 if (result != null) {
453 expect(future, completion(predicate((actualResult) { 572 matcher = new SolveSuccessMatcher(result, maxTries);
454 for (var actualId in actualResult) {
455 if (!result.containsKey(actualId.name)) return false;
456 var expectedId = result.remove(actualId.name);
457 if (actualId != expectedId) return false;
458 }
459 return result.isEmpty;
460 }, 'packages to match $result')));
461 } else if (error != null) { 573 } else if (error != null) {
462 expect(future, throwsA(error)); 574 matcher = error(maxTries);
463 } 575 }
576
577 expect(future, completion(matcher));
464 }); 578 });
465 } 579 }
466 580
581 typedef SolveFailMatcher FailMatcherBuilder(int maxTries);
582
583 FailMatcherBuilder noVersion(List<String> packages) {
584 return (maxTries) => new SolveFailMatcher(packages, maxTries,
585 NoVersionException);
586 }
587
588 FailMatcherBuilder disjointConstraint(List<String> packages) {
589 return (maxTries) => new SolveFailMatcher(packages, 123,
590 DisjointConstraintException);
591 }
592
593 FailMatcherBuilder descriptionMismatch(String package1, String package2) {
594 return (maxTries) => new SolveFailMatcher([package1, package2], 123,
595 DescriptionMismatchException);
596 }
597
598 // If no solution can be found, the solver just reports the last failure that
599 // happened during propagation. Since we don't specify the order that solutions
600 // are tried, this just validates that *some* failure occurred, but not which.
601 SolveFailMatcher couldNotSolve(maxTries) =>
602 new SolveFailMatcher([], maxTries, null);
603
604 FailMatcherBuilder sourceMismatch(String package1, String package2) {
605 return (maxTries) => new SolveFailMatcher([package1, package2], maxTries,
606 SourceMismatchException);
607 }
608
609 class SolveSuccessMatcher implements Matcher {
610 /// The expected concrete package selections.
611 final Map<String, PackageId> _expected;
612
613 /// The maximum number of attempts that should have been tried before finding
614 /// the solution.
615 final int _maxTries;
616
617 SolveSuccessMatcher(this._expected, this._maxTries);
618
619 Description describe(Description description) {
620 return description.add(
621 'Solver to use at most $_maxTries attempts to find:\n'
622 '${_listPackages(_expected.values)}');
623 }
624
625 Description describeMismatch(SolveResult result,
626 Description description,
627 MatchState state, bool verbose) {
628 if (!result.succeeded) {
629 description.add('Solver failed with:\n${result.error}');
630 return;
631 }
632
633 description.add('Resolved:\n${_listPackages(result.packages)}\n');
634 description.add(state.state);
635 return description;
636 }
637
638 bool matches(SolveResult result, MatchState state) {
639 if (!result.succeeded) return false;
640
641 var expected = new Map.from(_expected);
642 for (var id in result.packages) {
643 if (!expected.containsKey(id.name)) {
644 state.state = 'Should not have selected $id';
645 return false;
646 }
647
648 var expectedId = expected.remove(id.name);
649 if (id != expectedId) {
650 state.state = 'Expected $expectedId, not $id';
651 return false;
652 }
653 }
654
655 if (!expected.isEmpty) {
656 state.state = 'Missing:\n${_listPackages(expected.values)}';
657 return false;
658 }
659
660 if (result.attemptedSolutions > _maxTries) {
661 state.state = 'Took ${result.attemptedSolutions} attempts';
662 return false;
663 }
664
665 return true;
nweiz 2013/04/10 22:56:34 I'm worried that early errors will clobber later o
Bob Nystrom 2013/04/11 00:55:11 I ordered them in terms of most significant first
nweiz 2013/04/11 22:12:04 I do. Unittest does this when displaying errors fo
Bob Nystrom 2013/04/16 18:34:16 Done.
666 }
667
668 String _listPackages(Iterable<PackageId> packages) {
669 return '- ${packages.join('\n- ')}';
670 }
671 }
672
673 class SolveFailMatcher implements Matcher {
674 /// The strings that should appear in the resulting error message.
675 final Iterable<String> _expected;
nweiz 2013/04/10 22:56:34 This seems to be used just as a list of packages.
Bob Nystrom 2013/04/11 00:55:11 This is preserving the existing behavior of the ol
nweiz 2013/04/11 22:12:04 It's not especially important, just a neatness thi
Bob Nystrom 2013/04/16 18:34:16 Added a TODO.
676
677 /// The maximum number of attempts that should be tried before failing.
678 final int _maxTries;
679
680 /// The concrete error type that should be found, or `null` if any
681 /// [SolveFailure] is allowed.
682 final Type _expectedType;
683
684 SolveFailMatcher(this._expected, this._maxTries, this._expectedType);
685
686 Description describe(Description description) {
687 description.add('Solver should fail after at most $_maxTries attempts.');
688 if (!_expected.isEmpty) {
689 var textList = _expected.map((s) => '"$s"').join(", ");
690 description.add(' The error should contain $textList.');
691 }
692 return description;
693 }
694
695 Description describeMismatch(SolveResult result,
696 Description description,
697 MatchState state, bool verbose) {
698 if (result.succeeded) {
699 description.add('Solver succeeded');
700 return;
701 }
702
703 description.add(state.state);
704 return description;
705 }
706
707 bool matches(SolveResult result, MatchState state) {
708 if (result.succeeded) return false;
709
710 if (_expectedType != null && result.error.runtimeType != _expectedType) {
711 state.state = 'Should have error type $_expectedType, got '
712 '${result.error.runtimeType}';
713 return false;
714 }
715
716 var message = result.error.toString();
717 for (var expected in _expected) {
718 if (!message.contains(expected)) {
719 state = 'Expected error to contain "$expected", got:\n$message';
720 return false;
721 }
722 }
723
724 if (result.attemptedSolutions > _maxTries) {
725 state.state = 'Took ${result.attemptedSolutions} attempts';
726 return false;
727 }
728
729 return true;
730 }
731 }
732
467 /// A source used for testing. This both creates mock package objects and acts 733 /// A source used for testing. This both creates mock package objects and acts
468 /// as a source for them. 734 /// as a source for them.
469 /// 735 ///
470 /// In order to support testing packages that have the same name but different 736 /// In order to support testing packages that have the same name but different
471 /// descriptions, a package's name is calculated by taking the description 737 /// descriptions, a package's name is calculated by taking the description
472 /// string and stripping off any trailing hyphen followed by non-hyphen 738 /// string and stripping off any trailing hyphen followed by non-hyphen
473 /// characters. 739 /// characters.
474 class MockSource extends Source { 740 class MockSource extends Source {
475 final Map<String, Map<Version, Package>> _packages; 741 final _packages = <String, Map<Version, Package>>{};
742
743 /// Keeps track of which package version lists have been requested. Ensures
744 /// that a source is only hit once for a given package and that pub
745 /// internally caches the results.
746 final _requestedVersions = new Set<String>();
747
748 /// Keeps track of which package pubspecs have been requested. Ensures that a
749 /// source is only hit once for a given package and that pub internally
750 /// caches the results.
751 final _requestedPubspecs = new Map<String, Set<Version>>();
476 752
477 final String name; 753 final String name;
478 bool get shouldCache => true; 754 bool get shouldCache => true;
479 755
480 MockSource(this.name) 756 MockSource(this.name);
481 : _packages = <String, Map<Version, Package>>{};
482 757
483 Future<List<Version>> getVersions(String name, String description) { 758 Future<List<Version>> getVersions(String name, String description) {
484 return new Future.of(() => _packages[description].keys.toList()); 759 return new Future.of(() {
760 // Make sure the solver doesn't request the same thing twice.
761 if (_requestedVersions.contains(description)) {
762 throw 'Version list for $description was already requested.';
763 }
764
765 _requestedVersions.add(description);
766
767 if (!_packages.containsKey(description)){
768 throw 'MockSource does not have a package matching "$description".';
769 }
770 return _packages[description].keys.toList();
771 });
485 } 772 }
486 773
487 Future<Pubspec> describe(PackageId id) { 774 Future<Pubspec> describe(PackageId id) {
488 return new Future.of(() => _packages[id.name][id.version].pubspec); 775 return new Future.of(() {
776 // Make sure the solver doesn't request the same thing twice.
777 if (_requestedPubspecs.containsKey(id.description) &&
778 _requestedPubspecs[id.description].contains(id.version)) {
779 throw 'Pubspec for $id was already requested.';
780 }
781
782 _requestedPubspecs.putIfAbsent(id.description, () => new Set<Version>());
783 _requestedPubspecs[id.description].add(id.version);
784
785 return _packages[id.description][id.version].pubspec;
786 });
489 } 787 }
490 788
491 Future<bool> install(PackageId id, String path) { 789 Future<bool> install(PackageId id, String path) {
492 throw 'no'; 790 throw 'no';
493 } 791 }
494 792
495 Package mockPackage(String description, String version, 793 void addPackage(String description, Package package) {
496 Map dependencyStrings) { 794 _packages.putIfAbsent(description, () => new Map<Version, Package>());
497 // Build the pubspec dependencies. 795 _packages[description][package.version] = package;
498 var dependencies = <PackageRef>[];
499 var devDependencies = <PackageRef>[];
500
501 dependencyStrings.forEach((name, constraint) {
502 parseSource(name, (isDev, name, source) {
503 var packageName = name.replaceFirst(new RegExp(r"-[^-]+$"), "");
504 var ref = new PackageRef(packageName, source,
505 new VersionConstraint.parse(constraint), name);
506
507 if (isDev) {
508 devDependencies.add(ref);
509 } else {
510 dependencies.add(ref);
511 }
512 });
513 });
514
515 var pubspec = new Pubspec(
516 description, new Version.parse(version), dependencies, devDependencies,
517 new PubspecEnvironment());
518 return new Package.inMemory(pubspec);
519 }
520
521 void addPackage(Package package) {
522 _packages.putIfAbsent(package.name, () => new Map<Version, Package>());
523 _packages[package.name][package.version] = package;
524 } 796 }
525 } 797 }
526 798
799 Package mockPackage(String description, String version,
800 Map dependencyStrings) {
801 // Build the pubspec dependencies.
802 var dependencies = <PackageRef>[];
803 var devDependencies = <PackageRef>[];
804
805 dependencyStrings.forEach((name, constraint) {
806 parseSource(name, (isDev, name, source) {
807 var packageName = name.replaceFirst(new RegExp(r"-[^-]+$"), "");
808 var ref = new PackageRef(packageName, source,
809 new VersionConstraint.parse(constraint), name);
810
811 if (isDev) {
812 devDependencies.add(ref);
813 } else {
814 dependencies.add(ref);
815 }
816 });
817 });
818
819 var name = description.replaceFirst(new RegExp(r"-[^-]+$"), "");
820 var pubspec = new Pubspec(
821 name, new Version.parse(version), dependencies, devDependencies,
822 new PubspecEnvironment());
823 return new Package.inMemory(pubspec);
824 }
825
527 void parseSource(String description, 826 void parseSource(String description,
528 callback(bool isDev, String name, Source source)) { 827 callback(bool isDev, String name, Source source)) {
529 var isDev = false; 828 var isDev = false;
530 829
531 if (description.startsWith("(dev) ")) { 830 if (description.startsWith("(dev) ")) {
532 description = description.substring("(dev) ".length); 831 description = description.substring("(dev) ".length);
533 isDev = true; 832 isDev = true;
534 } 833 }
535 834
536 var name = description; 835 var name = description;
537 var source = source1; 836 var source = source1;
538 837
539 var sourceNames = { 838 var sourceNames = {
540 'mock1': source1, 839 'mock1': source1,
541 'mock2': source2, 840 'mock2': source2,
542 'root': null 841 'root': null
543 }; 842 };
544 843
545 var match = new RegExp(r"(.*) from (.*)").firstMatch(description); 844 var match = new RegExp(r"(.*) from (.*)").firstMatch(description);
546 if (match != null) { 845 if (match != null) {
547 name = match[1]; 846 name = match[1];
548 source = sourceNames[match[2]]; 847 source = sourceNames[match[2]];
549 } 848 }
550 849
551 callback(isDev, name, source); 850 callback(isDev, name, source);
552 } 851 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698