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

Side by Side Diff: test/version_solver_test.dart

Issue 2044253003: Refactor Source and SourceRegistry. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Rename LiveSource to BoundSource. Created 4 years, 6 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
« no previous file with comments | « test/test_pub.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:pub/src/lock_file.dart'; 7 import 'package:pub/src/lock_file.dart';
8 import 'package:pub/src/log.dart' as log; 8 import 'package:pub/src/log.dart' as log;
9 import 'package:pub/src/package.dart'; 9 import 'package:pub/src/package.dart';
10 import 'package:pub/src/pubspec.dart'; 10 import 'package:pub/src/pubspec.dart';
11 import 'package:pub/src/sdk.dart' as sdk; 11 import 'package:pub/src/sdk.dart' as sdk;
12 import 'package:pub/src/solver/version_solver.dart'; 12 import 'package:pub/src/solver/version_solver.dart';
13 import 'package:pub/src/source.dart';
13 import 'package:pub/src/source/cached.dart'; 14 import 'package:pub/src/source/cached.dart';
14 import 'package:pub/src/system_cache.dart'; 15 import 'package:pub/src/system_cache.dart';
15 import 'package:pub/src/utils.dart'; 16 import 'package:pub/src/utils.dart';
16 import 'package:pub_semver/pub_semver.dart'; 17 import 'package:pub_semver/pub_semver.dart';
17 import 'package:test/test.dart'; 18 import 'package:test/test.dart';
18 19
19 MockSource source1; 20 MockSource source1;
20 MockSource source2; 21 MockSource source2;
21 22
22 main() { 23 main() {
(...skipping 1079 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 'a': '2.0.0-dev.1' 1103 'a': '2.0.0-dev.1'
1103 }, downgrade: true); 1104 }, downgrade: true);
1104 } 1105 }
1105 1106
1106 testResolve(String description, Map packages, { 1107 testResolve(String description, Map packages, {
1107 Map lockfile, Map overrides, Map result, FailMatcherBuilder error, 1108 Map lockfile, Map overrides, Map result, FailMatcherBuilder error,
1108 int maxTries, bool downgrade: false}) { 1109 int maxTries, bool downgrade: false}) {
1109 if (maxTries == null) maxTries = 1; 1110 if (maxTries == null) maxTries = 1;
1110 1111
1111 test(description, () { 1112 test(description, () {
1112 var cache = new SystemCache('.');
1113 source1 = new MockSource('mock1'); 1113 source1 = new MockSource('mock1');
1114 source2 = new MockSource('mock2'); 1114 source2 = new MockSource('mock2');
1115 cache.register(source1); 1115
1116 cache.register(source2); 1116 var cache = new SystemCache(rootDir: '.');
1117 cache.sources.register(source1);
1118 cache.sources.register(source2);
1117 cache.sources.setDefault(source1.name); 1119 cache.sources.setDefault(source1.name);
1118 1120
1119 // Build the test package graph. 1121 // Build the test package graph.
1120 var root; 1122 var root;
1121 packages.forEach((description, dependencies) { 1123 packages.forEach((description, dependencies) {
1122 var id = parseSpec(description); 1124 var id = parseSpec(description);
1123 var package = mockPackage(id, dependencies, 1125 var package = mockPackage(id, dependencies,
1124 id.name == 'myapp' ? overrides : null); 1126 id.name == 'myapp' ? overrides : null);
1125 if (id.name == 'myapp') { 1127 if (id.name == 'myapp') {
1126 // Don't add the root package to the server, so we can verify that Pub 1128 // Don't add the root package to the server, so we can verify that Pub
1127 // doesn't try to look up information about the local package on the 1129 // doesn't try to look up information about the local package on the
1128 // remote server. 1130 // remote server.
1129 root = package; 1131 root = package;
1130 } else { 1132 } else {
1131 (cache.sources[id.source] as MockSource).addPackage( 1133 (cache.source(id.source) as BoundMockSource)
1132 id.description, package); 1134 .addPackage(id.description, package);
1133 } 1135 }
1134 }); 1136 });
1135 1137
1136 // Clean up the expectation. 1138 // Clean up the expectation.
1137 if (result != null) { 1139 if (result != null) {
1138 var newResult = {}; 1140 var newResult = {};
1139 result.forEach((description, version) { 1141 result.forEach((description, version) {
1140 var id = parseSpec(description, version); 1142 var id = parseSpec(description, version);
1141 newResult[id.name] = id; 1143 newResult[id.name] = id;
1142 }); 1144 });
1143 result = newResult; 1145 result = newResult;
1144 } 1146 }
1145 1147
1146 // Parse the lockfile. 1148 // Parse the lockfile.
1147 var realLockFile; 1149 var realLockFile;
1148 if (lockfile == null) { 1150 if (lockfile == null) {
1149 realLockFile = new LockFile.empty(cache.sources); 1151 realLockFile = new LockFile.empty(cache.sources);
1150 } else { 1152 } else {
1151 realLockFile = new LockFile(lockfile.keys.map((name) { 1153 realLockFile = new LockFile(lockfile.keys.map((name) {
1152 var version = new Version.parse(lockfile[name]); 1154 var version = new Version.parse(lockfile[name]);
1153 return new PackageId(name, source1.name, version, name); 1155 return new PackageId(name, source1.name, version, name);
1154 }), cache.sources); 1156 }), cache.sources);
1155 } 1157 }
1156 1158
1157 // Resolve the versions. 1159 // Resolve the versions.
1158 log.verbosity = log.Verbosity.NONE; 1160 log.verbosity = log.Verbosity.NONE;
1159 var future = resolveVersions( 1161 var future = resolveVersions(
1160 downgrade ? SolveType.DOWNGRADE : SolveType.GET, 1162 downgrade ? SolveType.DOWNGRADE : SolveType.GET,
1161 cache.sources, root, lockFile: realLockFile); 1163 cache, root, lockFile: realLockFile);
1162 1164
1163 var matcher; 1165 var matcher;
1164 if (result != null) { 1166 if (result != null) {
1165 matcher = new SolveSuccessMatcher(result, maxTries); 1167 matcher = new SolveSuccessMatcher(result, maxTries);
1166 } else if (error != null) { 1168 } else if (error != null) {
1167 matcher = error(maxTries); 1169 matcher = error(maxTries);
1168 } 1170 }
1169 1171
1170 expect(future, completion(matcher)); 1172 expect(future, completion(matcher));
1171 }); 1173 });
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1340 } 1342 }
1341 } 1343 }
1342 1344
1343 /// A source used for testing. This both creates mock package objects and acts 1345 /// A source used for testing. This both creates mock package objects and acts
1344 /// as a source for them. 1346 /// as a source for them.
1345 /// 1347 ///
1346 /// In order to support testing packages that have the same name but different 1348 /// In order to support testing packages that have the same name but different
1347 /// descriptions, a package's name is calculated by taking the description 1349 /// descriptions, a package's name is calculated by taking the description
1348 /// string and stripping off any trailing hyphen followed by non-hyphen 1350 /// string and stripping off any trailing hyphen followed by non-hyphen
1349 /// characters. 1351 /// characters.
1350 class MockSource extends CachedSource { 1352 class MockSource extends Source {
1351 final _packages = <String, Map<Version, Package>>{};
1352
1353 /// Keeps track of which package version lists have been requested. Ensures
1354 /// that a source is only hit once for a given package and that pub
1355 /// internally caches the results.
1356 final _requestedVersions = new Set<String>();
1357
1358 /// Keeps track of which package pubspecs have been requested. Ensures that a
1359 /// source is only hit once for a given package and that pub internally
1360 /// caches the results.
1361 final _requestedPubspecs = new Map<String, Set<Version>>();
1362
1363 final String name; 1353 final String name;
1364 final hasMultipleVersions = true; 1354 final hasMultipleVersions = true;
1365 1355
1366 MockSource(this.name); 1356 MockSource(this.name);
1367 1357
1358 BoundSource bind(SystemCache cache) => new BoundMockSource(this, cache);
1359
1368 PackageRef parseRef(String name, description, {String containingPath}) => 1360 PackageRef parseRef(String name, description, {String containingPath}) =>
1369 new PackageRef(name, this.name, description); 1361 new PackageRef(name, this.name, description);
1370 1362
1371 PackageId parseId(String name, Version version, description) => 1363 PackageId parseId(String name, Version version, description) =>
1372 new PackageId(name, this.name, version, description); 1364 new PackageId(name, this.name, version, description);
1373 1365
1374 bool descriptionsEqual(description1, description2) => 1366 bool descriptionsEqual(description1, description2) =>
1375 description1 == description2; 1367 description1 == description2;
1368 }
1369
1370 class BoundMockSource extends CachedSource {
1371 final SystemCache systemCache;
1372
1373 final MockSource source;
1374
1375 final _packages = <String, Map<Version, Package>>{};
1376
1377 /// Keeps track of which package version lists have been requested. Ensures
1378 /// that a source is only hit once for a given package and that pub
1379 /// internally caches the results.
1380 final _requestedVersions = new Set<String>();
1381
1382 /// Keeps track of which package pubspecs have been requested. Ensures that a
1383 /// source is only hit once for a given package and that pub internally
1384 /// caches the results.
1385 final _requestedPubspecs = new Map<String, Set<Version>>();
1386
1387 BoundMockSource(this.source, this.systemCache);
1376 1388
1377 String getDirectory(PackageId id) => '${id.name}-${id.version}'; 1389 String getDirectory(PackageId id) => '${id.name}-${id.version}';
1378 1390
1379 Future<List<PackageId>> doGetVersions(PackageRef ref) async { 1391 Future<List<PackageId>> doGetVersions(PackageRef ref) async {
1380 // Make sure the solver doesn't request the same thing twice. 1392 // Make sure the solver doesn't request the same thing twice.
1381 if (_requestedVersions.contains(ref.description)) { 1393 if (_requestedVersions.contains(ref.description)) {
1382 throw new Exception('Version list for ${ref.description} was already ' 1394 throw new Exception('Version list for ${ref.description} was already '
1383 'requested.'); 1395 'requested.');
1384 } 1396 }
1385 1397
1386 _requestedVersions.add(ref.description); 1398 _requestedVersions.add(ref.description);
1387 1399
1388 if (!_packages.containsKey(ref.description)){ 1400 if (!_packages.containsKey(ref.description)){
1389 throw new Exception('MockSource does not have a package matching ' 1401 throw new Exception('MockSource does not have a package matching '
1390 '"${ref.description}".'); 1402 '"${ref.description}".');
1391 } 1403 }
1392 1404
1393 return _packages[ref.description].values.map((package) { 1405 return _packages[ref.description].values.map((package) {
1394 return new PackageId( 1406 return new PackageId(
1395 ref.name, this.name, package.version, ref.description); 1407 ref.name, source.name, package.version, ref.description);
1396 }).toList(); 1408 }).toList();
1397 } 1409 }
1398 1410
1399 Future<Pubspec> describeUncached(PackageId id) { 1411 Future<Pubspec> describeUncached(PackageId id) {
1400 return new Future.sync(() { 1412 return new Future.sync(() {
1401 // Make sure the solver doesn't request the same thing twice. 1413 // Make sure the solver doesn't request the same thing twice.
1402 if (_requestedPubspecs.containsKey(id.description) && 1414 if (_requestedPubspecs.containsKey(id.description) &&
1403 _requestedPubspecs[id.description].contains(id.version)) { 1415 _requestedPubspecs[id.description].contains(id.version)) {
1404 throw new Exception('Pubspec for $id was already requested.'); 1416 throw new Exception('Pubspec for $id was already requested.');
1405 } 1417 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1512 } 1524 }
1513 1525
1514 var source = "mock1"; 1526 var source = "mock1";
1515 if (match[7] != null) { 1527 if (match[7] != null) {
1516 source = match[7]; 1528 source = match[7];
1517 if (source == "root") source = null; 1529 if (source == "root") source = null;
1518 } 1530 }
1519 1531
1520 return new PackageId(name, source, parsedVersion, description); 1532 return new PackageId(name, source, parsedVersion, description);
1521 } 1533 }
OLDNEW
« no previous file with comments | « test/test_pub.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698