OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library record_and_replay; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:json' as json; |
| 9 import 'dart:utf'; |
| 10 |
| 11 import 'test_runner.dart'; |
| 12 |
| 13 /* |
| 14 * Json files look like this: |
| 15 * |
| 16 * [ |
| 17 * { |
| 18 * 'name' : '...', |
| 19 * 'configuration' : '...', |
| 20 * 'command' : { |
| 21 * 'timeout_limit' : 60, |
| 22 * 'executable' : '...', |
| 23 * 'arguments' : ['arg1, 'arg2', '...'], |
| 24 * }, |
| 25 * 'command_output' : { |
| 26 * 'exit_code' : 42, |
| 27 * 'stdout' : '...', |
| 28 * 'stderr' : '...', |
| 29 * 'duration' : 1.5, |
| 30 * 'did_timeout' : false, |
| 31 * }, |
| 32 * }, |
| 33 * .... |
| 34 * ] |
| 35 */ |
| 36 |
| 37 |
| 38 class TestCaseRecorder { |
| 39 Path _outputPath; |
| 40 List<Map> _recordedCommandInvocations = []; |
| 41 var _cwd; |
| 42 |
| 43 TestCaseRecorder(this._outputPath) { |
| 44 _cwd = new Directory.current().path; |
| 45 } |
| 46 |
| 47 void nextTestCase(TestCase testCase) { |
| 48 assert(testCase.commands.length == 1); |
| 49 |
| 50 var command = testCase.commands[0]; |
| 51 assert(command.environment == null); |
| 52 |
| 53 var arguments = []; |
| 54 for (var rawArgument in command.arguments) { |
| 55 if (rawArgument.startsWith(_cwd)) { |
| 56 var relative = new Path(rawArgument).relativeTo(new Path(_cwd)); |
| 57 arguments.add(relative.toNativePath()); |
| 58 } else { |
| 59 arguments.add(rawArgument); |
| 60 } |
| 61 } |
| 62 |
| 63 var commandExecution = { |
| 64 'name' : testCase.displayName, |
| 65 'configuration' : testCase.configurationString, |
| 66 'command' : { |
| 67 'timeout_limit' : testCase.timeout, |
| 68 'executable' : command.executable, |
| 69 'arguments' : arguments, |
| 70 }, |
| 71 }; |
| 72 _recordedCommandInvocations.add(commandExecution); |
| 73 } |
| 74 |
| 75 void finish() { |
| 76 var file = new File.fromPath(_outputPath); |
| 77 var jsonString = json.stringify(_recordedCommandInvocations); |
| 78 file.writeAsStringSync(jsonString); |
| 79 print("TestCaseRecorder: written all TestCases to ${_outputPath}"); |
| 80 } |
| 81 } |
| 82 |
| 83 class TestCaseOutputArchive { |
| 84 Map<String, Map> _testCaseOutputRecords; |
| 85 |
| 86 void loadFromPath(Path recordingPath) { |
| 87 var file = new File.fromPath(recordingPath); |
| 88 var testCases = json.parse(file.readAsStringSync()); |
| 89 _testCaseOutputRecords = {}; |
| 90 for (var testCase in testCases) { |
| 91 var key = _indexKey(testCase['configuration'], testCase['name']); |
| 92 _testCaseOutputRecords[key] = testCase['command_output']; |
| 93 } |
| 94 } |
| 95 |
| 96 CommandOutput outputOf(TestCase testCase) { |
| 97 var key = _indexKey(testCase.configurationString, testCase.displayName); |
| 98 var command_output = _testCaseOutputRecords[key]; |
| 99 if (command_output == null) { |
| 100 print("Sorry, but there is no command output for " |
| 101 "${testCase.displayName}"); |
| 102 |
| 103 exit(42); |
| 104 } |
| 105 |
| 106 double seconds = command_output['duration']; |
| 107 var duration = new Duration(seconds: seconds.round(), |
| 108 milliseconds: (seconds/1000).round()); |
| 109 var commandOutput = new CommandOutput.fromCase( |
| 110 testCase, |
| 111 testCase.commands.first, |
| 112 command_output['exit_code'], |
| 113 false, |
| 114 command_output['did_timeout'], |
| 115 encodeUtf8(command_output['stdout']), |
| 116 encodeUtf8(command_output['stderr']), |
| 117 duration, |
| 118 false); |
| 119 return commandOutput; |
| 120 } |
| 121 |
| 122 String _indexKey(String configuration, String name) { |
| 123 return "${configuration}__$name"; |
| 124 } |
| 125 } |
| 126 |
OLD | NEW |