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 [getRequestedPaths]. |
| 62 final _requestedPaths = <String>[]; |
| 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). |
| 82 Future<List<String>> getRequestedPaths() { |
| 83 return schedule(() { |
| 84 var paths = _requestedPaths.toList(); |
| 85 _requestedPaths.clear(); |
| 86 return paths; |
| 87 }); |
| 88 } |
| 89 |
76 /// Creates an HTTP server to serve [contents] as static files. This server will | 90 /// Creates an HTTP server to serve [contents] as static files. This server will |
77 /// exist only for the duration of the pub run. | 91 /// exist only for the duration of the pub run. |
78 /// | 92 /// |
79 /// Subsequent calls to [serve] will replace the previous server. | 93 /// Subsequent calls to [serve] will replace the previous server. |
80 void serve([List<d.Descriptor> contents]) { | 94 void serve([List<d.Descriptor> contents]) { |
81 var baseDir = d.dir("serve-dir", contents); | 95 var baseDir = d.dir("serve-dir", contents); |
82 | 96 |
83 schedule(() { | 97 schedule(() { |
84 return _closeServer().then((_) { | 98 return _closeServer().then((_) { |
85 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { | 99 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { |
86 _server = server; | 100 _server = server; |
87 server.listen((request) { | 101 server.listen((request) { |
88 var response = request.response; | 102 var response = request.response; |
89 try { | 103 try { |
90 var path = request.uri.path.replaceFirst("/", ""); | 104 var path = request.uri.path.replaceFirst("/", ""); |
| 105 |
| 106 if (_requestedPaths == null) _requestedPaths = <String>[]; |
| 107 _requestedPaths.add(path); |
| 108 |
91 response.persistentConnection = false; | 109 response.persistentConnection = false; |
92 var stream = baseDir.load(path); | 110 var stream = baseDir.load(path); |
93 | 111 |
94 new ByteStream(stream).toBytes().then((data) { | 112 new ByteStream(stream).toBytes().then((data) { |
95 response.statusCode = 200; | 113 response.statusCode = 200; |
96 response.contentLength = data.length; | 114 response.contentLength = data.length; |
97 response.add(data); | 115 response.add(data); |
98 response.close(); | 116 response.close(); |
99 }).catchError((e) { | 117 }).catchError((e) { |
100 response.statusCode = 404; | 118 response.statusCode = 404; |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 bool matches(item, MatchState matchState) { | 628 bool matches(item, MatchState matchState) { |
611 if (item is! Pair) return false; | 629 if (item is! Pair) return false; |
612 return _firstMatcher.matches(item.first, matchState) && | 630 return _firstMatcher.matches(item.first, matchState) && |
613 _lastMatcher.matches(item.last, matchState); | 631 _lastMatcher.matches(item.last, matchState); |
614 } | 632 } |
615 | 633 |
616 Description describe(Description description) { | 634 Description describe(Description description) { |
617 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 635 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
618 } | 636 } |
619 } | 637 } |
OLD | NEW |