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:async'; |
| 6 import 'dart:convert'; |
| 7 import 'dart:io'; |
| 8 import 'dart:isolate'; |
| 9 |
| 10 import 'package:path/path.dart' as p; |
| 11 import 'package:stack_trace/stack_trace.dart'; |
| 12 import 'package:test/test.dart'; |
| 13 |
| 14 import 'package:package_resolver/package_resolver.dart'; |
| 15 |
| 16 void main() { |
| 17 // It's important to test these, because they use PackageConfig.current and |
| 18 // they're used to verify the output of the inner isolate's |
| 19 // PackageConfig.current. |
| 20 test("_packageResolverLibUri is correct", () async { |
| 21 var libPath = p.fromUri(await _packageResolverLibUri); |
| 22 expect(new File(p.join(libPath, 'package_resolver.dart')).exists(), |
| 23 completion(isTrue)); |
| 24 }); |
| 25 |
| 26 test("_pathLibUri is correct", () async { |
| 27 var libPath = p.fromUri(await _pathLibUri); |
| 28 expect(new File(p.join(libPath, 'path.dart')).exists(), completion(isTrue)); |
| 29 }); |
| 30 |
| 31 group("with a package config", () { |
| 32 var resolver; |
| 33 setUp(() async { |
| 34 var map; |
| 35 var currentIsolateMap = await PackageResolver.current.packageConfigMap; |
| 36 if (currentIsolateMap != null) { |
| 37 map = new Map.from(currentIsolateMap); |
| 38 } else { |
| 39 // If the isolate running this test isn't using package config, create |
| 40 // one from scratch with the same resolution semantics. |
| 41 var map = {}; |
| 42 var root = p.fromUri(await PackageResolver.current.packageRoot); |
| 43 await for (var link in new Directory(root).list(followLinks: false)) { |
| 44 assert(link is Link); |
| 45 map[p.basename(link.path)] = |
| 46 p.toUri(await link.resolveSymbolicLinks()); |
| 47 } |
| 48 } |
| 49 |
| 50 // Ensure that we have at least one URI that ends with "/" and one that |
| 51 // doesn't. Both of these cases need to be tested. |
| 52 expect(map["package_resolver"].path, endsWith("/")); |
| 53 map["path"] = Uri.parse(p.url.normalize(map["path"].toString())); |
| 54 expect(map["path"].path, isNot(endsWith("/"))); |
| 55 |
| 56 resolver = new PackageResolver.config(map); |
| 57 }); |
| 58 |
| 59 test("exposes the config map", () async { |
| 60 expect(await _spawn("""() async { |
| 61 var serializable = {}; |
| 62 (await PackageResolver.current.packageConfigMap) |
| 63 .forEach((package, uri) { |
| 64 serializable[package] = uri.toString(); |
| 65 }); |
| 66 return serializable; |
| 67 }()""", resolver), |
| 68 containsPair("package_resolver", await _packageResolverLibUri)); |
| 69 }); |
| 70 |
| 71 test("exposes the config URI", () async { |
| 72 expect( |
| 73 await _spawn( |
| 74 "(await PackageResolver.current.packageConfigUri).toString()", |
| 75 resolver), |
| 76 equals((await resolver.packageConfigUri).toString())); |
| 77 }); |
| 78 |
| 79 test("exposes a null package root", () async { |
| 80 expect( |
| 81 // Use "== null" because if it *is* a URI, it'll crash the isolate |
| 82 // when we try to send it over the port. |
| 83 await _spawn( |
| 84 "(await PackageResolver.current.packageRoot) == null", resolver), |
| 85 isTrue); |
| 86 }); |
| 87 |
| 88 test("processArgument uses --packages", () async { |
| 89 expect( |
| 90 await _spawn("PackageResolver.current.processArgument", resolver), |
| 91 equals(await resolver.processArgument)); |
| 92 }); |
| 93 |
| 94 group("resolveUri", () { |
| 95 test("with a matching package", () async { |
| 96 expect(await _spawn("""() async { |
| 97 var uri = await PackageResolver.current.resolveUri( |
| 98 'package:package_resolver/foo/bar.dart'); |
| 99 return uri.toString(); |
| 100 }()""", resolver), |
| 101 equals(p.url.join(await _packageResolverLibUri, "foo/bar.dart"))); |
| 102 |
| 103 expect(await _spawn("""() async { |
| 104 var uri = await PackageResolver.current.resolveUri( |
| 105 'package:path/foo/bar.dart'); |
| 106 return uri.toString(); |
| 107 }()""", resolver), |
| 108 equals(p.url.join(await _pathLibUri, "foo/bar.dart"))); |
| 109 }); |
| 110 |
| 111 test("with a matching package with no path", () async { |
| 112 expect(await _spawn("""() async { |
| 113 var uri = await PackageResolver.current.resolveUri( |
| 114 'package:package_resolver'); |
| 115 return uri == null; |
| 116 }()""", resolver), isTrue); |
| 117 |
| 118 expect(await _spawn("""() async { |
| 119 var uri = await PackageResolver.current.resolveUri('package:path'); |
| 120 return uri == null; |
| 121 }()""", resolver), isTrue); |
| 122 }); |
| 123 |
| 124 test("with a matching package with an empty path", |
| 125 () async { |
| 126 expect(await _spawn("""() async { |
| 127 var uri = await PackageResolver.current.resolveUri( |
| 128 'package:package_resolver/'); |
| 129 return uri.toString(); |
| 130 }()""", resolver), (await _packageResolverLibUri).toString()); |
| 131 |
| 132 expect(await _spawn("""() async { |
| 133 var uri = await PackageResolver.current.resolveUri('package:path/'); |
| 134 return uri.toString(); |
| 135 }()""", resolver), (await _pathLibUri).toString()); |
| 136 }); |
| 137 |
| 138 test("with a URI object", () async { |
| 139 expect(await _spawn("""() async { |
| 140 var uri = await PackageResolver.current.resolveUri( |
| 141 Uri.parse('package:package_resolver/foo/bar.dart')); |
| 142 return uri.toString(); |
| 143 }()""", resolver), |
| 144 equals(p.url.join(await _packageResolverLibUri, 'foo/bar.dart'))); |
| 145 }); |
| 146 |
| 147 test("with a non-matching package", () async { |
| 148 expect(await _spawn("""() async { |
| 149 var uri = await PackageResolver.current.resolveUri( |
| 150 Uri.parse('package:not-a-package/foo/bar.dart')); |
| 151 return uri == null; |
| 152 }()""", resolver), isTrue); |
| 153 }); |
| 154 |
| 155 test("with an invalid argument type", () async { |
| 156 expect(await _spawn("""() async { |
| 157 try { |
| 158 await PackageResolver.current.resolveUri(12); |
| 159 return false; |
| 160 } on ArgumentError catch (_) { |
| 161 return true; |
| 162 } |
| 163 }()""", resolver), isTrue); |
| 164 }); |
| 165 |
| 166 test("with a non-package URI", () async { |
| 167 expect(await _spawn("""() async { |
| 168 try { |
| 169 await PackageResolver.current.resolveUri('file:///zip/zap'); |
| 170 return false; |
| 171 } on FormatException catch (_) { |
| 172 return true; |
| 173 } |
| 174 }()""", resolver), isTrue); |
| 175 }); |
| 176 |
| 177 test("with an invalid package URI", () async { |
| 178 expect(await _spawn("""() async { |
| 179 try { |
| 180 await PackageResolver.current.resolveUri("package:"); |
| 181 return false; |
| 182 } on FormatException catch (_) { |
| 183 return true; |
| 184 } |
| 185 }()""", resolver), isTrue); |
| 186 }); |
| 187 }); |
| 188 }); |
| 189 } |
| 190 |
| 191 Future<String> get _packageResolverLibUri async => |
| 192 (await PackageResolver.current.urlFor("package_resolver")).toString(); |
| 193 |
| 194 Future<String> get _pathLibUri async => |
| 195 (await PackageResolver.current.urlFor("path")).toString(); |
| 196 |
| 197 Future _spawn(String expression, PackageResolver packageResolver) async { |
| 198 var data = new UriData.fromString(""" |
| 199 import 'dart:convert'; |
| 200 import 'dart:isolate'; |
| 201 |
| 202 import 'package:package_resolver/package_resolver.dart'; |
| 203 |
| 204 main(_, SendPort message) async { |
| 205 message.send(await ($expression)); |
| 206 } |
| 207 """, mimeType: "application/dart", parameters: {"charset": "utf-8"}); |
| 208 |
| 209 var receivePort = new ReceivePort(); |
| 210 var errorPort = new ReceivePort(); |
| 211 try { |
| 212 var isolate = await Isolate.spawnUri(data.uri, [], receivePort.sendPort, |
| 213 packageRoot: await packageResolver.packageRoot, |
| 214 packageConfig: await packageResolver.packageConfigUri, |
| 215 paused: true); |
| 216 |
| 217 isolate.addErrorListener(errorPort.sendPort); |
| 218 errorPort.listen((message) { |
| 219 registerException(message[0], |
| 220 message[1] == null ? null : new Trace.parse(message[1])); |
| 221 errorPort.close(); |
| 222 receivePort.close(); |
| 223 }); |
| 224 isolate.resume(isolate.pauseCapability); |
| 225 |
| 226 var value = await receivePort.first; |
| 227 isolate.kill(); |
| 228 return value; |
| 229 } finally { |
| 230 errorPort.close(); |
| 231 receivePort.close(); |
| 232 } |
| 233 } |
OLD | NEW |