-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathdiagnostics.test.ts
334 lines (312 loc) · 12.1 KB
/
diagnostics.test.ts
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
import { Neovim } from '@chemzqm/neovim'
import os from 'os'
import path from 'path'
import { v4 as uuid } from 'uuid'
import { CancellationToken, DocumentDiagnosticRequest, Position, TextEdit } from 'vscode-languageserver-protocol'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { URI } from 'vscode-uri'
import * as lsclient from '../../language-client'
import { BackgroundScheduler, DocumentPullStateTracker, PullState } from '../../language-client/diagnostic'
import workspace from '../../workspace'
import helper from '../helper'
function getId(uri: string): number {
let ms = uri.match(/\d+$/)
return ms ? Number(ms[0]) : undefined
}
function createDocument(id: number, version = 1): TextDocument {
let uri = `file:///${id}`
return TextDocument.create(uri, '', version, '')
}
function createUri(id: number): URI {
return URI.file(id.toString())
}
describe('BackgroundScheduler', () => {
it('should schedule documents by add', async () => {
let uris: string[] = []
let s = new BackgroundScheduler({
pull(document) {
uris.push(document.uri)
}
})
s.add(createDocument(1))
s.add(createDocument(1))
s.add(createDocument(2))
s.add(createDocument(3))
await helper.waitValue(() => {
return uris.length
}, 3)
let ids = uris.map(u => getId(u))
expect(ids).toEqual([1, 2, 3])
})
it('should schedule documents by remove', async () => {
let uris: string[] = []
let s = new BackgroundScheduler({
pull(document) {
uris.push(document.uri)
}
})
s.add(createDocument(1))
s.add(createDocument(2))
s.remove(createDocument(2))
s.add(createDocument(3))
s.remove(createDocument(3))
s.remove(createDocument(1))
await helper.waitValue(() => {
return uris.length
}, 3)
let ids = uris.map(u => getId(u))
expect(ids).toEqual([2, 3, 1])
s.dispose()
})
})
describe('DocumentPullStateTracker', () => {
it('should track document', async () => {
let tracker = new DocumentPullStateTracker()
let state = tracker.track(PullState.document, createDocument(1))
let other = tracker.track(PullState.document, createDocument(1))
expect(state).toBe(other)
tracker.track(PullState.workspace, createDocument(3))
let id = 'dcf06d3b-79f6-4a5e-bc8d-d3334f7b4cad'
tracker.update(PullState.document, createDocument(1, 2), id)
tracker.update(PullState.document, createDocument(2, 2), 'f758ae47-c94e-406e-ba41-0f3bb2fe4fc7')
let curr = tracker.getResultId(PullState.document, createDocument(1, 2))
expect(curr).toBe(id)
expect(tracker.getResultId(PullState.workspace, createDocument(1, 2))).toBeUndefined()
tracker.unTrack(PullState.document, createDocument(2, 2))
expect(tracker.trackingDocuments()).toEqual(['file:///1'])
tracker.update(PullState.workspace, createDocument(3, 2), 'fcb905e2-8edb-4239-9150-198c8175ed4a')
tracker.update(PullState.workspace, createDocument(1, 2), 'fe96d175-c19f-4705-bff1-101bf83b2953')
expect(tracker.tracks(PullState.workspace, createDocument(3, 1))).toBe(true)
expect(tracker.tracks(PullState.document, createDocument(4, 1))).toBe(false)
let res = tracker.getAllResultIds()
expect(res.length).toBe(2)
})
it('should track URI', async () => {
let tracker = new DocumentPullStateTracker()
let state = tracker.track(PullState.document, createUri(1), undefined)
let other = tracker.track(PullState.document, createUri(1), undefined)
expect(state).toBe(other)
tracker.track(PullState.workspace, createUri(3), undefined)
let id = 'dcf06d3b-79f6-4a5e-bc8d-d3334f7b4cad'
tracker.update(PullState.document, createUri(1), undefined, id)
tracker.update(PullState.document, createUri(2), undefined, 'f758ae47-c94e-406e-ba41-0f3bb2fe4fc7')
let curr = tracker.getResultId(PullState.document, createUri(1))
expect(curr).toBe(id)
tracker.unTrack(PullState.document, createUri(2))
expect(tracker.trackingDocuments()).toEqual(['file:///1'])
tracker.update(PullState.workspace, createUri(3), undefined, undefined)
tracker.update(PullState.workspace, createUri(1), undefined, 'fe96d175-c19f-4705-bff1-101bf83b2953')
expect(tracker.tracks(PullState.workspace, createUri(3))).toBe(true)
expect(tracker.tracks(PullState.document, createUri(4))).toBe(false)
let res = tracker.getAllResultIds()
expect(res.length).toBe(1)
})
})
describe('DiagnosticFeature', () => {
let nvim: Neovim
beforeAll(async () => {
await helper.setup()
nvim = workspace.nvim
})
afterAll(async () => {
await helper.shutdown()
})
afterEach(async () => {
await helper.reset()
})
async function createServer(interFileDependencies: boolean, workspaceDiagnostics = false, middleware: lsclient.Middleware = {}, fun?: (opt: lsclient.LanguageClientOptions) => void) {
let clientOptions: lsclient.LanguageClientOptions = {
documentSelector: [{ language: '*' }],
middleware,
initializationOptions: {
interFileDependencies: interFileDependencies == true,
workspaceDiagnostics
}
}
if (fun) fun(clientOptions)
let serverModule = path.join(__dirname, './server/diagnosticServer.js')
let serverOptions: lsclient.ServerOptions = {
module: serverModule,
transport: lsclient.TransportKind.ipc
}
let client = new lsclient.LanguageClient('html', 'Test Language Server', serverOptions, clientOptions)
await client.start()
return client
}
function getUri(s: number | string): string {
let fsPath = path.join(os.tmpdir(), s.toString())
return URI.file(fsPath).toString()
}
it('should work when change visible editor', async () => {
let doc = await workspace.loadFile(getUri(1), 'edit')
await workspace.loadFile(getUri(3), 'tabe')
let client = await createServer(true)
await helper.wait(30)
await workspace.loadFile(getUri(2), 'edit')
await helper.wait(30)
await workspace.loadFile(getUri(3), 'tabe')
await helper.wait(30)
let feature = client.getFeature(DocumentDiagnosticRequest.method)
expect(feature).toBeDefined()
let provider = feature.getProvider(doc.textDocument)
let res = provider.knows(PullState.document, doc.textDocument)
expect(res).toBe(false)
await client.stop()
})
it('should filter by document selector', async () => {
let client = await createServer(true, false, {}, opt => {
opt.documentSelector = [{ language: 'vim' }]
})
let doc = await workspace.loadFile(getUri(1), 'edit')
await helper.wait(10)
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let provider = feature.getProvider(TextDocument.create('file:///1', 'vim', 1, ''))
let res = provider.knows(PullState.document, doc.textDocument)
expect(res).toBe(false)
await client.stop()
})
it('should filter by ignore', async () => {
let client = await createServer(true, false, {}, opt => {
opt.diagnosticPullOptions = {
ignored: ['**/*.ts']
}
})
let doc = await workspace.loadFile(getUri('a.ts'), 'edit')
await helper.wait(10)
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let provider = feature.getProvider(doc.textDocument)
let res = provider.knows(PullState.document, doc.textDocument)
expect(res).toBe(false)
await client.stop()
})
it('should not throw on request error', async () => {
let client = await createServer(true)
await workspace.loadFile(getUri('error'), 'edit')
await workspace.loadFile(getUri('cancel'), 'tabe')
await workspace.loadFile(getUri('retrigger'), 'tabe')
await helper.wait(10)
await nvim.command('normal! 2gt')
await workspace.loadFile(getUri('unchanged'), 'edit')
await helper.wait(20)
await client.stop()
})
it('should pull diagnostic on change', async () => {
let doc = await workspace.loadFile(getUri('change'), 'edit')
let client = await createServer(true, false, {}, opt => {
opt.diagnosticPullOptions = {
onChange: true,
filter: doc => {
return doc.uri.endsWith('filtered')
}
}
})
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let provider = feature.getProvider(doc.textDocument)
await helper.waitValue(() => {
return provider.knows(PullState.document, doc.textDocument)
}, true)
await doc.applyEdits([TextEdit.insert(Position.create(0, 0), 'foo')])
await helper.waitValue(async () => {
return await client.sendRequest('getChangeCount')
}, 2)
await nvim.call('setline', [1, 'foo'])
let d = await workspace.loadFile(getUri('filtered'), 'tabe')
await d.applyEdits([TextEdit.insert(Position.create(0, 0), 'foo')])
await helper.wait(30)
await nvim.command(`bd! ${doc.bufnr}`)
await client.stop()
})
it('should pull diagnostic on save', async () => {
let doc = await workspace.loadFile(getUri(uuid() + 'filtered'), 'edit')
await doc.applyEdits([TextEdit.insert(Position.create(0, 0), 'foo')])
doc = await workspace.loadFile(getUri(uuid() + 'save'), 'tabe')
let client = await createServer(true, false, {}, opt => {
opt.diagnosticPullOptions = {
onSave: true,
filter: doc => {
return doc.uri.endsWith('filtered')
}
}
})
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let provider = feature.getProvider(doc.textDocument)
await helper.waitValue(() => {
return provider.knows(PullState.document, doc.textDocument)
}, true)
await doc.applyEdits([TextEdit.insert(Position.create(0, 0), 'foo')])
await nvim.command('wa')
await helper.wait(10)
await client.stop()
})
it('should use provideDiagnostics middleware', async () => {
let called = false
let callHandle = false
let client = await createServer(true, false, {
provideDiagnostics: (doc, id, token, next) => {
called = true
return next(doc, id, token)
},
handleDiagnostics: (uri, diagnostics, next) => {
callHandle = true
return next(uri, diagnostics)
}
})
let feature = client.getFeature(DocumentDiagnosticRequest.method)
expect(feature).toBeDefined()
let textDocument = TextDocument.create(getUri('empty'), 'e', 1, '')
let provider = feature.getProvider(textDocument)
let res = await provider.diagnostics.provideDiagnostics(textDocument, '', CancellationToken.None)
expect(called).toBe(true)
expect(res).toEqual({ kind: 'full', items: [] })
await helper.waitValue(() => {
return callHandle
}, true)
await client.stop()
})
it('should use provideWorkspaceDiagnostics middleware', async () => {
let called = false
let client = await createServer(false, true, {
provideWorkspaceDiagnostics: (resultIds, token, resultReporter, next) => {
called = true
return next(resultIds, token, resultReporter)
}
})
expect(called).toBe(true)
await helper.waitValue(async () => {
let count = await client.sendRequest('getWorkspceCount')
return count > 1
}, true)
await client.stop()
})
it('should receive partial result', async () => {
let client = await createServer(false, true, {}, opt => {
opt.diagnosticPullOptions = {
workspace: false
}
})
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let textDocument = TextDocument.create(getUri('empty'), 'e', 1, '')
let provider = feature.getProvider(textDocument)
let n = 0
await provider.diagnostics.provideWorkspaceDiagnostics([{ uri: 'uri', value: '1' }], CancellationToken.None, chunk => {
n++
})
expect(n).toBe(4)
await client.stop()
})
it('should fire refresh event', async () => {
let client = await createServer(true, false, {})
let feature = client.getFeature(DocumentDiagnosticRequest.method)
let textDocument = TextDocument.create(getUri('1'), 'e', 1, '')
let provider = feature.getProvider(textDocument)
let called = false
provider.onDidChangeDiagnosticsEmitter.event(() => {
called = true
})
await client.sendNotification('fireRefresh')
await helper.waitValue(() => {
return called
}, true)
await client.stop()
})
})