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. Unlike typical unit tests, most pub | 5 /// Test infrastructure for testing pub. Unlike typical unit tests, most pub |
| 6 /// tests are integration tests that stage some stuff on the file system, run | 6 /// tests are integration tests that stage some stuff on the file system, run |
| 7 /// pub, and then validate the results. This library provides an API to build | 7 /// pub, and then validate the results. This library provides an API to build |
| 8 /// tests like that. | 8 /// tests like that. |
| 9 library test_pub; | 9 library test_pub; |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 /// Returns whether we're running on a Dart build bot. | 53 /// Returns whether we're running on a Dart build bot. |
| 54 bool get runningOnBuildbot => | 54 bool get runningOnBuildbot => |
| 55 Platform.environment.containsKey('BUILDBOT_BUILDERNAME'); | 55 Platform.environment.containsKey('BUILDBOT_BUILDERNAME'); |
| 56 | 56 |
| 57 /// The current [HttpServer] created using [serve]. | 57 /// The current [HttpServer] created using [serve]. |
| 58 var _server; | 58 var _server; |
| 59 | 59 |
| 60 /// The list of paths that have been requested from the server since the last | |
| 61 /// call to ???. | |
|
nweiz
2013/04/18 22:50:03
???
Bob Nystrom
2013/04/18 23:07:10
Oops. Wrote that comment before I figured out what
| |
| 62 var _requestedPaths; | |
|
nweiz
2013/04/18 22:50:03
This should have a type annotation since it doesn'
Bob Nystrom
2013/04/18 23:07:10
Done.
| |
| 63 | |
| 60 /// The cached value for [_portCompleter]. | 64 /// The cached value for [_portCompleter]. |
| 61 Completer<int> _portCompleterCache; | 65 Completer<int> _portCompleterCache; |
| 62 | 66 |
| 63 /// The completer for [port]. | 67 /// The completer for [port]. |
| 64 Completer<int> get _portCompleter { | 68 Completer<int> get _portCompleter { |
| 65 if (_portCompleterCache != null) return _portCompleterCache; | 69 if (_portCompleterCache != null) return _portCompleterCache; |
| 66 _portCompleterCache = new Completer<int>(); | 70 _portCompleterCache = new Completer<int>(); |
| 67 currentSchedule.onComplete.schedule(() { | 71 currentSchedule.onComplete.schedule(() { |
| 68 _portCompleterCache = null; | 72 _portCompleterCache = null; |
| 69 }, 'clearing the port completer'); | 73 }, 'clearing the port completer'); |
| 70 return _portCompleterCache; | 74 return _portCompleterCache; |
| 71 } | 75 } |
| 72 | 76 |
| 73 /// A future that will complete to the port used for the current server. | 77 /// A future that will complete to the port used for the current server. |
| 74 Future<int> get port => _portCompleter.future; | 78 Future<int> get port => _portCompleter.future; |
| 75 | 79 |
| 80 /// Gets the list of paths that have been requested from the server since the | |
| 81 /// last time this was called (or since the server was first spun up). Since | |
| 82 /// this method is synchronous, make sure to call it from within [schedule] to | |
| 83 /// ensure it's scheduled with other tasks. | |
|
nweiz
2013/04/18 22:50:03
It would be cleaner to make this return a Future<L
Bob Nystrom
2013/04/18 23:07:10
Done.
| |
| 84 List<String> getRequestedPaths() { | |
| 85 if (_requestedPaths == null) return []; | |
| 86 | |
| 87 var paths = _requestedPaths; | |
| 88 _requestedPaths = null; | |
| 89 return paths; | |
| 90 } | |
| 91 | |
| 76 /// Creates an HTTP server to serve [contents] as static files. This server will | 92 /// Creates an HTTP server to serve [contents] as static files. This server will |
| 77 /// exist only for the duration of the pub run. | 93 /// exist only for the duration of the pub run. |
| 78 /// | 94 /// |
| 79 /// Subsequent calls to [serve] will replace the previous server. | 95 /// Subsequent calls to [serve] will replace the previous server. |
| 80 void serve([List<d.Descriptor> contents]) { | 96 void serve([List<d.Descriptor> contents]) { |
| 81 var baseDir = d.dir("serve-dir", contents); | 97 var baseDir = d.dir("serve-dir", contents); |
| 82 | 98 |
| 83 schedule(() { | 99 schedule(() { |
| 84 return _closeServer().then((_) { | 100 return _closeServer().then((_) { |
| 85 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { | 101 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { |
| 86 _server = server; | 102 _server = server; |
| 87 server.listen((request) { | 103 server.listen((request) { |
| 88 var response = request.response; | 104 var response = request.response; |
| 89 try { | 105 try { |
| 90 var path = request.uri.path.replaceFirst("/", ""); | 106 var path = request.uri.path.replaceFirst("/", ""); |
| 107 | |
| 108 if (_requestedPaths == null) _requestedPaths = <String>[]; | |
| 109 _requestedPaths.add(path); | |
| 110 | |
| 91 response.persistentConnection = false; | 111 response.persistentConnection = false; |
| 92 var stream = baseDir.load(path); | 112 var stream = baseDir.load(path); |
| 93 | 113 |
| 94 new ByteStream(stream).toBytes().then((data) { | 114 new ByteStream(stream).toBytes().then((data) { |
| 95 response.statusCode = 200; | 115 response.statusCode = 200; |
| 96 response.contentLength = data.length; | 116 response.contentLength = data.length; |
| 97 response.add(data); | 117 response.add(data); |
| 98 response.close(); | 118 response.close(); |
| 99 }).catchError((e) { | 119 }).catchError((e) { |
| 100 response.statusCode = 404; | 120 response.statusCode = 404; |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 610 bool matches(item, MatchState matchState) { | 630 bool matches(item, MatchState matchState) { |
| 611 if (item is! Pair) return false; | 631 if (item is! Pair) return false; |
| 612 return _firstMatcher.matches(item.first, matchState) && | 632 return _firstMatcher.matches(item.first, matchState) && |
| 613 _lastMatcher.matches(item.last, matchState); | 633 _lastMatcher.matches(item.last, matchState); |
| 614 } | 634 } |
| 615 | 635 |
| 616 Description describe(Description description) { | 636 Description describe(Description description) { |
| 617 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 637 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
| 618 } | 638 } |
| 619 } | 639 } |
| OLD | NEW |