Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// Test infrastructure for testing pub. | 5 /// Test infrastructure for testing pub. |
| 6 /// | 6 /// |
| 7 /// Unlike typical unit tests, most pub tests are integration tests that stage | 7 /// Unlike typical unit tests, most pub tests are integration tests that stage |
| 8 /// some stuff on the file system, run pub, and then validate the results. This | 8 /// some stuff on the file system, run pub, and then validate the results. This |
| 9 /// library provides an API to build tests like that. | 9 /// library provides an API to build tests like that. |
| 10 library test_pub; | 10 library test_pub; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 d.DirectoryDescriptor _servedApiPackageDir; | 231 d.DirectoryDescriptor _servedApiPackageDir; |
| 232 | 232 |
| 233 /// The [d.DirectoryDescriptor] describing the server layout of `/packages` on | 233 /// The [d.DirectoryDescriptor] describing the server layout of `/packages` on |
| 234 /// the test server. | 234 /// the test server. |
| 235 /// | 235 /// |
| 236 /// This contains the tarballs for packages that are being served via | 236 /// This contains the tarballs for packages that are being served via |
| 237 /// [servePackages]. It's `null` if [servePackages] has not yet been called for | 237 /// [servePackages]. It's `null` if [servePackages] has not yet been called for |
| 238 /// this test. | 238 /// this test. |
| 239 d.DirectoryDescriptor _servedPackageDir; | 239 d.DirectoryDescriptor _servedPackageDir; |
| 240 | 240 |
| 241 /// A map from package names to parsed pubspec maps for those packages. | 241 /// A map from package names to parsed pubspec maps and contents for those |
| 242 /// packages. | |
| 242 /// | 243 /// |
| 243 /// This represents the packages currently being served by [servePackages], and | 244 /// This represents the packages currently being served by [servePackages], and |
| 244 /// is `null` if [servePackages] has not yet been called for this test. | 245 /// is `null` if [servePackages] has not yet been called for this test. |
| 245 Map<String, List<Map>> _servedPackages; | 246 Map<String, List<Pair<Map, Iterable<d.Descriptor>>>> _servedPackages; |
| 246 | 247 |
| 247 /// Creates an HTTP server that replicates the structure of pub.dartlang.org. | 248 /// Creates an HTTP server that replicates the structure of pub.dartlang.org. |
| 248 /// | 249 /// |
| 249 /// [pubspecs] is a list of unserialized pubspecs representing the packages to | 250 /// [pubspecs] is a list of unserialized pubspecs representing the packages to |
| 250 /// serve. | 251 /// serve. |
| 251 /// | 252 /// |
| 252 /// If [replace] is false, subsequent calls to [servePackages] will add to the | 253 /// If [replace] is false, subsequent calls to [servePackages] will add to the |
| 253 /// set of packages that are being served. Previous packages will continue to be | 254 /// set of packages that are being served. Previous packages will continue to be |
| 254 /// served. Otherwise, the previous packages will no longer be served. | 255 /// served. Otherwise, the previous packages will no longer be served. |
| 255 /// | 256 /// |
| 256 /// If [contents] is given, its contents are added to every served | 257 /// If [contents] is given, it should be a map from package names and versions |
| 257 /// package. | 258 /// (e.g. "foo-1.2.3") to their contents. |
|
Bob Nystrom
2014/08/20 23:50:23
Having the mention the package names and versions
nweiz
2014/08/21 01:21:23
Done.
| |
| 258 /// | 259 /// |
| 259 /// If [serveBarback] is true, the repo versions of barback and its dependencies | 260 /// If [serveBarback] is true, the repo versions of barback and its dependencies |
| 260 /// will be served as well. | 261 /// will be served as well. |
| 261 void servePackages(List<Map> pubspecs, {bool replace: false, | 262 void servePackages(List<Map> pubspecs, {bool replace: false, |
| 262 Iterable<d.Descriptor> contents, bool serveBarback: false}) { | 263 Map<String, Iterable<d.Descriptor>> contents, bool serveBarback: false}) { |
| 264 if (contents == null) contents = {}; | |
| 265 | |
| 263 if (_servedPackages == null || _servedPackageDir == null) { | 266 if (_servedPackages == null || _servedPackageDir == null) { |
| 264 _servedPackages = <String, List<Map>>{}; | 267 _servedPackages = <String, List<Pair<Map, Iterable<d.Descriptor>>>>{}; |
| 265 _servedApiPackageDir = d.dir('packages', []); | 268 _servedApiPackageDir = d.dir('packages', []); |
| 266 _servedPackageDir = d.dir('packages', []); | 269 _servedPackageDir = d.dir('packages', []); |
| 267 serve([ | 270 serve([ |
| 268 d.dir('api', [_servedApiPackageDir]), | 271 d.dir('api', [_servedApiPackageDir]), |
| 269 _servedPackageDir | 272 _servedPackageDir |
| 270 ]); | 273 ]); |
| 271 | 274 |
| 272 currentSchedule.onComplete.schedule(() { | 275 currentSchedule.onComplete.schedule(() { |
| 273 _servedPackages = null; | 276 _servedPackages = null; |
| 274 _servedApiPackageDir = null; | 277 _servedApiPackageDir = null; |
| 275 _servedPackageDir = null; | 278 _servedPackageDir = null; |
| 276 }, 'cleaning up served packages'); | 279 }, 'cleaning up served packages'); |
| 277 } | 280 } |
| 278 | 281 |
| 279 schedule(() { | 282 schedule(() { |
| 280 return awaitObject(pubspecs).then((resolvedPubspecs) { | 283 return awaitObject(pubspecs).then((resolvedPubspecs) { |
| 281 if (replace) _servedPackages.clear(); | 284 if (replace) _servedPackages.clear(); |
| 282 | 285 |
| 283 for (var pubspec in resolvedPubspecs) { | 286 for (var pubspec in resolvedPubspecs) { |
| 284 var name = pubspec['name']; | 287 var name = pubspec['name']; |
| 285 var version = pubspec['version']; | 288 var version = pubspec['version']; |
| 286 var versions = _servedPackages.putIfAbsent(name, () => []); | 289 var versions = _servedPackages.putIfAbsent(name, () => []); |
| 287 versions.add(pubspec); | 290 var packageContents = contents['$name-$version']; |
| 291 if (packageContents == null) { | |
| 292 packageContents = [d.libDir(name, '$name $version')]; | |
| 293 } | |
| 294 versions.add(new Pair(pubspec, packageContents)); | |
| 288 } | 295 } |
| 289 | 296 |
| 290 var repoPackages = new Set(); | 297 var repoPackages = new Set(); |
| 291 if (serveBarback) { | 298 if (serveBarback) { |
| 292 _addPackage(name) { | 299 _addPackage(name) { |
| 293 if (_servedPackages.containsKey(name)) return; | 300 if (_servedPackages.containsKey(name)) return; |
| 294 repoPackages.add(name); | 301 repoPackages.add(name); |
| 295 | 302 |
| 296 var pubspec = new Map.from(loadYaml( | 303 var pubspec = new Map.from(loadYaml( |
| 297 readTextFile(path.join(repoRoot, 'pkg', name, 'pubspec.yaml')))); | 304 readTextFile(path.join(repoRoot, 'pkg', name, 'pubspec.yaml')))); |
| 298 | 305 |
| 299 // Remove any SDK constraints since we don't have a valid SDK version | 306 // Remove any SDK constraints since we don't have a valid SDK version |
| 300 // while testing. | 307 // while testing. |
| 301 pubspec.remove('environment'); | 308 pubspec.remove('environment'); |
| 302 | 309 |
| 303 _servedPackages[name] = [pubspec]; | 310 _servedPackages[name] = [ |
| 311 new Pair(pubspec, [ | |
| 312 new d.DirectoryDescriptor.fromFilesystem('lib', | |
| 313 path.join(repoRoot, 'pkg', name, 'lib')) | |
| 314 ]) | |
| 315 ]; | |
| 304 if (pubspec.containsKey('dependencies')) { | 316 if (pubspec.containsKey('dependencies')) { |
| 305 pubspec['dependencies'].keys.forEach(_addPackage); | 317 pubspec['dependencies'].keys.forEach(_addPackage); |
| 306 } | 318 } |
| 307 } | 319 } |
| 308 | 320 |
| 309 _addPackage('barback'); | 321 _addPackage('barback'); |
| 310 } | 322 } |
| 311 | 323 |
| 312 _servedApiPackageDir.contents.clear(); | 324 _servedApiPackageDir.contents.clear(); |
| 313 _servedPackageDir.contents.clear(); | 325 _servedPackageDir.contents.clear(); |
| 314 for (var name in _servedPackages.keys) { | 326 for (var name in _servedPackages.keys) { |
| 315 _servedApiPackageDir.contents.addAll([ | 327 _servedApiPackageDir.contents.addAll([ |
| 316 d.file('$name', JSON.encode({ | 328 d.file('$name', JSON.encode({ |
| 317 'name': name, | 329 'name': name, |
| 318 'uploaders': ['nweiz@google.com'], | 330 'uploaders': ['nweiz@google.com'], |
| 319 'versions': _servedPackages[name].map(packageVersionApiMap).toList() | 331 'versions': _servedPackages[name] |
| 332 .map((pair) => packageVersionApiMap(pair.first)).toList() | |
| 320 })), | 333 })), |
| 321 d.dir(name, [ | 334 d.dir(name, [ |
| 322 d.dir('versions', _servedPackages[name].map((pubspec) { | 335 d.dir('versions', _servedPackages[name].map((pair) { |
| 336 var pubspec = pair.first; | |
| 323 return d.file(pubspec['version'], JSON.encode( | 337 return d.file(pubspec['version'], JSON.encode( |
| 324 packageVersionApiMap(pubspec, full: true))); | 338 packageVersionApiMap(pubspec, full: true))); |
| 325 })) | 339 })) |
| 326 ]) | 340 ]) |
| 327 ]); | 341 ]); |
| 328 | 342 |
| 329 _servedPackageDir.contents.add(d.dir(name, [ | 343 _servedPackageDir.contents.add(d.dir(name, [ |
| 330 d.dir('versions', _servedPackages[name].map((pubspec) { | 344 d.dir('versions', _servedPackages[name].map((pair) { |
| 345 var pubspec = pair.first; | |
| 346 var packageContents = pair.last; | |
| 331 var version = pubspec['version']; | 347 var version = pubspec['version']; |
| 332 | 348 |
| 333 if (repoPackages.contains(name)) { | 349 if (repoPackages.contains(name)) { |
| 334 return d.tar('$version.tar.gz', [ | 350 return d.tar('$version.tar.gz', [ |
| 335 d.file('pubspec.yaml', JSON.encode(pubspec)), | 351 d.file('pubspec.yaml', JSON.encode(pubspec)), |
| 336 new d.DirectoryDescriptor.fromFilesystem('lib', | 352 new d.DirectoryDescriptor.fromFilesystem('lib', |
| 337 path.join(repoRoot, 'pkg', name, 'lib')) | 353 path.join(repoRoot, 'pkg', name, 'lib')) |
| 338 ]); | 354 ]); |
| 339 } | 355 } |
| 340 | 356 |
| 341 var archiveContents = [ | 357 return d.tar('$version.tar.gz', [ |
| 342 d.file('pubspec.yaml', JSON.encode(pubspec)), | 358 d.file('pubspec.yaml', JSON.encode(pubspec)) |
| 343 d.libDir(name, '$name $version') | 359 ]..addAll(packageContents)); |
| 344 ]; | |
| 345 | |
| 346 if (contents != null) { | |
| 347 archiveContents.addAll(contents); | |
| 348 } | |
| 349 | |
| 350 return d.tar('$version.tar.gz', archiveContents); | |
| 351 })) | 360 })) |
| 352 ])); | 361 ])); |
| 353 } | 362 } |
| 354 }); | 363 }); |
| 355 }, 'initializing the package server'); | 364 }, 'initializing the package server'); |
| 356 } | 365 } |
| 357 | 366 |
| 358 /// Converts [value] into a YAML string. | 367 /// Converts [value] into a YAML string. |
| 359 String yaml(value) => JSON.encode(value); | 368 String yaml(value) => JSON.encode(value); |
| 360 | 369 |
| (...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1073 _lastMatcher.matches(item.last, matchState); | 1082 _lastMatcher.matches(item.last, matchState); |
| 1074 } | 1083 } |
| 1075 | 1084 |
| 1076 Description describe(Description description) { | 1085 Description describe(Description description) { |
| 1077 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 1086 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
| 1078 } | 1087 } |
| 1079 } | 1088 } |
| 1080 | 1089 |
| 1081 /// A [StreamMatcher] that matches multiple lines of output. | 1090 /// A [StreamMatcher] that matches multiple lines of output. |
| 1082 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); | 1091 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); |
| OLD | NEW |