OLD | NEW |
---|---|
(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, {bool enumerate: true} ) { | |
Søren Gjesse
2015/05/26 08:11:53
Long line.
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Done.
| |
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 (enumerate) { | |
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() { | |
Søren Gjesse
2015/05/26 08:11:53
Can't you always make a fileTest and httpTest from
Lasse Reichstein Nielsen
2015/05/26 09:24:30
For the directly accessed .package files and packa
| |
58 fileTest(".packages", | |
59 {".packages": packagesFile, "script.dart": "main(){}"}, | |
60 (Uri location) async { | |
61 Packages resolver; | |
62 resolver = await findPackages(location); | |
63 validatePackagesFile(resolver, location); | |
64 resolver = await findPackages(location.resolve("script.dart")); | |
65 validatePackagesFile(resolver, location); | |
66 resolver = findPackagesFromFile(location); | |
67 validatePackagesFile(resolver, location); | |
68 resolver = findPackagesFromFile(location.resolve("script.dart")); | |
69 validatePackagesFile(resolver, location); | |
70 }); | |
71 | |
72 fileTest(".packages recursive", | |
73 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}}, | |
74 (Uri location) async { | |
75 Packages resolver; | |
76 resolver = await findPackages(location.resolve("subdir/")); | |
77 validatePackagesFile(resolver, location); | |
78 resolver = await findPackages(location.resolve("subdir/script.dart")); | |
79 validatePackagesFile(resolver, location); | |
80 resolver = findPackagesFromFile(location.resolve("subdir/")); | |
81 validatePackagesFile(resolver, location); | |
82 resolver = findPackagesFromFile(location.resolve("subdir/script.dart")); | |
83 validatePackagesFile(resolver, location); | |
84 }); | |
85 | |
86 fileTest("packages/", | |
87 {"packages": { "foo": {}, "bar": {}, "baz": {}}, "script.dart": "main( ){}"}, | |
Søren Gjesse
2015/05/26 08:11:53
Long line.
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Done.
| |
88 (Uri location) async { | |
89 Packages resolver; | |
90 resolver = await findPackages(location); | |
91 validatePackagesDir(resolver, location); | |
92 resolver = await findPackages(location.resolve("script.dart")); | |
93 validatePackagesDir(resolver, location); | |
94 resolver = findPackagesFromFile(location); | |
95 validatePackagesDir(resolver, location); | |
96 resolver = findPackagesFromFile(location.resolve("script.dart")); | |
97 validatePackagesDir(resolver, location); | |
98 }); | |
99 | |
100 fileTest("no packages", | |
101 {"script.dart": "main(){}"}, | |
102 (Uri location) async { | |
103 Packages resolver; | |
104 resolver = await findPackages(location); | |
105 expect(resolver, same(Packages.noPackages)); | |
106 resolver = await findPackages(location.resolve("script.dart")); | |
107 expect(resolver, same(Packages.noPackages)); | |
108 resolver = findPackagesFromFile(location); | |
109 expect(resolver, same(Packages.noPackages)); | |
110 resolver = findPackagesFromFile(location.resolve("script.dart")); | |
111 expect(resolver, same(Packages.noPackages)); | |
112 }); | |
113 | |
114 httpTest(".packages", | |
115 {".packages": packagesFile, "script.dart": "main(){}"}, | |
116 (Uri location) async { | |
117 Packages resolver; | |
118 resolver = await findPackages(location); | |
119 validatePackagesFile(resolver, location); | |
120 resolver = await findPackages(location.resolve("script.dart")); | |
121 validatePackagesFile(resolver, location); | |
122 resolver = await findPackagesFromNonFile(location); | |
Søren Gjesse
2015/05/26 08:11:53
Not related to this CL. but what is a "NonFile" lo
Lasse Reichstein Nielsen
2015/05/26 09:24:29
It's any URI that doesn't start with "file:".
| |
123 validatePackagesFile(resolver, location); | |
124 resolver = await findPackagesFromNonFile(location.resolve("script.dart")); | |
125 validatePackagesFile(resolver, location); | |
126 }); | |
127 | |
128 httpTest(".packages not from subdir", | |
Søren Gjesse
2015/05/26 08:11:53
The file version of this test is called '.packages
Lasse Reichstein Nielsen
2015/05/26 09:24:29
So this one should be "not recursive". Fixed.
| |
129 {".packages": packagesFile, "subdir": {"script.dart": "main(){}"}}, | |
130 (Uri location) async { | |
131 Packages resolver; | |
132 var subdir = location.resolve("subdir/"); | |
133 resolver = await findPackages(subdir); | |
134 validatePackagesDir(resolver, subdir, enumerate: false); | |
135 resolver = await findPackages(subdir.resolve("script.dart")); | |
136 validatePackagesDir(resolver, subdir, enumerate: false); | |
137 resolver = await findPackagesFromNonFile(subdir); | |
138 validatePackagesDir(resolver, subdir, enumerate: false); | |
139 resolver = await findPackagesFromNonFile(subdir.resolve("script.dart")); | |
140 validatePackagesDir(resolver, subdir, enumerate: false); | |
141 }); | |
142 | |
143 httpTest("packages dir", | |
144 {"packages": {"foo":{}, "bar":{}, "baz":{}}, | |
145 "script.dart": "main(){}"}, | |
146 (Uri location) async { | |
147 Packages resolver; | |
148 resolver = await findPackages(location); | |
149 validatePackagesDir(resolver, location, enumerate: false); | |
150 resolver = await findPackages(location.resolve("script.dart")); | |
151 validatePackagesDir(resolver, location, enumerate: false); | |
152 resolver = await findPackagesFromNonFile(location); | |
153 validatePackagesDir(resolver, location, enumerate: false); | |
154 resolver = await findPackagesFromNonFile(location.resolve("script.dart")); | |
155 validatePackagesDir(resolver, location, enumerate: false); | |
156 }); | |
157 | |
158 httpTest("no packages", | |
159 {"script.dart": "main(){}"}, | |
160 (Uri location) async { | |
161 // Assumes a packages dir exists, and resolves relative to that. | |
162 Packages resolver; | |
163 resolver = await findPackages(location); | |
164 validatePackagesDir(resolver, location, enumerate: false); | |
165 resolver = await findPackages(location.resolve("script.dart")); | |
166 validatePackagesDir(resolver, location, enumerate: false); | |
167 resolver = await findPackagesFromNonFile(location); | |
168 validatePackagesDir(resolver, location, enumerate: false); | |
169 resolver = await findPackagesFromNonFile(location.resolve("script.dart")); | |
170 validatePackagesDir(resolver, location, enumerate: false); | |
171 }); | |
172 } | |
173 | |
174 /// Create a directory structure from [description] and run [fileTest]. | |
175 /// | |
176 /// Description is a map, each key is a file entry. If the value is a map, | |
177 /// it's a sub-dir, otherwise it's a file and the value is the content | |
178 /// as a string. | |
179 void fileTest(String name, | |
180 Map description, | |
181 Future fileTest(Uri directory)) { | |
182 group("file-test", () { | |
183 Directory tempDir = Directory.systemTemp.createTempSync("file-test"); | |
184 setUp(() { | |
185 _createFiles(tempDir, description); | |
186 }); | |
187 tearDown(() { | |
188 tempDir.deleteSync(recursive: true); | |
189 }); | |
190 test(name, () => fileTest(new Uri.directory(tempDir.path))); | |
191 }); | |
192 } | |
193 | |
194 /// HTTP-server the directory structure from [description] and run [htpTest]. | |
195 /// | |
196 /// Description is a map, each key is a file entry. If the value is a map, | |
197 /// it's a sub-dir, otherwise it's a file and the value is the content | |
198 /// as a string. | |
199 void httpTest(String name, Map description, Future httpTest(Uri directory)) { | |
200 group("http-test", () { | |
201 var serverSub; | |
202 var uri; | |
203 setUp(() { | |
204 return HttpServer | |
205 .bind(InternetAddress.LOOPBACK_IP_V4, 0) | |
206 .then((server) { | |
207 uri = new Uri(scheme: "http", | |
208 host: "127.0.0.1", | |
209 port: server.port, | |
210 path: "/"); | |
211 serverSub = server.listen((HttpRequest request) { | |
212 // No error handling. | |
213 var path = request.uri.path; | |
214 if (path.startsWith('/')) path = path.substring(1); | |
215 if (path.endsWith('/')) path = path.substring(0, path.length - 1); | |
216 var parts = path.split('/'); | |
217 var fileOrDir = description; | |
218 for (int i = 0; i < parts.length; i++) { | |
219 fileOrDir = fileOrDir[parts[i]]; | |
220 if (fileOrDir == null) { | |
221 request.response.statusCode = 404; | |
222 request.response.close(); | |
223 } | |
224 } | |
225 request.response.write(fileOrDir); | |
Søren Gjesse
2015/05/26 08:11:53
How does this work of fileOrDir refers to a subdir
Lasse Reichstein Nielsen
2015/05/26 09:24:30
Should probably make it an explicit error. It won'
| |
226 request.response.close(); | |
227 }); | |
228 }); | |
229 }); | |
230 tearDown(() { serverSub.cancel(); }); | |
231 test(name, () => httpTest(uri)); | |
232 }); | |
233 } | |
234 | |
235 | |
236 void _createFiles(Directory target, Map description) { | |
237 description.forEach((name, content) { | |
238 if (content is Map) { | |
239 Directory subDir = new Directory(path.join(target.path, name)); | |
240 subDir.createSync(); | |
241 _createFiles(subDir, content); | |
242 } else { | |
243 File file = new File(path.join(target.path, name)); | |
244 file.writeAsStringSync(content, flush: true); | |
245 } | |
246 }); | |
247 } | |
248 | |
249 | |
OLD | NEW |