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

Side by Side Diff: test/discovery_test.dart

Issue 1152173005: Add more tests (Closed) Base URL: https://github.com/dart-lang/package_config.git@master
Patch Set: Add test to all.dart Created 5 years, 7 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
« no previous file with comments | « test/all.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 import "dart:async";
6 import "dart:io";
7 import "package:test/test.dart";
8 import "package:package_config/packages.dart";
9 import "package:package_config/discovery.dart";
10 import "package:path/path.dart" as path;
11
12 const packagesFile = """
13 # A comment
14 foo=file:///dart/packages/foo/
15 bar=http://example.com/dart/packages/bar/
16 baz=packages/baz/
17 """;
18
19 void validatePackagesFile(Packages resolver, Uri location) {
20 expect(resolver, isNotNull);
21 expect(resolver.resolve(pkg("foo", "bar/baz")),
22 equals(Uri.parse("file:///dart/packages/foo/bar/baz")));
23 expect(resolver.resolve(pkg("bar", "baz/qux")),
24 equals(Uri.parse("http://example.com/dart/packages/bar/baz/qux")));
25 expect(resolver.resolve(pkg("baz", "qux/foo")),
26 equals(location.resolve("packages/baz/qux/foo")));
27 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"]));
28 }
29
30 void validatePackagesDir(Packages resolver, Uri location) {
31 // Expect three packages: foo, bar and baz
32 expect(resolver, isNotNull);
33 expect(resolver.resolve(pkg("foo", "bar/baz")),
34 equals(location.resolve("packages/foo/bar/baz")));
35 expect(resolver.resolve(pkg("bar", "baz/qux")),
36 equals(location.resolve("packages/bar/baz/qux")));
37 expect(resolver.resolve(pkg("baz", "qux/foo")),
38 equals(location.resolve("packages/baz/qux/foo")));
39 if (location.scheme == "file") {
40 expect(resolver.packages, unorderedEquals(["foo", "bar", "baz"]));
41 } else {
42 expect(() => resolver.packages, throws);
43 }
44 }
45
46
47 Uri pkg(String packageName, String packagePath) {
48 var path;
49 if (packagePath.startsWith('/')) {
50 path = "$packageName$packagePath";
51 } else {
52 path = "$packageName/$packagePath";
53 }
54 return new Uri(scheme: "package", path: path);
55 }
56
57 main() {
58 generalTest(".packages",
59 {".packages": packagesFile,
60 "script.dart": "main(){}",
61 "packages": {"shouldNotBeFound": {}}},
62 (Uri location) async {
63 Packages resolver;
64 resolver = await findPackages(location);
65 validatePackagesFile(resolver, location);
66 resolver = await findPackages(location.resolve("script.dart"));
67 validatePackagesFile(resolver, location);
68 var specificDiscovery = (location.scheme == "file")
69 ? findPackagesFromFile
70 : findPackagesFromNonFile;
71 resolver = await specificDiscovery(location);
72 validatePackagesFile(resolver, location);
73 resolver = await specificDiscovery(location.resolve("script.dart"));
74 validatePackagesFile(resolver, location);
75 });
76
77 generalTest("packages/",
78 {"packages": { "foo": {}, "bar": {}, "baz": {}},
79 "script.dart": "main(){}"},
80 (Uri location) async {
81 Packages resolver;
82 bool isFile = (location.scheme == "file");
83 resolver = await findPackages(location);
84 validatePackagesDir(resolver, location);
85 resolver = await findPackages(location.resolve("script.dart"));
86 validatePackagesDir(resolver, location);
87 var specificDiscovery = isFile
88 ? findPackagesFromFile
89 : findPackagesFromNonFile;
90 resolver = await specificDiscovery(location);
91 validatePackagesDir(resolver, location);
92 resolver = await specificDiscovery(location.resolve("script.dart"));
93 validatePackagesDir(resolver, location);
94 });
95
96 fileTest(".packages recursive",
97 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}},
98 (Uri location) async {
99 Packages resolver;
100 resolver = await findPackages(location.resolve("subdir/"));
101 validatePackagesFile(resolver, location);
102 resolver = await findPackages(location.resolve("subdir/script.dart"));
103 validatePackagesFile(resolver, location);
104 resolver = await findPackagesFromFile(location.resolve("subdir/"));
105 validatePackagesFile(resolver, location);
106 resolver =
107 await findPackagesFromFile(location.resolve("subdir/script.dart"));
108 validatePackagesFile(resolver, location);
109 });
110
111 httpTest(".packages not recursive",
112 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}},
113 (Uri location) async {
114 Packages resolver;
115 var subdir = location.resolve("subdir/");
116 resolver = await findPackages(subdir);
117 validatePackagesDir(resolver, subdir);
118 resolver = await findPackages(subdir.resolve("script.dart"));
119 validatePackagesDir(resolver, subdir);
120 resolver = await findPackagesFromNonFile(subdir);
121 validatePackagesDir(resolver, subdir);
122 resolver = await findPackagesFromNonFile(subdir.resolve("script.dart"));
123 validatePackagesDir(resolver, subdir);
124 });
125
126 fileTest("no packages",
127 {"script.dart": "main(){}"},
128 (Uri location) async {
129 // A file: location with no .packages or packages returns
130 // Packages.noPackages.
131 Packages resolver;
132 resolver = await findPackages(location);
133 expect(resolver, same(Packages.noPackages));
134 resolver = await findPackages(location.resolve("script.dart"));
135 expect(resolver, same(Packages.noPackages));
136 resolver = findPackagesFromFile(location);
137 expect(resolver, same(Packages.noPackages));
138 resolver = findPackagesFromFile(location.resolve("script.dart"));
139 expect(resolver, same(Packages.noPackages));
140 });
141
142 httpTest("no packages",
143 {"script.dart": "main(){}"},
144 (Uri location) async {
145 // A non-file: location with no .packages or packages/:
146 // Assumes a packages dir exists, and resolves relative to that.
147 Packages resolver;
148 resolver = await findPackages(location);
149 validatePackagesDir(resolver, location);
150 resolver = await findPackages(location.resolve("script.dart"));
151 validatePackagesDir(resolver, location);
152 resolver = await findPackagesFromNonFile(location);
153 validatePackagesDir(resolver, location);
154 resolver = await findPackagesFromNonFile(location.resolve("script.dart"));
155 validatePackagesDir(resolver, location);
156 });
157
158 test(".packages w/ loader", () async {
159 Uri location = Uri.parse("krutch://example.com/path/");
160 Future loader(Uri file) async {
161 if (file.path.endsWith(".packages")) {
162 return packagesFile.codeUnits;
163 }
164 throw "not found";
165 }
166 // A non-file: location with no .packages or packages/:
167 // Assumes a packages dir exists, and resolves relative to that.
168 Packages resolver;
169 resolver = await findPackages(location, loader: loader);
170 validatePackagesFile(resolver, location);
171 resolver = await findPackages(location.resolve("script.dart"),
172 loader: loader);
173 validatePackagesFile(resolver, location);
174 resolver = await findPackagesFromNonFile(location, loader: loader);
175 validatePackagesFile(resolver, location);
176 resolver = await findPackagesFromNonFile(location.resolve("script.dart"),
177 loader: loader);
178 validatePackagesFile(resolver, location);
179 });
180
181 test("no packages w/ loader", () async {
182 Uri location = Uri.parse("krutch://example.com/path/");
183 Future loader(Uri file) async {
184 throw "not found";
185 }
186 // A non-file: location with no .packages or packages/:
187 // Assumes a packages dir exists, and resolves relative to that.
188 Packages resolver;
189 resolver = await findPackages(location, loader: loader);
190 validatePackagesDir(resolver, location);
191 resolver = await findPackages(location.resolve("script.dart"),
192 loader: loader);
193 validatePackagesDir(resolver, location);
194 resolver = await findPackagesFromNonFile(location, loader: loader);
195 validatePackagesDir(resolver, location);
196 resolver = await findPackagesFromNonFile(location.resolve("script.dart"),
197 loader:loader);
198 validatePackagesDir(resolver, location);
199 });
200 }
201
202 /// Create a directory structure from [description] and run [fileTest].
203 ///
204 /// Description is a map, each key is a file entry. If the value is a map,
205 /// it's a sub-dir, otherwise it's a file and the value is the content
206 /// as a string.
207 void fileTest(String name,
208 Map description,
209 Future fileTest(Uri directory)) {
210 group("file-test", () {
211 Directory tempDir = Directory.systemTemp.createTempSync("file-test");
212 setUp(() {
213 _createFiles(tempDir, description);
214 });
215 tearDown(() {
216 tempDir.deleteSync(recursive: true);
217 });
218 test(name, () => fileTest(new Uri.directory(tempDir.path)));
219 });
220 }
221
222 /// HTTP-server the directory structure from [description] and run [htpTest].
223 ///
224 /// Description is a map, each key is a file entry. If the value is a map,
225 /// it's a sub-dir, otherwise it's a file and the value is the content
226 /// as a string.
227 void httpTest(String name, Map description, Future httpTest(Uri directory)) {
228 group("http-test", () {
229 var serverSub;
230 var uri;
231 setUp(() {
232 return HttpServer
233 .bind(InternetAddress.LOOPBACK_IP_V4, 0)
234 .then((server) {
235 uri = new Uri(scheme: "http",
236 host: "127.0.0.1",
237 port: server.port,
238 path: "/");
239 serverSub = server.listen((HttpRequest request) {
240 // No error handling.
241 var path = request.uri.path;
242 if (path.startsWith('/')) path = path.substring(1);
243 if (path.endsWith('/')) path = path.substring(0, path.length - 1);
244 var parts = path.split('/');
245 var fileOrDir = description;
246 for (int i = 0; i < parts.length; i++) {
247 fileOrDir = fileOrDir[parts[i]];
248 if (fileOrDir == null) {
249 request.response.statusCode = 404;
250 request.response.close();
251 }
252 }
253 request.response.write(fileOrDir);
254 request.response.close();
255 });
256 });
257 });
258 tearDown(() => serverSub.cancel());
259 test(name, () => httpTest(uri));
260 });
261 }
262
263 void generalTest(String name, Map description, Future action(Uri location)) {
264 fileTest(name, description, action);
265 httpTest(name, description, action);
266 }
267
268 void _createFiles(Directory target, Map description) {
269 description.forEach((name, content) {
270 if (content is Map) {
271 Directory subDir = new Directory(path.join(target.path, name));
272 subDir.createSync();
273 _createFiles(subDir, content);
274 } else {
275 File file = new File(path.join(target.path, name));
276 file.writeAsStringSync(content, flush: true);
277 }
278 });
279 }
280
281
OLDNEW
« no previous file with comments | « test/all.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698