OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * Test infrastructure for testing pub. Unlike typical unit tests, most pub | 6 * Test infrastructure for testing pub. Unlike typical unit tests, most pub |
7 * tests are integration tests that stage some stuff on the file system, run | 7 * tests are integration tests that stage some stuff on the file system, run |
8 * pub, and then validate the results. This library provides an API to build | 8 * pub, and then validate the results. This library provides an API to build |
9 * tests like that. | 9 * tests like that. |
10 */ | 10 */ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 GitRepoDescriptor git(Pattern name, [List<Descriptor> contents]) => | 45 GitRepoDescriptor git(Pattern name, [List<Descriptor> contents]) => |
46 new GitRepoDescriptor(name, contents); | 46 new GitRepoDescriptor(name, contents); |
47 | 47 |
48 /** | 48 /** |
49 * Creates a new [TarFileDescriptor] with [name] and [contents]. | 49 * Creates a new [TarFileDescriptor] with [name] and [contents]. |
50 */ | 50 */ |
51 TarFileDescriptor tar(Pattern name, [List<Descriptor> contents]) => | 51 TarFileDescriptor tar(Pattern name, [List<Descriptor> contents]) => |
52 new TarFileDescriptor(name, contents); | 52 new TarFileDescriptor(name, contents); |
53 | 53 |
54 /** | 54 /** |
| 55 * The current [HttpServer] created using [serve]. |
| 56 */ |
| 57 var _server; |
| 58 |
| 59 /** |
55 * Creates an HTTP server to serve [contents] as static files. This server will | 60 * Creates an HTTP server to serve [contents] as static files. This server will |
56 * exist only for the duration of the pub run. | 61 * exist only for the duration of the pub run. |
| 62 * |
| 63 * Subsequent calls to [serve] will replace the previous server. |
57 */ | 64 */ |
58 void serve(String host, int port, [List<Descriptor> contents]) { | 65 void serve(String host, int port, [List<Descriptor> contents]) { |
59 var baseDir = dir("serve-dir", contents); | 66 var baseDir = dir("serve-dir", contents); |
60 if (host == 'localhost') { | 67 if (host == 'localhost') { |
61 host = '127.0.0.1'; | 68 host = '127.0.0.1'; |
62 } | 69 } |
63 | 70 |
64 _schedule((_) { | 71 _schedule((_) { |
65 var server = new HttpServer(); | 72 if (_server != null) _server.close(); |
66 server.defaultRequestHandler = (request, response) { | 73 _server = new HttpServer(); |
| 74 _server.defaultRequestHandler = (request, response) { |
67 var path = request.uri.replaceFirst("/", "").split("/"); | 75 var path = request.uri.replaceFirst("/", "").split("/"); |
68 response.persistentConnection = false; | 76 response.persistentConnection = false; |
69 var stream; | 77 var stream; |
70 try { | 78 try { |
71 stream = baseDir.load(path); | 79 stream = baseDir.load(path); |
72 } catch (var e) { | 80 } catch (var e) { |
73 response.statusCode = 404; | 81 response.statusCode = 404; |
74 response.contentLength = 0; | 82 response.contentLength = 0; |
75 response.outputStream.close(); | 83 response.outputStream.close(); |
76 return; | 84 return; |
77 } | 85 } |
78 | 86 |
79 var future = consumeInputStream(stream); | 87 var future = consumeInputStream(stream); |
80 future.then((data) { | 88 future.then((data) { |
81 response.statusCode = 200; | 89 response.statusCode = 200; |
82 response.contentLength = data.length; | 90 response.contentLength = data.length; |
83 response.outputStream.write(data); | 91 response.outputStream.write(data); |
84 response.outputStream.close(); | 92 response.outputStream.close(); |
85 }); | 93 }); |
86 | 94 |
87 future.handleException((e) { | 95 future.handleException((e) { |
88 print("Exception while handling ${request.uri}: $e"); | 96 print("Exception while handling ${request.uri}: $e"); |
89 response.statusCode = 500; | 97 response.statusCode = 500; |
90 response.reasonPhrase = e.message; | 98 response.reasonPhrase = e.message; |
91 response.outputStream.close(); | 99 response.outputStream.close(); |
92 }); | 100 }); |
93 }; | 101 }; |
94 server.listen(host, port); | 102 _server.listen(host, port); |
95 _scheduleCleanup((_) => server.close()); | 103 _scheduleCleanup((_) { |
| 104 if (_server != null) _server.close(); |
| 105 _server = null; |
| 106 }); |
96 | 107 |
97 return new Future.immediate(null); | 108 return new Future.immediate(null); |
98 }); | 109 }); |
99 } | 110 } |
100 | 111 |
101 /** | 112 /** |
102 * Creates an HTTP server that replicates the structure of pub.dartlang.org. | 113 * Creates an HTTP server that replicates the structure of pub.dartlang.org. |
103 * [pubspecs] is a list of YAML-format pubspecs representing the packages to | 114 * [pubspecs] is a list of YAML-format pubspecs representing the packages to |
104 * serve. | 115 * serve. |
105 */ | 116 */ |
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 } | 795 } |
785 | 796 |
786 /** | 797 /** |
787 * Schedules a callback to be called after Pub is run with [runPub], even if it | 798 * Schedules a callback to be called after Pub is run with [runPub], even if it |
788 * fails. | 799 * fails. |
789 */ | 800 */ |
790 void _scheduleCleanup(_ScheduledEvent event) { | 801 void _scheduleCleanup(_ScheduledEvent event) { |
791 if (_scheduledCleanup == null) _scheduledCleanup = []; | 802 if (_scheduledCleanup == null) _scheduledCleanup = []; |
792 _scheduledCleanup.add(event); | 803 _scheduledCleanup.add(event); |
793 } | 804 } |
OLD | NEW |