OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library package_config.discovery_analysis_test; |
| 6 |
| 7 import "dart:async"; |
| 8 import "dart:io"; |
| 9 |
| 10 import "package:package_config/discovery_analysis.dart"; |
| 11 import "package:package_config/packages.dart"; |
| 12 import "package:path/path.dart" as path; |
| 13 import "package:test/test.dart"; |
| 14 |
| 15 main() { |
| 16 fileTest("basic", |
| 17 {".packages": packagesFile, |
| 18 "foo": {".packages": packagesFile}, |
| 19 "bar": {"packages": {"foo": {}, "bar":{}, "baz": {}}}, |
| 20 "baz": {}}, |
| 21 (Directory directory) { |
| 22 var dirUri = new Uri.directory(directory.path); |
| 23 PackageContext ctx = PackageContext.findAll(directory); |
| 24 PackageContext root = ctx[directory]; |
| 25 expect(root, same(ctx)); |
| 26 validatePackagesFile(root.packages, dirUri); |
| 27 var fooDir = sub(directory, "foo"); |
| 28 PackageContext foo = ctx[fooDir]; |
| 29 expect(identical(root, foo), isFalse); |
| 30 validatePackagesFile(foo.packages, dirUri.resolve("foo/")); |
| 31 var barDir = sub(directory, "bar"); |
| 32 PackageContext bar = ctx[sub(directory, "bar")]; |
| 33 validatePackagesDir(bar.packages, dirUri.resolve("bar/")); |
| 34 PackageContext barbar = ctx[sub(barDir, "bar")]; |
| 35 expect(barbar, same(bar)); // inherited. |
| 36 PackageContext baz = ctx[sub(directory, "baz")]; |
| 37 expect(baz, same(root)); // inherited. |
| 38 |
| 39 var map = ctx.asMap(); |
| 40 expect(map.keys.map((dir) => dir.path), |
| 41 unorderedEquals([directory.path, fooDir.path, barDir.path])); |
| 42 }); |
| 43 } |
| 44 |
| 45 Directory sub(Directory parent, String dirName) { |
| 46 return new Directory(path.join(parent.path, dirName)); |
| 47 } |
| 48 |
| 49 const packagesFile = """ |
| 50 # A comment |
| 51 foo:file:///dart/packages/foo/ |
| 52 bar:http://example.com/dart/packages/bar/ |
| 53 baz:packages/baz/ |
| 54 """; |
| 55 |
| 56 void validatePackagesFile(Packages resolver, Uri location) { |
| 57 expect(resolver, isNotNull); |
| 58 expect(resolver.resolve(pkg("foo", "bar/baz")), |
| 59 equals(Uri.parse("file:///dart/packages/foo/bar/baz"))); |
| 60 expect(resolver.resolve(pkg("bar", "baz/qux")), |
| 61 equals(Uri.parse("http://example.com/dart/packages/bar/baz/qux"))); |
| 62 expect(resolver.resolve(pkg("baz", "qux/foo")), |
| 63 equals(location.resolve("packages/baz/qux/foo"))); |
| 64 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"])); |
| 65 } |
| 66 |
| 67 void validatePackagesDir(Packages resolver, Uri location) { |
| 68 // Expect three packages: foo, bar and baz |
| 69 expect(resolver, isNotNull); |
| 70 expect(resolver.resolve(pkg("foo", "bar/baz")), |
| 71 equals(location.resolve("packages/foo/bar/baz"))); |
| 72 expect(resolver.resolve(pkg("bar", "baz/qux")), |
| 73 equals(location.resolve("packages/bar/baz/qux"))); |
| 74 expect(resolver.resolve(pkg("baz", "qux/foo")), |
| 75 equals(location.resolve("packages/baz/qux/foo"))); |
| 76 if (location.scheme == "file") { |
| 77 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"])); |
| 78 } else { |
| 79 expect(() => resolver.packages, throws); |
| 80 } |
| 81 } |
| 82 |
| 83 Uri pkg(String packageName, String packagePath) { |
| 84 var path; |
| 85 if (packagePath.startsWith('/')) { |
| 86 path = "$packageName$packagePath"; |
| 87 } else { |
| 88 path = "$packageName/$packagePath"; |
| 89 } |
| 90 return new Uri(scheme: "package", path: path); |
| 91 } |
| 92 |
| 93 /// Create a directory structure from [description] and run [fileTest]. |
| 94 /// |
| 95 /// Description is a map, each key is a file entry. If the value is a map, |
| 96 /// it's a sub-dir, otherwise it's a file and the value is the content |
| 97 /// as a string. |
| 98 void fileTest(String name, |
| 99 Map description, |
| 100 Future fileTest(Directory directory)) { |
| 101 group("file-test", () { |
| 102 Directory tempDir = Directory.systemTemp.createTempSync("file-test"); |
| 103 setUp(() { |
| 104 _createFiles(tempDir, description); |
| 105 }); |
| 106 tearDown(() { |
| 107 tempDir.deleteSync(recursive: true); |
| 108 }); |
| 109 test(name, () => fileTest(tempDir)); |
| 110 }); |
| 111 } |
| 112 |
| 113 void _createFiles(Directory target, Map description) { |
| 114 description.forEach((name, content) { |
| 115 if (content is Map) { |
| 116 Directory subDir = new Directory(path.join(target.path, name)); |
| 117 subDir.createSync(); |
| 118 _createFiles(subDir, content); |
| 119 } else { |
| 120 File file = new File(path.join(target.path, name)); |
| 121 file.writeAsStringSync(content, flush: true); |
| 122 } |
| 123 }); |
| 124 } |
OLD | NEW |