現在開いているバッファのDjangoのテストをEmacsから実行する

s.elを使っているのでinstallしておく。

```emacs-lisp
(use-package s :ensure t)

(require 's)

(defun our-django--get-project-root-directory ()
  "Djangoのprojectのrootディレクトリを取得する"
  (locate-dominating-file (or buffer-file-name default-directory)
              "manage.py"))


(defun our-django--generate-command (command &optional options)
  "Djangoのコマンドを生成する"
  (format "python manage.py %s %s"
      command (or options "")))


(defun our-django--get-run-test-command (file-name current-directory)
  "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 ()
  "カレントバッファからテスト実行対象へのパスを取得する"
  (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 ()
  "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)
  "DjangoのFixtureをloadする"
  (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 ()
  "Djangoのshellを実行する"
  (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)
  "Djangoのmakemigrationsを実行する"
  (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)
  "Djangoのmigrateを実行する"
  (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)
  "Djangoのshowmigrationsを実行する"
  (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 ()
  "Djangoのdoctestを実行する"
  (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)
  "Djangoのsquashmigrationsを実行する"
  (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)

```


テストが書かれているファイルを開いて `M-x our-django-test-single` で実行。


キーバインドするならこんな感じ。