forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test_runner.py
executable file
·364 lines (320 loc) · 11.1 KB
/
e2e_test_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/python
import json
import os
import importlib
import re
import shutil
import subprocess
#CQUERY_PATH = 'build/release/bin/cquery'
CQUERY_PATH = 'x64/Debug/cquery.exe'
CACHE_DIR = 'e2e_cache'
# We write test files in python. The test runner collects all python files in
# the directory and executes them. The test function just creates a test object
# which specifies expected stdin/stdout.
#
# Test functions are automatically discovered; they just need to be in the
# global environment and start with `Test_`.
# If found in json output with quotes surrounding this value it will be replaced
# with null, ie, "~~null~~" -> null
NULL_MAGIC_VALUE = "~~null~~"
class TestBuilder:
def __init__(self):
self.sent = []
self.expected = []
self.documents = {}
def IndexFile(self, path, contents):
"""
Indexes the given file with contents.
"""
self.documents[path] = contents
self.Send({
'method': '$cquery/indexFile',
'params': {
'path': path,
'contents': contents,
'args': [
'clang',
'-xc++',
'-std=c++11',
path
]
}
})
return self
def SendDidOpen(self, path):
self.Send({
'method': 'textDocument/didOpen',
'params': {
'textDocument': {
'uri': path,
'languageId': 'cpp',
'version': 0,
'text': self.documents[path]
},
}
})
return self
def WaitForIdle(self):
"""
cquery will wait until the import pipeline is idle
"""
self.Send({'method': '$cquery/wait'})
return self
def Send(self, stdin):
"""
Send the given message to the language server.
"""
if not isinstance(stdin, str):
stdin['jsonrpc'] = '2.0'
self.sent.append(stdin)
return self
def Expect(self, expected):
"""
Expect a message from the language server.
"""
expected['jsonrpc'] = '2.0'
self.expected.append(expected)
return self
def SetupCommonInit(self):
"""
Add initialize/initialized messages.
"""
self.Send({
'id': 0,
'method': 'initialize',
'params': {
'processId': 123,
'rootUri': 'cquery',
'capabilities': {
'textDocument': {
'codeLens': NULL_MAGIC_VALUE
}
},
'trace': 'off',
'initializationOptions': {
'cacheDirectory': CACHE_DIR
}
}
})
self.Expect({
'id': 0,
'result': {
'capabilities': {
'textDocumentSync': 2,
'hoverProvider': True,
'completionProvider': {
'resolveProvider': False,
'triggerCharacters': ['.', ':', '>', '#', '<', '"', '/']
},
'signatureHelpProvider': {
'triggerCharacters': ['(', ',']
},
'definitionProvider': True,
'referencesProvider': True,
'documentHighlightProvider': True,
'documentSymbolProvider': True,
'workspaceSymbolProvider': True,
'codeActionProvider': True,
'codeLensProvider': {
'resolveProvider': False
},
'documentFormattingProvider': False,
'documentRangeFormattingProvider': False,
'renameProvider': True,
'documentLinkProvider': {
'resolveProvider': True
},
'executeCommandProvider': {
'commands': []
}
}
}
})
return self
class Diff(object):
def __init__(self, first, second, with_values=True, vice_versa=False):
self.difference = []
self.check(first, second, with_values=with_values)
if vice_versa:
self.check(second, first, with_values=with_values)
def check(self, first, second, path='', with_values=False):
if second != None:
if not isinstance(first, type(second)):
message = '%s- %s, %s' % (path, type(first), type(second))
self.save_diff(message, TYPE)
if isinstance(first, dict):
for key in first:
# the first part of path must not have trailing dot.
if len(path) == 0:
new_path = key
else:
new_path = "%s.%s" % (path, key)
if isinstance(second, dict):
if key in second:
sec = second[key]
else:
# there are key in the first, that is not presented in the second
self.save_diff(new_path, path)
# prevent further values checking.
sec = None
# recursive call
self.check(first[key], sec, path=new_path, with_values=with_values)
else:
# second is not dict. every key from first goes to the difference
self.save_diff(new_path, 'path')
self.check(first[key], second, path=new_path, with_values=with_values)
# if object is list, loop over it and check.
elif isinstance(first, list):
for (index, item) in enumerate(first):
new_path = "%s[%s]" % (path, index)
# try to get the same index from second
sec = None
if second != None:
try:
sec = second[index]
except (IndexError, KeyError):
# goes to difference
self.save_diff('%s - %s, %s' % (new_path, type(first), type(second)), 'type')
# recursive call
self.check(first[index], sec, path=new_path, with_values=with_values)
# not list, not dict. check for equality (only if with_values is True) and return.
else:
if with_values and second != None:
if first != second:
self.save_diff('%s - %s | %s' % (path, first, second), 'VALUE')
return
def save_diff(self, diff_message, type_):
message = '%s: %s' % (type_, diff_message)
if diff_message not in self.difference:
self.difference.append(message)
def _ExecuteTest(name, func):
"""
Executes a specific test.
|func| must return a TestBuilder object.
"""
# Delete cache directory.
shutil.rmtree(CACHE_DIR, ignore_errors=True)
test_builder = func()
# if not isinstance(test_builder, TestBuilder):
if not test_builder.__class__.__name__ == 'TestBuilder':
raise Exception('%s does not return a TestBuilder instance' % name)
# Add a final exit message.
test_builder.WaitForIdle()
test_builder.Send({'method': 'exit'})
# Convert messages to a stdin byte array.
stdin = ''
for message in test_builder.sent:
payload = message
if not isinstance(payload, str):
payload = json.dumps(message)
payload = payload.replace('"' + NULL_MAGIC_VALUE + '"', 'null')
wrapped = 'Content-Length: %s\r\n\r\n%s' % (len(payload), payload)
stdin += wrapped
stdin_bytes = stdin.encode(encoding='UTF-8')
# Finds all messages in |string| by parsing Content-Length headers.
def GetMessages(string):
messages = []
for match in re.finditer('Content-Length: (\d+)\r\n\r\n', string):
start = match.span()[1]
length = int(match.groups()[0])
message = string[start:start + length]
decoded = json.loads(message)
# Do not report '$cquery/progress' messages.
if 'method' in decoded and decoded['method'] == '$cquery/progress':
continue
# Do not report '$cquery/setInactiveRegions' messages.
if 'method' in decoded and decoded['method'] == '$cquery/setInactiveRegions':
continue
# Do not report 'textDocument/publishDiagnostic' messages.
if 'method' in decoded and decoded['method'] == 'textDocument/publishDiagnostics':
continue
messages.append(decoded)
return messages
# Utility method to print a byte array.
def PrintByteArray(bytes):
for line in bytes.split(b'\r\n'):
print(line.decode('utf8'))
# Execute program.
cmd = [CQUERY_PATH, '--language-server', '--log-all-to-stderr']
process = subprocess.Popen(
cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = process.communicate(stdin_bytes)
exit_code = process.wait()
# Check if test succeeded.
success = exit_code == 0
actual = GetMessages(stdout.decode('utf8'))
expected = test_builder.expected
common_end = min(len(test_builder.expected), len(actual))
for i in range(0, common_end):
diff = Diff(actual[i], expected[i])
if len(diff.difference) != 0:
print('############')
print('Actual')
print(actual[i])
print('Expected')
print(expected[i])
success = False
for d in diff.difference:
print(d)
# Print failure messages.
if success:
print('== Passed %s with exit_code=%s ==' % (name, exit_code))
else:
print('== FAILED %s with exit_code=%s ==' % (name, exit_code))
print('## STDIN:')
for message in GetMessages(stdin):
print(json.dumps(message, indent=True))
if stdout:
print('## STDOUT:')
for message in GetMessages(stdout.decode('utf8')):
print(json.dumps(message, indent=True))
if stderr:
print('## STDERR:')
PrintByteArray(stderr)
print('## Expected output')
for message in test_builder.expected:
print(message)
print('## Actual output')
for message in actual:
print(message)
print('## Difference')
common_end = min(len(test_builder.expected), len(actual))
for i in range(0, common_end):
if test_builder.expected[i] != actual[i]:
print('i=%s' % i)
print('- Expected %s' % str(test_builder.expected[i]))
print('- Actual %s' % str(actual[i]))
for i in range(common_end, len(test_builder.expected)):
print('Extra expected: %s' % str(test_builder.expected[i]))
for i in range(common_end, len(actual)):
print('Extra actual: %s' % str(actual[i]))
def _LoadAllModulesFromDir(dirname):
# https://stackoverflow.com/a/1057765
result = []
for item in os.listdir(dirname):
if item == '__init__.py' or item[-3:] != '.py':
continue
module_path = dirname + '.' + item[:-3]
print('Importing ' + module_path)
module = importlib.import_module(module_path)
result.append(module)
return result
def _DiscoverTests():
"""
Discover and return all tests.
"""
for module in _LoadAllModulesFromDir('e2e_tests'):
for name, value in module.__dict__.items():
if not callable(value):
continue
if not name.startswith('Test_'):
continue
yield (name, value)
def _RunTests():
"""
Executes all tests.
"""
for name, func in _DiscoverTests():
_ExecuteTest(name, func)
if __name__ == '__main__':
_RunTests()