[PYTHON] Führen Sie Django-Tests für den aktuell geöffneten Puffer von Emacs aus

Ich benutze s.el, also installiere es.

(use-package s :ensure t)

(require 's)

(defun our-django--get-project-root-directory ()
  "Holen Sie sich das Stammverzeichnis des Django-Projekts"
  (locate-dominating-file (or buffer-file-name default-directory)
              "manage.py"))


(defun our-django--generate-command (command &optional options)
  "Generieren Sie Django-Befehle"
  (format "python manage.py %s %s"
      command (or options "")))


(defun our-django--get-run-test-command (file-name current-directory)
  "Generieren Sie Django-Testlaufbefehle"
  (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 ()
  "Rufen Sie den Pfad vom aktuellen Puffer zum Testausführungsziel ab"
  (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 ()
  "Führen Sie Django-Tests durch"
  (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)
  "Laden Sie das Django-Gerät"
  (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 ()
  "Führen Sie Djangos Shell aus"
  (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)
  "Führen Sie Djangos Migrationen durch"
  (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)
  "Führen Sie Djangos Migration aus"
  (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)
  "Führen Sie Djangos Showmigrationen aus"
  (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 ()
  "Führen Sie Djangos Doktest aus"
  (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)
  "Führen Sie Djangos Squash-Migrationen aus"
  (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)

Öffnen Sie die Datei mit dem Test und führen Sie sie mit "M-x our-django-test-single" aus.

Wenn Sie eine Schlüsselbindung durchführen möchten, sieht dies folgendermaßen aus.

Recommended Posts

Führen Sie Django-Tests für den aktuell geöffneten Puffer von Emacs aus
Führen Sie Open Modelica im Jupyter Lab aus
(Für mich) Flask_7 (Datenbank von Flask öffnen)
Führen Sie Python-Dateien mit Django aus HTML aus