ccls/test_runner_e2e.py

306 lines
8.1 KiB
Python
Raw Normal View History

2017-12-28 16:55:46 +00:00
#!/usr/bin/python
2017-05-03 06:45:10 +00:00
import json
2017-09-13 03:35:27 +00:00
import re
import shlex
2017-12-28 16:55:46 +00:00
import shutil
from subprocess import Popen, PIPE
2017-09-13 03:35:27 +00:00
2017-12-28 16:55:46 +00:00
# FIXME: instead of $cquery/exitWhenIdle, just send $cquery/wait and the normal
# lsp exit. This requires renaming $cquery/queryDbWaitForIdle to just
# $cquery/wait.
CQUERY_PATH = 'build/asan/bin/cquery'
2017-09-13 03:35:27 +00:00
CACHE_DIR = 'e2e_CACHE'
2017-12-28 16:55:46 +00:00
# Content-Length: ...\r\n
# \r\n
# {
# "jsonrpc": "2.0",
# "id": 1,
# "method": "textDocument/didOpen",
# "params": {
# ...
# }
# }
2017-05-03 06:45:10 +00:00
# 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_`.
2017-12-28 16:55:46 +00:00
class TestBuilder:
2017-05-03 06:45:10 +00:00
def __init__(self):
self.sent = []
2017-09-13 03:35:27 +00:00
self.expected = []
2017-05-03 06:45:10 +00:00
2017-09-13 03:35:27 +00:00
def IndexFile(self, path, contents):
"""
2017-12-28 16:55:46 +00:00
Indexes the given file with contents.
"""
2017-09-13 03:35:27 +00:00
self.Send({
2017-12-28 16:55:46 +00:00
'method': '$cquery/indexFile',
'params': {
'path': path,
'contents': contents,
'args': [
'-xc++',
'-std=c++11'
]
}
2017-09-13 03:35:27 +00:00
})
return self
def WaitForIdle(self):
"""
2017-12-28 16:55:46 +00:00
cquery will pause processing messages until it is idle.
2017-09-13 03:35:27 +00:00
"""
2017-12-28 16:55:46 +00:00
self.Send({'method': '$cquery/wait'})
2017-05-03 06:45:10 +00:00
return self
def Send(self, stdin):
"""
Send the given message to the language server.
"""
2017-05-03 06:45:10 +00:00
stdin['jsonrpc'] = '2.0'
self.sent.append(stdin)
return self
2017-09-13 03:35:27 +00:00
def Expect(self, expected):
"""
Expect a message from the language server.
"""
2017-09-13 03:35:27 +00:00
expected['jsonrpc'] = '2.0'
self.expected.append(expected)
2017-05-03 06:45:10 +00:00
return self
2017-05-03 06:45:10 +00:00
def SetupCommonInit(self):
"""
Add initialize/initialized messages.
"""
2017-05-03 06:45:10 +00:00
self.Send({
2017-12-28 16:55:46 +00:00
'id': 0,
'method': 'initialize',
'params': {
'processId': 123,
'rootUri': 'cquery',
'capabilities': {},
'trace': 'off',
'initializationOptions': {
'cacheDirectory': CACHE_DIR
}
2017-09-13 03:35:27 +00:00
}
2017-05-03 06:45:10 +00:00
})
self.Expect({
2017-12-28 16:55:46 +00:00
'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
}
}
2017-09-13 03:35:27 +00:00
}
2017-05-03 06:45:10 +00:00
})
return self
2017-12-28 16:55:46 +00:00
2017-05-03 06:45:10 +00:00
def _ExecuteTest(name, func):
"""
Executes a specific test.
|func| must return a TestBuilder object.
"""
2017-12-28 16:55:46 +00:00
# Delete cache directory.
shutil.rmtree(CACHE_DIR, ignore_errors=True)
2017-05-03 06:45:10 +00:00
test_builder = func()
if not isinstance(test_builder, TestBuilder):
raise Exception('%s does not return a TestBuilder instance' % name)
2017-09-13 03:35:27 +00:00
# Add a final exit message.
2017-12-28 16:55:46 +00:00
test_builder.Send({'method': '$cquery/wait'})
test_builder.Send({'method': 'exit'})
2017-05-03 06:45:10 +00:00
2017-09-13 03:35:27 +00:00
# Convert messages to a stdin byte array.
2017-05-03 06:45:10 +00:00
stdin = ''
for message in test_builder.sent:
payload = json.dumps(message)
wrapped = 'Content-Length: %s\r\n\r\n%s' % (len(payload), payload)
stdin += wrapped
2017-09-13 03:35:27 +00:00
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]
2017-12-28 16:55:46 +00:00
decoded = json.loads(message)
# Do not report '$cquery/progress' messages.
if 'method' in decoded and decoded['method'] == '$cquery/progress':
continue
# Do not report 'textDocument/publishDiagnostic' messages.
if 'method' in decoded and decoded['method'] == 'textDocument/publishDiagnostics':
continue
messages.append(decoded)
2017-09-13 03:35:27 +00:00
return messages
# Utility method to print a byte array.
def PrintByteArray(bytes):
for line in bytes.split(b'\r\n'):
print(line.decode('utf8'))
2017-05-03 06:45:10 +00:00
2017-09-13 03:35:27 +00:00
# Execute program.
2017-12-28 16:55:46 +00:00
cmd = "%s --language-server --log-all-to-stderr" % CQUERY_PATH
2017-09-13 03:35:27 +00:00
process = Popen(shlex.split(cmd), stdin=PIPE, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = process.communicate(stdin_bytes)
2017-12-28 16:55:46 +00:00
exit_code = process.wait()
2017-05-03 06:45:10 +00:00
2017-09-13 03:35:27 +00:00
# Check if test succeeded.
actual = GetMessages(stdout.decode('utf8'))
success = 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]))
2017-05-03 06:45:10 +00:00
2017-05-03 06:45:10 +00:00
def _DiscoverTests():
"""
Discover and return all tests.
"""
for name, value in globals().items():
if not callable(value):
continue
if not name.startswith('Test_'):
continue
yield (name, value)
2017-12-28 16:55:46 +00:00
2017-05-03 06:45:10 +00:00
def _RunTests():
"""
Executes all tests.
"""
for name, func in _DiscoverTests():
_ExecuteTest(name, func)
2017-09-13 03:35:27 +00:00
#### EXAMPLE TESTS ####
2017-05-03 06:45:10 +00:00
class lsSymbolKind:
Function = 1
2017-12-28 16:55:46 +00:00
2017-05-03 06:45:10 +00:00
def lsSymbolInfo(name, position, kind):
return {
2017-12-28 16:55:46 +00:00
'name': name,
'position': position,
'kind': kind
2017-05-03 06:45:10 +00:00
}
2017-12-28 16:55:46 +00:00
2017-09-13 03:35:27 +00:00
def DISABLED_Test_Init():
2017-05-03 06:45:10 +00:00
return (TestBuilder()
2017-12-28 16:55:46 +00:00
.SetupCommonInit()
)
2017-05-03 06:45:10 +00:00
2017-09-13 03:35:27 +00:00
def Test_Outline():
2017-05-03 06:45:10 +00:00
return (TestBuilder()
2017-12-28 16:55:46 +00:00
.SetupCommonInit()
.IndexFile("foo.cc",
"""void foobar();""")
.WaitForIdle()
.Send({
'id': 1,
'method': 'textDocument/documentSymbol',
'params': {
'textDocument': {
'uri': 'foo.cc'
}
}
})
.Expect({
'jsonrpc': '2.0',
'id': 1, 'result': [
{
'containerName': 'void foobar()',
'kind': 12,
'name': 'foobar',
'location': {
'range': {
'start': {
'line': 0,
'character': 5},
'end': {
'line': 0,
'character': 11
}
},
'uri': 'file://foo.cc'
}
}]}))
2017-05-03 06:45:10 +00:00
if __name__ == '__main__':
2017-09-13 03:35:27 +00:00
_RunTests()