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