+django-tests.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; +django-tests.el -*- lexical-binding: t; -*-
  2. (require 'projectile)
  3. (require 'python)
  4. (require 'subr-x)
  5. (defun django--project-root ()
  6. (or (projectile-project-root)
  7. (user-error "Not in a Projectile project")))
  8. (defun django--module-from-file (file root)
  9. (let* ((rel (file-relative-name file root))
  10. (no-ext (file-name-sans-extension rel)))
  11. (replace-regexp-in-string "/" "." no-ext)))
  12. (defun django--def-name-at-point ()
  13. (save-excursion
  14. (when (ignore-errors (python-nav-beginning-of-defun) t)
  15. (when (looking-at (rx bol (* space) "def" (+ space)
  16. (group (+ (or word ?_))) (* space) "("))
  17. (match-string-no-properties 1)))))
  18. (defun django--enclosing-class-name ()
  19. (save-excursion
  20. (let ((found nil)
  21. (limit (point-min)))
  22. (while (and (not found) (> (point) limit))
  23. (python-nav-beginning-of-statement)
  24. (when (looking-at (rx bol (* space) "class" (+ space)
  25. (group (+ (or word ?_)))))
  26. (setq found (match-string-no-properties 1)))
  27. (condition-case _err
  28. (python-nav-backward-block)
  29. (error (goto-char limit))))
  30. found)))
  31. (defun django--selector-at-point ()
  32. (let ((fn (django--def-name-at-point))
  33. (cls (django--enclosing-class-name)))
  34. (cond
  35. ((and fn (string-prefix-p "test_" fn))
  36. (if cls (format "%s.%s" cls fn) fn))
  37. (cls cls)
  38. (t nil))))
  39. (defun django--ensure-env (root)
  40. (let ((default-directory root))
  41. (when (fboundp 'envrc-reload)
  42. (envrc-reload))))
  43. (defface django-test-command-face
  44. '((t :weight bold :foreground "orange" :height 1.15))
  45. "Face used for Django test commands.")
  46. (defun django--insert-command-header (buf root command)
  47. "Insert a loud command header at the top of BUF."
  48. (with-current-buffer (get-buffer-create buf)
  49. (let ((inhibit-read-only t))
  50. (save-excursion
  51. (goto-char (point-min))
  52. (insert (propertize (make-string 72 ?═) 'face 'shadow) "\n")
  53. (insert (propertize
  54. (format "$ (cd %s && %s)\n" root command)
  55. 'face 'django-test-command-face))
  56. (insert (propertize (make-string 72 ?═) 'face 'shadow) "\n\n")))))
  57. (defun django-run-tests (&optional module selector)
  58. (interactive)
  59. (let* ((root (django--project-root))
  60. (module-part (or module ""))
  61. (selector-part (if selector (concat "." selector) ""))
  62. (param (string-trim (concat module-part selector-part)))
  63. (command (format "python manage.py test --keepdb %s" param))
  64. (buf "*Django tests*"))
  65. ;; Ensure envrc/direnv is current
  66. (let ((default-directory root))
  67. (when (fboundp 'envrc-reload) (envrc-reload)))
  68. ;; Start the process (this may clear/initialize the buffer)
  69. (projectile-run-async-shell-command-in-root command buf)
  70. ;; Now insert header at top (won't get wiped)
  71. (django--insert-command-header buf root command)
  72. (pop-to-buffer buf)))
  73. (defun django-run-test-at-point ()
  74. (interactive)
  75. (let* ((root (django--project-root))
  76. (file (or (buffer-file-name) (user-error "Buffer is not visiting a file")))
  77. (module (django--module-from-file file root))
  78. (sel (django--selector-at-point)))
  79. (django-run-tests module sel)))
  80. (defun django-run-tests-for-current-file ()
  81. (interactive)
  82. (let* ((root (django--project-root))
  83. (file (or (buffer-file-name) (user-error "Buffer is not visiting a file")))
  84. (module (django--module-from-file file root)))
  85. (django-run-tests module nil)))
  86. (provide '+django-tests)