| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS d.file | 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS d.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 pub_tests; | 5 library pub_tests; | 
| 6 | 6 | 
| 7 import 'dart:async'; | 7 import 'dart:async'; | 
| 8 | 8 | 
| 9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; | 
| 10 import 'package:scheduled_test/scheduled_process.dart'; | 10 import 'package:scheduled_test/scheduled_process.dart'; | 
| 11 import 'package:scheduled_test/scheduled_test.dart'; | 11 import 'package:scheduled_test/scheduled_test.dart'; | 
| 12 | 12 | 
| 13 import '../test_pub.dart'; | 13 import '../test_pub.dart'; | 
| 14 | 14 | 
| 15 /// The pub process running "pub serve". | 15 /// The pub process running "pub serve". | 
| 16 ScheduledProcess _pubServer; | 16 ScheduledProcess _pubServer; | 
| 17 | 17 | 
| 18 /// The ephemeral port assigned to the running server. | 18 /// The ephemeral port assigned to the running server. | 
| 19 int _port; | 19 int _port; | 
| 20 | 20 | 
|  | 21 /// The code for a transformer that renames ".txt" files to ".out" and adds a | 
|  | 22 /// ".out" suffix. | 
|  | 23 const REWRITE_TRANSFORMER = """ | 
|  | 24 import 'dart:async'; | 
|  | 25 | 
|  | 26 import 'package:barback/barback.dart'; | 
|  | 27 | 
|  | 28 class RewriteTransformer extends Transformer { | 
|  | 29   RewriteTransformer(); | 
|  | 30 | 
|  | 31   String get allowedExtensions => '.txt'; | 
|  | 32 | 
|  | 33   Future apply(Transform transform) { | 
|  | 34     return transform.primaryInput | 
|  | 35         .then((input) => input.readAsString()) | 
|  | 36         .then((contents) { | 
|  | 37       var id = transform.primaryId.changeExtension(".out"); | 
|  | 38       transform.addOutput(new Asset.fromString(id, "\$contents.out")); | 
|  | 39     }); | 
|  | 40   } | 
|  | 41 } | 
|  | 42 """; | 
|  | 43 | 
| 21 /// Schedules starting the "pub serve" process. | 44 /// Schedules starting the "pub serve" process. | 
| 22 /// | 45 /// | 
| 23 /// If [shouldInstallFirst] is `true`, validates that pub install is run first. | 46 /// If [shouldInstallFirst] is `true`, validates that pub install is run first. | 
| 24 void startPubServe({bool shouldInstallFirst: false}) { | 47 /// | 
|  | 48 /// Returns the `pub serve` process. | 
|  | 49 ScheduledProcess startPubServe({bool shouldInstallFirst: false}) { | 
| 25   // Use port 0 to get an ephemeral port. | 50   // Use port 0 to get an ephemeral port. | 
| 26   _pubServer = startPub(args: ["serve", "--port=0"]); | 51   _pubServer = startPub(args: ["serve", "--port=0"]); | 
| 27 | 52 | 
| 28   if (shouldInstallFirst) { | 53   if (shouldInstallFirst) { | 
| 29     expect(_pubServer.nextLine(), | 54     expect(_pubServer.nextLine(), | 
| 30         completion(startsWith("Dependencies have changed"))); | 55         completion(startsWith("Dependencies have changed"))); | 
| 31     expect(_pubServer.nextLine(), | 56     expect(_pubServer.nextLine(), | 
| 32         completion(startsWith("Resolving dependencies..."))); | 57         completion(startsWith("Resolving dependencies..."))); | 
| 33     expect(_pubServer.nextLine(), | 58     expect(_pubServer.nextLine(), | 
| 34         completion(equals("Dependencies installed!"))); | 59         completion(equals("Dependencies installed!"))); | 
| 35   } | 60   } | 
| 36 | 61 | 
| 37   expect(_pubServer.nextLine().then(_parsePort), completes); | 62   expect(_pubServer.nextLine().then(_parsePort), completes); | 
|  | 63   return _pubServer; | 
| 38 } | 64 } | 
| 39 | 65 | 
| 40 /// Parses the port number from the "Serving blah on localhost:1234" line | 66 /// Parses the port number from the "Serving blah on localhost:1234" line | 
| 41 /// printed by pub serve. | 67 /// printed by pub serve. | 
| 42 void _parsePort(String line) { | 68 void _parsePort(String line) { | 
| 43   var match = new RegExp(r"localhost:(\d+)").firstMatch(line); | 69   var match = new RegExp(r"localhost:(\d+)").firstMatch(line); | 
| 44   assert(match != null); | 70   assert(match != null); | 
| 45   _port = int.parse(match[1]); | 71   _port = int.parse(match[1]); | 
| 46 } | 72 } | 
| 47 | 73 | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 62 /// Schedules an HTTP request to the running pub server with [urlPath] and | 88 /// Schedules an HTTP request to the running pub server with [urlPath] and | 
| 63 /// verifies that it responds with a 404. | 89 /// verifies that it responds with a 404. | 
| 64 void requestShould404(String urlPath) { | 90 void requestShould404(String urlPath) { | 
| 65   schedule(() { | 91   schedule(() { | 
| 66     return http.get("http://localhost:$_port/$urlPath").then((response) { | 92     return http.get("http://localhost:$_port/$urlPath").then((response) { | 
| 67       expect(response.statusCode, equals(404)); | 93       expect(response.statusCode, equals(404)); | 
| 68     }); | 94     }); | 
| 69   }, "request $urlPath"); | 95   }, "request $urlPath"); | 
| 70 } | 96 } | 
| 71 | 97 | 
|  | 98 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies | 
|  | 99 /// that it responds with a 405. | 
|  | 100 void postShould405(String urlPath) { | 
|  | 101   schedule(() { | 
|  | 102     return http.post("http://localhost:$_port/$urlPath").then((response) { | 
|  | 103       expect(response.statusCode, equals(405)); | 
|  | 104     }); | 
|  | 105   }, "request $urlPath"); | 
|  | 106 } | 
|  | 107 | 
| 72 /// Reads lines from pub serve's stdout until it prints the build success | 108 /// Reads lines from pub serve's stdout until it prints the build success | 
| 73 /// message. | 109 /// message. | 
| 74 /// | 110 /// | 
| 75 /// The schedule will not proceed until the output is found. If not found, it | 111 /// The schedule will not proceed until the output is found. If not found, it | 
| 76 /// will eventually time out. | 112 /// will eventually time out. | 
| 77 void waitForBuildSuccess() { | 113 void waitForBuildSuccess() { | 
| 78   nextLine() { | 114   nextLine() { | 
| 79     return _pubServer.nextLine().then((line) { | 115     return _pubServer.nextLine().then((line) { | 
| 80       if (line.contains("successfully")) return; | 116       if (line.contains("successfully")) return; | 
| 81 | 117 | 
| 82       // This line wasn't it, so ignore it and keep trying. | 118       // This line wasn't it, so ignore it and keep trying. | 
| 83       return nextLine(); | 119       return nextLine(); | 
| 84     }); | 120     }); | 
| 85   } | 121   } | 
| 86 | 122 | 
| 87   schedule(nextLine); | 123   schedule(nextLine); | 
| 88 } | 124 } | 
| OLD | NEW | 
|---|