Skip to content

Commit

Permalink
Add a python-pytest-directories command (#31)
Browse files Browse the repository at this point in the history
This is like python-pytest-files, but for directories:
it interactively prompts for directories (only those containing test
files), then runs only tests in those directories.

A variant to select only a single directory doesn't add much value,
since the multi-select version can also be used to select only
a single directory.

Fixes #21.
  • Loading branch information
wbolster authored Aug 10, 2020
1 parent 6a3b4e5 commit 9ca8c94
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
13 changes: 11 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ __ https://melpa.org/
__ https://stable.melpa.org/


3.0.0 (not yet released)
------------------------

* add a ``python-pytest-directories`` command with interactive
multi-directory selection
(`#21 <https://github.com/wbolster/emacs-python-pytest/issues/21>`_)
(`#31 <https://github.com/wbolster/emacs-python-pytest/pull/31>`_)

2.0.0 (2020-08-04)
------------------

Expand All @@ -384,9 +392,10 @@ __ https://stable.melpa.org/
(`#18 <https://github.com/wbolster/emacs-python-pytest/issues/18>`_)
(`#26 <https://github.com/wbolster/emacs-python-pytest/pull/26>`_)

* add python-pytest-files command with interactive multi-file selection
* add ``python-pytest-files`` command with interactive multi-file
selection

* improve python-pytest-file-dwim heuristic for nested functions/classes
* improve ``python-pytest-file-dwim`` heuristic for nested functions/classes

* make ``next-error`` and related-commands work

Expand Down
56 changes: 40 additions & 16 deletions python-pytest.el
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ When non-nil only ‘test_foo()’ will match, and nothing else."
["Run tests for specific files"
("f" "Test file (dwim)" python-pytest-file-dwim)
("F" "Test this file" python-pytest-file)
("m" "Test multiple files" python-pytest-files)]
("m" "Test multiple files" python-pytest-files)
("m" "Test multiple directories" python-pytest-directories)]
["Run tests for current function/class"
("d" "Test def/class (dwim)" python-pytest-function-dwim)
("D" "Test this def/class" python-pytest-function)]])
Expand Down Expand Up @@ -201,13 +202,30 @@ Additional ARGS are passed along to pytest.
With a prefix argument, allow editing."
(interactive
(list
(python-pytest--select-test-files)
(python-pytest--select-test-files :type 'file)
(python-pytest-arguments)))
(setq args (-concat args (-map 'python-pytest--shell-quote files)))
(python-pytest--run
:args args
:edit current-prefix-arg))

;;;###autoload
(defun python-pytest-directories (directories &optional args)
"Run pytest on DIRECTORIES, using ARGS.
When run interactively, this allows for interactive directory selection.
Additional ARGS are passed along to pytest.
With a prefix argument, allow editing."
(interactive
(list
(python-pytest--select-test-files :type 'directory)
(python-pytest-arguments)))
(setq args (-concat args (-map 'python-pytest--shell-quote directories)))
(python-pytest--run
:args args
:edit current-prefix-arg))

;;;###autoload
(defun python-pytest-function (file func args)
"Run pytest on FILE with FUNC (or class).
Expand Down Expand Up @@ -516,30 +534,36 @@ Example: ‘MyABCThingy.__repr__’ becomes ‘test_my_abc_thingy_repr’."
(python-pytest--relative-file-name file)
(python-pytest--find-test-file file)))

(cl-defun python-pytest--select-test-files ()
(cl-defun python-pytest--select-test-files (&key type)
"Interactively choose test files."
(cl-block nil
(let ((test-files
(->> (projectile-project-files (python-pytest--project-root))
(-sort 'string<)
(projectile-sort-by-recentf-first)
(projectile-test-files)))
(done-message (propertize "[finish test file selection]" 'face 'success))
(choices)
(choice)
(selection-active t))
(unless test-files
(let* ((test-files
(->> (projectile-project-files (python-pytest--project-root))
(-sort 'string<)
(projectile-sort-by-recentf-first)
(projectile-test-files)))
(test-directories
(->> test-files
(-map 'file-name-directory)
(-uniq)
(-sort 'string<)))
(candidates (if (eq type 'file) test-files test-directories))
(done-message (propertize "[finish test file selection]" 'face 'success))
(choices)
(choice)
(selection-active t))
(unless candidates
(user-error "No test files found"))
(while (and selection-active test-files)
(while (and selection-active candidates)
(setq choice (completing-read
"Choose test files: "
(if choices (cons done-message test-files) test-files)
(if choices (cons done-message candidates) candidates)
nil t))
(if (s-equals-p choice done-message)
(setq selection-active nil)
(setq
choices (cons choice choices)
test-files (-remove-item choice test-files))))
candidates (-remove-item choice candidates))))
(cl-return (reverse choices)))))

(defun python-pytest--maybe-save-buffers ()
Expand Down

0 comments on commit 9ca8c94

Please sign in to comment.