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