Skip to content

Commit

Permalink
Correctly handle -m and -k flags (#37)
Browse files Browse the repository at this point in the history
Add a special reader helper to read shell arguments that should go after
a short-form pytest flag.

Fixes #36.
  • Loading branch information
wbolster authored Nov 20, 2020
1 parent a2f88b1 commit 4a1c4c8
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions python-pytest.el
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ When non-nil only ‘test_foo()’ will match, and nothing else."
("-s" "no output capture" "--capture=no")
(python-pytest:-v)]]
["Selection, filtering, ordering"
[("-k" "only names matching expression" "-k=")
("-m" "only marks matching expression" "-m=")
[(python-pytest:-k)
(python-pytest:-m)
" "] ;; visual alignment
[("--dm" "run doctests" "--doctest-modules")
("--nf" "new first" "--new-first")
Expand Down Expand Up @@ -417,9 +417,7 @@ With a prefix ARG, allow editing."
"Transform ARGS so that pytest understands them."
(-->
args
(python-pytest--switch-to-option it "--color" "--color=yes" "--color=no")
(python-pytest--quote-string-option it "-k")
(python-pytest--quote-string-option it "-m")))
(python-pytest--switch-to-option it "--color" "--color=yes" "--color=no")))

(defun python-pytest--switch-to-option (args name on-replacement off-replacement)
"Look in ARGS for switch NAME and turn it into option with a value.
Expand All @@ -441,6 +439,29 @@ When present ON-REPLACEMENT is substituted, else OFF-REPLACEMENT is appended."
(format "%s %s" option it)))
args))

(defun python-pytest--read-quoted-argument-for-short-flag (prompt initial-input history)
"Read a quoted string for use as a argument after a short-form command line flag."
(let* ((input (read-from-minibuffer prompt initial-input nil nil history))
(quoted-input (python-pytest--shell-quote input))
(formatted-input (format " %s" quoted-input)))
formatted-input))

(transient-define-argument python-pytest:-k ()
:description "only names matching expression"
:class 'transient-option
:argument "-k"
:allow-empty nil
:key "-k"
:reader 'python-pytest--read-quoted-argument-for-short-flag)

(transient-define-argument python-pytest:-m ()
:description "only marks matching expression"
:class 'transient-option
:argument "-m"
:allow-empty nil
:key "-m"
:reader 'python-pytest--read-quoted-argument-for-short-flag)

(transient-define-argument python-pytest:-v ()
:description "verbosity"
:class 'transient-switches
Expand Down

0 comments on commit 4a1c4c8

Please sign in to comment.