| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// Pub-specific scheduled_test descriptors. | 5 /// Pub-specific scheduled_test descriptors. |
| 6 import "dart:io" show File; | 6 import "dart:io" show File; |
| 7 import "dart:async" show Future; | 7 import "dart:async" show Future; |
| 8 import "dart:convert" show UTF8; | 8 import "dart:convert" show UTF8; |
| 9 | 9 |
| 10 import 'package:package_config/packages_file.dart' as packages_file; | 10 import 'package:package_config/packages_file.dart' as packages_file; |
| 11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:pub_semver/pub_semver.dart'; |
| 12 import 'package:scheduled_test/descriptor.dart'; | 13 import 'package:scheduled_test/descriptor.dart'; |
| 13 import 'package:scheduled_test/scheduled_test.dart'; | 14 import 'package:scheduled_test/scheduled_test.dart'; |
| 14 | 15 |
| 15 import '../test_pub.dart'; | 16 import '../test_pub.dart'; |
| 16 | 17 |
| 17 /// Describes a `.packages` file and its contents. | 18 /// Describes a `.packages` file and its contents. |
| 18 class PackagesFileDescriptor extends Descriptor { | 19 class PackagesFileDescriptor extends Descriptor { |
| 19 // RegExp recognizing semantic version numbers. | |
| 20 static final _semverRE = | |
| 21 new RegExp(r"^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)" | |
| 22 r"(?:-[a-zA-Z\d-]+)?(?:\+[a-zA-Z\d-]+)?$"); | |
| 23 | |
| 24 /// A map from package names to strings describing where the packages are | 20 /// A map from package names to strings describing where the packages are |
| 25 /// located on disk. | 21 /// located on disk. |
| 26 final Map<String, String> _dependencies; | 22 final Map<String, String> _dependencies; |
| 27 | 23 |
| 28 /// Describes a `.packages` file with the given dependencies. | 24 /// Describes a `.packages` file with the given dependencies. |
| 29 /// | 25 /// |
| 30 /// [dependencies] maps package names to strings describing where the packages | 26 /// [dependencies] maps package names to strings describing where the packages |
| 31 /// are located on disk. | 27 /// are located on disk. |
| 32 PackagesFileDescriptor([this._dependencies]) : super('.packages'); | 28 PackagesFileDescriptor([this._dependencies]) : super('.packages'); |
| 33 | 29 |
| 34 Future create([String parent]) => schedule(() { | 30 Future create([String parent]) => schedule(() { |
| 35 if (parent == null) parent = defaultRoot; | 31 if (parent == null) parent = defaultRoot; |
| 36 var contents = const <int>[]; | 32 var contents = const <int>[]; |
| 37 if (_dependencies != null) { | 33 if (_dependencies != null) { |
| 38 var mapping = <String, Uri>{}; | 34 var mapping = <String, Uri>{}; |
| 39 _dependencies.forEach((package, version) { | 35 _dependencies.forEach((package, version) { |
| 40 var packagePath; | 36 var packagePath; |
| 41 if (_semverRE.hasMatch(version)) { | 37 if (_isSemver(version)) { |
| 42 // If it's a semver, it's a cache reference. | 38 // It's a cache reference. |
| 43 packagePath = p.join(cachePath, "$package-$version"); | 39 packagePath = p.join(cachePath, "$package-$version"); |
| 44 } else { | 40 } else { |
| 45 // Otherwise it's a path relative to the pubspec file, | 41 // Otherwise it's a path relative to the pubspec file, |
| 46 // which is also relative to the .packages file. | 42 // which is also relative to the .packages file. |
| 47 packagePath = p.fromUri(version); | 43 packagePath = p.fromUri(version); |
| 48 } | 44 } |
| 49 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); | 45 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); |
| 50 }); | 46 }); |
| 51 var buffer = new StringBuffer(); | 47 var buffer = new StringBuffer(); |
| 52 packages_file.write(buffer, mapping); | 48 packages_file.write(buffer, mapping); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 77 // "." due to sdk#23809. | 73 // "." due to sdk#23809. |
| 78 var base = "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p"; | 74 var base = "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p"; |
| 79 var map = packages_file.parse(binaryContents, Uri.parse(base)); | 75 var map = packages_file.parse(binaryContents, Uri.parse(base)); |
| 80 | 76 |
| 81 for (var package in _dependencies.keys) { | 77 for (var package in _dependencies.keys) { |
| 82 if (!map.containsKey(package)) { | 78 if (!map.containsKey(package)) { |
| 83 fail(".packages does not contain $package entry"); | 79 fail(".packages does not contain $package entry"); |
| 84 } | 80 } |
| 85 | 81 |
| 86 var description = _dependencies[package]; | 82 var description = _dependencies[package]; |
| 87 if (_semverRE.hasMatch(description)) { | 83 if (_isSemver(description)) { |
| 88 if (!map[package].path.contains(description)) { | 84 if (!map[package].path.contains(description)) { |
| 89 fail(".packages of $package has incorrect version. " | 85 fail(".packages of $package has incorrect version. " |
| 90 "Expected $description, found location: ${map[package]}."); | 86 "Expected $description, found location: ${map[package]}."); |
| 91 } | 87 } |
| 92 } else { | 88 } else { |
| 93 var expected = p.normalize(p.join(p.fromUri(description), 'lib')); | 89 var expected = p.normalize(p.join(p.fromUri(description), 'lib')); |
| 94 var actual = p.normalize(p.fromUri( | 90 var actual = p.normalize(p.fromUri( |
| 95 p.url.relative(map[package].toString(), from: p.dirname(base)))); | 91 p.url.relative(map[package].toString(), from: p.dirname(base)))); |
| 96 | 92 |
| 97 if (expected != actual) { | 93 if (expected != actual) { |
| 98 fail("Relative path: Expected $expected, found $actual"); | 94 fail("Relative path: Expected $expected, found $actual"); |
| 99 } | 95 } |
| 100 } | 96 } |
| 101 } | 97 } |
| 102 | 98 |
| 103 if (map.length != _dependencies.length) { | 99 if (map.length != _dependencies.length) { |
| 104 for (var key in map.keys) { | 100 for (var key in map.keys) { |
| 105 if (!_dependencies.containsKey(key)) { | 101 if (!_dependencies.containsKey(key)) { |
| 106 fail(".packages file contains unexpected entry: $key"); | 102 fail(".packages file contains unexpected entry: $key"); |
| 107 } | 103 } |
| 108 } | 104 } |
| 109 } | 105 } |
| 110 } | 106 } |
| 111 | 107 |
| 108 /// Returns `true` if [text] is a valid semantic version number string. |
| 109 bool _isSemver(String text) { |
| 110 try { |
| 111 // See if it's a semver. |
| 112 new Version.parse(text); |
| 113 return true; |
| 114 } on FormatException catch (_) { |
| 115 // Do nothing. |
| 116 } |
| 117 return false; |
| 118 } |
| 119 |
| 112 String describe() => name; | 120 String describe() => name; |
| 113 } | 121 } |
| OLD | NEW |