Skip to content

Commit ef23010

Browse files
committed
Add tag filtering functionality to RSpec mode
This commit adds four functions: - rspec-verify-tagged: only verify the tag(s) in the current file - rspec-verify-all-tagged: only verify the tag(s) in the whole suite - rspec-dired-verify-tagged: only verify the tag(s) in the current dired scope - rspec-dired-verify-single-tagged: only verify the tag(s) in the currently marked files It also adds the corresponding key bindings The functions prompt the user for one or more tags following the rspec syntax and appends them to the rspec command. See https://rspec.info/features/3-12/rspec-core/command-line/tag/
1 parent 29df3d0 commit ef23010

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ automatically when `ruby-mode` is started.
3333

3434
These keybindings are available in any Ruby source file:
3535

36-
Keybinding | Description |
37-
------------|-------------------------------------------------------------------------------|
38-
`C-c , v` | Verify the spec file associated with the current buffer |
39-
`C-c , a` | Run spec for entire project |
40-
`C-c , t` | Toggle back and forth between a spec and its target |
41-
`C-c , e` | Toggle back and forth between a method and its examples in the spec file |
42-
`C-c , 4 t` | Find in the other window the spec or the target file |
43-
`C-c , 4 e` | As above, but try to navigate to the example or method corresponding to point |
44-
`C-c , r` | Re-run the last verification process |
45-
`C-c , y` | Yank the last verification command to clipboard |
46-
`C-c , m` | Run all specs related to the current buffer |
47-
`C-c , c` | Run the current spec and all after it |
48-
`C-c , s` | Verify the example or method defined at point |
49-
`C-c , f` | Re-run just the failed examples from the last run |
36+
| Keybinding | Description |
37+
| -------------- | ----------------------------------------------------------------------------------------- |
38+
| `C-c , v` | Verify the spec file associated with the current buffer |
39+
| `C-c , a` | Run spec for entire project |
40+
| `C-c , t` | Toggle back and forth between a spec and its target |
41+
| `C-c , e` | Toggle back and forth between a method and its examples in the spec file |
42+
| `C-c , 4 t` | Find in the other window the spec or the target file |
43+
| `C-c , 4 e` | As above, but try to navigate to the example or method corresponding to point |
44+
| `C-c , r` | Re-run the last verification process |
45+
| `C-c , y` | Yank the last verification command to clipboard |
46+
| `C-c , m` | Run all specs related to the current buffer |
47+
| `C-c , c` | Run the current spec and all after it |
48+
| `C-c , s` | Verify the example or method defined at point |
49+
| `C-c , f` | Re-run just the failed examples from the last run |
50+
| `C-c , g v` | Verify the spec file associated with the current buffer, filtered by one or multiple tags |
51+
| `C-c , g a` | Run spec for entire project, filtered by one or multiple tags |
5052

5153
### RSpec mode
5254

@@ -61,12 +63,15 @@ Keybinding | Description |
6163

6264
These keybindings are available in Dired buffers:
6365

64-
Keybinding | Description |
65-
-----------|----------------------------------------------------------------|
66-
`C-c , v` | Run all specs in the current directory |
67-
`C-c , s` | Run marked specs or spec at point (works with directories too) |
68-
`C-c , a` | Run the 'spec' rake task for the project of the current file |
69-
`C-c , r` | Re-run the last RSpec invocation |
66+
Keybinding | Description |
67+
-------------|--------------------------------------------------------------------------------------------------|
68+
`C-c , v` | Run all specs in the current directory |
69+
`C-c , s` | Run marked specs or spec at point (works with directories too) |
70+
`C-c , a` | Run the 'spec' rake task for the project of the current file |
71+
`C-c , r` | Re-run the last RSpec invocation |
72+
`C-c , g v` | Run all specs in the current directory, filtered by one or multiple tags |
73+
`C-c , g s` | Run marked specs or spec at point, filtered by one or multiple tags (works with directories too) |
74+
`C-c , g a` | Run the 'spec' rake task for the project of the current file, filtered by one or multiple tags |
7075

7176
See `rspec-mode.el` for further usage.
7277

