Index: test/descriptor.dart |
diff --git a/test/descriptor.dart b/test/descriptor.dart |
index 9890ba3685e691572fa65bcd92688f670bd64cce..f5c7601383a84b2b087222ebaeeb243d4a8ed730 100644 |
--- a/test/descriptor.dart |
+++ b/test/descriptor.dart |
@@ -5,11 +5,16 @@ |
/// Pub-specific scheduled_test descriptors. |
library descriptor; |
+import "dart:io" show File; |
+ |
import 'package:oauth2/oauth2.dart' as oauth2; |
+import 'package:path/path.dart' as p; |
import 'package:pub/src/io.dart'; |
import 'package:pub/src/utils.dart'; |
import 'package:scheduled_test/descriptor.dart'; |
import 'package:scheduled_test/scheduled_server.dart'; |
+import 'package:package_config/packages_file.dart' as packages_file; |
+import 'package:stack_trace/stack_trace.dart'; |
import 'descriptor/git.dart'; |
import 'descriptor/tar.dart'; |
@@ -182,3 +187,80 @@ Descriptor credentialsFile( |
/// the given [dependencies]. |
DirectoryDescriptor appDir([Map dependencies]) => |
dir(appPath, [appPubspec(dependencies)]); |
+ |
+/// Describes a `.packages` file. |
+/// |
+/// The [dependencies] maps package names to version strings. |
nweiz
2015/06/12 23:48:08
Remove "The"
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
|
+/// |
+/// Validation checks that the `.packages` file exists, has the expected |
+/// entries (one per key in []) with a path that contains the version string. |
nweiz
2015/06/12 23:48:08
"[]" -> "[dependencies]"
Lasse Reichstein Nielsen
2015/06/23 14:54:34
Done.
|
+Descriptor packagesFile([Map dependencies]) => |
+ new _PackagesFileDescriptor(dependencies); |
+ |
+class _PackagesFileDescriptor extends Descriptor { |
nweiz
2015/06/12 23:48:08
This is big enough now it probably warrants its ow
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
|
+ final _dependencies; |
+ |
+ _PackagesFileDescriptor([Map<String, String> contents]) |
+ : _dependencies = contents, super('.packages'); |
+ |
+ Future create([String parent]) => schedule(() { |
+ if (parent == null) parent = defaultRoot; |
+ var contents = const <int>[]; |
+ if (_dependencies != null) { |
+ var mapping = {}; |
+ _dependencies.forEach((k, v) { |
nweiz
2015/06/12 23:48:08
Nit: use full words for variables. Also, something
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
|
+ mapping[k] = p.toUri(p.join(cachePath, "$k-$v", "lib", "")); |
+ }); |
+ var buffer = new StringBuffer(); |
+ packages_file.write(buffer, mapping); |
+ contents = UTF8.encode(buffer.toString()); |
+ } |
+ return Chain.track(new File(p.join(parent, name)) |
nweiz
2015/06/12 23:48:08
Chain.track isn't necessary here anymore.
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
|
+ .writeAsBytes(contents)); |
+ }, "creating file '$name'"); |
+ |
+ Future validate([String parent]) => |
+ schedule(() => validateNow(parent), "validating file '$name'"); |
+ |
+ Future validateNow([String parent]) { |
+ // Copied from FileDescriptor in scheduled_test. |
+ if (parent == null) parent = defaultRoot; |
+ var fullPath = p.join(parent, name); |
+ if (!new File(fullPath).existsSync()) { |
+ fail("File not found: '$fullPath'."); |
+ } |
+ return Chain.track(new File(fullPath).readAsBytes()) |
nweiz
2015/06/12 23:48:08
Ditto.
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
|
+ .then((bytes) => _validateNow(bytes, fullPath)); |
+ } |
+ |
+ // TODO(nweiz): rather than setting up an inheritance chain, just store a |
+ // Matcher for validation. This would require better error messages from the |
+ // matcher library, though. |
nweiz
2015/06/12 23:48:08
Remove this TODO.
Lasse Reichstein Nielsen
2015/06/23 14:54:34
Done.
|
+ /// A function that throws an error if [binaryContents] doesn't match the |
+ /// expected contents of the descriptor. |
+ void _validateNow(List<int> binaryContents, String fullPath) { |
+ var fileUri = p.toUri(fullPath); |
+ var map = packages_file.parse(binaryContents, fileUri); |
+ |
+ for (var packageName in _dependencies.keys) { |
+ if (!map.containsKey(packageName)) { |
+ fail(".packages does not contain $packageName entry"); |
+ } |
+ var version = _dependencies[packageName]; |
+ if (!map[packageName].path.contains(version)) { |
+ fail(".packages of $packageName has incorrect version. " |
+ "Expected $version, found location: ${map[packageName]}."); |
+ } |
+ } |
+ |
+ if (map.length != _dependencies.length) { |
+ for (var key in map.keys) { |
+ if (!_dependencies.containsKey(key)) { |
+ fail(".packages file contains unexpected entry: $key"); |
+ } |
+ } |
+ } |
+ } |
+ |
+ String describe() => name; |
+} |