Add debug variants of the nosetests commands

This commit is contained in:
Jeremy Dormitzer 2020-02-25 13:58:14 -05:00
parent dd354e9046
commit 36e5bfab7f

View File

@ -2745,29 +2745,37 @@ And borrowing some functions from python-pytest, we can get some nosetests suppo
(defvar nosetests-args "" (defvar nosetests-args ""
"Additional args to pass to nosetests") "Additional args to pass to nosetests")
(defun run-nose (args) (defun run-nose (args &optional debug)
"Runs nosetests with `args'" "Runs nosetests with `args'
(let ((nosetests-cmd (executable-find "nosetests")))
If `debug' is non-nil, run in a GUD PDB session."
(let* ((nosetests-cmd (executable-find "nosetests"))
(cmdline (format "%s %s" nosetests-cmd args)))
(when (not nosetests-cmd) (when (not nosetests-cmd)
(user-error "Nosetests command not found")) (user-error "Nosetests command not found"))
(compile (format "%s %s" nosetests-cmd args) t))) (if debug
(pdb cmdline)
(compile cmdline))))
(defun run-nose-reading-args (arg nose-args) (defun run-nose-reading-args (arg nose-args &optional debug)
"Runs nosetests with default args "Runs nosetests with default args or prompts for args with prefix
or prompts for args with prefix"
If `debug' is non-nil, run in a GUD PDB session."
(let ((args (if arg (let ((args (if arg
(read-string "Nosetests arguments: " (read-string "Nosetests arguments: "
nil nil nosetests-args) nil nil nosetests-args)
nosetests-args))) nosetests-args)))
(run-nose (format "%s %s" args nose-args)))) (run-nose (format "%s %s" args nose-args) debug)))
(defun run-nose-in-project (arg nose-args) (defun run-nose-in-project (arg nose-args &optional debug)
"Runs nosetests from the project root" "Runs nosetests from the project root
If `debug' is non-nil, run in a GUD PDB session."
(let ((dir (or (projectile-project-root) (let ((dir (or (projectile-project-root)
default-directory))) default-directory)))
(with-temp-buffer (with-temp-buffer
(cd dir) (cd dir)
(run-nose-reading-args arg nose-args)))) (run-nose-reading-args arg nose-args debug))))
(defun nosetests-all (arg) (defun nosetests-all (arg)
"Runs nosetests on all project tests, prompting for the tests directory" "Runs nosetests on all project tests, prompting for the tests directory"
@ -2778,6 +2786,15 @@ And borrowing some functions from python-pytest, we can get some nosetests suppo
nil t "tests" #'directory-name-p))) nil t "tests" #'directory-name-p)))
(run-nose-in-project arg (directory-file-name test-dir)))) (run-nose-in-project arg (directory-file-name test-dir))))
(defun nosetests-debug-all (arg)
"Runs nosetests in a GUD session on all project tests"
(interactive "P")
(let ((test-dir (read-file-name "Test directory: "
(or (projectile-project-root)
default-directory)
nil t "tests" #'directory-name-p)))
(run-nose-in-project arg (directory-file-name test-dir) t)))
(defun nosetests-module (arg module) (defun nosetests-module (arg module)
"Runs nosetests in the module of the current file" "Runs nosetests in the module of the current file"
(interactive (list current-prefix-arg (interactive (list current-prefix-arg
@ -2788,6 +2805,16 @@ And borrowing some functions from python-pytest, we can get some nosetests suppo
#'directory-name-p))) #'directory-name-p)))
(run-nose-in-project arg (directory-file-name module))) (run-nose-in-project arg (directory-file-name module)))
(defun nosetests-debug-module (arg module)
"Runs nosetests in a GUD session in the module of the current file"
(interactive (list current-prefix-arg
(read-file-name "Run nosetests on module: "
nil nil t
(file-name-directory
(buffer-file-name))
#'directory-name-p)))
(run-nose-in-project arg (directory-file-name module) t))
(defun nosetests-file (arg file) (defun nosetests-file (arg file)
"Runs nosetests on the current file" "Runs nosetests on the current file"
(interactive (list current-prefix-arg (interactive (list current-prefix-arg
@ -2795,6 +2822,13 @@ And borrowing some functions from python-pytest, we can get some nosetests suppo
nil nil t (buffer-file-name)))) nil nil t (buffer-file-name))))
(run-nose-in-project arg file)) (run-nose-in-project arg file))
(defun nosetests-debug-file (arg file)
"Runs nosetests in a GUD session on the current file"
(interactive (list current-prefix-arg
(read-file-name "Run nosetests on file: "
nil nil t (buffer-file-name))))
(run-nose-in-project arg file t))
(defun nosetests-def (arg def) (defun nosetests-def (arg def)
"Runs nosetests on the enclosing function at point" "Runs nosetests on the enclosing function at point"
(interactive (list current-prefix-arg (interactive (list current-prefix-arg
@ -2803,17 +2837,38 @@ And borrowing some functions from python-pytest, we can get some nosetests suppo
(buffer-file-name) (buffer-file-name)
def))) def)))
(defun nosetests-debug-def (arg def)
"Runs nosetests in a GUD session on the enclosing function at point"
(interactive (list current-prefix-arg
(python-pytest--current-defun)))
(run-nose-in-project arg
(format "%s:%s"
(buffer-file-name)
def)
t))
(defvar nosetests-map (make-sparse-keymap) (defvar nosetests-map (make-sparse-keymap)
"Keymap for nosetests") "Keymap for nosetests")
(defvar nosetests-debug-map (make-sparse-keymap)
"Keymap for debugging nosetests")
(general-def python-mode-map "C-c t n" nosetests-map) (general-def python-mode-map "C-c t n" nosetests-map)
(general-def nosetests-map "a" #'nosetests-all) (general-def nosetests-map "a" #'nosetests-all)
(general-def nosetests-map "m" #'nosetests-module) (general-def nosetests-map "m" #'nosetests-module)
(general-def nosetests-map "f" #'nosetests-file) (general-def nosetests-map "f" #'nosetests-file)
(general-def nosetests-map "d" #'nosetests-def) (general-def nosetests-map "d" #'nosetests-def)
(general-def nosetests-map "b" nosetests-debug-map)
(general-def nosetests-debug-map "a" #'nosetests-debug-all)
(general-def nosetests-debug-map "m" #'nosetests-debug-module)
(general-def nosetests-debug-map "f" #'nosetests-debug-file)
(general-def nosetests-debug-map "d" #'nosetests-debug-def)
(which-key-add-major-mode-key-based-replacements (which-key-add-major-mode-key-based-replacements
'python-mode "C-c t n" "nosetests") 'python-mode "C-c t n" "nosetests")
(which-key-add-major-mode-key-based-replacements
'python-mode "C-c t n b" "debug")
#+END_SRC #+END_SRC
* Hy * Hy