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