@@ -3,19 +3,34 @@ set cpoptions&vim
3
3
4
4
let s: runningTest = 0
5
5
6
- function ! OmniSharp#actions#test#Run ( ... ) abort
6
+ function ! s: BindTest ( bufnr , Callback ) abort
7
7
if ! s: CheckCapabilities () | return | endif
8
- let bufnr = a: 0 ? a: 1 : bufnr (' %' )
9
- if ! has_key (OmniSharp#GetHost (bufnr ), ' project' )
8
+ if ! has_key (OmniSharp#GetHost (a: bufnr ), ' project' )
10
9
" Initialize the test by fetching the project for the buffer - then call
11
10
" this function again in the callback
12
- call OmniSharp#actions#project#Get (bufnr ,
13
- \ function (' OmniSharp#actions#test#Run ' , [bufnr ]))
11
+ call OmniSharp#actions#project#Get (a: bufnr ,
12
+ \ function (' s:BindTest ' , [a: bufnr, a: Callback ]))
14
13
return
15
14
endif
16
15
let s: runningTest = 1
17
- call OmniSharp#actions#codestructure#Get (bufnr ,
18
- \ function (' s:RunTest' , [function (' s:CBRunTest' )]))
16
+ call OmniSharp#actions#codestructure#Get (a: bufnr ,
17
+ \ a: Callback )
18
+ endfunction
19
+
20
+ function ! OmniSharp#actions#test#Run (... ) abort
21
+ let bufnr = a: 0 ? a: 1 : bufnr (' %' )
22
+ call s: BindTest (bufnr , function (' s:RunTest' , [function (' s:CBRunTest' )]))
23
+ endfunction
24
+
25
+ function ! OmniSharp#actions#test#Debug (... ) abort
26
+ if ! exists (' g:vimspector_home' )
27
+ echohl WarningMsg
28
+ echomsg ' Vimspector required to debug tests'
29
+ echohl None
30
+ return
31
+ endif
32
+ let bufnr = a: 0 ? a: 1 : bufnr (' %' )
33
+ call s: BindTest (bufnr , function (' s:DebugTest' , [function (' s:CBDebugTest' )]))
19
34
endfunction
20
35
21
36
function ! s: CBRunTest (summary) abort
@@ -38,6 +53,15 @@ function! s:CBRunTest(summary) abort
38
53
endif
39
54
endfunction
40
55
56
+ function ! s: CBDebugTest (response) abort
57
+ if ! a: response .Success
58
+ echohl WarningMsg
59
+ echomsg ' Error debugging unit test'
60
+ echomsg a: response .Message
61
+ echohl None
62
+ endif
63
+ endfunction
64
+
41
65
function ! OmniSharp#actions#test#RunInFile (... ) abort
42
66
if ! s: CheckCapabilities () | return | endif
43
67
if a: 0 && type (a: 1 ) == type ([])
@@ -181,6 +205,66 @@ function! s:RunTestsRH(Callback, bufnr, tests, response) abort
181
205
call a: Callback (summary)
182
206
endfunction
183
207
208
+ function ! s: DebugTest (Callback, bufnr , codeElements) abort
209
+ let tests = s: FindTests (a: codeElements )
210
+ let currentTest = s: FindTest (tests)
211
+ if type (currentTest) != type ({})
212
+ echohl WarningMsg | echom ' No test found' | echohl None
213
+ let s: runningTest = 0
214
+ return
215
+ endif
216
+ let project = OmniSharp#GetHost (a: bufnr ).project
217
+ let targetFramework = project.MsBuildProject.TargetFramework
218
+ let opts = {
219
+ \ ' ResponseHandler' : function (' s:DebugTestsRH' , [a: Callback , a: bufnr , tests]),
220
+ \ ' Parameters' : {
221
+ \ ' MethodName' : currentTest.name,
222
+ \ ' TestFrameworkName' : currentTest.framework,
223
+ \ ' TargetFrameworkVersion' : targetFramework
224
+ \ },
225
+ \ ' SendBuffer' : 0
226
+ \}
227
+ echomsg ' Debugging test ' . currentTest.name
228
+ call OmniSharp#stdio#Request (' /v2/debugtest/getstartinfo' , opts)
229
+ endfunction
230
+
231
+ function ! s: DebugTestsRH (Callback, bufnr , tests, response) abort
232
+ let testhost = [a: response .Body.FileName] + split (substitute (a: response .Body.Arguments , ' \"' , ' ' , ' g' ), ' ' )
233
+ let testhost_job_pid = s: StartTestProcess (testhost)
234
+ let g: testhost_job_pid = testhost_job_pid
235
+
236
+ let host = OmniSharp#GetHost ()
237
+ let s: omnisharp_pre_debug_cwd = getcwd ()
238
+ let new_cwd = fnamemodify (host.sln_or_dir, ' :p:h' )
239
+ call vimspector#LaunchWithConfigurations ({
240
+ \ ' attach' : {
241
+ \ ' adapter' : ' netcoredbg' ,
242
+ \ ' configuration' : {
243
+ \ ' request' : ' attach' ,
244
+ \ ' processId' : testhost_job_pid
245
+ \ }
246
+ \ }
247
+ \} )
248
+ execute ' tcd ' .new_cwd
249
+
250
+ call s: LaunchDebuggedTest (a: Callback , testhost_job_pid)
251
+ endfunction
252
+
253
+ function ! s: LaunchDebuggedTest (Callback, pid) abort
254
+ let opts = {
255
+ \ ' ResponseHandler' : function (' s:LaunchDebuggedTestRH' , [a: Callback , a: pid ]),
256
+ \ ' Parameters' : {
257
+ \ ' TargetProcessId' : a: pid
258
+ \ }
259
+ \}
260
+ echomsg ' Launching debugged test'
261
+ call OmniSharp#stdio#Request (' /v2/debugtest/launch' , opts)
262
+ endfunction
263
+
264
+ function ! s: LaunchDebuggedTestRH (Callback, pid, response) abort
265
+ call a: Callback (a: response )
266
+ endfunction
267
+
184
268
function ! s: FindTestsInFiles (Callback, buffers , ... ) abort
185
269
call OmniSharp#util#AwaitParallel (
186
270
\ map (copy (a: buffers ), {i ,b - > function (' OmniSharp#actions#codestructure#Get' , [b ])}),
@@ -284,6 +368,29 @@ function! s:CheckCapabilities() abort
284
368
return 1
285
369
endfunction
286
370
371
+ function ! s: TestProcessClosed (... ) abort
372
+ call OmniSharp#stdio#Request (' /v2/debugtest/stop' , {})
373
+ let s: runningTest = 0
374
+ call vimspector#Reset ()
375
+ execute ' tcd ' .s: omnisharp_pre_debug_cwd
376
+ unlet s: omnisharp_pre_debug_cwd
377
+ endfunction
378
+
379
+ function ! s: StartTestProcess (command ) abort
380
+ if OmniSharp#proc#supportsNeovimJobs ()
381
+ let job = jobpid (jobstart (a: command , {
382
+ \ ' on_exit' : function (' s:TestProcessClosed' )
383
+ \ }))
384
+ elseif OmniSharp#proc#supportsVimJobs ()
385
+ let job = split (job_start (a: command , {
386
+ \ ' close_cb' : function (' s:TestProcessClosed' )
387
+ \ }), ' ' ,)[1 ]
388
+ else
389
+ echohl WarningMsg | echomsg ' Cannot launch test process.' | echohl None
390
+ endif
391
+ return job
392
+ endfunction
393
+
287
394
let &cpoptions = s: save_cpo
288
395
unlet s: save_cpo
289
396
0 commit comments