Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: utils/testrunner/testrunner.dart

Issue 10897016: Testrunner for 3rd parties. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:executable
+ *
OLDNEW
(Empty)
1 //#!/usr/bin/env dart
2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file.
5
6 /**
7 * testrunner is a program to run Dart unit tests. Unlike $DART/tools/test.dart,
8 * this program is intended for 3rd parties to be able to run unit tests in
9 * a batched fashion. As such, it adds some features and removes others. Some
10 * of the removed features are:
11 *
12 * - No support for test.status files. The assumption is that tests are
13 * expected to pass.
14 * - A restricted set of runtimes. The assumption here is that the Dart
15 * libraries deal with platform dependencies, and so the primary
16 * SKUs that a user of this app would be concerned with would be
17 * Dart-native versus compiled, and client (browser) vs server. To
18 * support these, three runtimes are allowed: 'drt-dart' and 'drt-js' (for
19 * client native and client-compiled, respectively), and 'vm'
20 * (for server-side native).
21 * - No sharding of test processes.
22 *
23 * On the other hand, a number of features have been added:
24 *
25 * - The ability to filter tests by group or name.
26 * - The ability to run tests in isolates.
27 * - The ability to customize the format of the test result messages.
28 * - The ability to list the tests available.
29 *
30 * By default, testrunner will run all tests in the current directory.
31 * With a -R option, it will recurse into subdirectories.
32 * Directories can also be specified on the command line; if
33 * any are specified they will override the use of the current directory.
34 * All files that match the `--test-file-pattern` will be included; by default
35 * this is files with names that end in _test.dart.
36 *
37 * Options can be specified on the command line, via a configuration
38 * file (`--config`) or via a test.config file in the test directory,
39 * in decreasing order of priority.
40 *
41 * The three runtimes are:
42 *
43 * vm - run native Dart in the VM; i.e. using $DARTSDK/dart-sdk/bin/dart.
44 * drt-dart - run native Dart in DumpRenderTree, the headless version of
45 * Dartium, which is located in $DARTSDK/chromium/DumpRenderTree, if
46 * you intsalled the SDK that is bundled with the editor, or available
47 * from http://gsdview.appspot.com/dartium-archive/continuous/
48 * otherwise.
49 *
50 * drt-js - run Dart compiled to Javascript in DumpRenderTree.
51 */
52
53 /* TODO(gram) - Layout tests. The plan here will be to look for a file
54 * with a .layout extension that corresponds to the .dart file, that contains
55 * multiple layouts, one for each test. Each test will be run in its own
56 * instance of DRT and and the result compared with the expected layout.
57 *
58 * TODO(gram) - for TDD, add the ability to indicate that some test cases are
59 * expected to fail temporarily.
60 */
61 #library('testrunner');
62 #import('dart:io');
63 #import('dart:isolate');
64 #import('dart:math');
65 #import('../../pkg/args/args.dart');
66
67 #source('configuration.dart');
68 #source('dart_task.dart');
69 #source('dart_wrap_task.dart');
70 #source('dart2js_task.dart');
71 #source('delete_task.dart');
72 #source('drt_task.dart');
73 #source('html_wrap_task.dart');
74 #source('macros.dart');
75 #source('options.dart');
76 #source('pipeline_runner.dart');
77 #source('pipeline_task.dart');
78 #source('run_process_task.dart');
79 #source('utils.dart');
80
81 /** The set of [PipelineRunner]s to execute. */
82 List _tasks;
83
84 /** The maximum number of pipelines that can run concurrently. */
85 int _maxTasks;
86
87 /** The number of pipelines currently running. */
88 int _numTasks;
89
90 /** The index of the next pipeline runner to execute. */
91 int _nextTask;
92
93 /** The stream to use for high-value messages, like test results. */
94 OutputStream _outStream;
95
96 /** The stream to use for low-value messages, like verbose output. */
97 OutputStream _logStream;
98
99 /** The full set of options. */
100 Configuration config;
101
102 /**
103 * The user can specify output streams on the command line, using 'none',
104 * 'stdout', 'stderr', or a file path; [getStream] will take such a name
105 * and return an appropriate [OutputStream].
106 */
107 OutputStream getStream(String name) {
108 if (name == 'none') {
109 return null;
110 }
111 if (name == 'stdout') {
112 return stdout;
113 }
114 if (name == 'stderr') {
115 return stderr;
116 }
117 return new File(name).openOutputStream(FileMode.WRITE);
118 }
119
120 /**
121 * Generate a templated list of commands that should be executed for each test
122 * file. Each command is an instance of a [PipelineTask].
123 * The commands can make use of a number of metatokens that will be
124 * expanded before execution (see the [Meta] class for details).
125 */
126 List getPipelineTemplate(String runtime, bool checkedMode, bool keepTests) {
127 var pipeline = new List();
128 var pathSep = Platform.pathSeparator;
129 Directory tempDir = new Directory(config.tempDir);
130
131 if (!tempDir.existsSync()) {
132 tempDir.createSync();
133 }
134
135 // Templates for the generated files that are used to run the wrapped test.
136 var basePath =
137 '${config.tempDir}$pathSep${Macros.flattenedDirectory}_'
138 '${Macros.filenameNoExtension}';
139 var tempDartFile = '${basePath}.dart';
140 var tempJsFile = '${basePath}.js';
141 var tempHTMLFile = '${basePath}.html';
142 var tempCSSFile = '${basePath}.css';
143
144 // Add step for wrapping in Dart scaffold.
145 pipeline.add(new DartWrapTask(Macros.fullFilePath, tempDartFile));
146
147 // Add the compiler step, unless we are running native Dart.
148 if (runtime == 'drt-js') {
149 if (checkedMode) {
150 pipeline.add(new Dart2jsTask.checked(tempDartFile, tempJsFile));
151 } else {
152 pipeline.add(new Dart2jsTask(tempDartFile, tempJsFile));
153 }
154 }
155
156 // Add step for wrapping in HTML, if we are running in DRT.
157 if (runtime != 'vm') {
158 // The user can have pre-existing HTML and CSS files for the test in the
159 // same directory and using the same name. The paths to these are matched
160 // by these two templates.
161 var HTMLFile =
162 '${Macros.directory}$pathSep${Macros.filenameNoExtension}.html';
163 var CSSFile =
164 '${Macros.directory}$pathSep${Macros.filenameNoExtension}.css';
165 pipeline.add(new HtmlWrapTask(Macros.fullFilePath,
166 HTMLFile, tempHTMLFile, CSSFile, tempCSSFile));
167 }
168
169 // Add the execution step.
170 if (runtime == 'vm') {
171 if (checkedMode) {
172 pipeline.add(new DartTask.checked(tempDartFile));
173 } else {
174 pipeline.add(new DartTask(tempDartFile));
175 }
176 } else {
177 pipeline.add(new DrtTask(tempHTMLFile));
178 }
179 return pipeline;
180 }
181
182 /**
183 * Given a [List] of [testFiles], either print the list or create
184 * and execute pipelines for the files.
185 */
186 void processTests(List pipelineTemplate, List testFiles) {
187 _outStream = getStream(config.outputStream);
188 _logStream = getStream(config.logStream);
189 if (config.listFiles) {
190 if (_outStream != null) {
191 for (var i = 0; i < testFiles.length; i++) {
192 _outStream.writeString(testFiles[i]);
193 _outStream.writeString('\n');
194 }
195 }
196 } else {
197 // Create execution pipelines for each test file from the pipeline
198 // template and the concrete test file path, and then kick
199 // off execution of the first batch.
200 _tasks = new List();
201 for (var i = 0; i < testFiles.length; i++) {
202 _tasks.add(new PipelineRunner(pipelineTemplate, testFiles[i],
203 config.verbose, completeHandler));
204 }
205
206 _maxTasks = min(config.maxTasks, testFiles.length);
207 _numTasks = 0;
208 _nextTask = 0;
209 spawnTasks();
210 }
211 }
212
213 /** Execute as many tasks as possible up to the maxTasks limit. */
214 void spawnTasks() {
215 while (_numTasks < _maxTasks && _nextTask < _tasks.length) {
216 ++_numTasks;
217 _tasks[_nextTask++].execute();
218 }
219 }
220
221 /**
222 * Handle the completion of a task. Kick off more tasks if we
223 * have them.
224 */
225 void completeHandler(String testFile,
226 int exitCode,
227 List _stdout,
228 List _stderr) {
229 writelog(_stdout, _outStream, _logStream);
230 writelog(_stderr, _outStream, _logStream);
231 --_numTasks;
232 if (exitCode == 0 || !config.stopOnFailure) {
233 spawnTasks();
234 }
235 if (_numTasks == 0) {
236 // No outstanding tasks; we're all done.
237 // We could later print a summary report here.
238 }
239 }
240
241 /**
242 * Our tests are configured so that critical messages have a '###' prefix.
243 * [writeLog] takes the output from a pipeline execution and writes it to
244 * our output streams. It will strip the '###' if necessary on critical
245 * messages; other messages will only be written if verbose output was
246 * specified.
247 */
248 void writelog(List messages, OutputStream out, OutputStream log) {
249 for (var i = 0; i < messages.length; i++) {
250 var msg = messages[i];
251 if (msg.startsWith('###')) {
252 if (out != null) {
253 out.writeString(msg.substring(3));
254 out.writeString('\n');
255 }
256 } else if (config.verbose) {
257 if (log != null) {
258 log.writeString(msg);
259 log.writeString('\n');
260 }
261 }
262 }
263 }
264
265 main() {
266 var optionsParser = getOptionParser();
267 var options = loadConfiguration(optionsParser);
268 if (isSane(options)) {
269 if (options['list-options']) {
270 printOptions(optionsParser, options, false, stdout);
271 } else if (options['list-all-options']) {
272 printOptions(optionsParser, options, true, stdout);
273 } else {
274 config = new Configuration(optionsParser, options);
275 // Build the command templates needed for test compile and execute.
276 var pipelineTemplate = getPipelineTemplate(config.runtime,
277 config.checkedMode,
278 config.keepTests);
279 if (pipelineTemplate != null) {
280 // Build the list of tests and then execute them.
281 List dirs = options.rest;
282 if (dirs.length == 0) {
283 dirs.add('.'); // Use current working directory as default.
284 }
285 buildFileList(dirs,
286 new RegExp(options['test-file-pattern']), options['recurse'],
287 (f) => processTests(pipelineTemplate, f));
288 }
289 }
290 }
291 }
292
293
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698