Chromium Code Reviews| Index: test/discovery_test.dart |
| diff --git a/test/discovery_test.dart b/test/discovery_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..44ad2754e0c4208a662bab95035b4f8cb12a8eff |
| --- /dev/null |
| +++ b/test/discovery_test.dart |
| @@ -0,0 +1,249 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "dart:async"; |
| +import "dart:io"; |
| +import "package:test/test.dart"; |
| +import "package:package_config/packages.dart"; |
| +import "package:package_config/discovery.dart"; |
| +import "package:path/path.dart" as path; |
| + |
| +const packagesFile = """ |
| +# A comment |
| +foo=file:///dart/packages/foo/ |
| +bar=http://example.com/dart/packages/bar/ |
| +baz=packages/baz/ |
| +"""; |
| + |
| +void validatePackagesFile(Packages resolver, Uri location) { |
| + expect(resolver, isNotNull); |
| + expect(resolver.resolve(pkg("foo", "bar/baz")), |
| + equals(Uri.parse("file:///dart/packages/foo/bar/baz"))); |
| + expect(resolver.resolve(pkg("bar", "baz/qux")), |
| + equals(Uri.parse("http://example.com/dart/packages/bar/baz/qux"))); |
| + expect(resolver.resolve(pkg("baz", "qux/foo")), |
| + equals(location.resolve("packages/baz/qux/foo"))); |
| + expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"])); |
| +} |
| + |
| +void validatePackagesDir(Packages resolver, Uri location, {bool enumerate: true}) { |
|
Søren Gjesse
2015/05/26 08:11:53
Long line.
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Done.
|
| + // Expect three packages: foo, bar and baz |
| + expect(resolver, isNotNull); |
| + expect(resolver.resolve(pkg("foo", "bar/baz")), |
| + equals(location.resolve("packages/foo/bar/baz"))); |
| + expect(resolver.resolve(pkg("bar", "baz/qux")), |
| + equals(location.resolve("packages/bar/baz/qux"))); |
| + expect(resolver.resolve(pkg("baz", "qux/foo")), |
| + equals(location.resolve("packages/baz/qux/foo"))); |
| + if (enumerate) { |
| + expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"])); |
| + } else { |
| + expect(() => resolver.packages, throws); |
| + } |
| +} |
| + |
| + |
| +Uri pkg(String packageName, String packagePath) { |
| + var path; |
| + if (packagePath.startsWith('/')) { |
| + path = "$packageName$packagePath"; |
| + } else { |
| + path = "$packageName/$packagePath"; |
| + } |
| + return new Uri(scheme: "package", path: path); |
| +} |
| + |
| +main() { |
|
Søren Gjesse
2015/05/26 08:11:53
Can't you always make a fileTest and httpTest from
Lasse Reichstein Nielsen
2015/05/26 09:24:30
For the directly accessed .package files and packa
|
| + fileTest(".packages", |
| + {".packages": packagesFile, "script.dart": "main(){}"}, |
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + validatePackagesFile(resolver, location); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + validatePackagesFile(resolver, location); |
| + resolver = findPackagesFromFile(location); |
| + validatePackagesFile(resolver, location); |
| + resolver = findPackagesFromFile(location.resolve("script.dart")); |
| + validatePackagesFile(resolver, location); |
| + }); |
| + |
| + fileTest(".packages recursive", |
| + {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}}, |
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location.resolve("subdir/")); |
| + validatePackagesFile(resolver, location); |
| + resolver = await findPackages(location.resolve("subdir/script.dart")); |
| + validatePackagesFile(resolver, location); |
| + resolver = findPackagesFromFile(location.resolve("subdir/")); |
| + validatePackagesFile(resolver, location); |
| + resolver = findPackagesFromFile(location.resolve("subdir/script.dart")); |
| + validatePackagesFile(resolver, location); |
| + }); |
| + |
| + fileTest("packages/", |
| + {"packages": { "foo": {}, "bar": {}, "baz": {}}, "script.dart": "main(){}"}, |
|
Søren Gjesse
2015/05/26 08:11:53
Long line.
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Done.
|
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + validatePackagesDir(resolver, location); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location); |
| + resolver = findPackagesFromFile(location); |
| + validatePackagesDir(resolver, location); |
| + resolver = findPackagesFromFile(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location); |
| + }); |
| + |
| + fileTest("no packages", |
| + {"script.dart": "main(){}"}, |
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + expect(resolver, same(Packages.noPackages)); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + expect(resolver, same(Packages.noPackages)); |
| + resolver = findPackagesFromFile(location); |
| + expect(resolver, same(Packages.noPackages)); |
| + resolver = findPackagesFromFile(location.resolve("script.dart")); |
| + expect(resolver, same(Packages.noPackages)); |
| + }); |
| + |
| + httpTest(".packages", |
| + {".packages": packagesFile, "script.dart": "main(){}"}, |
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + validatePackagesFile(resolver, location); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + validatePackagesFile(resolver, location); |
| + resolver = await findPackagesFromNonFile(location); |
|
Søren Gjesse
2015/05/26 08:11:53
Not related to this CL. but what is a "NonFile" lo
Lasse Reichstein Nielsen
2015/05/26 09:24:29
It's any URI that doesn't start with "file:".
|
| + validatePackagesFile(resolver, location); |
| + resolver = await findPackagesFromNonFile(location.resolve("script.dart")); |
| + validatePackagesFile(resolver, location); |
| + }); |
| + |
| + httpTest(".packages not from subdir", |
|
Søren Gjesse
2015/05/26 08:11:53
The file version of this test is called '.packages
Lasse Reichstein Nielsen
2015/05/26 09:24:29
So this one should be "not recursive". Fixed.
|
| + {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}}, |
| + (Uri location) async { |
| + Packages resolver; |
| + var subdir = location.resolve("subdir/"); |
| + resolver = await findPackages(subdir); |
| + validatePackagesDir(resolver, subdir, enumerate: false); |
| + resolver = await findPackages(subdir.resolve("script.dart")); |
| + validatePackagesDir(resolver, subdir, enumerate: false); |
| + resolver = await findPackagesFromNonFile(subdir); |
| + validatePackagesDir(resolver, subdir, enumerate: false); |
| + resolver = await findPackagesFromNonFile(subdir.resolve("script.dart")); |
| + validatePackagesDir(resolver, subdir, enumerate: false); |
| + }); |
| + |
| + httpTest("packages dir", |
| + {"packages": {"foo":{}, "bar":{}, "baz":{}}, |
| + "script.dart": "main(){}"}, |
| + (Uri location) async { |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackagesFromNonFile(location); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackagesFromNonFile(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + }); |
| + |
| + httpTest("no packages", |
| + {"script.dart": "main(){}"}, |
| + (Uri location) async { |
| + // Assumes a packages dir exists, and resolves relative to that. |
| + Packages resolver; |
| + resolver = await findPackages(location); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackages(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackagesFromNonFile(location); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + resolver = await findPackagesFromNonFile(location.resolve("script.dart")); |
| + validatePackagesDir(resolver, location, enumerate: false); |
| + }); |
| +} |
| + |
| +/// Create a directory structure from [description] and run [fileTest]. |
| +/// |
| +/// Description is a map, each key is a file entry. If the value is a map, |
| +/// it's a sub-dir, otherwise it's a file and the value is the content |
| +/// as a string. |
| +void fileTest(String name, |
| + Map description, |
| + Future fileTest(Uri directory)) { |
| + group("file-test", () { |
| + Directory tempDir = Directory.systemTemp.createTempSync("file-test"); |
| + setUp(() { |
| + _createFiles(tempDir, description); |
| + }); |
| + tearDown(() { |
| + tempDir.deleteSync(recursive: true); |
| + }); |
| + test(name, () => fileTest(new Uri.directory(tempDir.path))); |
| + }); |
| +} |
| + |
| +/// HTTP-server the directory structure from [description] and run [htpTest]. |
| +/// |
| +/// Description is a map, each key is a file entry. If the value is a map, |
| +/// it's a sub-dir, otherwise it's a file and the value is the content |
| +/// as a string. |
| +void httpTest(String name, Map description, Future httpTest(Uri directory)) { |
| + group("http-test", () { |
| + var serverSub; |
| + var uri; |
| + setUp(() { |
| + return HttpServer |
| + .bind(InternetAddress.LOOPBACK_IP_V4, 0) |
| + .then((server) { |
| + uri = new Uri(scheme: "http", |
| + host: "127.0.0.1", |
| + port: server.port, |
| + path: "/"); |
| + serverSub = server.listen((HttpRequest request) { |
| + // No error handling. |
| + var path = request.uri.path; |
| + if (path.startsWith('/')) path = path.substring(1); |
| + if (path.endsWith('/')) path = path.substring(0, path.length - 1); |
| + var parts = path.split('/'); |
| + var fileOrDir = description; |
| + for (int i = 0; i < parts.length; i++) { |
| + fileOrDir = fileOrDir[parts[i]]; |
| + if (fileOrDir == null) { |
| + request.response.statusCode = 404; |
| + request.response.close(); |
| + } |
| + } |
| + request.response.write(fileOrDir); |
|
Søren Gjesse
2015/05/26 08:11:53
How does this work of fileOrDir refers to a subdir
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Should probably make it an explicit error. It won'
|
| + request.response.close(); |
| + }); |
| + }); |
| + }); |
| + tearDown(() { serverSub.cancel(); }); |
| + test(name, () => httpTest(uri)); |
| + }); |
| +} |
| + |
| + |
| +void _createFiles(Directory target, Map description) { |
| + description.forEach((name, content) { |
| + if (content is Map) { |
| + Directory subDir = new Directory(path.join(target.path, name)); |
| + subDir.createSync(); |
| + _createFiles(subDir, content); |
| + } else { |
| + File file = new File(path.join(target.path, name)); |
| + file.writeAsStringSync(content, flush: true); |
| + } |
| + }); |
| +} |
| + |
| + |