« ^ »

Djangoのdoctestをflycheckで実行する

所要時間: 約 1分
(require 'flycheck)

(require 's)


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


(defun our-django--get-dotted-name (file-name)
  "pythonファイルのdotted nameを取得する"
  (s-replace "/" "." (string-trim-right
		      (file-relative-name file-name default-directory) ".py")))
		      

(flycheck-define-checker python-doctest
  "Run doctest by python"
  :command ("python"
	    "run_doctest.py"
	    (eval (let ((default-directory (our-django--get-project-root-directory)))
		    (our-django--get-dotted-name buffer-file-name))))
  :working-directory our-django--get-project-root-directory
  :error-patterns
  ((error line-start "File \"" (file-name) "\", line " line ", in " (message) line-end))
  :modes python-mode)
(require 'flycheck)


(defun flycheck+-python-get-project-root-directory(file-or-directory-name)
  "Projectのrootディレクトリを返す"
  (locate-dominating-file (or buffer-file-name default-directory)
			  file-or-directory-name))

(defun flycheck+-python-get-dotted-name(file-path)
  "Pythonファイルのdotted nameを取得する"
  (s-replace "/" "." (string-trim-right
		      (file-relative-name file-name default-directory) ".py")))



(flycheck-define-checker django-doctest
  "Execute django doctest"
  )

(flycheck-define-checker python-doctest
  "Execute python doctest"
  :command ("python" "-m" "doctest" source-inplace)
  :working-directory (lambda ()
		       (or
			(flycheck+-python-get-project-root-directory "pyproject.toml")
			(flycheck+-python-get-project-root-directory "setup.py")
			(flycheck+-python-get-project-root-directory ".git")
			default-directory))
  :error-patterns
  ((error line-start "File \"" (file-name) "\", line " line ", in " (message) line-end))
  :modes python-mode)