summaryrefslogtreecommitdiff
path: root/test/lsp/test_lsp_startup.vader
blob: 028ec9b159c4d3e1bbb0e5756634c563edb2cd97 (plain)
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
365
366
Before:
  Save g:ale_run_synchronously

  let g:ale_run_synchronously = 1
  unlet! g:ale_run_synchronously_callbacks
  unlet! g:ale_run_synchronously_emulate_commands

  runtime autoload/ale/lsp.vim
  runtime autoload/ale/lsp_linter.vim
  runtime autoload/ale/engine.vim
  runtime autoload/ale/job.vim
  runtime autoload/ale/socket.vim

  let g:job_map = {}
  let g:emulate_job_failure = 0
  let g:next_job_id = 1

  let g:socket_map = {}
  let g:emulate_socket_failure = 0
  let g:next_channel_id = 0

  let g:message_buffer = ''
  let g:calls = []

  function! ale#engine#IsExecutable(buffer, executable) abort
    return !empty(a:executable)
  endfunction

  function! ale#job#HasOpenChannel(job_id) abort
    return has_key(g:job_map, a:job_id)
  endfunction

  function! ale#job#Stop(job_id) abort
    if has_key(g:job_map, a:job_id)
      call remove(g:job_map, a:job_id)
    endif
  endfunction

  function! ale#job#Start(command, options) abort
    if g:emulate_job_failure
      return 0
    endif

    let l:job_id = g:next_job_id
    let g:next_job_id += 1
    let g:job_map[l:job_id] = [a:command, a:options]

    return l:job_id
  endfunction

  function! ale#job#SendRaw(job_id, data) abort
    let g:message_buffer .= a:data
  endfunction

  function! ale#socket#IsOpen(channel_id) abort
    return has_key(g:socket_map, a:channel_id)
  endfunction

  function! ale#socket#Close(channel_id) abort
    if has_key(g:socket_map, a:channel_id)
      call remove(g:socket_map, a:channel_id)
    endif
  endfunction

  function! ale#socket#Open(address, options) abort
    if g:emulate_socket_failure
      return -1
    endif

    let l:channel_id = g:next_channel_id
    let g:next_channel_id += 1
    let g:socket_map[l:channel_id] = [a:address, a:options]

    return l:channel_id
  endfunction

  function! ale#socket#Send(channel_id, data) abort
    let g:message_buffer .= a:data
  endfunction

  function! PopMessages() abort
    let l:message_list = []

    for l:line in split(g:message_buffer, '\(\r\|\n\|Content-Length\)\+')
      if l:line[:0] is '{'
        let l:data = json_decode(l:line)

        call add(l:message_list, l:data)
      endif
    endfor

    let g:message_buffer = ''

    return l:message_list
  endfunction

  function! SendMessage(message) abort
    let l:conn_id = keys(ale#lsp#GetConnections())[0]
    let l:body = json_encode(a:message)
    let l:data = 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body

    call ale#lsp#HandleMessage(l:conn_id, l:data)
  endfunction

  function! Start() abort
    let l:linter = values(ale#linter#GetLintersLoaded())[0][0]

    return ale#lsp_linter#StartLSP(
    \ bufnr(''),
    \ l:linter,
    \ {linter, details -> add(g:calls, [linter.name, details])},
    \)
  endfunction

  function! AssertInitSuccess(linter_name, conn_prefix, language, root, command) abort
    let l:messages = PopMessages()

    if a:linter_name is# 'tsserver'
      AssertEqual
      \ [
      \   {
      \     'seq': v:null,
      \     'arguments': {
      \       'file': expand('%:p'),
      \     },
      \     'type': 'request',
      \     'command': 'open',
      \   },
      \ ],
      \ l:messages
    else
      AssertEqual
      \ [
      \   {
      \     'method': 'initialize',
      \     'jsonrpc': '2.0',
      \     'id': 1,
      \     'params': {
      \       'initializationOptions': {},
      \       'rootUri': ale#path#ToURI(a:root),
      \       'capabilities': {},
      \       'rootPath': a:root,
      \       'processId': getpid(),
      \     },
      \   },
      \ ],
      \ l:messages

      call SendMessage({
      \ 'jsonrpc': '2.0',
      \ 'id': 1,
      \ 'result': {
      \   'capabilities': {
      \     'renameProvider': v:true,
      \     'executeCommandProvider': {
      \       'commands': [],
      \     },
      \     'hoverProvider': v:true,
      \     'documentSymbolProvider': v:true,
      \     'documentRangeFormattingProvider': v:true,
      \     'codeLensProvider': {
      \       'resolveProvider': v:false
      \     },
      \     'referencesProvider': v:true,
      \     'textDocumentSync': 2,
      \     'documentFormattingProvider': v:true,
      \     'codeActionProvider': v:true,
      \     'signatureHelpProvider': {
      \       'triggerCharacters': ['(', ','],
      \     },
      \     'completionProvider': {
      \       'triggerCharacters': ['.'],
      \       'resolveProvider': v:false
      \     },
      \     'definitionProvider': v:true,
      \     'experimental': {},
      \     'documentHighlightProvider': v:true,
      \     'workspaceSymbolProvider': v:true,
      \   },
      \ },
      \})

      let l:messages = PopMessages()

      AssertEqual
      \ [
      \   {
      \     'method': 'initialized',
      \     'jsonrpc': '2.0',
      \     'params': {},
      \   },
      \   {
      \     'method': 'textDocument/didOpen',
      \     'jsonrpc': '2.0',
      \     'params': {
      \       'textDocument': {
      \         'uri': ale#path#ToURI(expand('%:p')),
      \         'version': ale#lsp#message#GetNextVersionID() - 1,
      \         'languageId': a:language,
      \         'text': "\n",
      \       },
      \     },
      \   },
      \ ],
      \ l:messages
    endif

    AssertEqual
    \ [
    \   [
    \     a:linter_name,
    \     {
    \       'connection_id': a:conn_prefix . ':' . a:root,
    \       'project_root': a:root,
    \       'buffer': bufnr(''),
    \       'command': !empty(a:command) ? ale#job#PrepareCommand(bufnr(''), a:command) : '',
    \     },
    \   ],
    \ ],
    \ g:calls
  endfunction

  function! AssertInitFailure() abort
    let l:messages = PopMessages()

    AssertEqual [], l:messages
    AssertEqual [], g:calls
  endfunction

  call ale#linter#Reset()

After:
  Restore

  call ale#linter#Reset()
  call ale#lsp#ResetConnections()

  unlet! g:ale_run_synchronously_callbacks
  unlet! g:job_map
  unlet! g:emulate_job_failure
  unlet! g:next_job_id

  unlet! g:socket_map
  unlet! g:emulate_socket_failure
  unlet! g:next_channel_id

  unlet! g:message_buffer
  unlet! g:calls

  delfunction PopMessages
  delfunction Start
  delfunction AssertInitSuccess
  delfunction AssertInitFailure

  runtime autoload/ale/engine.vim
  runtime autoload/ale/job.vim
  runtime autoload/ale/socket.vim

Execute(tsserver should be started correctly):
  runtime ale_linters/typescript/tsserver.vim

  Assert Start()
  call AssertInitSuccess('tsserver', 'tsserver', '', '', ale#Escape('tsserver'))

Execute(tsserver failures should be handled appropriately):
  runtime ale_linters/typescript/tsserver.vim

  let g:emulate_job_failure = 1

  Assert !Start()
  call AssertInitFailure()

Execute(LSP jobs should start correctly):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'stdio',
  \   'executable': 'foo',
  \   'command': 'foo',
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  Assert Start()
  call AssertInitSuccess('foo', 'foo', 'foobar', '/foo/bar', 'foo')

Execute(LSP job failures should be handled):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'stdio',
  \   'executable': 'foo',
  \   'command': 'foo',
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  let g:emulate_job_failure = 1

  Assert !Start()
  call AssertInitFailure()

Execute(LSP TCP connections should start correctly):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'socket',
  \   'address': 'foo',
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  Assert Start()
  call AssertInitSuccess('foo', 'foo', 'foobar', '/foo/bar', '')

Execute(LSP TCP connection failures should be handled):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'socket',
  \   'address': 'foo',
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  let g:emulate_socket_failure = 1

  Assert !Start()
  call AssertInitFailure()

Execute(Deferred executables should be handled correctly):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'stdio',
  \   'executable': {b -> ale#command#Run(b, 'echo', {-> 'foo'})},
  \   'command': '%e -c',
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  Assert Start()
  call ale#test#FlushJobs()
  call AssertInitSuccess('foo', 'foo', 'foobar', '/foo/bar', ale#Escape('foo') . ' -c')

Execute(Deferred commands should be handled correctly):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'stdio',
  \   'executable': 'foo',
  \   'command': {b -> ale#command#Run(b, 'echo', {-> '%e -c'})},
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  Assert Start()
  call ale#test#FlushJobs()
  call AssertInitSuccess('foo', 'foo', 'foobar', '/foo/bar', ale#Escape('foo') . ' -c')

Execute(Deferred addresses should be handled correctly):
  call ale#linter#Define('foobar', {
  \   'name': 'foo',
  \   'lsp': 'socket',
  \   'address': {b -> ale#command#Run(b, 'echo', {-> 'localhost:1234'})},
  \   'project_root': '/foo/bar',
  \   'initialization_options': {},
  \})

  Assert Start()
  call ale#test#FlushJobs()
  call AssertInitSuccess('foo', 'localhost:1234', 'foobar', '/foo/bar', '')