Skip to content

Commit 97d87d6

Browse files
authored
Interactive netcore test command (#828)
* Add dap-netcore functions to test in debug mode and attach the debugger to the given pid. * Find pid from dotnet process output and attach it to the debugger. * Add interactive debug for tests.
1 parent 30084e7 commit 97d87d6

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

dap-netcore.el

+71
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,76 @@ the function needs to examine, starting with FILE."
195195
:name "NetCoreDbg::Launch"
196196
:dap-compilation "dotnet build"))
197197

198+
(defun dap-netcore-debug-attach (pid)
199+
"Attach the debugger to a .NET process with a given PID using the registered template."
200+
(let* ((config-name ".Net Core Attach (Console)")
201+
(config-cell (assoc config-name dap-debug-template-configurations))
202+
(config-plist (cdr config-cell)))
203+
(setcdr config-cell (plist-put config-plist :processId pid))
204+
(dap-debug (cdr config-cell))))
205+
206+
(defun dap-netcore-test-run (attach-buffer &optional args-string)
207+
"Run .NET tests process to obtain PID to attach for debugging."
208+
(with-environment-variables (("VSTEST_HOST_DEBUG" "1"))
209+
(start-process "dap-netcore-attach-process"
210+
attach-buffer
211+
"dotnet"
212+
"test"
213+
"--verbosity=Quiet"
214+
(concat "" args-string))))
215+
216+
(defun dap-netcore-debug-tests-filter-pid (process output)
217+
"Custom filter to extract PID from the process output in real-time."
218+
(let ((buffer (process-buffer process)))
219+
;; Insert the output into the buffer
220+
(when (buffer-live-p buffer)
221+
(with-current-buffer buffer
222+
(let ((moving (= (point) (process-mark process))))
223+
(save-excursion
224+
(goto-char (process-mark process))
225+
(insert output)
226+
(set-marker (process-mark process) (point)))
227+
(if moving (goto-char (process-mark process))))))
228+
;; Check for PID in the buffer
229+
(when (buffer-live-p buffer)
230+
(with-current-buffer buffer
231+
(save-excursion
232+
(goto-char (point-min))
233+
(when (search-forward "Process Id: " nil t)
234+
(let ((pid-string (buffer-substring (point) (line-end-position))))
235+
;; Debug with PID
236+
(dap-netcore-debug-attach (string-to-number pid-string))
237+
;; Remove the filter to avoid further checks
238+
(set-process-filter process nil))))))))
239+
240+
(defun dap-netcore-debug-test-init (&optional args-string)
241+
"Prepare .NET process to attach its PID for debugging."
242+
(let ((attach-buffer "*dap-netcore-attach*"))
243+
;; Kill existing buffer if it exists
244+
(when (get-buffer attach-buffer)
245+
(kill-buffer attach-buffer))
246+
;; Run dotnet process
247+
(let ((dotnet-process (dap-netcore-test-run attach-buffer args-string)))
248+
(when dotnet-process
249+
(set-process-filter dotnet-process #'dap-netcore-debug-tests-filter-pid)
250+
;; Set process finalization event
251+
(set-process-sentinel
252+
dotnet-process
253+
(lambda (process event)
254+
(when (string-match "exited\\|finished" event)
255+
(message "Process exited with status: %d" (process-exit-status process))
256+
(display-buffer attach-buffer))))))))
257+
258+
(defun dap-netcore-debug-test (&optional directory)
259+
"Debug .NET tests with optional params."
260+
(interactive)
261+
(let ((params '())
262+
(filter (read-string "Filter: ")))
263+
(unless (string-empty-p filter)
264+
(push (concat params " --filter=" filter) params))
265+
(when directory
266+
(push directory params))
267+
(dap-netcore-debug-test-init (string-join (reverse params) " "))))
268+
198269
(provide 'dap-netcore)
199270
;;; dap-netcore.el ends here

0 commit comments

Comments
 (0)