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