Debian Codegraph @

testsuite/systemtap.language_server/stap_language_server_tester.py

raw
1 # Language server tests
2 # Copyright (C) 2022 Red Hat Inc.
3 #
4 # This file is part of systemtap, and is free software. You can
5 # redistribute it and/or modify it under the terms of the GNU General
6 # Public License (GPL); either version 2, or (at your option) any
7 # later version.
8 from jsonrpc import ReadWriter, JSONRPC2Connection
9 import sys
10 import os
11 import argparse
12 import unittest
13 import subprocess
14 try:
15 from unittest import mock
16 except ImportError:
17 import mock
18 from typing import Union
19
20 class Method():
21 # General
22 EXIT = 'exit'
23 INITIALIZE = 'initialize'
24 INITIALIZED = 'initialized'
25 SHUTDOWN = 'shutdown'
26 TEXT_DOCUMENT_DID_CHANGE = 'textDocument/didChange'
27 TEXT_DOCUMENT_DID_CLOSE = 'textDocument/didClose'
28 TEXT_DOCUMENT_DID_OPEN = 'textDocument/didOpen'
29 TEXT_DOCUMENT_DID_SAVE = 'textDocument/didSave'
30 # Language Features
31 COMPLETION = 'textDocument/completion'
32
33
34 class MockClient():
35 def __init__(self):
36 self.server_conn = None
37 verbose = CMD_ARGS.verbose
38
39 args = [CMD_ARGS.stap_path, '--language-server']
40 if verbose > 0:
41 args.extend(['-vvv'])
42 server = subprocess.Popen(
43 args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False)
44 self.server_conn = JSONRPC2Connection(
45 ReadWriter(reader=server.stdout, writer=server.stdin))
46
47 # Setup and open a doccument
48
49 _ = self.server_conn.send_request(
50 method=Method.INITIALIZE,
51 params=dict(
52 rootUri=f'file://{os.getcwd()}',
53 capabilities=dict(),
54 rootPath=os.getcwd(),
55 clientInfo=dict(
56 name='test-language-server-client',
57 processId=os.getpid(),
58 trace='off'
59 )
60 )
61 )
62
63 self.server_conn.send_notification(Method.INITIALIZED, dict())
64
65 self.text_document = dict(
66 uri='file://fake_doc.txt',
67 languageId='systemtap',
68 version=1,
69 text=''
70 )
71
72 self.server_conn.send_notification(
73 method=Method.TEXT_DOCUMENT_DID_OPEN,
74 params=dict(textDocument=self.text_document)
75 )
76
77 def __del__(self):
78 # Close and showdown
79
80 self.server_conn.send_notification(
81 method=Method.TEXT_DOCUMENT_DID_CLOSE,
82 params=dict(textDocument=dict(uri=self.text_document['uri']))
83 )
84
85 self.server_conn.send_notification(Method.SHUTDOWN, dict())
86
87 self.server_conn.send_notification(Method.EXIT, dict())
88
89 def completion_request_full(self, code_snippet, position=None):
90 # If position is not provided, assume we're at the snippet end
91 if not position:
92 lines = code_snippet.split("\n")
93 position = dict(line=len(lines)-1, character=len(lines[-1]))
94
95 self.server_conn.send_notification(
96 method=Method.TEXT_DOCUMENT_DID_CHANGE,
97 params=dict(
98 textDocument=dict(
99 uri=self.text_document['uri'],
100 version=1
101 ),
102 contentChanges=[dict(
103 text=code_snippet,
104 )]
105 )
106 )
107
108 return self.server_conn.send_request(
109 method=Method.COMPLETION,
110 params=dict(
111 textDocument=dict(uri=self.text_document['uri']),
112 position=position
113 )
114 )
115
116 def completion_request_inc(self, changes, position):
117 # The first change must be Full, and the remainder can be either full or incremental
118 assert('range' not in changes[0])
119
120 for v, c in enumerate(changes):
121 self.server_conn.send_notification(
122 method=Method.TEXT_DOCUMENT_DID_CHANGE,
123 params=dict(
124 textDocument=dict(
125 uri=self.text_document['uri'],
126 version=v+1
127 ),
128 contentChanges=[c if isinstance(c, dict) else dict(text=c) ]
129 )
130 )
131
132 # The insertion position should be specified when testing incremental changes
133 assert(position is not None)
134
135 return self.server_conn.send_request(
136 method=Method.COMPLETION,
137 params=dict(
138 textDocument=dict(uri=self.text_document['uri']),
139 position=position
140 )
141 )
142
143 class ServerCompletionTests(unittest.TestCase):
144 def __init__(self, *args, **kwargs) -> None:
145 super().__init__(*args, **kwargs)
146 self.client = MockClient() # Each testcase gets its own mockclient
147
148 def labels_match(self, change: Union[str, list], expected, position=None, exactMatch=True, type=None, sync_kind = "full"):
149 comp_func = MockClient.completion_request_inc if sync_kind != "full" else MockClient.completion_request_full
150
151 completion_list = comp_func(self.client,
152 change, position).get("result", dict())
153
154 completion_items = completion_list.get("items", [])
155
156 # Get the labels (the default insertion text) of type detail (if a type is provided, else everything)
157 actual = [ci["label"]
158 for ci in completion_items if not type or ci["detail"] == type]
159
160 # Expected is a subset of actual
161 subset_match = all(e in actual for e in expected)
162 # We may be ok with just having a subset match, or we might need an exact match
163 length_match = not exactMatch or len(actual) == len(expected)
164
165 self.assertTrue(subset_match and length_match,
166 f'\nCompleting "{change}"\nGot: {actual}\nExpected{"" if exactMatch else " Superset Of " }: {expected}')
167
168 def test_basic(self):
169 # General completions for an unknown context
170 self.labels_match('pr', ['probe', 'private'])
171 self.labels_match('func', ['function'])
172 self.labels_match('g', ['global'])
173 # the private prefix just skips private and completes like normal
174 self.labels_match('private pro', ['probe'])
175 # Some text/whitespace before the completion doesn't change anything
176 self.labels_match('\t\n\n p', ['probe', 'private'])
177 self.labels_match(
178 '%%edit foobar\t \n\n function foo ()\n{}\n globa', ['global'])
179
180 def test_probe_comps(self):
181 self.labels_match('probe', [])
182 # Uses match nodes correctly
183 self.labels_match('probe one', ['oneshot'])
184 self.labels_match('probe proces', ['process', 'process(', 'process("'])
185 # Completing a second token
186 self.labels_match('probe process', [
187 'process', 'process(', 'process("'])
188 self.labels_match('probe process(12345678).en', ['end', 'end('])
189 # Just look for a subset of expected in actual
190 self.labels_match('probe process.', ['begin'], exactMatch=False)
191 # Doesn't match anything
192 self.labels_match('probe twosh', [])
193
194 def test_string(self):
195 file_path = os.getenv("TEST_C_FILE")
196
197 process_prefix = 'probe process("'
198 # Absolute path completion
199 self.labels_match(process_prefix + '/', ['/root'], exactMatch=False)
200 self.labels_match(process_prefix + '/li', ['/lib', '/lib64'])
201 # Relative path completion
202 self.labels_match(process_prefix + 'foo/', ['bar'])
203 self.labels_match(process_prefix + 'foo/bar/', ['baz', 'baz.c'])
204
205 # Check the text to be inserted as well, not just the label (textEdit has the same content as insertText so it's covered by this test as well)
206 insert_text_test_code = process_prefix + 'foo/ba'
207 actual = [(ci["label"], 'foo/ba'+ci["insertText"]) for ci in self.client.completion_request_full(
208 insert_text_test_code).get("result", dict(items=[]))["items"]]
209 expected = [('bar', 'foo/bar')] # = (label, prefix + insertion text)
210 self.assertTrue(all(e in actual for e in expected) and len(actual) == len(expected),
211 f'\nCompleting "{insert_text_test_code}"\nGot: {actual}\nExpected: {expected}')
212 # TODO: Test with globs once they're supported
213
214 # Test function/statement completion using absolute path
215 self.labels_match(f'probe process("{os.getcwd().replace("build", "systemtap")}/systemtap.language_server/foo/bar/baz").function("s',
216 [f'"spam@{file_path}:1"',
217 f'"space@{file_path}:5"'])
218
219 def test_body(self):
220 prefix = "probe oneshot, foo, bar, baz { \n"
221 # Body completion does not determine the structure, it will just return the available key/atwords/macros/functions
222 # first line testing
223 self.labels_match(prefix + 'fo', ['for', 'foreach'], type="keyword")
224 self.labels_match(
225 prefix + '@pr', ['probewrite', 'prometheus_dump_array1'], exactMatch=False)
226 self.labels_match(
227 prefix + 'ti', ['tid', 'timer_pending'], type="function")
228
229 # Complex line testing
230 line = "\t\tif ( cpu() == 0 && getti<CURSOR> > 1140498000)"
231 pos = dict(line=1, character=line.find("<CURSOR>"))
232 self.labels_match(prefix + line,
233 ['gettimeofday_ms', 'gettimeofday_ns', 'gettimeofday_s', 'gettimeofday_us'], position=pos)
234
235 line = "foreach ([a,b] in foo+ lim"
236 self.labels_match(prefix + line, ['limit'])
237
238 # multiline testing
239 lines = """
240 function foo (a, b, c)
241 {
242 if (a < 1) return 0
243 else if (b < 2) return 1
244 else return modu
245 """
246 self.labels_match(lines, ['module_name', 'module_size'])
247
248 def test_multiple_contexts(self):
249 lines = """
250 global baz = 42
251
252 probe syscallgroup.io = syscall.open,
253 syscall.read { groupname = "io" }
254 function f(x) { printf("%s\n", x) }
255 probe end
256 {
257 f("hello world");
258 foreach (eg+ in groups)
259 @coun
260 """
261 self.labels_match(lines, ['count'])
262
263 def test_no_completions(self):
264 # Global defintions, function signatures and embedded C have no completions
265 # TODO: Complete C
266 self.labels_match("global x = ", [])
267 self.labels_match("function foo (a, ", [])
268 self.labels_match("probe", [])
269 self.labels_match("function foo () %{ struc ", [])
270
271 def test_local_definitions(self):
272 code = """
273 global afoo = 42
274
275 function abar(x) { printf("%s", x) }
276 probe end
277 {
278 f("hello world");
279 foreach (eg+ in groups)
280 a<CURSOR>
281 }
282 global baz = -1
283
284 global abaz = "hello"
285 """
286 lines = code.split('\n')
287 cursor_line = [idx for idx, l in enumerate(lines) if '<CURSOR>' in l][0]
288 pos = dict(line=cursor_line, character=lines[cursor_line].find("<CURSOR>"))
289 self.labels_match(code, ['afoo', 'abar', 'abaz'], pos, False)
290
291 def test_target_vars(self):
292 code = 'function vars() {}\nprobe kernel.function("vfs_read") { $'
293 self.labels_match(code, ['$file','$buf','$count','$pos', '$$vars', '$$locals', "$$parms"])
294
295 def test_incremental_changes(self):
296 def make_position(ln, chr):
297 return dict(line=ln, character=chr)
298 def make_change(text, range_start, range_end = None):
299 if range_end is None: range_end = range_start
300 return dict(
301 text = text,
302 range = dict(start = range_start, end = range_end)
303 )
304
305 # The first change is a full, and the remainder are incremental
306 self.labels_match( [ 'pr', make_change('o', make_position(0,2)) ] , ['probe'], make_position(0, 3), True, None, "incremental")
307
308 # Prepend a new line and then complete the new line under it (between 2 lines)
309 """
310 global bar = 10
311 function f() { ba<CURSOR>
312 probe oneshot {}
313 """
314 self.labels_match(
315 [ 'probe oneshot {}',
316 make_change('global bar = 10\n', make_position(0,0)),
317 make_change('function f() { ba\n', make_position(1,0))
318 ],
319 ['bar'], make_position(1, 16), False, None, "incremental")
320
321 # Append a new line and then complete the new line before it (between 2 lines)
322 """
323 probe oneshot {}
324 function f() { ba<CURSOR>
325 global bar = 10
326 """
327 self.labels_match(
328 [ 'probe oneshot {}',
329 make_change('\nglobal bar = 10', make_position(1,0)),
330 make_change('function f() { ba\n', make_position(1,0))
331 ],
332 ['backtrace', 'bdevname', 'big_endian2', 'big_endian4', 'big_endian8',
333 'bio_op', 'bio_rw_num', 'bio_rw_str', 'bytes_to_string', 'break'],
334 make_position(1, 16), True, None, "incremental") # Don't match bar since its now inside the function body so its not a global
335
336 # Modify the first and last lines and complete between them
337 """
338 global bar
339
340 function f() { ba<CURSOR>}
341 global baz
342 """
343 self.labels_match(
344 [ 'global ham\nfunction f() { ba}\nglobal spam = 42',
345 make_change('bar\n', make_position(0, 7), make_position(0, 10)), # replace ham with bar\n
346 make_change('baz', make_position(3, 7), make_position(3, 11)), # replace spam with baz
347 ],
348 ['bar', 'baz'], make_position(2, 16), False, None, "incremental")
349
350 # Modify the first and last characters and complete between
351 """
352 global bar
353 function f() { ba<CURSOR> }
354 global baz
355 """
356 self.labels_match(
357 [ 'flobal bar\nfunction f() { ba }\nglobal bam',
358 make_change('g', make_position(0, 0), make_position(0, 1)), # replace f with g
359 make_change('z', make_position(2, 9), make_position(2, 10)), # replace m with z
360 ],
361 ['bar', 'baz'], make_position(1, 16), False, None, "incremental")
362
363 # Remove lines and replace with new lines
364 """
365 probe one<CURSOR>
366 """
367 self.labels_match(
368 [ 'global bar\nfunction foo() { return 10 }\nglobal baz',
369 make_change('', make_position(0, 0), make_position(3, 11)), # remove first 3 lines
370 make_change('probe one', make_position(0, 0)), # insert into start of doc
371 ],
372 ['oneshot'], make_position(0, 9), False, None, "incremental")
373
374 class ServerIntegrationTests(unittest.TestCase):
375 def __init__(self, *args, **kwargs) -> None:
376 super().__init__(*args, **kwargs)
377 self.verbose = CMD_ARGS.verbose
378 self.server_conn = None
379 self.maxDiff = None # When asserting, show the full diff of actual and expected
380
381 def _test_request(self, request_method: Method, request_params: dict, expected_result: dict):
382 response = self.server_conn.send_request(
383 request_method, request_params)
384 self.assertDictEqual(response.get("result", {}), expected_result)
385
386 def _send_notification(self, notification_method: Method, notification_params: dict = dict()):
387 self.server_conn.send_notification(
388 notification_method, notification_params)
389
390 def test_basic(self):
391 args = [CMD_ARGS.stap_path, '--language-server']
392 if self.verbose > 0:
393 args.extend(['-'+'v'*self.verbose])
394 server = subprocess.Popen(
395 args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False)
396 self.server_conn = JSONRPC2Connection(
397 ReadWriter(reader=server.stdout, writer=server.stdin))
398
399 # Request that the server initialize
400 self._test_request(
401 request_method=Method.INITIALIZE,
402 request_params=dict(
403 rootUri=f'file://{os.getcwd()}',
404 capabilities=dict(),
405 rootPath=os.getcwd(),
406 clientInfo=dict(
407 name='test-language-server-client',
408 processId=os.getpid(),
409 trace='off'
410 )
411 ),
412 expected_result=dict(
413 capabilities=dict(
414 textDocumentSync=2, # Inc Sync
415 completionProvider=dict(
416 resolveProvider=False,
417 triggerCharacters=['.', '/', '@', "$", "*"]
418 )
419 ),
420 serverInfo=dict(
421 name="systemtap-language-server",
422 version="1.0"
423 )
424 )
425 )
426
427 # Send the server a notification that the initialization was successful.
428 # NOTE: Notifications don't have results, we just check their side effects to ensure
429 # correctness
430 self._send_notification(notification_method=Method.INITIALIZED)
431
432 textDocument = dict(
433 uri='file://fake_doc.txt',
434 languageId='systemtap',
435 version=1,
436 text='pr'
437 )
438
439 self._send_notification(
440 notification_method=Method.TEXT_DOCUMENT_DID_OPEN,
441 notification_params=dict(
442 textDocument=textDocument
443 )
444 )
445
446 # By changing the textDocument's text from pr to pro the completion request verifies
447 # the change was correct since only 'probe' should be returned, not 'private' as well
448 self._send_notification(
449 notification_method=Method.TEXT_DOCUMENT_DID_CHANGE,
450 notification_params=dict(
451 textDocument=dict(
452 uri=textDocument['uri'],
453 version=textDocument['version']+1
454 ),
455 contentChanges=[dict(
456 text=textDocument['text'] + 'o',
457 )]
458 )
459 )
460
461 # Make sure the unknown methods don't cause issues
462 response = self.server_conn.send_request(
463 "foobar", dict())
464 MethodNotFoundError = -32601
465 self.assertEqual(response.get("error", {}).get(
466 "code", -1), MethodNotFoundError)
467
468 self._test_request(
469 request_method=Method.COMPLETION,
470 request_params=dict(
471 textDocument=dict(
472 uri=textDocument['uri']
473 ),
474 position=dict(
475 character=3,
476 line=0
477 )
478 ),
479 expected_result=dict(
480 isIncomplete=False,
481 items=[dict(
482 detail='keyword',
483 label='probe'
484 )]
485 )
486 )
487
488 self._send_notification(
489 notification_method=Method.TEXT_DOCUMENT_DID_CLOSE,
490 notification_params=dict(
491 textDocument=dict(uri=textDocument['uri'])
492 )
493 )
494
495 # Technically this should be a request, but its not an issue since we don't need a response here
496 self._send_notification(notification_method=Method.SHUTDOWN)
497
498 # Make sure the shutdown worked, since now only exit notifications should
499 # be accepted and all others should error
500 response = self.server_conn.send_request(
501 Method.COMPLETION, dict())
502 InvalidRequestError = -32600
503 self.assertEqual(response.get("error", {}).get(
504 "code", -1), InvalidRequestError)
505
506 self._send_notification(Method.EXIT)
507
508 ret_code = server.wait(timeout=10)
509 self.assertEqual(ret_code, 0)
510
511
512 def test_suite(test_completion, test_integration):
513 suite = unittest.TestSuite()
514 if test_completion:
515 suite.addTests([
516 ServerCompletionTests('test_basic'),
517 ServerCompletionTests('test_probe_comps'),
518 ServerCompletionTests('test_string'),
519 ServerCompletionTests('test_body'),
520 ServerCompletionTests('test_multiple_contexts'),
521 ServerCompletionTests('test_no_completions'),
522 ServerCompletionTests('test_incremental_changes'),
523 ServerCompletionTests('test_local_definitions'),
524 ServerCompletionTests("test_target_vars")
525 ])
526
527 if test_integration:
528 suite.addTest(ServerIntegrationTests('test_basic'))
529
530 return suite
531
532
533 if __name__ == '__main__':
534 parser = argparse.ArgumentParser()
535 parser.description = "Systemtap language server tests"
536 parser.add_argument(
537 "-v",
538 "--verbose",
539 help="increase verbosity of test",
540 action="count",
541 default=0,
542 )
543 parser.add_argument(
544 "--stap-path",
545 help="the path to the stap executable",
546 type=str,
547 )
548
549 global CMD_ARGS
550 CMD_ARGS = parser.parse_args()
551
552 runner = unittest.TextTestRunner(failfast=True)
553 ENV = dict(
554 PATH=os.getenv("TEST_PATH"),
555 TEST_C_FILE=os.getenv("TEST_C_FILE")
556 )
557
558 with mock.patch.dict(os.environ, ENV):
559 res = runner.run(test_suite(
560 test_completion=True, test_integration=True))
561 sys.exit(0 if res.wasSuccessful() else 1)