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