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