Create a keybinding for running Python tests
This commit is contained in:
parent
4c8feb9cc6
commit
4304c91381
1 changed files with 32 additions and 1 deletions
|
|
@ -168,10 +168,41 @@
|
|||
(lsp))
|
||||
(pyvenv-deactivate))))
|
||||
|
||||
(defun symbols-at-point ()
|
||||
(let* ((lsp--document-symbols-request-async t)
|
||||
(symbols (lsp--get-document-symbols))
|
||||
(symbols-hierarchy (lsp--symbols->document-symbols-hierarchy symbols)))
|
||||
(mapcar (lambda (symb)
|
||||
(list (gethash "name" symb) (lsp--get-symbol-type symb)))
|
||||
symbols-hierarchy)))
|
||||
|
||||
(defun python-test-at-point ()
|
||||
(let ((symbols (symbols-at-point)))
|
||||
(pcase symbols
|
||||
(`((,class "Class") (,method "Method") . ,_)
|
||||
(if (equal "test_" (substring method 0 5))
|
||||
`(,class ,method)
|
||||
`(,class)))
|
||||
(`((,class "Class") . ,_) `(,class))
|
||||
(_ ()))))
|
||||
|
||||
(defun python-test-name-at-point ()
|
||||
(let* ((path (mapcar #'substring-no-properties lsp-headerline--path-up-to-project-segments))
|
||||
(filename (f-no-ext (f-filename (buffer-file-name)))))
|
||||
(string-join (append path (list filename) (python-test-at-point)) ".")))
|
||||
|
||||
(defun python-run-test-at-point ()
|
||||
(interactive)
|
||||
(let ((python (executable-find "python"))
|
||||
(test-name (python-test-name-at-point)))
|
||||
(compile (concat python " -m unittest " test-name))))
|
||||
|
||||
(use-package python
|
||||
:bind (:map python-mode-map
|
||||
("C-c C-p" . nil)
|
||||
("C-c C-l" . nil))
|
||||
("C-c C-l" . nil)
|
||||
("C-c C-t" . python-run-test-at-point)
|
||||
("C-M-t" . recompile))
|
||||
:hook ((python-mode . load-python-env)))
|
||||
|
||||
; Requires the Python package "black"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue