Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /// Pub-specific scheduled_test descriptors. | |
| 6 library descriptor.packages_file; | |
| 7 | |
| 8 import "dart:io" show File; | |
| 9 import "dart:async" show Future; | |
| 10 import "dart:convert" show UTF8; | |
| 11 | |
| 12 import 'package:package_config/packages_file.dart' as packages_file; | |
| 13 import 'package:path/path.dart' as p; | |
| 14 import 'package:scheduled_test/descriptor.dart'; | |
| 15 import 'package:scheduled_test/scheduled_test.dart'; | |
| 16 | |
| 17 import '../test_pub.dart'; | |
| 18 | |
|
nweiz
2015/06/23 19:56:43
Nit: extra newline.
| |
| 19 | |
| 20 /// Describes a `.packages` file and its contents. | |
| 21 class PackagesFileDescriptor extends Descriptor { | |
| 22 // RegExp recognizing semantic version numbers. | |
| 23 static final _semverRE = | |
| 24 new RegExp(r"^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)" | |
| 25 r"(?:-[a-zA-Z\d-]+)?(?:\+[a-zA-Z\d-]+)?$"); | |
| 26 | |
| 27 final _dependencies; | |
| 28 | |
| 29 /// Describes a `.packages` file with the given dependencies. | |
| 30 /// | |
| 31 /// Dependencies maps package name to semantic version. | |
| 32 PackagesFileDescriptor([Map<String, String> dependencies]) | |
| 33 : _dependencies = dependencies, | |
| 34 super('.packages'); | |
| 35 | |
| 36 Future create([String parent]) => schedule(() { | |
| 37 if (parent == null) parent = defaultRoot; | |
| 38 var contents = const <int>[]; | |
| 39 if (_dependencies != null) { | |
| 40 var mapping = {}; | |
| 41 _dependencies.forEach((package, version) { | |
| 42 var packagePath; | |
| 43 if (_semverRE.hasMatch(version)) { | |
| 44 // If it's a semver, it's a cache reference. | |
| 45 packagePath = p.join(cachePath, "$package-$version"); | |
| 46 } else { | |
| 47 // Otherwise it's a path relative to the .pubspec file, | |
| 48 // which is also the relative path wrt. the .packages file. | |
| 49 packagePath = version; | |
| 50 } | |
| 51 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); | |
| 52 }); | |
| 53 var buffer = new StringBuffer(); | |
| 54 packages_file.write(buffer, mapping); | |
| 55 contents = UTF8.encode(buffer.toString()); | |
| 56 } | |
| 57 return new File(p.join(parent, name)).writeAsBytes(contents); | |
| 58 }, "creating file '$name'"); | |
| 59 | |
| 60 Future validate([String parent]) => | |
| 61 schedule(() => validateNow(parent), "validating file '$name'"); | |
| 62 | |
| 63 Future validateNow([String parent]) { | |
| 64 // Copied from FileDescriptor in scheduled_test. | |
| 65 if (parent == null) parent = defaultRoot; | |
| 66 var fullPath = p.join(parent, name); | |
| 67 if (!new File(fullPath).existsSync()) { | |
| 68 fail("File not found: '$fullPath'."); | |
| 69 } | |
| 70 return new File(fullPath).readAsBytes() | |
| 71 .then((bytes) => _validateNow(bytes, fullPath)); | |
| 72 } | |
| 73 | |
| 74 /// A function that throws an error if [binaryContents] doesn't match the | |
| 75 /// expected contents of the descriptor. | |
| 76 void _validateNow(List<int> binaryContents, String fullPath) { | |
| 77 var fileUri = p.toUri(fullPath); | |
| 78 var map = packages_file.parse(binaryContents, fileUri); | |
| 79 | |
| 80 for (var package in _dependencies.keys) { | |
| 81 if (!map.containsKey(package)) { | |
| 82 fail(".packages does not contain $package entry"); | |
| 83 } | |
| 84 var version = _dependencies[package]; | |
| 85 if (_semverRE.hasMatch(version)) { | |
| 86 if (!map[package].path.contains(version)) { | |
| 87 fail(".packages of $package has incorrect version. " | |
| 88 "Expected $version, found location: ${map[package]}."); | |
| 89 } | |
| 90 } else { | |
| 91 var packagePath = fileUri.resolve("$version/lib/"); | |
| 92 if (!packagePath.path.endsWith('/')) { | |
| 93 packagePath = packagePath.replace(path: packagePath.path + '/'); | |
| 94 } | |
| 95 if ("${map[package]}" != "$packagePath") { | |
| 96 fail("Relative path: Expected $packagePath, found ${map[package]}"); | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 if (map.length != _dependencies.length) { | |
| 102 for (var key in map.keys) { | |
| 103 if (!_dependencies.containsKey(key)) { | |
| 104 fail(".packages file contains unexpected entry: $key"); | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 String describe() => name; | |
| 111 } | |
| OLD | NEW |