ionrock / pytest-el

Run py.test on testing functions, classes, modules and entire suites in Emacs.
54 stars 26 forks source link

Use directory for current buffer if pytest-project-root-files are not found #26

Open sr105 opened 6 years ago

sr105 commented 6 years ago

I don't know elisp well enough to know if there's a better way to do this (hence an issue and not a pull request). This code returns the directory of the current buffer if there isn't a project root. That way, I can write tests in a single file where I'm just testing out some code.

Untouched; only changed the name.

(defun pytest-find-project-root-recursive (&optional dirname)
  (let ((dn
         (if dirname
             dirname
           (file-name-directory buffer-file-name))))
    (cond ((funcall pytest-project-root-test dn) (expand-file-name dn))
          ((equal (expand-file-name dn) "/") nil)
        (t (pytest-find-project-root-recursive
             (file-name-directory (directory-file-name dn)))))))

Try to find a project root and fall back to the current directory.

(defun pytest-find-project-root (&optional dirname)
  (or (pytest-find-project-root-recursive dirname)
      (file-name-directory buffer-file-name)))
sr105 commented 6 years ago

In .emacs:

(defun pytest-find-project-root-or-current-dir (orig-fn &rest args)
  (or (apply orig-fn args)
      (file-name-directory buffer-file-name)))
(advice-add 'pytest-find-project-root :around 'pytest-find-project-root-or-current-dir)