Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: packages/package_config/test/discovery_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library package_config.discovery_test;
6
7 import "dart:async";
8 import "dart:io";
9 import "package:test/test.dart";
10 import "package:package_config/packages.dart";
11 import "package:package_config/discovery.dart";
12 import "package:path/path.dart" as path;
13
14 const packagesFile = """
15 # A comment
16 foo:file:///dart/packages/foo/
17 bar:http://example.com/dart/packages/bar/
18 baz:packages/baz/
19 """;
20
21 void validatePackagesFile(Packages resolver, Uri location) {
22 expect(resolver, isNotNull);
23 expect(resolver.resolve(pkg("foo", "bar/baz")),
24 equals(Uri.parse("file:///dart/packages/foo/bar/baz")));
25 expect(resolver.resolve(pkg("bar", "baz/qux")),
26 equals(Uri.parse("http://example.com/dart/packages/bar/baz/qux")));
27 expect(resolver.resolve(pkg("baz", "qux/foo")),
28 equals(location.resolve("packages/baz/qux/foo")));
29 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"]));
30 }
31
32 void validatePackagesDir(Packages resolver, Uri location) {
33 // Expect three packages: foo, bar and baz
34 expect(resolver, isNotNull);
35 expect(resolver.resolve(pkg("foo", "bar/baz")),
36 equals(location.resolve("packages/foo/bar/baz")));
37 expect(resolver.resolve(pkg("bar", "baz/qux")),
38 equals(location.resolve("packages/bar/baz/qux")));
39 expect(resolver.resolve(pkg("baz", "qux/foo")),
40 equals(location.resolve("packages/baz/qux/foo")));
41 if (location.scheme == "file") {
42 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"]));
43 } else {
44 expect(() => resolver.packages, throws);
45 }
46 }
47
48
49 Uri pkg(String packageName, String packagePath) {
50 var path;
51 if (packagePath.startsWith('/')) {
52 path = "$packageName$packagePath";
53 } else {
54 path = "$packageName/$packagePath";
55 }
56 return new Uri(scheme: "package", path: path);
57 }
58
59 main() {
60 generalTest(".packages",
61 {".packages": packagesFile,
62 "script.dart": "main(){}",
63 "packages": {"shouldNotBeFound": {}}},
64 (Uri location) async {
65 Packages resolver;
66 resolver = await findPackages(location);
67 validatePackagesFile(resolver, location);
68 resolver = await findPackages(location.resolve("script.dart"));
69 validatePackagesFile(resolver, location);
70 var specificDiscovery = (location.scheme == "file")
71 ? findPackagesFromFile
72 : findPackagesFromNonFile;
73 resolver = await specificDiscovery(location);
74 validatePackagesFile(resolver, location);
75 resolver = await specificDiscovery(location.resolve("script.dart"));
76 validatePackagesFile(resolver, location);
77 });
78
79 generalTest("packages/",
80 {"packages": { "foo": {}, "bar": {}, "baz": {}},
81 "script.dart": "main(){}"},
82 (Uri location) async {
83 Packages resolver;
84 bool isFile = (location.scheme == "file");
85 resolver = await findPackages(location);
86 validatePackagesDir(resolver, location);
87 resolver = await findPackages(location.resolve("script.dart"));
88 validatePackagesDir(resolver, location);
89 var specificDiscovery = isFile
90 ? findPackagesFromFile
91 : findPackagesFromNonFile;
92 resolver = await specificDiscovery(location);
93 validatePackagesDir(resolver, location);
94 resolver = await specificDiscovery(location.resolve("script.dart"));
95 validatePackagesDir(resolver, location);
96 });
97
98 generalTest("underscore packages",
99 {"packages": {"_foo": {}}},
100 (Uri location) async {
101 Packages resolver = await findPackages(location);
102 expect(resolver.resolve(pkg("_foo", "foo.dart")),
103 equals(location.resolve("packages/_foo/foo.dart")));
104 });
105
106 fileTest(".packages recursive",
107 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}},
108 (Uri location) async {
109 Packages resolver;
110 resolver = await findPackages(location.resolve("subdir/"));
111 validatePackagesFile(resolver, location);
112 resolver = await findPackages(location.resolve("subdir/script.dart"));
113 validatePackagesFile(resolver, location);
114 resolver = await findPackagesFromFile(location.resolve("subdir/"));
115 validatePackagesFile(resolver, location);
116 resolver =
117 await findPackagesFromFile(location.resolve("subdir/script.dart"));
118 validatePackagesFile(resolver, location);
119 });
120
121 httpTest(".packages not recursive",
122 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}},
123 (Uri location) async {
124 Packages resolver;
125 var subdir = location.resolve("subdir/");
126 resolver = await findPackages(subdir);
127 validatePackagesDir(resolver, subdir);
128 resolver = await findPackages(subdir.resolve("script.dart"));
129 validatePackagesDir(resolver, subdir);
130 resolver = await findPackagesFromNonFile(subdir);
131 validatePackagesDir(resolver, subdir);
132 resolver = await findPackagesFromNonFile(subdir.resolve("script.dart"));
133 validatePackagesDir(resolver, subdir);
134 });
135
136 fileTest("no packages",
137 {"script.dart": "main(){}"},
138 (Uri location) async {
139 // A file: location with no .packages or packages returns
140 // Packages.noPackages.
141 Packages resolver;
142 resolver = await findPackages(location);
143 expect(resolver, same(Packages.noPackages));
144 resolver = await findPackages(location.resolve("script.dart"));
145 expect(resolver, same(Packages.noPackages));
146 resolver = findPackagesFromFile(location);
147 expect(resolver, same(Packages.noPackages));
148 resolver = findPackagesFromFile(location.resolve("script.dart"));
149 expect(resolver, same(Packages.noPackages));
150 });
151
152 httpTest("no packages",
153 {"script.dart": "main(){}"},
154 (Uri location) async {
155 // A non-file: location with no .packages or packages/:
156 // Assumes a packages dir exists, and resolves relative to that.
157 Packages resolver;
158 resolver = await findPackages(location);
159 validatePackagesDir(resolver, location);
160 resolver = await findPackages(location.resolve("script.dart"));
161 validatePackagesDir(resolver, location);
162 resolver = await findPackagesFromNonFile(location);
163 validatePackagesDir(resolver, location);
164 resolver = await findPackagesFromNonFile(location.resolve("script.dart"));
165 validatePackagesDir(resolver, location);
166 });
167
168 test(".packages w/ loader", () async {
169 Uri location = Uri.parse("krutch://example.com/path/");
170 Future loader(Uri file) async {
171 if (file.path.endsWith(".packages")) {
172 return packagesFile.codeUnits;
173 }
174 throw "not found";
175 }
176 // A non-file: location with no .packages or packages/:
177 // Assumes a packages dir exists, and resolves relative to that.
178 Packages resolver;
179 resolver = await findPackages(location, loader: loader);
180 validatePackagesFile(resolver, location);
181 resolver = await findPackages(location.resolve("script.dart"),
182 loader: loader);
183 validatePackagesFile(resolver, location);
184 resolver = await findPackagesFromNonFile(location, loader: loader);
185 validatePackagesFile(resolver, location);
186 resolver = await findPackagesFromNonFile(location.resolve("script.dart"),
187 loader: loader);
188 validatePackagesFile(resolver, location);
189 });
190
191 test("no packages w/ loader", () async {
192 Uri location = Uri.parse("krutch://example.com/path/");
193 Future loader(Uri file) async {
194 throw "not found";
195 }
196 // A non-file: location with no .packages or packages/:
197 // Assumes a packages dir exists, and resolves relative to that.
198 Packages resolver;
199 resolver = await findPackages(location, loader: loader);
200 validatePackagesDir(resolver, location);
201 resolver = await findPackages(location.resolve("script.dart"),
202 loader: loader);
203 validatePackagesDir(resolver, location);
204 resolver = await findPackagesFromNonFile(location, loader: loader);
205 validatePackagesDir(resolver, location);
206 resolver = await findPackagesFromNonFile(location.resolve("script.dart"),
207 loader:loader);
208 validatePackagesDir(resolver, location);
209 });
210
211 generalTest("loadPackagesFile",
212 {".packages": packagesFile},
213 (Uri directory) async {
214 Uri file = directory.resolve(".packages");
215 Packages resolver = await loadPackagesFile(file);
216 validatePackagesFile(resolver, file);
217 });
218
219 generalTest("loadPackagesFile non-default name",
220 {"pheldagriff": packagesFile},
221 (Uri directory) async {
222 Uri file = directory.resolve("pheldagriff");
223 Packages resolver = await loadPackagesFile(file);
224 validatePackagesFile(resolver, file);
225 });
226
227 test("loadPackagesFile w/ loader", () async {
228 loader(Uri uri) async => packagesFile.codeUnits;
229 Uri file = Uri.parse("krutz://example.com/.packages");
230 Packages resolver = await loadPackagesFile(file, loader: loader);
231 validatePackagesFile(resolver, file);
232 });
233
234 generalTest("loadPackagesFile not found",
235 {},
236 (Uri directory) async {
237 Uri file = directory.resolve(".packages");
238 expect(loadPackagesFile(file), throws);
239 });
240
241 generalTest("loadPackagesFile syntax error",
242 {".packages": "syntax error"},
243 (Uri directory) async {
244 Uri file = directory.resolve(".packages");
245 expect(loadPackagesFile(file), throws);
246 });
247
248 generalTest("getPackagesDir",
249 {"packages": {"foo": {}, "bar": {}, "baz": {}}},
250 (Uri directory) async {
251 Uri packages = directory.resolve("packages/");
252 Packages resolver = getPackagesDirectory(packages);
253 Uri resolved = resolver.resolve(pkg("foo","flip/flop"));
254 expect(resolved, packages.resolve("foo/flip/flop"));
255 });
256 }
257
258 /// Create a directory structure from [description] and run [fileTest].
259 ///
260 /// Description is a map, each key is a file entry. If the value is a map,
261 /// it's a sub-dir, otherwise it's a file and the value is the content
262 /// as a string.
263 void fileTest(String name,
264 Map description,
265 Future fileTest(Uri directory)) {
266 group("file-test", () {
267 Directory tempDir = Directory.systemTemp.createTempSync("file-test");
268 setUp(() {
269 _createFiles(tempDir, description);
270 });
271 tearDown(() {
272 tempDir.deleteSync(recursive: true);
273 });
274 test(name, () => fileTest(new Uri.file(path.join(tempDir.path, "."))));
275 });
276 }
277
278 /// HTTP-server the directory structure from [description] and run [htpTest].
279 ///
280 /// Description is a map, each key is a file entry. If the value is a map,
281 /// it's a sub-dir, otherwise it's a file and the value is the content
282 /// as a string.
283 void httpTest(String name, Map description, Future httpTest(Uri directory)) {
284 group("http-test", () {
285 var serverSub;
286 var uri;
287 setUp(() {
288 return HttpServer
289 .bind(InternetAddress.LOOPBACK_IP_V4, 0)
290 .then((server) {
291 uri = new Uri(scheme: "http",
292 host: "127.0.0.1",
293 port: server.port,
294 path: "/");
295 serverSub = server.listen((HttpRequest request) {
296 // No error handling.
297 var path = request.uri.path;
298 if (path.startsWith('/')) path = path.substring(1);
299 if (path.endsWith('/')) path = path.substring(0, path.length - 1);
300 var parts = path.split('/');
301 var fileOrDir = description;
302 for (int i = 0; i < parts.length; i++) {
303 fileOrDir = fileOrDir[parts[i]];
304 if (fileOrDir == null) {
305 request.response.statusCode = 404;
306 request.response.close();
307 }
308 }
309 request.response.write(fileOrDir);
310 request.response.close();
311 });
312 });
313 });
314 tearDown(() => serverSub.cancel());
315 test(name, () => httpTest(uri));
316 });
317 }
318
319 void generalTest(String name, Map description, Future action(Uri location)) {
320 fileTest(name, description, action);
321 httpTest(name, description, action);
322 }
323
324 void _createFiles(Directory target, Map description) {
325 description.forEach((name, content) {
326 if (content is Map) {
327 Directory subDir = new Directory(path.join(target.path, name));
328 subDir.createSync();
329 _createFiles(subDir, content);
330 } else {
331 File file = new File(path.join(target.path, name));
332 file.writeAsStringSync(content, flush: true);
333 }
334 });
335 }
OLDNEW
« no previous file with comments | « packages/package_config/test/discovery_analysis_test.dart ('k') | packages/package_config/test/parse_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698