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

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: Clean up some test code. 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 '../../../pkg/unittest/lib/unittest.dart'; 10 import '../../../pkg/unittest/lib/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/version_solver2.dart';
21 import 'test_pub.dart'; 21 import 'test_pub.dart';
22 22
23 Matcher noVersion(List<String> packages) { 23 Matcher noVersion(List<String> packages) {
24 return predicate((x) { 24 return predicate((x) {
25 if (x is! NoVersionException) return false; 25 if (x is! NoVersionException) return false;
26 26
27 // Make sure the error string mentions the conflicting dependers. 27 // Make sure the error string mentions the conflicting dependers.
28 var message = x.toString(); 28 var message = x.toString();
29 return packages.every((package) => message.contains(package)); 29 return packages.every((package) => message.contains(package));
30 }, "is a NoVersionException"); 30 }, "is a NoVersionException");
(...skipping 14 matching lines...) Expand all
45 if (x is! DescriptionMismatchException) return false; 45 if (x is! DescriptionMismatchException) return false;
46 46
47 // Make sure the error string mentions the conflicting dependers. 47 // Make sure the error string mentions the conflicting dependers.
48 if (!x.toString().contains(package1)) return false; 48 if (!x.toString().contains(package1)) return false;
49 if (!x.toString().contains(package2)) return false; 49 if (!x.toString().contains(package2)) return false;
50 50
51 return true; 51 return true;
52 }, "is a DescriptionMismatchException"); 52 }, "is a DescriptionMismatchException");
53 } 53 }
54 54
55 final couldNotSolve = predicate((x) => x is CouldNotSolveException, 55 // If no solution can be found, the solver just reports the last failure that
56 "is a CouldNotSolveException"); 56 // happened during propagation. Since we don't specify the order that solutions
57 // are tried, this just validates that *some* failure occurred, but not which.
58 final couldNotSolve = predicate((x) => x is SolverFailure,
59 "is a SolverFailure");
57 60
58 Matcher sourceMismatch(String package1, String package2) { 61 Matcher sourceMismatch(String package1, String package2) {
59 return predicate((x) { 62 return predicate((x) {
60 if (x is! SourceMismatchException) return false; 63 if (x is! SourceMismatchException) return false;
61 64
62 // Make sure the error string mentions the conflicting dependers. 65 // Make sure the error string mentions the conflicting dependers.
63 if (!x.toString().contains(package1)) return false; 66 if (!x.toString().contains(package1)) return false;
64 if (!x.toString().contains(package2)) return false; 67 if (!x.toString().contains(package2)) return false;
65 68
66 return true; 69 return true;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 'bar 1.0.2': {}, 201 'bar 1.0.2': {},
199 'baz 1.0.0': {} 202 'baz 1.0.0': {}
200 }, lockfile: { 203 }, lockfile: {
201 'baz': '1.0.0' 204 'baz': '1.0.0'
202 }, result: { 205 }, result: {
203 'myapp from root': '0.0.0', 206 'myapp from root': '0.0.0',
204 'foo': '1.0.2', 207 'foo': '1.0.2',
205 'bar': '1.0.2' 208 'bar': '1.0.2'
206 }); 209 });
207 210
211 testResolve('unlocks dependencies if necessary to ensure that a new '
212 'dependency is satisfied', {
213 'myapp 0.0.0': {
214 'foo': 'any',
215 'newdep': 'any'
216 },
217 'foo 1.0.0': { 'bar': '<2.0.0' },
218 'bar 1.0.0': { 'baz': '<2.0.0' },
219 'baz 1.0.0': { 'qux': '<2.0.0' },
220 'qux 1.0.0': {},
221 'foo 2.0.0': { 'bar': '<3.0.0' },
222 'bar 2.0.0': { 'baz': '<3.0.0' },
223 'baz 2.0.0': { 'qux': '<3.0.0' },
224 'qux 2.0.0': {},
225 'newdep 2.0.0': { 'baz': '>=1.5.0' }
226 }, lockfile: {
227 'foo': '1.0.0',
228 'bar': '1.0.0',
229 'baz': '1.0.0',
230 'qux': '1.0.0'
231 }, result: {
232 'myapp from root': '0.0.0',
233 'foo': '2.0.0',
234 'bar': '2.0.0',
235 'baz': '2.0.0',
236 'qux': '1.0.0',
237 'newdep': '2.0.0'
238 });
239
208 testResolve('circular dependency', { 240 testResolve('circular dependency', {
209 'myapp 1.0.0': { 241 'myapp 1.0.0': {
210 'foo': '1.0.0' 242 'foo': '1.0.0'
211 }, 243 },
212 'foo 1.0.0': { 244 'foo 1.0.0': {
213 'bar': '1.0.0' 245 'bar': '1.0.0'
214 }, 246 },
215 'bar 1.0.0': { 247 'bar 1.0.0': {
216 'foo': '1.0.0' 248 'foo': '1.0.0'
217 } 249 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 } 290 }
259 }, error: sourceMismatch('foo', 'bar')); 291 }, error: sourceMismatch('foo', 'bar'));
260 292
261 testResolve('dependency back onto root package with wrong version', { 293 testResolve('dependency back onto root package with wrong version', {
262 'myapp 1.0.0': { 294 'myapp 1.0.0': {
263 'foo': '1.0.0' 295 'foo': '1.0.0'
264 }, 296 },
265 'foo 1.0.0': { 297 'foo 1.0.0': {
266 'myapp': '<1.0.0' 298 'myapp': '<1.0.0'
267 } 299 }
268 }, error: disjointConstraint(['foo'])); 300 }, error: noVersion(['foo']));
269 301
270 testResolve('no version that matches requirement', { 302 testResolve('no version that matches requirement', {
271 'myapp 0.0.0': { 303 'myapp 0.0.0': {
272 'foo': '>=1.0.0 <2.0.0' 304 'foo': '>=1.0.0 <2.0.0'
273 }, 305 },
274 'foo 2.0.0': {}, 306 'foo 2.0.0': {},
275 'foo 2.1.3': {} 307 'foo 2.1.3': {}
276 }, error: noVersion(['myapp'])); 308 }, error: noVersion(['myapp']));
277 309
278 testResolve('no version that matches combined constraint', { 310 testResolve('no version that matches combined constraint', {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 'foo 1.0.0': { 360 'foo 1.0.0': {
329 'shared': '1.0.0' 361 'shared': '1.0.0'
330 }, 362 },
331 'bar 1.0.0': { 363 'bar 1.0.0': {
332 'shared from mock2': '1.0.0' 364 'shared from mock2': '1.0.0'
333 }, 365 },
334 'shared 1.0.0': {}, 366 'shared 1.0.0': {},
335 'shared 1.0.0 from mock2': {} 367 'shared 1.0.0 from mock2': {}
336 }, error: sourceMismatch('foo', 'bar')); 368 }, error: sourceMismatch('foo', 'bar'));
337 369
338 testResolve('unstable dependency graph', { 370 // This used to fail on the old solver because it requires downgrading an
371 // earlier-selected dependency (a) in order to find a solution.
372 testResolve('circular dependency on older version', {
339 'myapp 0.0.0': { 373 'myapp 0.0.0': {
340 'a': '>=1.0.0' 374 'a': '>=1.0.0'
341 }, 375 },
342 'a 1.0.0': {}, 376 'a 1.0.0': {},
343 'a 2.0.0': { 377 'a 2.0.0': {
344 'b': '1.0.0' 378 'b': '1.0.0'
345 }, 379 },
346 'b 1.0.0': { 380 'b 1.0.0': {
347 'a': '1.0.0' 381 'a': '1.0.0'
348 } 382 }
383 }, result: {
384 'myapp from root': '0.0.0',
385 'a': '1.0.0'
386 });
387
388 testResolve('unsolvable', {
389 'myapp 0.0.0': {
390 'a': 'any',
391 'b': 'any'
392 },
393 'a 1.0.0': {
394 'b': '1.0.0'
395 },
396 'a 2.0.0': {
397 'b': '2.0.0'
398 },
399 'b 1.0.0': {
400 'a': '2.0.0'
401 },
402 'b 2.0.0': {
403 'a': '1.0.0'
404 }
349 }, error: couldNotSolve); 405 }, error: couldNotSolve);
350 406
351 group('dev dependencies', () { 407 group('dev dependencies', () {
352 testResolve("includes root package's dev dependencies", { 408 testResolve("includes root package's dev dependencies", {
353 'myapp 1.0.0': { 409 'myapp 1.0.0': {
354 '(dev) foo': '1.0.0', 410 '(dev) foo': '1.0.0',
355 '(dev) bar': '1.0.0' 411 '(dev) bar': '1.0.0'
356 }, 412 },
357 'foo 1.0.0': {}, 413 'foo 1.0.0': {},
358 'bar 1.0.0': {} 414 'bar 1.0.0': {}
(...skipping 23 matching lines...) Expand all
382 }, 438 },
383 'foo 1.0.0': { 439 'foo 1.0.0': {
384 '(dev) bar': '1.0.0' 440 '(dev) bar': '1.0.0'
385 }, 441 },
386 'bar 1.0.0': {} 442 'bar 1.0.0': {}
387 }, result: { 443 }, result: {
388 'myapp from root': '1.0.0', 444 'myapp from root': '1.0.0',
389 'foo': '1.0.0' 445 'foo': '1.0.0'
390 }); 446 });
391 }); 447 });
448
449 group('backtracking', () {
450 testResolve('simple backtracking', {
451 'myapp 0.0.0': {
452 'foo': 'any'
453 },
454 'foo 1.0.0': {
455 'bar': '1.0.0'
456 },
457 'foo 2.0.0': {
458 'bar': '2.0.0'
459 },
460 'foo 3.0.0': {
461 'bar': '3.0.0'
462 },
463 'bar 1.0.0': {
464 'baz': 'any'
465 },
466 'bar 2.0.0': {
467 'baz': '2.0.0'
468 },
469 'bar 3.0.0': {
470 'baz': '3.0.0'
471 },
472 'baz 1.0.0': {}
473 }, result: {
474 'myapp from root': '0.0.0',
475 'foo': '1.0.0',
476 'bar': '1.0.0',
477 'baz': '1.0.0'
478 });
479
480 // Pathological example.
nweiz 2013/03/29 01:58:25 Pathological in what sense?
481 // TODO(rnystrom): Document better.
nweiz 2013/03/29 01:58:25 Please do. I don't understand the purpose of this
Bob Nystrom 2013/03/30 00:15:55 Added better docs.
482 var map = {
483 'myapp 0.0.0': {
484 'foo': 'any',
485 'bar': 'any'
486 },
487 'baz 0.0.0': {}
488 };
489
490 for (var i = 0; i < 10; i++) {
491 for (var j = 0; j < 10; j++) {
492 map['foo $i.$j.0'] = {'baz': '$i.0.0'};
493 map['bar $i.$j.0'] = {'baz': '0.$j.0'};
494 }
495 }
496
497 testResolve('nasty backtrack', map, result: {
498 'myapp from root': '0.0.0',
499 'foo': '0.9.0',
500 'bar': '9.0.0',
501 'baz': '0.0.0'
502 });
503 });
nweiz 2013/03/29 01:58:25 It seems like a lot more tests are necessarily to
nweiz 2013/04/03 00:28:42 You don't necessarily need to add these in this CL
Bob Nystrom 2013/04/08 22:12:59 Agreed. I've added some, but I'd like to add more
Bob Nystrom 2013/04/08 22:12:59 Done.
392 } 504 }
393 505
394 // TODO(rnystrom): More stuff to test: 506 // TODO(rnystrom): More stuff to test:
395 // - Depending on a non-existent package. 507 // - Depending on a non-existent package.
396 // - Test that only a certain number requests are sent to the mock source so we 508 // - Test that only a certain number requests are sent to the mock source so we
397 // can keep track of server traffic. 509 // can keep track of server traffic.
398 510
511 solo_testResolve(description, packages, {lockfile, result, Matcher error}) {
512 _testResolve(solo_test, description, packages,
513 lockfile: lockfile, result: result, error: error);
514 }
515
399 testResolve(description, packages, {lockfile, result, Matcher error}) { 516 testResolve(description, packages, {lockfile, result, Matcher error}) {
400 test(description, () { 517 _testResolve(test, description, packages,
518 lockfile: lockfile, result: result, error: error);
519 }
520
521 _testResolve(testFn, description, packages, {lockfile, result, Matcher error}) {
522 testFn(description, () {
401 var cache = new SystemCache('.'); 523 var cache = new SystemCache('.');
402 source1 = new MockSource('mock1'); 524 source1 = new MockSource('mock1');
403 source2 = new MockSource('mock2'); 525 source2 = new MockSource('mock2');
404 cache.register(source1); 526 cache.register(source1);
405 cache.register(source2); 527 cache.register(source2);
406 cache.sources.setDefault(source1.name); 528 cache.sources.setDefault(source1.name);
407 529
408 // Build the test package graph. 530 // Build the test package graph.
409 var root; 531 var root;
410 packages.forEach((nameVersion, dependencies) { 532 packages.forEach((nameVersion, dependencies) {
411 var parsed = parseSource(nameVersion, (isDev, nameVersion, source) { 533 var parsed = parseSource(nameVersion, (isDev, nameVersion, source) {
412 var parts = nameVersion.split(' '); 534 var parts = nameVersion.split(' ');
413 var name = parts[0]; 535 var name = parts[0];
414 var version = parts[1]; 536 var version = parts[1];
415 537
416 var package = source1.mockPackage(name, version, dependencies); 538 var package = mockPackage(name, version, dependencies);
417 if (name == 'myapp') { 539 if (name == 'myapp') {
418 // Don't add the root package to the server, so we can verify that Pub 540 // 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 541 // doesn't try to look up information about the local package on the
420 // remote server. 542 // remote server.
421 root = package; 543 root = package;
422 } else { 544 } else {
423 source.addPackage(package); 545 source.addPackage(name, package);
424 } 546 }
425 }); 547 });
426 }); 548 });
427 549
428 // Clean up the expectation. 550 // Clean up the expectation.
429 if (result != null) { 551 if (result != null) {
430 var newResult = {}; 552 var newResult = {};
431 result.forEach((name, version) { 553 result.forEach((name, version) {
432 parseSource(name, (isDev, name, source) { 554 parseSource(name, (isDev, name, source) {
433 version = new Version.parse(version); 555 version = new Version.parse(version);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 } 587 }
466 588
467 /// A source used for testing. This both creates mock package objects and acts 589 /// A source used for testing. This both creates mock package objects and acts
468 /// as a source for them. 590 /// as a source for them.
469 /// 591 ///
470 /// In order to support testing packages that have the same name but different 592 /// 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 593 /// descriptions, a package's name is calculated by taking the description
472 /// string and stripping off any trailing hyphen followed by non-hyphen 594 /// string and stripping off any trailing hyphen followed by non-hyphen
473 /// characters. 595 /// characters.
474 class MockSource extends Source { 596 class MockSource extends Source {
475 final Map<String, Map<Version, Package>> _packages; 597 final _packages = <String, Map<Version, Package>>{};
598
599 /// Keeps track of which package version lists have been requested. Ensures
600 /// that a source is only hit once for a given package and that pub
601 /// internally caches the results.
602 final _requestedVersions = new Set<String>();
603
604 /// Keeps track of which package pubspecs have been requested. Ensures that a
605 /// source is only hit once for a given package and that pub internally
606 /// caches the results.
607 final _requestedPubspecs = new Map<String, Set<Version>>();
476 608
477 final String name; 609 final String name;
478 bool get shouldCache => true; 610 bool get shouldCache => true;
479 611
480 MockSource(this.name) 612 MockSource(this.name);
481 : _packages = <String, Map<Version, Package>>{};
482 613
483 Future<List<Version>> getVersions(String name, String description) { 614 Future<List<Version>> getVersions(String name, String description) {
484 return defer(() => _packages[description].keys.toList()); 615 return defer(() {
616 // Make sure the solver doesn't request the same thing twice.
617 if (_requestedVersions.contains(description)) {
618 throw 'Version list for $description was already requested.';
619 }
620
621 _requestedVersions.add(description);
622
623 if (!_packages.containsKey(description)){
624 throw 'MockSource does not have a package matching "$description".';
625 }
626 return _packages[description].keys.toList();
627 });
485 } 628 }
486 629
487 Future<Pubspec> describe(PackageId id) { 630 Future<Pubspec> describe(PackageId id) {
488 return defer(() { 631 return defer(() {
489 return _packages[id.name][id.version].pubspec; 632 // Make sure the solver doesn't request the same thing twice.
633 if (_requestedPubspecs.containsKey(id.description) &&
634 _requestedPubspecs[id.description].contains(id.version)) {
635 throw 'Pubspec for $id was already requested.';
636 }
637
638 _requestedPubspecs.putIfAbsent(id.description, () => new Set<Version>());
639 _requestedPubspecs[id.description].add(id.version);
640
641 return _packages[id.description][id.version].pubspec;
490 }); 642 });
491 } 643 }
492 644
493 Future<bool> install(PackageId id, String path) { 645 Future<bool> install(PackageId id, String path) {
494 throw 'no'; 646 throw 'no';
495 } 647 }
496 648
497 Package mockPackage(String description, String version, 649 void addPackage(String description, Package package) {
498 Map dependencyStrings) { 650 _packages.putIfAbsent(description, () => new Map<Version, Package>());
499 // Build the pubspec dependencies. 651 _packages[description][package.version] = package;
500 var dependencies = <PackageRef>[];
501 var devDependencies = <PackageRef>[];
502
503 dependencyStrings.forEach((name, constraint) {
504 parseSource(name, (isDev, name, source) {
505 var packageName = name.replaceFirst(new RegExp(r"-[^-]+$"), "");
506 var ref = new PackageRef(packageName, source,
507 new VersionConstraint.parse(constraint), name);
508
509 if (isDev) {
510 devDependencies.add(ref);
511 } else {
512 dependencies.add(ref);
513 }
514 });
515 });
516
517 var pubspec = new Pubspec(
518 description, new Version.parse(version), dependencies, devDependencies,
519 new PubspecEnvironment());
520 return new Package.inMemory(pubspec);
521 }
522
523 void addPackage(Package package) {
524 _packages.putIfAbsent(package.name, () => new Map<Version, Package>());
525 _packages[package.name][package.version] = package;
526 } 652 }
527 } 653 }
528 654
655 Package mockPackage(String description, String version,
656 Map dependencyStrings) {
657 // Build the pubspec dependencies.
658 var dependencies = <PackageRef>[];
659 var devDependencies = <PackageRef>[];
660
661 dependencyStrings.forEach((name, constraint) {
662 parseSource(name, (isDev, name, source) {
663 var packageName = name.replaceFirst(new RegExp(r"-[^-]+$"), "");
664 var ref = new PackageRef(packageName, source,
665 new VersionConstraint.parse(constraint), name);
666
667 if (isDev) {
668 devDependencies.add(ref);
669 } else {
670 dependencies.add(ref);
671 }
672 });
673 });
674
675 var name = description.replaceFirst(new RegExp(r"-[^-]+$"), "");
676 var pubspec = new Pubspec(
677 name, new Version.parse(version), dependencies, devDependencies,
678 new PubspecEnvironment());
679 return new Package.inMemory(pubspec);
680 }
681
529 void parseSource(String description, 682 void parseSource(String description,
530 callback(bool isDev, String name, Source source)) { 683 callback(bool isDev, String name, Source source)) {
531 var isDev = false; 684 var isDev = false;
532 685
533 if (description.startsWith("(dev) ")) { 686 if (description.startsWith("(dev) ")) {
534 description = description.substring("(dev) ".length); 687 description = description.substring("(dev) ".length);
535 isDev = true; 688 isDev = true;
536 } 689 }
537 690
538 var name = description; 691 var name = description;
539 var source = source1; 692 var source = source1;
540 693
541 var sourceNames = { 694 var sourceNames = {
542 'mock1': source1, 695 'mock1': source1,
543 'mock2': source2, 696 'mock2': source2,
544 'root': null 697 'root': null
545 }; 698 };
546 699
547 var match = new RegExp(r"(.*) from (.*)").firstMatch(description); 700 var match = new RegExp(r"(.*) from (.*)").firstMatch(description);
548 if (match != null) { 701 if (match != null) {
549 name = match[1]; 702 name = match[1];
550 source = sourceNames[match[2]]; 703 source = sourceNames[match[2]];
551 } 704 }
552 705
553 callback(isDev, name, source); 706 callback(isDev, name, source);
554 } 707 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698