rspec-mode.el

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
(define-prefix-command 'rspec-verifiable-mode-keymap)
102102
(defvar rspec-mode-keymap)
103103
(define-prefix-command 'rspec-mode-keymap)
104+
(defvar rspec-verifiable-tags-mode-keymap)
105+
(define-prefix-command 'rspec-verifiable-tags-mode-keymap)
104106

105107
(define-key rspec-verifiable-mode-keymap (kbd "v") 'rspec-verify)
106108
(define-key rspec-verifiable-mode-keymap (kbd "a") 'rspec-verify-all)
@@ -115,18 +117,30 @@
115117
(define-key rspec-verifiable-mode-keymap (kbd "s") 'rspec-verify-method)
116118
(define-key rspec-verifiable-mode-keymap (kbd "f") 'rspec-run-last-failed)
117119

120+
(define-key rspec-verifiable-mode-keymap (kbd "g") rspec-verifiable-tags-mode-keymap)
121+
(define-key rspec-verifiable-tags-mode-keymap (kbd "v") 'rspec-verify-tags)
122+
(define-key rspec-verifiable-tags-mode-keymap (kbd "a") 'rspec-verify-tags-all)
123+
118124
(set-keymap-parent rspec-mode-keymap rspec-verifiable-mode-keymap)
119125

120126
(define-key rspec-mode-keymap (kbd "s") 'rspec-verify-single)
121127
(define-key rspec-mode-keymap (kbd "d") 'rspec-toggle-example-pendingness)
122128

123129
(defvar rspec-dired-mode-keymap)
124130
(define-prefix-command 'rspec-dired-mode-keymap)
131+
(defvar rspec-dired-tags-mode-keymap)
132+
(define-prefix-command 'rspec-dired-tags-mode-keymap)
133+
125134
(define-key rspec-dired-mode-keymap (kbd "v") 'rspec-dired-verify)
126135
(define-key rspec-dired-mode-keymap (kbd "s") 'rspec-dired-verify-single)
127136
(define-key rspec-dired-mode-keymap (kbd "a") 'rspec-verify-all)
128137
(define-key rspec-dired-mode-keymap (kbd "r") 'rspec-rerun)
129138

139+
(define-key rspec-dired-mode-keymap (kbd "g") rspec-dired-tags-mode-keymap)
140+
(define-key rspec-dired-tags-mode-keymap (kbd "v") 'rspec-dired-verify-tags)
141+
(define-key rspec-dired-tags-mode-keymap (kbd "a") 'rspec-verify-tags-all)
142+
(define-key rspec-dired-tags-mode-keymap (kbd "s") 'rspec-dired-verify-tags-single)
143+
130144
(defgroup rspec-mode nil
131145
"RSpec minor mode."
132146
:group 'languages)
@@ -466,6 +480,19 @@ buffers concurrently"
466480
(rspec-run-single-file (rspec-spec-file-for (buffer-file-name))
467481
(rspec-core-options)))
468482

483+
(defvar rspec-tags-history nil
484+
"History of tags used in rspec-verify-tags functions.")
485+
486+
(defun rspec-verify-tags ()
487+
"Run current spec file, filtered by one or multiple tags."
488+
(interactive)
489+
(rspec--autosave-buffer-maybe)
490+
(let* ((tags (completing-read-multiple "Select tags (separated by comma): " rspec-tags-history nil nil nil 'rspec-tags-history)))
491+
(rspec-run-single-file (rspec-spec-file-for (buffer-file-name))
492+
(concat (mapconcat #'(lambda (tag) (format " --tag %s" tag)) tags " ")
493+
" "
494+
(rspec-core-options)))))
495+
469496
(defun rspec-verify-matching ()
470497
"Run the specs related to the current buffer.
471498
This is more fuzzy that a simple verify."
@@ -517,17 +544,44 @@ in long-running test suites."
517544
(interactive)
518545
(rspec-run-single-file (dired-current-directory) (rspec-core-options)))
519546

547+
(defun rspec-dired-verify-tags ()
548+
"Run all specs in the current directory, filtered by one or multiple tags."
549+
(interactive)
550+
(let* ((tags (completing-read-multiple "Select tags (separated by comma): " rspec-tags-history nil nil nil 'rspec-tags-history))
551+
(rspec-command-options (concat (mapconcat #'(lambda (tag) (format " --tag %s" tag)) tags " ")
552+
" "
553+
(rspec-core-options))))
554+
(rspec-dired-verify)))
555+
520556
(defun rspec-dired-verify-single ()
521557
"Run marked specs or spec at point (works with directories too)."
522558
(interactive)
523559
(rspec-compile (dired-get-marked-files)
524560
(rspec-core-options)))
525561

562+
(defun rspec-dired-verify-tags-single ()
563+
"Run marked specs/spec at point, filtered by one or multiple tags."
564+
(interactive)
565+
(let* ((tags (completing-read-multiple "Select tags (separated by comma): " rspec-tags-history nil nil nil 'rspec-tags-history))
566+
(rspec-command-options (concat (mapconcat #'(lambda (tag) (format " --tag %s" tag)) tags " ")
567+
" "
568+
(rspec-core-options))))
569+
(rspec-dired-verify-single)))
570+
526571
(defun rspec-verify-all ()
527572
"Run the `spec' rake task for the project of the current file."
528573
(interactive)
529574
(rspec-run (rspec-core-options)))
530575

576+
(defun rspec-verify-tags-all ()
577+
"Run project specs, filtered by one or multiple tags."
578+
(interactive)
579+
(let* ((tags (completing-read-multiple "Select tags (separated by comma): " rspec-tags-history nil nil nil 'rspec-tags-history))
580+
(rspec-command-options (concat (mapconcat #'(lambda (tag) (format " --tag %s" tag)) tags " ")
581+
" "
582+
(rspec-core-options))))
583+
(rspec-verify-all)))
584+
531585
(defun rspec-toggle-spec-and-target ()
532586
"Switch to the spec or the target file for the current buffer.
533587
If the current buffer is visiting a spec file, switches to the

0 commit comments

Comments
 (0)