Chromium Code Reviews| 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 // See if it's a semver. |
| 39 new Version.parse(version); | |
|
nweiz
2016/09/29 00:32:41
I don't think you want to parse here anymore.
Bob Nystrom
2016/09/29 00:56:41
Oops, done.
| |
| 40 | |
| 41 // If we get here, it's a cache reference. | |
| 43 packagePath = p.join(cachePath, "$package-$version"); | 42 packagePath = p.join(cachePath, "$package-$version"); |
| 44 } else { | 43 } else { |
| 45 // Otherwise it's a path relative to the pubspec file, | 44 // Otherwise it's a path relative to the pubspec file, |
| 46 // which is also relative to the .packages file. | 45 // which is also relative to the .packages file. |
| 47 packagePath = p.fromUri(version); | 46 packagePath = p.fromUri(version); |
| 48 } | 47 } |
| 49 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); | 48 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); |
| 50 }); | 49 }); |
| 51 var buffer = new StringBuffer(); | 50 var buffer = new StringBuffer(); |
| 52 packages_file.write(buffer, mapping); | 51 packages_file.write(buffer, mapping); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 77 // "." due to sdk#23809. | 76 // "." due to sdk#23809. |
| 78 var base = "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p"; | 77 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)); | 78 var map = packages_file.parse(binaryContents, Uri.parse(base)); |
| 80 | 79 |
| 81 for (var package in _dependencies.keys) { | 80 for (var package in _dependencies.keys) { |
| 82 if (!map.containsKey(package)) { | 81 if (!map.containsKey(package)) { |
| 83 fail(".packages does not contain $package entry"); | 82 fail(".packages does not contain $package entry"); |
| 84 } | 83 } |
| 85 | 84 |
| 86 var description = _dependencies[package]; | 85 var description = _dependencies[package]; |
| 87 if (_semverRE.hasMatch(description)) { | 86 if (_isSemver(description)) { |
| 88 if (!map[package].path.contains(description)) { | 87 if (!map[package].path.contains(description)) { |
| 89 fail(".packages of $package has incorrect version. " | 88 fail(".packages of $package has incorrect version. " |
| 90 "Expected $description, found location: ${map[package]}."); | 89 "Expected $description, found location: ${map[package]}."); |
| 91 } | 90 } |
| 92 } else { | 91 } else { |
| 93 var expected = p.normalize(p.join(p.fromUri(description), 'lib')); | 92 var expected = p.normalize(p.join(p.fromUri(description), 'lib')); |
| 94 var actual = p.normalize(p.fromUri( | 93 var actual = p.normalize(p.fromUri( |
| 95 p.url.relative(map[package].toString(), from: p.dirname(base)))); | 94 p.url.relative(map[package].toString(), from: p.dirname(base)))); |
| 96 | 95 |
| 97 if (expected != actual) { | 96 if (expected != actual) { |
| 98 fail("Relative path: Expected $expected, found $actual"); | 97 fail("Relative path: Expected $expected, found $actual"); |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 } | 100 } |
| 102 | 101 |
| 103 if (map.length != _dependencies.length) { | 102 if (map.length != _dependencies.length) { |
| 104 for (var key in map.keys) { | 103 for (var key in map.keys) { |
| 105 if (!_dependencies.containsKey(key)) { | 104 if (!_dependencies.containsKey(key)) { |
| 106 fail(".packages file contains unexpected entry: $key"); | 105 fail(".packages file contains unexpected entry: $key"); |
| 107 } | 106 } |
| 108 } | 107 } |
| 109 } | 108 } |
| 110 } | 109 } |
| 111 | 110 |
| 111 /// Returns `true` if [text] is a valid semantic version number string. | |
| 112 bool _isSemver(String text) { | |
| 113 try { | |
| 114 // See if it's a semver. | |
| 115 new Version.parse(text); | |
| 116 return true; | |
| 117 } on FormatException catch (_) { | |
| 118 // Do nothing. | |
| 119 } | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 112 String describe() => name; | 123 String describe() => name; |
| 113 } | 124 } |
| OLD | NEW |