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

Side by Side Diff: tools/testing/dart/test_progress.dart

Issue 12302016: Refactoring of ProgressIndicator (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added the TimingPrinter EventListener Created 7 years, 10 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
« no previous file with comments | « tools/test.dart ('k') | tools/testing/dart/test_runner.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 library test_progress; 5 library test_progress;
6 6
7 import "dart:io"; 7 import "dart:io";
8 import "dart:io" as io; 8 import "dart:io" as io;
9 import "http_server.dart" as http_server; 9 import "http_server.dart" as http_server;
10 import "status_file_parser.dart"; 10 import "status_file_parser.dart";
11 import "test_runner.dart"; 11 import "test_runner.dart";
12 import "test_suite.dart"; 12 import "test_suite.dart";
13 import "utils.dart"; 13 import "utils.dart";
14 14
15 class ProgressIndicator { 15 String _padTime(int time) {
16 ProgressIndicator(this._startTime, this._printTiming) 16 if (time == 0) {
17 : _tests = [], _failureSummary = []; 17 return '00';
18 } else if (time < 10) {
19 return '0$time';
20 } else {
21 return '$time';
22 }
23 }
24
25 String _timeString(Duration d) {
26 var min = d.inMinutes;
27 var sec = d.inSeconds % 60;
28 return '${_padTime(min)}:${_padTime(sec)}';
29 }
30
31 class EventListener {
32 void testAdded() { }
33 void start(TestCase test) { }
34 void done(TestCase test) { }
35 void allTestsKnown() { }
36 void allDone() { }
37 }
38
39 class ExitCodeSetter extends EventListener {
40 bool _failingTest = false;
41
42 void done(TestCase test) {
43 _failingTest = _failingTest || test.lastCommandOutput.unexpectedOutput;
44 }
45 void allDone() {
46 if (_failingTest) {
47 io.exitCode = 1;
48 }
49 }
50 }
51
52 class SummaryPrinter extends EventListener {
53 void allTestsKnown() {
54 if (SummaryReport.total > 0) {
55 SummaryReport.printReport();
56 }
57 }
58 }
59
60 class TimingPrinter extends EventListener {
61 List<TestCase> _tests = <TestCase>[];
62 Date _startTime;
63
64 TimingPrinter(this._startTime);
65
66 void done(TestCase testCase) {
67 _tests.add(testCase);
68 }
69
70 void allDone() {
71 // TODO: We should take all the commands into account
ahe 2013/02/19 11:43:42 Please use "TODO(username): Comment".
72 Duration d = (new Date.now()).difference(_startTime);
73 print('\n--- Total time: ${_timeString(d)} ---');
74 _tests.sort((a, b) {
75 Duration aDuration = a.lastCommandOutput.time;
76 Duration bDuration = b.lastCommandOutput.time;
77 return bDuration.inMilliseconds - aDuration.inMilliseconds;
78 });
79 for (int i = 0; i < 20 && i < _tests.length; i++) {
80 var name = _tests[i].displayName;
81 var duration = _tests[i].lastCommandOutput.time;
82 var configuration = _tests[i].configurationString;
83 print('${duration} - $configuration $name');
84 }
85 }
86 }
87
88 class LineProgressIndicator extends EventListener {
89 void done(TestCase test) {
90 var status = 'pass';
91 if (test.lastCommandOutput.unexpectedOutput) {
92 status = 'fail';
93 }
94 print('Done ${test.configurationString} ${test.displayName}: $status');
95 }
96 }
97
98 class ProgressIndicator extends EventListener {
99 ProgressIndicator(this._startTime)
100 : _failureSummary = [];
18 101
19 factory ProgressIndicator.fromName(String name, 102 factory ProgressIndicator.fromName(String name,
20 Date startTime, 103 Date startTime) {
21 bool printTiming) {
22 switch (name) { 104 switch (name) {
23 case 'compact': 105 case 'compact':
24 return new CompactProgressIndicator(startTime, printTiming); 106 return new CompactProgressIndicator(startTime);
25 case 'color': 107 case 'color':
26 return new ColorProgressIndicator(startTime, printTiming); 108 return new ColorProgressIndicator(startTime);
27 case 'line': 109 case 'line':
28 return new LineProgressIndicator(startTime, printTiming); 110 return new LineProgressIndicator();
29 case 'verbose': 111 case 'verbose':
30 return new VerboseProgressIndicator(startTime, printTiming); 112 return new VerboseProgressIndicator(startTime);
31 case 'silent':
32 return new SilentProgressIndicator(startTime, printTiming);
33 case 'status': 113 case 'status':
34 return new StatusProgressIndicator(startTime, printTiming); 114 return new StatusProgressIndicator(startTime);
35 case 'buildbot': 115 case 'buildbot':
36 return new BuildbotProgressIndicator(startTime, printTiming); 116 return new BuildbotProgressIndicator(startTime);
37 case 'diff': 117 case 'diff':
38 return new DiffProgressIndicator(startTime, printTiming); 118 return new DiffProgressIndicator(startTime);
39 default: 119 default:
40 assert(false); 120 assert(false);
41 break; 121 break;
42 } 122 }
43 } 123 }
44 124
45 void testAdded() { _foundTests++; } 125 void testAdded() { _foundTests++; }
46 126
47 void start(TestCase test) { 127 void start(TestCase test) {
48 _printStartProgress(test); 128 _printStartProgress(test);
(...skipping 12 matching lines...) Expand all
61 _skippedCompilations++; 141 _skippedCompilations++;
62 } 142 }
63 143
64 if (test.lastCommandOutput.unexpectedOutput) { 144 if (test.lastCommandOutput.unexpectedOutput) {
65 _failedTests++; 145 _failedTests++;
66 _printFailureOutput(test); 146 _printFailureOutput(test);
67 } else { 147 } else {
68 _passedTests++; 148 _passedTests++;
69 } 149 }
70 _printDoneProgress(test); 150 _printDoneProgress(test);
71 // If we need to print timing information we hold on to all completed
72 // tests.
73 if (_printTiming) _tests.add(test);
74 } 151 }
75 152
76 void allTestsKnown() { 153 void allTestsKnown() {
77 if (!_allTestsKnown) SummaryReport.printReport();
78 _allTestsKnown = true; 154 _allTestsKnown = true;
79 } 155 }
80 156
81 void _printSkippedCompilationInfo() { 157 void _printSkippedCompilationInfo() {
82 if (_skippedCompilations > 0) { 158 if (_skippedCompilations > 0) {
83 print('\n$_skippedCompilations compilations were skipped because ' 159 print('\n$_skippedCompilations compilations were skipped because '
84 'the previous output was already up to date\n'); 160 'the previous output was already up to date\n');
85 } 161 }
86 } 162 }
87 163
88 void _printTimingInformation() {
89 if (_printTiming) {
90 // TODO: We should take all the commands into account
91 Duration d = (new Date.now()).difference(_startTime);
92 print('\n--- Total time: ${_timeString(d)} ---');
93 _tests.sort((a, b) {
94 Duration aDuration = a.lastCommandOutput.time;
95 Duration bDuration = b.lastCommandOutput.time;
96 return bDuration.inMilliseconds - aDuration.inMilliseconds;
97 });
98 for (int i = 0; i < 20 && i < _tests.length; i++) {
99 var name = _tests[i].displayName;
100 var duration = _tests[i].lastCommandOutput.time;
101 var configuration = _tests[i].configurationString;
102 print('${duration} - $configuration $name');
103 }
104 }
105 }
106
107 void allDone() { 164 void allDone() {
108 _printFailureSummary(); 165 _printFailureSummary();
109 _printStatus(); 166 _printStatus();
110 _printSkippedCompilationInfo(); 167 _printSkippedCompilationInfo();
111 _printTimingInformation();
112 stdout.close(); 168 stdout.close();
113 stderr.close(); 169 stderr.close();
114 if (_failedTests > 0) {
115 io.exitCode = 1;
116 }
117 } 170 }
118 171
119 void _printStartProgress(TestCase test) {} 172 void _printStartProgress(TestCase test) {}
120 void _printDoneProgress(TestCase test) {} 173 void _printDoneProgress(TestCase test) {}
121 174
122 String _pad(String s, int length) { 175 String _pad(String s, int length) {
123 StringBuffer buffer = new StringBuffer(); 176 StringBuffer buffer = new StringBuffer();
124 for (int i = s.length; i < length; i++) { 177 for (int i = s.length; i < length; i++) {
125 buffer.add(' '); 178 buffer.add(' ');
126 } 179 }
127 buffer.add(s); 180 buffer.add(s);
128 return buffer.toString(); 181 return buffer.toString();
129 } 182 }
130 183
131 String _padTime(int time) {
132 if (time == 0) {
133 return '00';
134 } else if (time < 10) {
135 return '0$time';
136 } else {
137 return '$time';
138 }
139 }
140
141 String _timeString(Duration d) {
142 var min = d.inMinutes;
143 var sec = d.inSeconds % 60;
144 return '${_padTime(min)}:${_padTime(sec)}';
145 }
146
147 String _header(String header) => header; 184 String _header(String header) => header;
148 185
149 void _printFailureOutput(TestCase test) { 186 void _printFailureOutput(TestCase test) {
150 var failureOutput = _buildFailureOutput(test); 187 var failureOutput = _buildFailureOutput(test);
151 for (var line in failureOutput) { 188 for (var line in failureOutput) {
152 print(line); 189 print(line);
153 } 190 }
154 _failureSummary.addAll(failureOutput); 191 _failureSummary.addAll(failureOutput);
155 } 192 }
156 193
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 int get numFailedTests => _failedTests; 287 int get numFailedTests => _failedTests;
251 288
252 int _completedTests() => _passedTests + _failedTests; 289 int _completedTests() => _passedTests + _failedTests;
253 290
254 int _foundTests = 0; 291 int _foundTests = 0;
255 int _passedTests = 0; 292 int _passedTests = 0;
256 int _failedTests = 0; 293 int _failedTests = 0;
257 int _skippedCompilations = 0; 294 int _skippedCompilations = 0;
258 bool _allTestsKnown = false; 295 bool _allTestsKnown = false;
259 Date _startTime; 296 Date _startTime;
260 bool _printTiming;
261 List<TestCase> _tests;
262 List<String> _failureSummary; 297 List<String> _failureSummary;
263 } 298 }
264 299
265
266 class SilentProgressIndicator extends ProgressIndicator {
267 SilentProgressIndicator(Date startTime, bool printTiming)
268 : super(startTime, printTiming);
269 void testAdded() { }
270 void start(TestCase test) { }
271 void done(TestCase test) { }
272 void _printStartProgress(TestCase test) { }
273 void _printDoneProgress(TestCase test) { }
274 void allTestsKnown() { }
275 void allDone() { }
276 }
277
278 abstract class CompactIndicator extends ProgressIndicator { 300 abstract class CompactIndicator extends ProgressIndicator {
279 CompactIndicator(Date startTime, bool printTiming) 301 CompactIndicator(Date startTime)
280 : super(startTime, printTiming); 302 : super(startTime);
281 303
282 void allDone() { 304 void allDone() {
283 stdout.write('\n'.charCodes); 305 stdout.write('\n'.charCodes);
284 _printFailureSummary(); 306 _printFailureSummary();
285 _printSkippedCompilationInfo(); 307 _printSkippedCompilationInfo();
286 _printTimingInformation();
287 if (_failedTests > 0) { 308 if (_failedTests > 0) {
288 // We may have printed many failure logs, so reprint the summary data. 309 // We may have printed many failure logs, so reprint the summary data.
289 _printProgress(); 310 _printProgress();
290 print(''); 311 print('');
291 } 312 }
292 stdout.close(); 313 stdout.close();
293 stderr.close(); 314 stderr.close();
294 if (_failedTests > 0) {
295 io.exitCode = 1;
296 }
297 }
298
299 void allTestsKnown() {
300 if (!_allTestsKnown && SummaryReport.total > 0) {
301 // Clear progress indicator before printing summary report.
302 stdout.write(
303 '\r \r'.charCodes);
304 SummaryReport.printReport();
305 }
306 _allTestsKnown = true;
307 } 315 }
308 316
309 void _printStartProgress(TestCase test) => _printProgress(); 317 void _printStartProgress(TestCase test) => _printProgress();
310 void _printDoneProgress(TestCase test) => _printProgress(); 318 void _printDoneProgress(TestCase test) => _printProgress();
311 319
312 void _printProgress(); 320 void _printProgress();
313 } 321 }
314 322
315 323
316 class CompactProgressIndicator extends CompactIndicator { 324 class CompactProgressIndicator extends CompactIndicator {
317 CompactProgressIndicator(Date startTime, bool printTiming) 325 CompactProgressIndicator(Date startTime)
318 : super(startTime, printTiming); 326 : super(startTime);
319 327
320 void _printProgress() { 328 void _printProgress() {
321 var percent = ((_completedTests() / _foundTests) * 100).toInt().toString(); 329 var percent = ((_completedTests() / _foundTests) * 100).toInt().toString();
322 var progressPadded = _pad(_allTestsKnown ? percent : '--', 3); 330 var progressPadded = _pad(_allTestsKnown ? percent : '--', 3);
323 var passedPadded = _pad(_passedTests.toString(), 5); 331 var passedPadded = _pad(_passedTests.toString(), 5);
324 var failedPadded = _pad(_failedTests.toString(), 5); 332 var failedPadded = _pad(_failedTests.toString(), 5);
325 Duration d = (new Date.now()).difference(_startTime); 333 Duration d = (new Date.now()).difference(_startTime);
326 var progressLine = 334 var progressLine =
327 '\r[${_timeString(d)} | $progressPadded% | ' 335 '\r[${_timeString(d)} | $progressPadded% | '
328 '+$passedPadded | -$failedPadded]'; 336 '+$passedPadded | -$failedPadded]';
329 stdout.write(progressLine.charCodes); 337 stdout.write(progressLine.charCodes);
330 } 338 }
331 } 339 }
332 340
333 341
334 class ColorProgressIndicator extends CompactIndicator { 342 class ColorProgressIndicator extends CompactIndicator {
335 ColorProgressIndicator(Date startTime, bool printTiming) 343 ColorProgressIndicator(Date startTime)
336 : super(startTime, printTiming); 344 : super(startTime);
337 345
338 static int BOLD = 1; 346 static int BOLD = 1;
339 static int GREEN = 32; 347 static int GREEN = 32;
340 static int RED = 31; 348 static int RED = 31;
341 static int NONE = 0; 349 static int NONE = 0;
342 350
343 addColorWrapped(List<int> codes, String string, int color) { 351 addColorWrapped(List<int> codes, String string, int color) {
344 codes.add(27); 352 codes.add(27);
345 codes.addAll('[${color}m'.charCodes); 353 codes.addAll('[${color}m'.charCodes);
346 codes.addAll(encodeUtf8(string)); 354 codes.addAll(encodeUtf8(string));
(...skipping 18 matching lines...) Expand all
365 } 373 }
366 374
367 String _header(String header) { 375 String _header(String header) {
368 var result = []; 376 var result = [];
369 addColorWrapped(result, header, BOLD); 377 addColorWrapped(result, header, BOLD);
370 return decodeUtf8(result); 378 return decodeUtf8(result);
371 } 379 }
372 } 380 }
373 381
374 382
375 class LineProgressIndicator extends ProgressIndicator {
376 LineProgressIndicator(Date startTime, bool printTiming)
377 : super(startTime, printTiming);
378
379 void _printStartProgress(TestCase test) {
380 }
381
382 void _printDoneProgress(TestCase test) {
383 var status = 'pass';
384 if (test.lastCommandOutput.unexpectedOutput) {
385 status = 'fail';
386 }
387 print('Done ${test.configurationString} ${test.displayName}: $status');
388 }
389 }
390
391
392 class VerboseProgressIndicator extends ProgressIndicator { 383 class VerboseProgressIndicator extends ProgressIndicator {
393 VerboseProgressIndicator(Date startTime, bool printTiming) 384 VerboseProgressIndicator(Date startTime)
394 : super(startTime, printTiming); 385 : super(startTime);
395 386
396 void _printStartProgress(TestCase test) { 387 void _printStartProgress(TestCase test) {
397 print('Starting ${test.configurationString} ${test.displayName}...'); 388 print('Starting ${test.configurationString} ${test.displayName}...');
398 } 389 }
399 390
400 void _printDoneProgress(TestCase test) { 391 void _printDoneProgress(TestCase test) {
401 var status = 'pass'; 392 var status = 'pass';
402 if (test.lastCommandOutput.unexpectedOutput) { 393 if (test.lastCommandOutput.unexpectedOutput) {
403 status = 'fail'; 394 status = 'fail';
404 } 395 }
405 print('Done ${test.configurationString} ${test.displayName}: $status'); 396 print('Done ${test.configurationString} ${test.displayName}: $status');
406 } 397 }
407 } 398 }
408 399
409 400
410 class StatusProgressIndicator extends ProgressIndicator { 401 class StatusProgressIndicator extends ProgressIndicator {
411 StatusProgressIndicator(Date startTime, bool printTiming) 402 StatusProgressIndicator(Date startTime)
412 : super(startTime, printTiming); 403 : super(startTime);
413 404
414 void _printStartProgress(TestCase test) { 405 void _printStartProgress(TestCase test) {
415 } 406 }
416 407
417 void _printDoneProgress(TestCase test) { 408 void _printDoneProgress(TestCase test) {
418 } 409 }
419 } 410 }
420 411
421 412
422 class BuildbotProgressIndicator extends ProgressIndicator { 413 class BuildbotProgressIndicator extends ProgressIndicator {
423 static String stepName; 414 static String stepName;
424 415
425 BuildbotProgressIndicator(Date startTime, bool printTiming) 416 BuildbotProgressIndicator(Date startTime)
426 : super(startTime, printTiming); 417 : super(startTime);
427 418
428 void _printStartProgress(TestCase test) { 419 void _printStartProgress(TestCase test) {
429 } 420 }
430 421
431 void _printDoneProgress(TestCase test) { 422 void _printDoneProgress(TestCase test) {
432 var status = 'pass'; 423 var status = 'pass';
433 if (test.lastCommandOutput.unexpectedOutput) { 424 if (test.lastCommandOutput.unexpectedOutput) {
434 status = 'fail'; 425 status = 'fail';
435 } 426 }
436 var percent = ((_completedTests() / _foundTests) * 100).toInt().toString(); 427 var percent = ((_completedTests() / _foundTests) * 100).toInt().toString();
437 print('Done ${test.configurationString} ${test.displayName}: $status'); 428 print('Done ${test.configurationString} ${test.displayName}: $status');
438 print('@@@STEP_CLEAR@@@'); 429 print('@@@STEP_CLEAR@@@');
439 print('@@@STEP_TEXT@ $percent% +$_passedTests -$_failedTests @@@'); 430 print('@@@STEP_TEXT@ $percent% +$_passedTests -$_failedTests @@@');
440 } 431 }
441 432
442 void _printFailureSummary() { 433 void _printFailureSummary() {
443 if (!_failureSummary.isEmpty && stepName != null) { 434 if (!_failureSummary.isEmpty && stepName != null) {
444 print('@@@STEP_FAILURE@@@'); 435 print('@@@STEP_FAILURE@@@');
445 print('@@@BUILD_STEP $stepName failures@@@'); 436 print('@@@BUILD_STEP $stepName failures@@@');
446 } 437 }
447 super._printFailureSummary(); 438 super._printFailureSummary();
448 } 439 }
449 } 440 }
450 441
451 class DiffProgressIndicator extends ColorProgressIndicator { 442 class DiffProgressIndicator extends ColorProgressIndicator {
452 Map<String, List<String>> statusToConfigs = new Map<String, List<String>>(); 443 Map<String, List<String>> statusToConfigs = new Map<String, List<String>>();
453 444
454 DiffProgressIndicator(Date startTime, bool printTiming) 445 DiffProgressIndicator(Date startTime)
455 : super(startTime, printTiming); 446 : super(startTime);
456 447
457 void _printFailureOutput(TestCase test) { 448 void _printFailureOutput(TestCase test) {
458 String status = '${test.displayName}: ${test.lastCommandOutput.result}'; 449 String status = '${test.displayName}: ${test.lastCommandOutput.result}';
459 List<String> configs = 450 List<String> configs =
460 statusToConfigs.putIfAbsent(status, () => <String>[]); 451 statusToConfigs.putIfAbsent(status, () => <String>[]);
461 configs.add(test.configurationString); 452 configs.add(test.configurationString);
462 if (test.lastCommandOutput.hasTimedOut) { 453 if (test.lastCommandOutput.hasTimedOut) {
463 print('\n${test.displayName} timed out on ${test.configurationString}'); 454 print('\n${test.displayName} timed out on ${test.configurationString}');
464 } 455 }
465 } 456 }
(...skipping 30 matching lines...) Expand all
496 print(''); 487 print('');
497 print('$config:'); 488 print('$config:');
498 statuses.sort((a, b) => a.compareTo(b)); 489 statuses.sort((a, b) => a.compareTo(b));
499 for (String status in statuses) { 490 for (String status in statuses) {
500 print(' $status'); 491 print(' $status');
501 } 492 }
502 }); 493 });
503 _printStatus(); 494 _printStatus();
504 } 495 }
505 } 496 }
OLDNEW
« no previous file with comments | « tools/test.dart ('k') | tools/testing/dart/test_runner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698