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 library descriptor.packages_file; | 6 library descriptor.packages_file; |
7 | 7 |
8 import "dart:io" show File; | 8 import "dart:io" show File; |
9 import "dart:async" show Future; | 9 import "dart:async" show Future; |
10 import "dart:convert" show UTF8; | 10 import "dart:convert" show UTF8; |
11 | 11 |
12 import 'package:package_config/packages_file.dart' as packages_file; | 12 import 'package:package_config/packages_file.dart' as packages_file; |
13 import 'package:path/path.dart' as p; | 13 import 'package:path/path.dart' as p; |
14 import 'package:scheduled_test/descriptor.dart'; | 14 import 'package:scheduled_test/descriptor.dart'; |
15 import 'package:scheduled_test/scheduled_test.dart'; | 15 import 'package:scheduled_test/scheduled_test.dart'; |
16 | 16 |
17 import '../test_pub.dart'; | 17 import '../test_pub.dart'; |
18 | 18 |
19 /// Describes a `.packages` file and its contents. | 19 /// Describes a `.packages` file and its contents. |
20 class PackagesFileDescriptor extends Descriptor { | 20 class PackagesFileDescriptor extends Descriptor { |
21 // RegExp recognizing semantic version numbers. | 21 // RegExp recognizing semantic version numbers. |
22 static final _semverRE = | 22 static final _semverRE = |
23 new RegExp(r"^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)" | 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-]+)?$"); | 24 r"(?:-[a-zA-Z\d-]+)?(?:\+[a-zA-Z\d-]+)?$"); |
25 | 25 |
26 final _dependencies; | 26 /// A map from package names to strings describing where the packages are |
| 27 /// located on disk. |
| 28 final Map<String, String> _dependencies; |
27 | 29 |
28 /// Describes a `.packages` file with the given dependencies. | 30 /// Describes a `.packages` file with the given dependencies. |
29 /// | 31 /// |
30 /// Dependencies maps package name to semantic version. | 32 /// [dependencies] maps package names to strings describing where the packages |
31 PackagesFileDescriptor([Map<String, String> dependencies]) | 33 /// are located on disk. |
32 : _dependencies = dependencies, | 34 PackagesFileDescriptor([this._dependencies]) : super('.packages'); |
33 super('.packages'); | |
34 | 35 |
35 Future create([String parent]) => schedule(() { | 36 Future create([String parent]) => schedule(() { |
36 if (parent == null) parent = defaultRoot; | 37 if (parent == null) parent = defaultRoot; |
37 var contents = const <int>[]; | 38 var contents = const <int>[]; |
38 if (_dependencies != null) { | 39 if (_dependencies != null) { |
39 var mapping = {}; | 40 var mapping = {}; |
40 _dependencies.forEach((package, version) { | 41 _dependencies.forEach((package, version) { |
41 var packagePath; | 42 var packagePath; |
42 if (_semverRE.hasMatch(version)) { | 43 if (_semverRE.hasMatch(version)) { |
43 // If it's a semver, it's a cache reference. | 44 // If it's a semver, it's a cache reference. |
44 packagePath = p.join(cachePath, "$package-$version"); | 45 packagePath = p.join(cachePath, "$package-$version"); |
45 } else { | 46 } else { |
46 // Otherwise it's a path relative to the .pubspec file, | 47 // Otherwise it's a path relative to the .pubspec file, |
47 // which is also the relative path wrt. the .packages file. | 48 // which is also the relative path wrt. the .packages file. |
48 packagePath = version; | 49 packagePath = p.fromUri(version); |
49 } | 50 } |
50 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); | 51 mapping[package] = p.toUri(p.join(packagePath, "lib", "")); |
51 }); | 52 }); |
52 var buffer = new StringBuffer(); | 53 var buffer = new StringBuffer(); |
53 packages_file.write(buffer, mapping); | 54 packages_file.write(buffer, mapping); |
54 contents = UTF8.encode(buffer.toString()); | 55 contents = UTF8.encode(buffer.toString()); |
55 } | 56 } |
56 return new File(p.join(parent, name)).writeAsBytes(contents); | 57 return new File(p.join(parent, name)).writeAsBytes(contents); |
57 }, "creating file '$name'"); | 58 }, "creating file '$name'"); |
58 | 59 |
(...skipping 14 matching lines...) Expand all Loading... |
73 /// A function that throws an error if [binaryContents] doesn't match the | 74 /// A function that throws an error if [binaryContents] doesn't match the |
74 /// expected contents of the descriptor. | 75 /// expected contents of the descriptor. |
75 void _validateNow(List<int> binaryContents, String fullPath) { | 76 void _validateNow(List<int> binaryContents, String fullPath) { |
76 var fileUri = p.toUri(fullPath); | 77 var fileUri = p.toUri(fullPath); |
77 var map = packages_file.parse(binaryContents, fileUri); | 78 var map = packages_file.parse(binaryContents, fileUri); |
78 | 79 |
79 for (var package in _dependencies.keys) { | 80 for (var package in _dependencies.keys) { |
80 if (!map.containsKey(package)) { | 81 if (!map.containsKey(package)) { |
81 fail(".packages does not contain $package entry"); | 82 fail(".packages does not contain $package entry"); |
82 } | 83 } |
83 var version = _dependencies[package]; | 84 |
84 if (_semverRE.hasMatch(version)) { | 85 var description = _dependencies[package]; |
85 if (!map[package].path.contains(version)) { | 86 if (_semverRE.hasMatch(description)) { |
| 87 if (!map[package].path.contains(description)) { |
86 fail(".packages of $package has incorrect version. " | 88 fail(".packages of $package has incorrect version. " |
87 "Expected $version, found location: ${map[package]}."); | 89 "Expected $description, found location: ${map[package]}."); |
88 } | 90 } |
89 } else { | 91 } else { |
90 var packagePath = fileUri.resolve("$version/lib/"); | 92 var expected = p.normalize(p.join( |
91 if (!packagePath.path.endsWith('/')) { | 93 p.dirname(fullPath), p.fromUri(description), 'lib')); |
92 packagePath = packagePath.replace(path: packagePath.path + '/'); | 94 expected = new File(expected).resolveSymbolicLinksSync(); |
93 } | 95 var actual = new File(p.normalize(p.absolute(p.fromUri(map[package])))) |
94 if ("${map[package]}" != "$packagePath") { | 96 .resolveSymbolicLinksSync(); |
95 fail("Relative path: Expected $packagePath, found ${map[package]}"); | 97 |
| 98 if (expected != actual) { |
| 99 fail("Relative path: Expected $description, found ${map[package]}"); |
96 } | 100 } |
97 } | 101 } |
98 } | 102 } |
99 | 103 |
100 if (map.length != _dependencies.length) { | 104 if (map.length != _dependencies.length) { |
101 for (var key in map.keys) { | 105 for (var key in map.keys) { |
102 if (!_dependencies.containsKey(key)) { | 106 if (!_dependencies.containsKey(key)) { |
103 fail(".packages file contains unexpected entry: $key"); | 107 fail(".packages file contains unexpected entry: $key"); |
104 } | 108 } |
105 } | 109 } |
106 } | 110 } |
107 } | 111 } |
108 | 112 |
109 String describe() => name; | 113 String describe() => name; |
110 } | 114 } |
OLD | NEW |