J'utilise s.el, alors installez-le.
(use-package s :ensure t)
(require 's)
(defun our-django--get-project-root-directory ()
"Récupérez le répertoire racine du projet Django"
(locate-dominating-file (or buffer-file-name default-directory)
"manage.py"))
(defun our-django--generate-command (command &optional options)
"Générer des commandes Django"
(format "python manage.py %s %s"
command (or options "")))
(defun our-django--get-run-test-command (file-name current-directory)
"Générer des commandes d'exécution de test Django"
(our-django--generate-command
"test --verbosity 3 --keepdb" (s-replace "/" "."
(string-trim-right
(file-relative-name file-name current-directory)
".py"))))
(defun our-django--get-current-test-target ()
"Récupère le chemin du tampon actuel vers la cible d'exécution du test"
(s-replace-regexp "/$" ""
(if buffer-file-name
;; file
(if (s-matches? "^test" (file-name-nondirectory buffer-file-name))
buffer-file-name
(file-name-directory buffer-file-name))
;; directory
default-directory)))
(defun our-django-test-single ()
"Exécuter des tests Django"
(interactive)
(let ((default-directory (our-django--get-project-root-directory)))
(our-async-exec
(our-django--get-run-test-command
(our-django--get-current-test-target)
default-directory))))
(defun our-django-load-fixture (fixture)
"Charger un appareil Django"
(interactive "MFixture: ")
(let ((cmd (our-django--generate-command "loaddata" fixture))
(default-directory (our-django--get-project-root-directory)))
(async-shell-command cmd)))
(defun our-django-shell ()
"Exécutez le shell de Django"
(interactive)
(let ((cmd (our-django--generate-command "shell"))
(default-directory (our-django--get-project-root-directory)))
(async-shell-command cmd)))
(defun our-django-make-migrations (&optional app_label)
"Exécutez les migrations de Django"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "makemigrations" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun our-django-migrate (&optional app_label)
"Lancer la migration de Django"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "migrate" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun our-django-show-migrations (&optional app_label)
"Exécutez les migrations de spectacles de Django"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "showmigrations" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun nour-django-doctest ()
"Exécutez le doctest de Django"
(interactive)
(let ((default-directory (our-django--get-project-root-directory)))
(our-async-exec "python run_doctest.py")))
(defun our-django-squash-migrations (&optional app_label start_migration_name migration_name)
"Exécutez les migrations squash de Django"
(interactive (list
(read-from-minibuffer "app_abel: ")
(read-from-minibuffer "start_migration_name: ")
(read-from-minibuffer "migration_name: ")))
(let ((cmd (our-django--generate-command "showmigrations"
(s-join " "
`(,app_label
,start_migration_name
,migration_name))))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(bind-key* "C-t C-f" 'our-django-load-fixture)
(bind-key* "C-t C-r" 'our-django-test-single)
(bind-key* "C-t C-s" 'our-django-shell)
(bind-key* "C-t C-d C-c" 'our-django-make-migrations)
(bind-key* "C-t C-d C-e" 'our-django-migrate)
(bind-key* "C-t C-d C-l" 'our-django-show-migrations)
Ouvrez le fichier contenant le test et lancez-le avec M-x our-django-test-single
.
Si vous souhaitez associer au clavier, cela ressemble à ceci.