forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test_runner.py
executable file
·279 lines (245 loc) · 7.83 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
#!/usr/bin/python
import json
import os
import importlib
import re
import shutil
import subprocess
CQUERY_PATH = 'build/release/bin/cquery'
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': [
'-xc++',
'-std=c++11'
]
}
})
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 pause processing messages until it 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': False
}
}
}
})
return self
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.
actual = GetMessages(stdout.decode('utf8'))
success = exit_code == 0 and actual == test_builder.expected
# 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()