wbolster / emacs-python-pytest

run pytest inside emacs
BSD 3-Clause "New" or "Revised" License
149 stars 26 forks source link

Adding custom plugins support easily #71

Open paquiteau opened 10 months ago

paquiteau commented 10 months ago

Hello there, I am using your package more and more, and keep liking it. In my python projects I usually defines some custom command lines arguments for pytest, and found a nice way to add them dynamically to the transient menu.

I rely on .dir-locals.el for projects variables, to store custom transients menu and load them accordingly:

(defvar pytest-local-transient '((:info "No local options")) "Local options for creating local prefixes." )

;; Custom Group specification 
;; https://www.gnu.org/software/emacs/manual/html_mono/transient.html#Group-Specifications
(transient-append-suffix 'python-pytest-dispatch '(-2)
 ["Local Options" 
  :setup-children (lambda (_) 
     (list (transient-parse-suffix transient--suffixes pytest-local-transient)))
]))

And then define custom transient suffix in a .dir-locals.el file:

;; $MY_PYTHON_PROJECT/.dir-locals.el
((python-mode (pytest-local-transient .
                ((
                    "b" "Backend to run" "--backend="
                    :multi-value repeat
                    :choices ("cpu" "gpu")
                )))
))

Let me know if you would like to have this as a PR, but as it is fairly minimal coda I think it will still be happy if it stayed comfy in my config.

wbolster commented 10 months ago

is there a way to not have that ‘local options’ section in the transient window at all? i'd like to avoid useless clutter for a feature that not many people will use and that doesn't do anything by default.

paquiteau commented 10 months ago

Yes !


(defvar pytest-local-transient nil "Local transient options for pytest")

(transient-define-prefix python-pytest-dispatch ()
  "Show popup for running pytest."
  :man-page "pytest"
  :incompatible '(("--exitfirst" "--maxfail="))
  :value '("--color")
  ["Output"
   [("-c" "color" "--color")
    ("-q" "quiet" "--quiet")
    ("-s" "no output capture" "--capture=no")
    (python-pytest:-v)]]
  ["Selection, filtering, ordering"
   [(python-pytest:-k)
    (python-pytest:-m)
    "                                          "] ;; visual alignment
   [("--dm" "run doctests" "--doctest-modules")
    ("--nf" "new first" "--new-first")
    ("--sw" "stepwise" "--stepwise")
    ("--co" "collect only" "--collect-only")]]
  ["Failures, errors, debugging"
   [("-l" "show locals" "--showlocals")
    ("-p" "debug on error" "--pdb")
    ("-x" "exit after first failure" "--exitfirst")]
   [("--ff" "failed first" "--failed-first")
    ("--ft" "full tracebacks" "--full-trace")
    ("--mf" "exit after N failures or errors" "--maxfail=")
    ("--rx" "run xfail tests" "--runxfail")
    (python-pytest:--tb)
    ("--tr" "debug on each test" "--trace")]]
  ["Options for pytest-xdist"
   [(python-pytest:-n)]
   [("-f" "loop on failure" "--looponfail")]]
  ["Local Options"
    :setup-children (lambda (_) (transient-parse-suffixes transient--suffixes pytest-local-transient))
    :if-non-nil pytest-local-transient
    (:info "No Local options")]
  ["Run tests"
   [("t" "all" python-pytest)]
   [("r" "repeat" python-pytest-repeat)
    ("x" "last failed" python-pytest-last-failed)]
   [("f" "file (dwim)" python-pytest-file-dwim)
    ("F" "file (this)" python-pytest-file)]
   [("m" "files" python-pytest-files)
    ("M" "directories" python-pytest-directories)]
   [("d" "def/class (dwim)" python-pytest-function-dwim)
    ("D" "def/class (this)" python-pytest-function)]])

I overwrite the definition for prototyping , using transient-append-suffix was creating a new section each time ...

Also now we can define multiple CLI arguments like so


 ((python-mode (pytest-local-transient .
                                [(
                                    "b" "Backend to run" "--backend="
                                    :multi-value repeat
                                    :choices ("cpu" "gpu")
                                )
                                ("c" "custom command" "--custom-command")])
))
ns-rse commented 1 month ago

Oooh, this looks really interesting.

I just discovered some of the options to pytest-mpl such as the ability to add the flag --mpl-generate-summary={html,json,basic-html} option and it would be great to have these available in the transient buffer.