OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:io'; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 import 'package:shelf/shelf.dart' as shelf; |
| 9 import 'package:shelf/shelf_io.dart' as shelf_io; |
| 10 import 'package:test/test.dart'; |
| 11 |
| 12 import 'package:package_resolver/package_resolver.dart'; |
| 13 |
| 14 void main() { |
| 15 var resolver; |
| 16 setUp(() { |
| 17 resolver = new SyncPackageResolver.config({ |
| 18 "foo": Uri.parse("file:///foo/bar/"), |
| 19 "bar": Uri.parse("http://dartlang.org/bar") |
| 20 }, uri: "file:///myapp/.packages"); |
| 21 }); |
| 22 |
| 23 group("constructor", () { |
| 24 test("with a URI object", () { |
| 25 var resolver = new SyncPackageResolver.config({}, |
| 26 uri: Uri.parse("file:///myapp/.packages")); |
| 27 expect(resolver.packageConfigUri, |
| 28 equals(Uri.parse("file:///myapp/.packages"))); |
| 29 }); |
| 30 |
| 31 test("with an invalid URI type", () { |
| 32 expect(() => new SyncPackageResolver.config({}, uri: 12), |
| 33 throwsArgumentError); |
| 34 }); |
| 35 }); |
| 36 |
| 37 test("exposes the config map", () { |
| 38 expect(resolver.packageConfigMap, equals({ |
| 39 "foo": Uri.parse("file:///foo/bar/"), |
| 40 "bar": Uri.parse("http://dartlang.org/bar/") |
| 41 })); |
| 42 }); |
| 43 |
| 44 test("exposes the config URI if passed", () { |
| 45 expect(resolver.packageConfigUri, |
| 46 equals(Uri.parse("file:///myapp/.packages"))); |
| 47 }); |
| 48 |
| 49 test("exposes a data: config URI if none is passed", () { |
| 50 resolver = new SyncPackageResolver.config(resolver.packageConfigMap); |
| 51 expect(resolver.packageConfigUri, equals(Uri.parse( |
| 52 "data:;charset=utf-8," |
| 53 "foo:file:///foo/bar/%0A" |
| 54 "bar:http://dartlang.org/bar/%0A"))); |
| 55 }); |
| 56 |
| 57 test("exposes a null root", () { |
| 58 expect(resolver.packageRoot, isNull); |
| 59 }); |
| 60 |
| 61 test("processArgument uses --packages", () { |
| 62 expect(resolver.processArgument, |
| 63 equals("--packages=file:///myapp/.packages")); |
| 64 }); |
| 65 |
| 66 group("resolveUri", () { |
| 67 test("with a matching package", () { |
| 68 expect(resolver.resolveUri("package:foo/bang/qux.dart"), |
| 69 equals(Uri.parse("file:///foo/bar/bang/qux.dart"))); |
| 70 expect(resolver.resolveUri("package:bar/bang/qux.dart"), |
| 71 equals(Uri.parse("http://dartlang.org/bar/bang/qux.dart"))); |
| 72 }); |
| 73 |
| 74 test("with a matching package with no path", () { |
| 75 expect(resolver.resolveUri("package:foo"), isNull); |
| 76 }); |
| 77 |
| 78 test("with a matching package with an empty path", () { |
| 79 expect(resolver.resolveUri("package:bar/"), |
| 80 equals(Uri.parse("http://dartlang.org/bar/"))); |
| 81 }); |
| 82 |
| 83 test("with a URI object", () { |
| 84 expect(resolver.resolveUri(Uri.parse("package:foo/bang/qux.dart")), |
| 85 equals(Uri.parse("file:///foo/bar/bang/qux.dart"))); |
| 86 }); |
| 87 |
| 88 test("with a non-matching package", () { |
| 89 expect(resolver.resolveUri("package:zap/bang/qux.dart"), isNull); |
| 90 }); |
| 91 |
| 92 test("with an invalid argument type", () { |
| 93 expect(() => resolver.resolveUri(12), throwsArgumentError); |
| 94 }); |
| 95 |
| 96 test("with a non-package URI", () { |
| 97 expect(() => resolver.resolveUri("file:///zip/zap"), |
| 98 throwsFormatException); |
| 99 }); |
| 100 |
| 101 test("with an invalid package URI", () { |
| 102 expect(() => resolver.resolveUri("package:"), throwsFormatException); |
| 103 }); |
| 104 }); |
| 105 |
| 106 group("urlFor", () { |
| 107 test("with a matching package and no path", () { |
| 108 expect(resolver.urlFor("foo"), equals(Uri.parse("file:///foo/bar/"))); |
| 109 expect(resolver.urlFor("bar"), |
| 110 equals(Uri.parse("http://dartlang.org/bar/"))); |
| 111 }); |
| 112 |
| 113 test("with a matching package and a path", () { |
| 114 expect(resolver.urlFor("foo", "bang/qux.dart"), |
| 115 equals(Uri.parse("file:///foo/bar/bang/qux.dart"))); |
| 116 expect(resolver.urlFor("bar", "bang/qux.dart"), |
| 117 equals(Uri.parse("http://dartlang.org/bar/bang/qux.dart"))); |
| 118 }); |
| 119 |
| 120 test("with a non-matching package and no path", () { |
| 121 expect(resolver.urlFor("zap"), isNull); |
| 122 }); |
| 123 }); |
| 124 |
| 125 group("packageUriFor", () { |
| 126 test("converts matching URIs to package:", () { |
| 127 expect(resolver.packageUriFor("file:///foo/bar/bang/qux.dart"), |
| 128 equals(Uri.parse("package:foo/bang/qux.dart"))); |
| 129 expect(resolver.packageUriFor("http://dartlang.org/bar/bang/qux.dart"), |
| 130 equals(Uri.parse("package:bar/bang/qux.dart"))); |
| 131 }); |
| 132 |
| 133 test("converts URIs with no paths", () { |
| 134 expect(resolver.packageUriFor("file:///foo/bar"), |
| 135 equals(Uri.parse("package:foo/"))); |
| 136 expect(resolver.packageUriFor("http://dartlang.org/bar/"), |
| 137 equals(Uri.parse("package:bar/"))); |
| 138 }); |
| 139 |
| 140 test("with a URI object", () { |
| 141 expect(resolver.packageUriFor(Uri.parse("file:///foo/bar/bang/qux.dart")), |
| 142 equals(Uri.parse("package:foo/bang/qux.dart"))); |
| 143 }); |
| 144 |
| 145 test("with an invalid argument type", () { |
| 146 expect(() => resolver.packageUriFor(12), throwsArgumentError); |
| 147 }); |
| 148 }); |
| 149 |
| 150 group("packagePath", () { |
| 151 setUp(() { |
| 152 resolver = new SyncPackageResolver.config({ |
| 153 "foo": p.toUri(p.join(p.current, 'lib')), |
| 154 "bar": Uri.parse("http://dartlang.org/bar") |
| 155 }); |
| 156 }); |
| 157 |
| 158 test("with a matching package", () { |
| 159 expect(resolver.packagePath("foo"), equals(p.current)); |
| 160 }); |
| 161 |
| 162 test("with a package with a non-file scheme", () { |
| 163 expect(resolver.packagePath("bar"), isNull); |
| 164 }); |
| 165 |
| 166 test("with a non-matching", () { |
| 167 expect(resolver.packagePath("baz"), isNull); |
| 168 }); |
| 169 }); |
| 170 |
| 171 group("loadConfig", () { |
| 172 var server; |
| 173 var sandbox; |
| 174 setUp(() async { |
| 175 sandbox = (await Directory.systemTemp.createTemp("package_resolver_test")) |
| 176 .path; |
| 177 }); |
| 178 |
| 179 tearDown(() async { |
| 180 if (server != null) await server.close(); |
| 181 await new Directory(sandbox).delete(recursive: true); |
| 182 }); |
| 183 |
| 184 test("with an http: URI", () async { |
| 185 server = await shelf_io.serve((request) { |
| 186 return new shelf.Response.ok( |
| 187 "foo:file:///foo/bar/\n" |
| 188 "bar:http://dartlang.org/bar/"); |
| 189 }, 'localhost', 0); |
| 190 |
| 191 var resolver = await SyncPackageResolver.loadConfig( |
| 192 "http://localhost:${server.port}"); |
| 193 |
| 194 expect(resolver.packageConfigMap, equals({ |
| 195 "foo": Uri.parse("file:///foo/bar/"), |
| 196 "bar": Uri.parse("http://dartlang.org/bar/") |
| 197 })); |
| 198 expect(resolver.packageConfigUri, |
| 199 equals(Uri.parse("http://localhost:${server.port}"))); |
| 200 }); |
| 201 |
| 202 test("with a file: URI", () async { |
| 203 var packagesPath = p.join(sandbox, ".packages"); |
| 204 new File(packagesPath).writeAsStringSync( |
| 205 "foo:file:///foo/bar/\n" |
| 206 "bar:http://dartlang.org/bar/"); |
| 207 |
| 208 var resolver = |
| 209 await SyncPackageResolver.loadConfig(p.toUri(packagesPath)); |
| 210 |
| 211 expect(resolver.packageConfigMap, equals({ |
| 212 "foo": Uri.parse("file:///foo/bar/"), |
| 213 "bar": Uri.parse("http://dartlang.org/bar/") |
| 214 })); |
| 215 expect(resolver.packageConfigUri, equals(p.toUri(packagesPath))); |
| 216 }); |
| 217 |
| 218 test("with a data: URI", () async { |
| 219 var data = Uri.parse( |
| 220 "data:;charset=utf-8," |
| 221 "foo:file:///foo/bar/%0A" |
| 222 "bar:http://dartlang.org/bar/%0A"); |
| 223 var resolver = await SyncPackageResolver.loadConfig(data); |
| 224 |
| 225 expect(resolver.packageConfigMap, equals({ |
| 226 "foo": Uri.parse("file:///foo/bar/"), |
| 227 "bar": Uri.parse("http://dartlang.org/bar/") |
| 228 })); |
| 229 expect(resolver.packageConfigUri, equals(data)); |
| 230 }); |
| 231 |
| 232 test("with a package: URI", () async { |
| 233 var resolver = await SyncPackageResolver.loadConfig( |
| 234 "package:package_resolver/src/test_package_config"); |
| 235 |
| 236 expect(resolver.packageConfigMap, equals({ |
| 237 "foo": Uri.parse("file:///foo/bar/"), |
| 238 "bar": Uri.parse("http://dartlang.org/bar/") |
| 239 })); |
| 240 expect(resolver.packageConfigUri, equals(Uri.parse( |
| 241 "package:package_resolver/src/test_package_config"))); |
| 242 }); |
| 243 |
| 244 test("with an unsupported scheme", () { |
| 245 expect(SyncPackageResolver.loadConfig("asdf:foo/bar"), |
| 246 throwsUnsupportedError); |
| 247 }); |
| 248 }); |
| 249 } |
OLD | NEW |