| 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 /** The default pipeline code for running a test file. */ | 5 /** The default pipeline code for running a test file. */ |
| 6 library pipeline; | 6 library pipeline; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:math'; |
| 9 part 'pipeline_utils.dart'; | 10 part 'pipeline_utils.dart'; |
| 10 | 11 |
| 11 /** | 12 /** |
| 12 * The configuration passed in to the pipeline runner; this essentially | 13 * The configuration passed in to the pipeline runner; this essentially |
| 13 * contains all the command line arguments passded to testrunner plus some | 14 * contains all the command line arguments passded to testrunner plus some |
| 14 * synthesized ones. | 15 * synthesized ones. |
| 15 */ | 16 */ |
| 16 Map config; | 17 Map config; |
| 17 | 18 |
| 18 /** Paths to the various generated temporary files. */ | 19 /** Paths to the various generated temporary files. */ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 30 | 31 |
| 31 /** Path to the script file referenced from the HTML wrapper (Dart or JS). */ | 32 /** Path to the script file referenced from the HTML wrapper (Dart or JS). */ |
| 32 String scriptFile = null; | 33 String scriptFile = null; |
| 33 | 34 |
| 34 /** MIME type of the script. */ | 35 /** MIME type of the script. */ |
| 35 String scriptType; | 36 String scriptType; |
| 36 | 37 |
| 37 /** Process id for the HTTP server. */ | 38 /** Process id for the HTTP server. */ |
| 38 int serverId; | 39 int serverId; |
| 39 | 40 |
| 41 /** Port used by HTTP server. */ |
| 42 int serverPort; |
| 43 |
| 44 /** Root directory for static files used by HTTP server. */ |
| 45 String serverRoot; |
| 46 |
| 47 /** Path of the HTTP server script. */ |
| 48 String serverPath; |
| 49 |
| 50 /** Number of attempts we will make to start the HTTP server. */ |
| 51 const int MAX_SERVER_TRIES = 10; |
| 52 |
| 40 void main() { | 53 void main() { |
| 41 port.receive((cfg, replyPort) { | 54 port.receive((cfg, replyPort) { |
| 42 config = cfg; | 55 config = cfg; |
| 43 initPipeline(replyPort); | 56 initPipeline(replyPort); |
| 44 wrapStage(); | 57 startHTTPServerStage(); |
| 58 }); |
| 59 } |
| 60 |
| 61 /** Initial pipeline stage - starts the HTTP server, if appropriate. */ |
| 62 |
| 63 startHTTPServerStage() { |
| 64 if (config["server"]) { |
| 65 serverPath = config["testfile"]; |
| 66 // Replace .dart with _server.dart to get test's server file, if any. |
| 67 var truncLen = serverPath.length - '.dart'.length; |
| 68 serverPath = '${serverPath.substring(0, truncLen)}_server.dart'; |
| 69 var serverFile = new File(serverPath); |
| 70 if (!serverFile.existsSync()) { |
| 71 // No custom server; run the default server. |
| 72 serverPath = '${config["runnerDir"]}/http_server_runner.dart'; |
| 73 } |
| 74 if (serverPath != null) { |
| 75 serverRoot = config["root"]; |
| 76 if (serverRoot == null) { |
| 77 // Set the root to be the directory containing the test file. |
| 78 serverRoot = getDirectory(config["testfile"]); |
| 79 } |
| 80 |
| 81 if (config["port"] == null) { |
| 82 // In this case we have to choose a random port and we need |
| 83 // to see if the server starts successfully on that port. |
| 84 var r = new Random(); |
| 85 tryStartHTTPServer(r, MAX_SERVER_TRIES); |
| 86 } else { |
| 87 serverPort = parseInt(config["port"]); |
| 88 // Start the HTTP server. |
| 89 serverId = startProcess(config["dart"], |
| 90 [ serverPath, '--port=$serverPort', '--root=$serverRoot']); |
| 91 } |
| 92 } |
| 93 } |
| 94 wrapStage(); |
| 95 } |
| 96 |
| 97 void tryStartHTTPServer(Random r, int remainingAttempts) { |
| 98 // Pick a port from 1024 to 32767. |
| 99 serverPort = 1024 + r.nextInt(32768 - 1024); |
| 100 logMessage('Trying ${config["dart"]} $serverPath --port=$serverPort ' |
| 101 '--root=$serverRoot'); |
| 102 serverId = startProcess(config["dart"], |
| 103 [ serverPath, '--port=$serverPort', '--root=$serverRoot'], |
| 104 (line) { |
| 105 if (line.startsWith('Server listening')) { |
| 106 wrapStage(); |
| 107 } else if (remainingAttempts == 0) { |
| 108 print('Failed to start HTTP server after $MAX_SERVER_TRIES' |
| 109 ' attempts; aborting.'); |
| 110 exit(1); |
| 111 } else { |
| 112 tryStartHTTPServer(r, remainingAttempts - 1); |
| 113 } |
| 45 }); | 114 }); |
| 46 } | 115 } |
| 47 | 116 |
| 48 /** Initial pipeline stage - generates Dart and HTML wrapper files. */ | 117 /** Initial pipeline stage - generates Dart and HTML wrapper files. */ |
| 49 wrapStage() { | 118 wrapStage() { |
| 50 var tmpDir = config["tempdir"]; | 119 var tmpDir = config["tempdir"]; |
| 51 var testFile = config["testfile"]; | 120 var testFile = config["testfile"]; |
| 52 | 121 |
| 53 // Make sure the temp dir exists. | 122 // Make sure the temp dir exists. |
| 54 var d = new Directory(tmpDir); | 123 var d = new Directory(tmpDir); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 import 'dart:isolate'; | 164 import 'dart:isolate'; |
| 96 import '${config["unittest"]}' as unittest; | 165 import '${config["unittest"]}' as unittest; |
| 97 import '${config["testfile"]}' as test; | 166 import '${config["testfile"]}' as test; |
| 98 part '${config["runnerDir"]}/standard_test_runner.dart'; | 167 part '${config["runnerDir"]}/standard_test_runner.dart'; |
| 99 '''; | 168 '''; |
| 100 extras = ''' | 169 extras = ''' |
| 101 includeFilters = ${config["include"]}; | 170 includeFilters = ${config["include"]}; |
| 102 excludeFilters = ${config["exclude"]}; | 171 excludeFilters = ${config["exclude"]}; |
| 103 tprint = (msg) => print('###\$msg'); | 172 tprint = (msg) => print('###\$msg'); |
| 104 notifyDone = (e) {}; | 173 notifyDone = (e) {}; |
| 174 unittest.testState["port"] = $serverPort; |
| 105 '''; | 175 '''; |
| 106 } else { | 176 } else { |
| 107 directives = ''' | 177 directives = ''' |
| 108 import 'dart:html'; | 178 import 'dart:html'; |
| 109 import 'dart:isolate'; | 179 import 'dart:isolate'; |
| 110 import '${config["unittest"]}' as unittest; | 180 import '${config["unittest"]}' as unittest; |
| 111 import '${config["testfile"]}' as test; | 181 import '${config["testfile"]}' as test; |
| 112 part '${config["runnerDir"]}/standard_test_runner.dart'; | 182 part '${config["runnerDir"]}/standard_test_runner.dart'; |
| 113 '''; | 183 '''; |
| 114 extras = ''' | 184 extras = ''' |
| 115 includeFilters = ${config["include"]}; | 185 includeFilters = ${config["include"]}; |
| 116 excludeFilters = ${config["exclude"]}; | 186 excludeFilters = ${config["exclude"]}; |
| 117 tprint = (msg) => query('#console').addText('###\$msg\\n'); | 187 tprint = (msg) => query('#console').addText('###\$msg\\n'); |
| 118 notifyDone = (e) => window.postMessage('done', '*'); | 188 notifyDone = (e) => window.postMessage('done', '*'); |
| 189 unittest.testState["port"] = $serverPort; |
| 119 '''; | 190 '''; |
| 120 } | 191 } |
| 121 | 192 |
| 122 var action = 'process(test.main, unittest.runTests)'; | 193 var action = 'process(test.main, unittest.runTests)'; |
| 123 if (config["layout-text"]) { | 194 if (config["layout-text"]) { |
| 124 action = 'runTextLayoutTests()'; | 195 action = 'runTextLayoutTests()'; |
| 125 } else if (config["layout-pixel"]) { | 196 } else if (config["layout-pixel"]) { |
| 126 action = 'runPixelLayoutTests()'; | 197 action = 'runPixelLayoutTests()'; |
| 127 } else if (config["list-tests"]) { | 198 } else if (config["list-tests"]) { |
| 128 action = 'process(test.main, listTests)'; | 199 action = 'process(test.main, listTests)'; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 import 'dart:isolate'; | 233 import 'dart:isolate'; |
| 163 import 'dart:html'; | 234 import 'dart:html'; |
| 164 import 'dart:uri'; | 235 import 'dart:uri'; |
| 165 import '${config["unittest"]}' as 'unittest' ; | 236 import '${config["unittest"]}' as 'unittest' ; |
| 166 import '$testFile', prefix: 'test' ; | 237 import '$testFile', prefix: 'test' ; |
| 167 part '${config["runnerDir"]}/layout_test_runner.dart'; | 238 part '${config["runnerDir"]}/layout_test_runner.dart'; |
| 168 | 239 |
| 169 main() { | 240 main() { |
| 170 includeFilters = ${config["include"]}; | 241 includeFilters = ${config["include"]}; |
| 171 excludeFilters = ${config["exclude"]}; | 242 excludeFilters = ${config["exclude"]}; |
| 243 unittest.testState["port"] = $serverPort; |
| 172 runTests(test.main); | 244 runTests(test.main); |
| 173 } | 245 } |
| 174 '''); | 246 '''); |
| 175 } | 247 } |
| 176 | 248 |
| 177 | |
| 178 // Create the HTML wrapper and compile to Javascript if necessary. | 249 // Create the HTML wrapper and compile to Javascript if necessary. |
| 179 var isJavascript = config["runtime"] == 'drt-js'; | 250 var isJavascript = config["runtime"] == 'drt-js'; |
| 180 if (config["runtime"] == 'drt-dart' || isJavascript) { | 251 if (config["runtime"] == 'drt-dart' || isJavascript) { |
| 181 var bodyElements, runAsText; | 252 var bodyElements, runAsText; |
| 182 | 253 |
| 183 if (config["layout"]) { | 254 if (config["layout"]) { |
| 184 sourceFile = tempChildDartFile; | 255 sourceFile = tempChildDartFile; |
| 185 scriptFile = isJavascript ? tempChildJsFile : tempChildDartFile; | 256 scriptFile = isJavascript ? tempChildJsFile : tempChildDartFile; |
| 186 bodyElements = ''; | 257 bodyElements = ''; |
| 187 } else { | 258 } else { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 [ '--out=$scriptFile', '$sourceFile' ]). | 315 [ '--out=$scriptFile', '$sourceFile' ]). |
| 245 then(runTestStage); | 316 then(runTestStage); |
| 246 } | 317 } |
| 247 } else { | 318 } else { |
| 248 runTestStage(0); | 319 runTestStage(0); |
| 249 } | 320 } |
| 250 } | 321 } |
| 251 | 322 |
| 252 /** Third stage of pipeline - runs the tests. */ | 323 /** Third stage of pipeline - runs the tests. */ |
| 253 runTestStage(_) { | 324 runTestStage(_) { |
| 254 if (config["server"]) { | |
| 255 var serverPath = config["testfile"]; | |
| 256 // Replace .dart with _server.dart to get test's server file, if any. | |
| 257 var truncLen = serverPath.length - '.dart'.length; | |
| 258 serverPath = '${serverPath.substring(0, truncLen)}_server.dart'; | |
| 259 var serverFile = new File(serverPath); | |
| 260 if (!serverFile.existsSync()) { | |
| 261 // No custom server; run the default server. | |
| 262 serverPath = '${config["runnerDir"]}/http_server_runner.dart'; | |
| 263 } | |
| 264 if (serverPath != null) { | |
| 265 var root = config["root"]; | |
| 266 if (root == null) { | |
| 267 // Set the root to be the directory containing the test file. | |
| 268 root = getDirectory(config["testfile"]); | |
| 269 } | |
| 270 // Start the HTTP server. | |
| 271 serverId = startProcess(config["dart"], | |
| 272 [ serverPath, '--port=${config["port"]}', '--root=$root']); | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 var cmd, args; | 325 var cmd, args; |
| 277 if (config["runtime"] == 'vm' || config["layout"]) { // Run the tests. | 326 if (config["runtime"] == 'vm' || config["layout"]) { // Run the tests. |
| 278 if (config["checked"]) { | 327 if (config["checked"]) { |
| 279 cmd = config["dart"]; | 328 cmd = config["dart"]; |
| 280 args = [ '--enable_asserts', '--enable_type_checks', tempDartFile ]; | 329 args = [ '--enable_asserts', '--enable_type_checks', tempDartFile ]; |
| 281 } else { | 330 } else { |
| 282 cmd = config["dart"]; | 331 cmd = config["dart"]; |
| 283 args = [ tempDartFile ]; | 332 args = [ tempDartFile ]; |
| 284 } | 333 } |
| 285 } else { | 334 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 300 | 349 |
| 301 if (!config["keep-files"]) { // Remove the temporary files. | 350 if (!config["keep-files"]) { // Remove the temporary files. |
| 302 cleanup(tempDartFile); | 351 cleanup(tempDartFile); |
| 303 cleanup(tempHtmlFile); | 352 cleanup(tempHtmlFile); |
| 304 cleanup(tempJsFile); | 353 cleanup(tempJsFile); |
| 305 cleanup(tempChildDartFile); | 354 cleanup(tempChildDartFile); |
| 306 cleanup(tempChildJsFile); | 355 cleanup(tempChildJsFile); |
| 307 } | 356 } |
| 308 completePipeline(exitcode); | 357 completePipeline(exitcode); |
| 309 } | 358 } |
| OLD | NEW |