| 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 |
| 36 List<String> makePathsRelativeToDart(String cwd, List<String> arguments) { |
| 37 var relativeArguments = []; |
| 38 for (var rawArgument in arguments) { |
| 39 if (rawArgument.startsWith(cwd)) { |
| 40 var relative = new Path(rawArgument).relativeTo(new Path(cwd)); |
| 41 relativeArguments.add(relative.toNativePath()); |
| 42 } else { |
| 43 relativeArguments.add(rawArgument); |
| 44 } |
| 45 } |
| 46 return relativeArguments; |
| 47 } |
| 37 | 48 |
| 38 class TestCaseRecorder { | 49 class TestCaseRecorder { |
| 39 Path _outputPath; | 50 Path _outputPath; |
| 40 List<Map> _recordedCommandInvocations = []; | 51 List<Map> _recordedCommandInvocations = []; |
| 41 var _cwd; | 52 var _cwd; |
| 42 | 53 |
| 43 TestCaseRecorder(this._outputPath) { | 54 TestCaseRecorder(this._outputPath) { |
| 44 _cwd = Directory.current.path; | 55 _cwd = Directory.current.path; |
| 45 } | 56 } |
| 46 | 57 |
| 47 void nextTestCase(TestCase testCase) { | 58 void nextCommand(Command command, int timeout) { |
| 48 assert(testCase.commands.length == 1); | 59 // Convert arguments from absolute to relative paths (relative to the dart |
| 49 | 60 // directory) because the absolute path on the machine where we record |
| 50 var command = testCase.commands[0]; | 61 // may be different from the absolute path on the machine where we execute |
| 51 assert(command.environment == null); | 62 // the commands. |
| 52 | 63 var arguments = makePathsRelativeToDart(_cwd, command.arguments); |
| 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 | 64 |
| 63 var commandExecution = { | 65 var commandExecution = { |
| 64 'name' : testCase.displayName, | 66 'name' : command.displayName, |
| 65 'configuration' : testCase.configurationString, | |
| 66 'command' : { | 67 'command' : { |
| 67 'timeout_limit' : testCase.timeout, | 68 'timeout_limit' : timeout, |
| 68 'executable' : command.executable, | 69 'executable' : command.executable, |
| 69 'arguments' : arguments, | 70 'arguments' : arguments, |
| 70 }, | 71 }, |
| 71 }; | 72 }; |
| 72 _recordedCommandInvocations.add(commandExecution); | 73 _recordedCommandInvocations.add(commandExecution); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void finish() { | 76 void finish() { |
| 76 var file = new File.fromPath(_outputPath); | 77 var file = new File.fromPath(_outputPath); |
| 77 var jsonString = json.stringify(_recordedCommandInvocations); | 78 var jsonString = json.stringify(_recordedCommandInvocations); |
| 78 file.writeAsStringSync(jsonString); | 79 file.writeAsStringSync(jsonString); |
| 79 print("TestCaseRecorder: written all TestCases to ${_outputPath}"); | 80 print("TestCaseRecorder: written all TestCases to ${_outputPath}"); |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 class TestCaseOutputArchive { | 84 class TestCaseOutputArchive { |
| 84 Map<String, Map> _testCaseOutputRecords; | 85 Map<String, Map> _commandOutputRecordings; |
| 86 var _cwd; |
| 87 |
| 88 TestCaseOutputArchive() { |
| 89 _cwd = Directory.current.path; |
| 90 } |
| 85 | 91 |
| 86 void loadFromPath(Path recordingPath) { | 92 void loadFromPath(Path recordingPath) { |
| 87 var file = new File.fromPath(recordingPath); | 93 var file = new File.fromPath(recordingPath); |
| 88 var testCases = json.parse(file.readAsStringSync()); | 94 var commandRecordings = json.parse(file.readAsStringSync()); |
| 89 _testCaseOutputRecords = {}; | 95 _commandOutputRecordings = {}; |
| 90 for (var testCase in testCases) { | 96 for (var commandRecording in commandRecordings) { |
| 91 var key = _indexKey(testCase['configuration'], testCase['name']); | 97 var key = _indexKey(commandRecording['command']['executable'], |
| 92 _testCaseOutputRecords[key] = testCase['command_output']; | 98 commandRecording['command']['arguments'].join(' ')); |
| 99 _commandOutputRecordings[key] = commandRecording['command_output']; |
| 93 } | 100 } |
| 94 } | 101 } |
| 95 | 102 |
| 96 CommandOutput outputOf(TestCase testCase) { | 103 CommandOutput outputOf(Command command) { |
| 97 var key = _indexKey(testCase.configurationString, testCase.displayName); | 104 // Convert arguments from absolute to relative paths (relative to the dart |
| 98 var command_output = _testCaseOutputRecords[key]; | 105 // directory) because the absolute path on the machine where we record |
| 106 // may be different from the absolute path on the machine where we execute |
| 107 // the commands. |
| 108 var arguments = makePathsRelativeToDart(_cwd, command.arguments); |
| 109 |
| 110 var key = _indexKey(command.executable, arguments.join(' ')); |
| 111 var command_output = _commandOutputRecordings[key]; |
| 99 if (command_output == null) { | 112 if (command_output == null) { |
| 100 print("Sorry, but there is no command output for " | 113 print("Sorry, but there is no command output for ${command.displayName}" |
| 101 "${testCase.displayName}"); | 114 " ($command)"); |
| 102 | |
| 103 exit(42); | 115 exit(42); |
| 104 } | 116 } |
| 105 | 117 |
| 106 double seconds = command_output['duration']; | 118 double seconds = command_output['duration']; |
| 107 var duration = new Duration(seconds: seconds.round(), | 119 var duration = new Duration(seconds: seconds.round(), |
| 108 milliseconds: (seconds/1000).round()); | 120 milliseconds: (seconds/1000).round()); |
| 109 var commandOutput = new CommandOutput.fromCase( | 121 var commandOutput = createCommandOutput( |
| 110 testCase, | 122 command, |
| 111 testCase.commands.first, | |
| 112 command_output['exit_code'], | 123 command_output['exit_code'], |
| 113 false, | |
| 114 command_output['did_timeout'], | 124 command_output['did_timeout'], |
| 115 encodeUtf8(command_output['stdout']), | 125 encodeUtf8(command_output['stdout']), |
| 116 encodeUtf8(command_output['stderr']), | 126 encodeUtf8(command_output['stderr']), |
| 117 duration, | 127 duration, |
| 118 false); | 128 false); |
| 119 return commandOutput; | 129 return commandOutput; |
| 120 } | 130 } |
| 121 | 131 |
| 122 String _indexKey(String configuration, String name) { | 132 String _indexKey(String executable, String arguments) { |
| 123 return "${configuration}__$name"; | 133 return "${executable}__$arguments"; |
| 124 } | 134 } |
| 125 } | 135 } |
| 126 | 136 |
| OLD | NEW |