-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
rspec-mode.el
1133 lines (978 loc) · 43.4 KB
/
rspec-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; rspec-mode.el --- Enhance ruby-mode for RSpec -*- lexical-binding: t -*-
;; Copyright (C) 2008-2015 Peter Williams <http://barelyenough.org> and others
;; Author: Peter Williams, et al.
;; URL: http://github.com/pezra/rspec-mode
;; Created: 2011
;; Version: 1.21
;; Keywords: rspec ruby
;; Package-Requires: ((ruby-mode "1.0") (cl-lib "0.4"))
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; See <http://www.gnu.org/licenses/> for a copy of the GNU General
;; Public License.
;;; Commentary:
;;
;; This minor mode provides some enhancements to ruby-mode in the
;; contexts of RSpec specifications. Refer to the README for a
;; summary of keybindings and their descriptions.
;;
;; You can choose whether to run specs using 'rake spec' or the 'rspec'
;; command. Use the customization interface (customize-group
;; rspec-mode) or override using (setq rspec-use-rake-when-possible TVAL).
;;
;; Options will be loaded from spec.opts or .rspec if it exists and
;; rspec-use-opts-file-when-available is not set to nil, otherwise it
;; will fallback to defaults.
;;
;; You can also launch specs from Dired buffers, to do that, add this:
;;
;; (add-hook 'dired-mode-hook 'rspec-dired-mode)
;;
;; It has almost the same keybindings, but there's no toggle-spec
;; command, and `rspec-dired-verify-single' runs all marked files, or
;; the file at point.
;;
;; Dependencies:
;;
;; If `rspec-use-rvm` is set to true `rvm.el' is required.
;;
;;; Change Log:
;;
;; 1.21 - New option `rspec-docker-file-name'.
;; 1.20 - Fix a regression of `rspec-run-last-failed'
;; 1.19 - Fix bugs about change of buffer naming
;; 1.18 - Add `rspec-before-verification-hook' and `rspec-after-verification-hook'
;; hooks
;; 1.17 - Support for running multiple rspec processes at once
;; 1.16 - Add `rspec-yank-last-command' function (Sergiy Kukunin)
;; 1.15 - Add option to run spec commands in a Docker container
;; through "docker exec".
;; 1.14 - Add option to run spec commands in a Vagrant box through
;; "vagrant ssh -c".
;; 1.13 - Add a variable to autosave current buffer where it makes sense
;; 1.12 - Run specs for single method (Renan Ranelli)
;; 1.11 - Switching between method, its specs and back (Renan Ranelli)
;; 1.9 - Support for RSpec 3.
;; 1.8 - Support for Capybara's acceptance test DSL (Ales Guzik)
;; 1.7 - Support for Spring (Tomy Kaira)
;; - New commands: `rspec-verify-matching', `rspec-verify-continue'
;; (Jean-Louis Giordano)
;; - Run specs from Dired (Adam Sokolnicki)
;; - Include Yasnippet snippets collection (Dmitry Gutov)
;; 1.6 - Improved keymaps and compile buffer (Dmitry Gutov)
;; 1.5 - Allow key prefix to be customized (`rspec-key-command-prefix')
;; 1.4 - Allow .rspec/spec.opts files to be ignored (user option
;; `rspec-use-opts-file-when-available')
;; 1.3 - Bundler support (JD Huntington)
;; 1.2 - Rspec2 compatibility (Anantha Kumaran)
;; 1.1 - Run verification processes from project root directory (Joe Hirn)
;; 1.0 - Advance to end of compilation buffer even if it not the other window (byplayer)
;; 0.8 - RVM support (Peter Williams)
;; 0.7 - follow RoR conventions for file in lib directory (Tim Harper)
;; 0.6 - support for arbitrary spec and rake commands (David Yeu)
;; 0.5 - minor changes from Tim Harper
;; 0.4 - ansi colorization of compilation buffers (teaforthecat)
;; 0.3 - Dave Nolan implements respect for spec.opts config and
;; custom option to use 'rake spec' task or 'spec' command
;; 0.2 - Tim Harper implemented support for imenu to generate a basic
;; tag outline
;; 0.1 - Pezra's version in master
;;; Code:
(require 'ruby-mode)
(require 'ansi-color)
(require 'compile)
(require 'cl-lib)
(defvar rspec-verifiable-mode-keymap)
(define-prefix-command 'rspec-verifiable-mode-keymap)
(defvar rspec-mode-keymap)
(define-prefix-command 'rspec-mode-keymap)
(define-key rspec-verifiable-mode-keymap (kbd "v") 'rspec-verify)
(define-key rspec-verifiable-mode-keymap (kbd "a") 'rspec-verify-all)
(define-key rspec-verifiable-mode-keymap (kbd "t") 'rspec-toggle-spec-and-target)
(define-key rspec-verifiable-mode-keymap (kbd "e") 'rspec-toggle-spec-and-target-find-example)
(define-key rspec-verifiable-mode-keymap (kbd "4 t") 'rspec-find-spec-or-target-other-window)
(define-key rspec-verifiable-mode-keymap (kbd "4 e") 'rspec-find-spec-or-target-find-example-other-window)
(define-key rspec-verifiable-mode-keymap (kbd "r") 'rspec-rerun)
(define-key rspec-verifiable-mode-keymap (kbd "y") 'rspec-yank-last-command)
(define-key rspec-verifiable-mode-keymap (kbd "m") 'rspec-verify-matching)
(define-key rspec-verifiable-mode-keymap (kbd "c") 'rspec-verify-continue)
(define-key rspec-verifiable-mode-keymap (kbd "s") 'rspec-verify-method)
(define-key rspec-verifiable-mode-keymap (kbd "f") 'rspec-run-last-failed)
(set-keymap-parent rspec-mode-keymap rspec-verifiable-mode-keymap)
(define-key rspec-mode-keymap (kbd "s") 'rspec-verify-single)
(define-key rspec-mode-keymap (kbd "d") 'rspec-toggle-example-pendingness)
(defvar rspec-dired-mode-keymap)
(define-prefix-command 'rspec-dired-mode-keymap)
(define-key rspec-dired-mode-keymap (kbd "v") 'rspec-dired-verify)
(define-key rspec-dired-mode-keymap (kbd "s") 'rspec-dired-verify-single)
(define-key rspec-dired-mode-keymap (kbd "a") 'rspec-verify-all)
(define-key rspec-dired-mode-keymap (kbd "r") 'rspec-rerun)
(defgroup rspec-mode nil
"RSpec minor mode."
:group 'languages)
(defcustom rspec-use-rake-when-possible nil
"When non-nil and Rakefile is present, run specs via rake spec task."
:tag "RSpec runner command"
:type '(radio (const :tag "Use 'rake spec' task" t)
(const :tag "Use 'rspec' command" nil))
:group 'rspec-mode)
(defcustom rspec-rake-command "rake"
"The command for rake."
:type 'string
:group 'rspec-mode)
(defcustom rspec-spec-command "rspec"
"The command for spec."
:type 'string
:group 'rspec-mode)
(defcustom rspec-use-rvm nil
"When t, use RVM. Requires rvm.el."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-chruby nil
"When t, use chruby. Requires chruby.el."
:type 'boolean
:group 'rspec-mode)
(defvar rspec--docker-commands
'("docker exec"
"docker run"
"docker-compose exec"
"docker-compose run"
"nerdctl compose exec"
"nerdctl compose run"
"nerdctl exec"
"nerdctl run"
"podman exec"
"podman run"
"podman-compose exec"
"podman-compose run")
"List of acceptable docker commands to use.")
(defcustom rspec-docker-command "docker-compose run"
"Docker command to run."
:type 'string
:group 'rspec-mode
:safe (lambda (value)
(member value rspec--docker-commands)))
(defcustom rspec-docker-container "rspec-container-name"
"Name of the docker container to run rspec in."
:type 'string
:group 'rspec-mode
:safe 'stringp)
(defcustom rspec-docker-cwd "/app/"
"Working directory when running inside Docker. Use trailing slash."
:type 'string
:group 'rspec-mode)
(defcustom rspec-docker-wrapper-fn 'rspec--docker-default-wrapper
"Function for wrapping a command for execution inside a dockerized environment."
:type 'function
:group 'rspec-mode)
(defcustom rspec-docker-file-name "docker-compose.yml"
"File name to look for to determine whether to use Docker."
:type 'string
:group 'rspec-mode
:safe 'stringp)
(defcustom rspec-vagrant-cwd "/vagrant/"
"Working directory when running inside Vagrant. Use trailing slash."
:type 'string
:group 'rspec-mode)
(defcustom rspec-use-relative-path nil
"When t the file path will be relative."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-bundler-when-possible t
"When t and Gemfile is present, run specs with `bundle exec'.
Not used when running specs using Zeus or Spring."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-docker-when-possible nil
"When t and a file `rspec-docker-file-name' exists, run specs inside Docker.
The command that will be used is defined by `rspec-docker-command'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-vagrant-when-possible nil
"When t and Vagrant file is present, run specs inside Vagrant box.
Use shell command `vagrant ssh -c'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-zeus-when-possible t
"When t and .zeus.sock is present, run specs with `zeus'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-spring-when-possible t
"When t and spring.pid is present, run specs with `spring'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-use-opts-file-when-available t
"When t, RSpec should use .rspec/spec.opts."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-key-command-prefix (kbd "C-c ,")
"The prefix for all rspec related key commands."
:type 'string
:group 'rspec-mode)
(defcustom rspec-command-options "--format documentation"
"Default options used with rspec-command."
:type 'string
:group 'rspec-mode)
(defcustom rspec-snippets-fg-syntax nil
"Defines whether to use the full or concise FactoryGirl syntax in snippets.
When the value is neither `full', nor `concise', use the concise syntax if
there's an `include FactoryGirl::Syntax::Methods' statement in spec_helper."
:type '(choice
(const full)
(const concise)
(const nil))
:group 'rspec-mode)
(defcustom rspec-factory-gem 'factory-girl
"Defines whether to use the FactoryGirl or FactoryBot module in snippets."
:type '(choice
(const factory-girl)
(const factory-bot))
:group 'rspec-mode)
(defcustom rspec-compilation-skip-threshold 2
"Compilation motion commands skip less important messages.
The value can be either 2 -- skip anything less than error, 1 --
skip anything less than warning or 0 -- don't skip any messages.
Note that all messages not positively identified as warning or
info, are considered errors."
:type '(choice (const :tag "Skip warnings and info" 2)
(const :tag "Skip info" 1)
(const :tag "No skip" 0))
:group 'rspec-mode)
(defcustom rspec-expose-dsl-globally nil
"Defines whether the RSpec DSL is assumed to be exposed globally.
If t, prepend snippets at the top level with `RSpec.'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-primary-source-dirs '("app" "lib")
"Suppression list when searching for spec files.
List of directories whose names should be omitted when looking
for spec files corresponding to files inside them."
:type '(repeat string)
:safe 'listp
:group 'rspec-mode)
(defcustom rspec-autosave-buffer nil
"If t save the current buffer when running
`rspec-verify', `rspec-verify-single', `rspec-verify-matching',
`rspec-verify-continue' & `rspec-run-last-failed'."
:type 'boolean
:group 'rspec-mode)
(defcustom rspec-after-verification-hook nil
"Hooks run after `rspec-verify' and its variants.
They execute after failures have been stored in `rspec-last-failed-specs'."
:type 'hook
:group 'rspec-mode)
(defcustom rspec-before-verification-hook nil
"Hooks run before `rspec-verify' and its variants."
:type 'hook
:group 'rspec-mode)
(defcustom rspec-allow-multiple-compilation-buffers nil
"If t allows multiple RSpec to run in distinct compilation
buffers concurrently"
:type 'boolean
:group 'rspec-mode)
(declare-function yas-activate-extra-mode "yasnippet" (mode))
(declare-function yas-deactivate-extra-mode "yasnippet" (mode))
(declare-function yas-load-directory "yasnippet" (top-level-dir &optional use-jit interactive))
;;;###autoload
(define-minor-mode rspec-mode
"Minor mode for RSpec files
\\{rspec-mode-map}"
:lighter " RSpec" :keymap `((,rspec-key-command-prefix . rspec-mode-keymap))
(if rspec-mode
(progn
(rspec-set-imenu-generic-expression)
(when (fboundp 'yas-activate-extra-mode)
(yas-activate-extra-mode 'rspec-mode)))
(setq imenu-create-index-function 'ruby-imenu-create-index)
(setq imenu-generic-expression nil)
(when (fboundp 'yas-deactivate-extra-mode)
(yas-deactivate-extra-mode 'rspec-mode))))
;;;###autoload
(define-minor-mode rspec-verifiable-mode
"Minor mode for Ruby files that have specs
\\{rspec-verifiable-mode-map}"
:lighter "" :keymap `((,rspec-key-command-prefix . rspec-verifiable-mode-keymap)))
;;;###autoload
(define-minor-mode rspec-dired-mode
"Minor mode for Dired buffers with spec files
\\{rspec-dired-mode-map}"
:lighter "" :keymap `((,rspec-key-command-prefix . rspec-dired-mode-keymap)))
(defconst rspec-imenu-generic-expression
'(("Methods" "^\\s *def\\s +\\([^\(\n; ]+\\)" 1)
("Examples" "^\\( *\\(its?\\|specify\\|example\\|describe\\|context\\|feature\\|scenario\\) +.+\\)" 1))
"The imenu regex to parse an outline of the rspec file")
(defconst rspec-spec-file-name-re "\\(_\\|-\\)spec\\.rb\\'"
"The regex to identify spec files")
(defun rspec-set-imenu-generic-expression ()
(make-local-variable 'imenu-generic-expression)
(make-local-variable 'imenu-create-index-function)
(setq imenu-create-index-function 'imenu-default-create-index-function)
(setq imenu-generic-expression rspec-imenu-generic-expression))
(defvar rspec-snippets-dir
(let ((current (or load-file-name (buffer-file-name))))
(expand-file-name "snippets" (file-name-directory current)))
"The directory containing rspec snippets.")
(defun rspec-install-snippets ()
"Add `rspec-snippets-dir' to `yas-snippet-dirs' and load snippets from it."
(require 'yasnippet)
(defvar yas-snippet-dirs)
(add-to-list 'yas-snippet-dirs rspec-snippets-dir t)
(yas-load-directory rspec-snippets-dir))
(defun rspec-class-from-file-name ()
"Guess the name of the class the spec is for."
(let* ((name (file-relative-name (buffer-file-name)
(rspec-spec-directory (buffer-file-name))))
(rules `((,rspec-spec-file-name-re . "") ("/" . "::") ("_" . "")))
(class (capitalize name)))
(dolist (rule rules)
(setq class (replace-regexp-in-string (car rule) (cdr rule) class t t)))
class))
(defun rspec-type-from-file-name ()
"Guess the type of the spec is for."
(let* ((name (file-relative-name (buffer-file-name)
(rspec-spec-directory (buffer-file-name))))
(rules `(("/.*" . "") ("s$" . ""))))
(dolist (rule rules)
(setq name (replace-regexp-in-string (car rule) (cdr rule) name)))
name))
(defun rspec-top-level-desc-p ()
"Return t if point is on the first \"describe\" block opener."
(save-excursion
(save-restriction
(widen)
(beginning-of-line)
(not (catch 'found
(while (re-search-backward "\\_<\\(describe\\|feature\\)\\_>" nil t)
(unless (nth 8 (syntax-ppss))
(throw 'found t))))))))
(defun rspec-beginning-of-example ()
"Moves point to the beginning of the example in which the point current is."
(interactive)
(let ((start (point)))
(goto-char
(save-excursion
(end-of-line)
(unless (and (re-search-backward "^[[:space:]]*\\(it\\|scenario\\)[[:space:]]*(?[\"']" nil t)
(save-excursion (ruby-end-of-block) (< start (point))))
(error "Unable to find an example"))
(point)))))
(defun rspec-example-pending-p ()
"True if the example under point is pending. Otherwise false."
(interactive)
(save-excursion
(rspec-beginning-of-example)
(re-search-forward "^[[:space:]]*pending\\([[:space:](]\\|$\\)" (save-excursion (ruby-end-of-block) (point)) t)))
(defun rspec-toggle-example-pendingness ()
"Disable active examples and enables pending examples."
(interactive)
(if (rspec-example-pending-p)
(rspec-enable-example)
(rspec-disable-example)))
(defun rspec-disable-example ()
"Disable the example at point."
(interactive)
(when (not (rspec-example-pending-p))
(save-excursion
(rspec-beginning-of-example)
(end-of-line)
(insert "\npending")
(indent-for-tab-command))))
(defun rspec-enable-example ()
"Enable the example at point."
(interactive)
(when (rspec-example-pending-p)
(save-excursion
(rspec-beginning-of-example)
(re-search-forward "^[[:space:]]*pending\\([[:space:](]\\|$\\)"
(save-excursion (ruby-end-of-block) (point)))
(beginning-of-line)
(delete-region (save-excursion (beginning-of-line) (point))
(save-excursion (forward-line 1) (point))))))
(defun rspec-verify ()
"Run the specified spec, or the spec file for the current buffer."
(interactive)
(rspec--autosave-buffer-maybe)
(rspec-run-single-file (rspec-spec-file-for (buffer-file-name))
(rspec-core-options)))
(defun rspec-verify-matching ()
"Run the specs related to the current buffer.
This is more fuzzy that a simple verify."
(interactive)
(rspec--autosave-buffer-maybe)
(rspec-run-multiple-files (rspec-all-related-spec-files (buffer-file-name))
(rspec-core-options)))
(defvar rspec-last-failed-specs nil
"The file and line number of the specs that failed during the last run.")
(defun rspec-run-last-failed ()
"Run just the specs that failed during the last invocation."
(interactive)
(rspec--autosave-buffer-maybe)
(rspec-run-multiple-files rspec-last-failed-specs (rspec-core-options)))
(defun rspec-verify-continue ()
"Run the current spec file and the spec files located after it.
This is most useful in combination with the option `--fail-fast',
in long-running test suites."
(interactive)
(rspec--autosave-buffer-maybe)
(let ((current-spec-file (rspec-compress-spec-file
(rspec-spec-file-for (buffer-file-name)))))
(rspec-run-multiple-files
(cl-loop for file in (rspec-all-spec-files (buffer-file-name))
when (not (string-lessp file current-spec-file))
collect file)
(rspec-core-options))))
(defun rspec-verify-single ()
"Run the specified example at point."
(interactive)
(rspec--autosave-buffer-maybe)
(rspec-run-single-file
(cons
(rspec-spec-file-for (buffer-file-name))
(save-restriction
(widen)
(number-to-string (line-number-at-pos))))
(rspec-core-options)))
(declare-function dired-current-directory "dired")
(declare-function dired-get-marked-files "dired")
(defun rspec-dired-verify ()
"Run all specs in the current directory."
(interactive)
(rspec-run-single-file (dired-current-directory) (rspec-core-options)))
(defun rspec-dired-verify-single ()
"Run marked specs or spec at point (works with directories too)."
(interactive)
(rspec-compile (dired-get-marked-files)
(rspec-core-options)))
(defun rspec-verify-all ()
"Run the `spec' rake task for the project of the current file."
(interactive)
(rspec-run (rspec-core-options)))
(defun rspec-toggle-spec-and-target ()
"Switch to the spec or the target file for the current buffer.
If the current buffer is visiting a spec file, switches to the
target, otherwise the spec."
(interactive)
(find-file (rspec-spec-or-target)))
(defun rspec-verify-method ()
"Just like `rspec-verify-single' but tries to find examples for
the method at point."
(interactive)
(save-excursion
(when (rspec--toggle-spec-and-target-find-method
(lambda () (set-buffer (find-file-noselect (rspec-spec-or-target)))))
(rspec-verify-single))))
(defun rspec--toggle-spec-and-target-find-method (toggle-function)
(cl-labels
((get-spec-name ()
(save-excursion
(end-of-line)
(or
(re-search-backward "\\(?:describe\\|context\\)\s*(?[\s\n]*['\"][#\\.]\\([a-zA-Z_?!]*\\)['\"].*[\n\s)]* ?do" nil t)
(error "No method spec before point"))
(match-string 1)))
(get-method-name ()
(save-excursion
(end-of-line)
(or
(re-search-backward "def \\(?:self\\)?\\(.?[a-zA-Z_?!]+\\)" nil t)
(error "No method definition before point"))
(match-string 1))))
(let* ((spec-p (rspec-buffer-is-spec-p))
(target-regexp (if spec-p
(format "def \\(self\\)?\\.?%s" (regexp-quote (get-spec-name)))
(format "\\(describe\\|context\\)[\s(\n]+['\"]#?%s['\"]" (regexp-quote (get-method-name))))))
(funcall toggle-function)
(if (string-match-p target-regexp (buffer-string))
(progn
(goto-char (point-min))
(re-search-forward target-regexp))
(message "No matching %s" (if spec-p "method" "spec"))
nil))))
(defun rspec-toggle-spec-and-target-find-example ()
"Just like `rspec-toggle-spec-and-target' but tries to toggle between
the method and its corresponding examples."
(interactive)
(rspec--toggle-spec-and-target-find-method 'rspec-toggle-spec-and-target))
(defun rspec-find-spec-or-target-other-window ()
"Find in the other window the spec or the target file.
If the current buffer is visiting a spec file, finds the target,
otherwise the spec."
(interactive)
(find-file-other-window (rspec-spec-or-target)))
(defun rspec-find-spec-or-target-find-example-other-window ()
"Find in the other window the spec or the target file, and try
to navigate to the example or method corresponding to point."
(interactive)
(rspec--toggle-spec-and-target-find-method 'rspec-find-spec-or-target-other-window))
(defun rspec-spec-or-target ()
(if (rspec-buffer-is-spec-p)
(rspec-target-file-for (buffer-file-name))
(rspec-spec-file-for (buffer-file-name))))
(defun rspec-spec-file-for (a-file-name)
"Find spec for the specified file."
(if (rspec-spec-file-p a-file-name)
a-file-name
(let ((replace-regex (if (rspec-target-in-holder-dir-p a-file-name)
"^\\.\\./[^/]+/"
"^\\.\\./"))
(relative-file-name (file-relative-name a-file-name (rspec-spec-directory a-file-name))))
(rspec-specize-file-name (expand-file-name (replace-regexp-in-string replace-regex "" relative-file-name)
(rspec-spec-directory a-file-name))))))
(defun rspec-target-in-holder-dir-p (a-file-name)
(string-match (concat "^" (concat
(regexp-quote
(rspec-project-root a-file-name))
(regexp-opt rspec-primary-source-dirs)
"/"))
a-file-name))
(defun rspec-target-file-for (a-spec-file-name)
"Find the target for A-SPEC-FILE-NAME."
(cl-loop for extension in (list "rb" "rake")
for candidate = (rspec-targetize-file-name a-spec-file-name
extension)
for filename = (cl-loop for dir in (cons "."
rspec-primary-source-dirs)
for target = (replace-regexp-in-string
"/spec/"
(concat "/" dir "/")
candidate)
if (file-exists-p target)
return target)
if filename
return filename))
(defun rspec-specize-file-name (a-file-name)
"Return A-FILE-NAME but converted in to a spec file name."
(concat
(file-name-directory a-file-name)
(replace-regexp-in-string "\\(\\.\\(rb\\|rake\\)\\)?$" "_spec.rb" (file-name-nondirectory a-file-name))))
(defun rspec-targetize-file-name (a-file-name extension)
"Return A-FILE-NAME but converted into a non-spec file name with EXTENSION."
(concat (file-name-directory a-file-name)
(rspec-file-name-with-default-extension
(replace-regexp-in-string "_spec\\.rb" (concat "." extension)
(file-name-nondirectory a-file-name)))))
(defun rspec-file-name-with-default-extension (a-file-name)
"Add .rb file extension to A-FILE-NAME if it does not already have an extension."
(if (file-name-extension a-file-name)
a-file-name ;; file has a extension already so do nothing
(concat a-file-name ".rb")))
(defun rspec-parent-directory (a-directory)
"Returns the directory of which A-DIRECTORY is a child"
(file-name-directory (directory-file-name a-directory)))
(defun rspec-root-directory-p (a-directory)
"Return t if A-DIRECTORY is the root."
(equal a-directory (rspec-parent-directory a-directory)))
(defun rspec-spec-directory (a-file)
"Return the nearest spec directory that could contain specs for A-FILE."
(if (file-directory-p a-file)
(or
(car (directory-files a-file t "^spec$"))
(if (rspec-root-directory-p a-file)
nil
(rspec-spec-directory (rspec-parent-directory a-file))))
(rspec-spec-directory (rspec-parent-directory a-file))))
(defun rspec-all-related-spec-files (a-file)
(let* ((expected-name (file-name-nondirectory (rspec-spec-file-for a-file)))
(expected-spec-file (concat "/" expected-name)))
(cl-loop for file in (rspec-all-spec-files a-file)
when (string-match-p expected-spec-file file)
collect file)))
(defun rspec-all-files-under-directory (dir)
(let ((files (file-expand-wildcards (concat dir "/*") nil)))
(if (null files)
files
(delete-dups
(append files
(rspec-all-files-under-directory (concat dir "/*")))))))
(defun rspec-compress-spec-file (a-file)
(file-relative-name a-file (rspec-project-root)))
(defun rspec-all-spec-files (a-file)
(mapcar 'rspec-compress-spec-file
(sort (cl-loop for file in (rspec-all-files-under-directory
(rspec-spec-directory a-file))
when (rspec-spec-file-p file)
collect file)
'string-lessp)))
(defun rspec-spec-file-p (a-file-name)
"Return true if the specified A-FILE-NAME is a spec."
(numberp (string-match rspec-spec-file-name-re a-file-name)))
(defun rspec-core-options ()
"Return string of options that instructs spec to use options
file if it exists, or sensible defaults otherwise."
(cond ((and rspec-use-opts-file-when-available
(file-readable-p (rspec-spec-opts-file)))
(concat "--options " (rspec--shell-quote-local (rspec-spec-opts-file))))
(t rspec-command-options)))
(defun rspec-bundle-p ()
(and rspec-use-bundler-when-possible
(file-readable-p (concat (rspec-project-root) "Gemfile"))))
(defun rspec-docker-p ()
(and rspec-use-docker-when-possible
(file-readable-p (concat (rspec-project-root) rspec-docker-file-name))))
(defun rspec-vagrant-p ()
(and rspec-use-vagrant-when-possible
(file-readable-p (concat (rspec-project-root) "Vagrantfile"))))
(defun rspec-zeus-file-path ()
(or (getenv "ZEUSSOCK")
(concat (rspec-project-root) ".zeus.sock")))
(defun rspec-zeus-p ()
(and rspec-use-zeus-when-possible
(file-exists-p (rspec-zeus-file-path))))
(defun rspec-rake-p ()
(and rspec-use-rake-when-possible
;; Looks inefficient, but the calculation of the root is quite
;; fast. Unless this is used over TRAMP, I suppose.
(not (or (rspec-spring-p) (rspec-zeus-p)))
(file-exists-p (concat (rspec-project-root) "Rakefile"))))
(defun rspec-spring-p ()
(and rspec-use-spring-when-possible
(let ((root (directory-file-name (rspec-project-root))))
(or
;; Older versions
(file-exists-p (format "%s/tmp/spring/spring.pid" root))
;; 0.9.2+
(file-exists-p (format "%s/spring/%s.pid" temporary-file-directory (md5 root)))
;; 1.2.0+
(let* ((path (or (getenv "XDG_RUNTIME_DIR") temporary-file-directory))
(ruby-version (shell-command-to-string "ruby -e 'print RUBY_VERSION'"))
(application-id (md5 (concat ruby-version root))))
(or
(file-exists-p (format "%s/spring/%s.pid" path application-id))
;; 1.5.0+
(file-exists-p (format "%s/spring-%s/%s.pid" path (user-real-uid) application-id))))))))
(defun rspec2-p ()
(or (string-match "rspec" rspec-spec-command)
(file-readable-p (concat (rspec-project-root) ".rspec"))))
(defun rspec-spec-opts-file ()
"Return filename of spec opts file."
(if (rspec2-p)
(expand-file-name ".rspec" (rspec-project-root))
(expand-file-name "spec.opts" (rspec-spec-directory (rspec-project-root)))))
(defun rspec--shell-quote-local (file)
(let ((remote (file-remote-p file))
(docker (rspec-docker-p))
(vagrant (rspec-vagrant-p)))
(shell-quote-argument
(cond
(rspec-use-relative-path (file-relative-name file (rspec-project-root)))
(remote (substring file (length remote)))
(docker (replace-regexp-in-string (regexp-quote (rspec-project-root))
rspec-docker-cwd file))
(vagrant (replace-regexp-in-string (regexp-quote (rspec-project-root))
rspec-vagrant-cwd file))
(t file)))))
(defun rspec--docker-default-wrapper (docker-command docker-container command)
"Function for wrapping a command for execution inside a dockerized environment. "
(format "%s %s sh -c \"%s\"" docker-command docker-container command))
(defun rspec--docker-wrapper (command)
(if (rspec-docker-p)
(funcall rspec-docker-wrapper-fn
rspec-docker-command
rspec-docker-container
command)
command))
(defun rspec--vagrant-wrapper (command)
(if (rspec-vagrant-p)
(format "vagrant ssh -c 'cd %s; %s'"
(shell-quote-argument rspec-vagrant-cwd)
command)
command))
(defun rspec-runner ()
"Return command line to run rspec."
(let ((bundle-command (if (rspec-bundle-p) "bundle exec " ""))
(zeus-command (if (rspec-zeus-p) "zeus " nil))
(spring-command (if (rspec-spring-p) "spring " nil)))
(concat (or zeus-command spring-command bundle-command)
(if (rspec-rake-p)
(concat rspec-rake-command " spec")
rspec-spec-command))))
(defun rspec-runner-options (&optional opts)
"Return string of options from OPTS for command line."
(let ((opts (if (listp opts)
opts
(list opts)))
(use-rake (rspec-rake-p)))
(concat (when use-rake "SPEC_OPTS=\'")
(mapconcat 'identity opts " ")
(when use-rake "\'"))))
(defun rspec-runner-target (target)
"Processes TARGET to pass it to the runner.
TARGET can be a file, a directory, a list of such,
or a cons (FILE . LINE), to run one example."
(let ((use-rake (rspec-compile-target-use-rake target))
(specs (rspec-compile-target-specs target)))
(concat (when use-rake "SPEC=\'")
(mapconcat (lambda (s)
(concat (rspec--shell-quote-local (car s))
(and (cdr s)
(concat ":" (cdr s)))))
specs
" ")
(when use-rake "\'"))))
;;;###autoload
(defun rspec-buffer-is-spec-p ()
"Return true if the current buffer is a spec."
(and (buffer-file-name)
(rspec-spec-file-p (buffer-file-name))))
(defun rspec-run (&optional opts)
"Run spec with the specified options OPTS."
(rspec-compile (rspec-spec-directory (rspec-project-root))
opts))
(defun rspec-run-single-file (spec-file &rest opts)
"Run spec on SPEC-FILE with the specified options OPTS."
(rspec-compile spec-file opts))
(defun rspec-run-multiple-files (spec-files &rest opts)
"Run spec on a list of SPEC-FILES with the specified options OPTS."
(if (null spec-files)
(message "No spec files found!")
(rspec-compile spec-files opts)))
(defvar rspec-last-directory nil
"Directory the last spec process ran in.")
(defvar rspec-last-arguments nil
"Arguments passed to `rspec-compile' at the last invocation.")
(cl-defstruct rspec-compile-target
use-rake specs directory)
(defun rspec-rerun ()
"Re-run the last RSpec invocation."
(interactive)
(if (not rspec-last-directory)
(error "No previous verification")
(let ((default-directory rspec-last-directory))
(apply #'rspec-compile rspec-last-arguments))))
(defun rspec-yank-last-command ()
"Yank the last RSpec command to the clipboard."
(interactive)
(if (not rspec-last-directory)
(error "No previous verification")
(let ((default-directory rspec-last-directory))
(kill-new (apply #'rspec-compile-command rspec-last-arguments)))))
(declare-function rvm-activate-corresponding-ruby nil)
(declare-function chruby-use-corresponding nil)
(defun rspec-compile (target &optional opts)
"Run a compile for TARGET with the specified options OPTS."
(let ((compile-target (rspec-make-rspec-compile-target target)))
(setq rspec-last-directory default-directory
rspec-last-arguments (list compile-target opts))
(if rspec-use-rvm
(rvm-activate-corresponding-ruby))
(if rspec-use-chruby
(chruby-use-corresponding))
(let ((default-directory (or (rspec-project-root) default-directory))
(compilation-buffer-name-function (and rspec-allow-multiple-compilation-buffers
'rspec-compilation-buffer-name))
(process-environment (cons "RUBY_DEBUG_NO_RELINE=true"
process-environment)))
(setf (rspec-compile-target-directory compile-target) default-directory)
(compile
(rspec-compile-command compile-target opts)
'rspec-compilation-mode))))
(defun rspec-make-rspec-compile-target (target)
"Processes TARGET to pass it to the runner.
TARGET can be a file, a directory, a list of such,
or a cons (FILE . LINE), to run one example."
(if (rspec-compile-target-p target)
target
(make-rspec-compile-target
:use-rake (rspec-rake-p)
:specs (cond ((and (listp target) (listp (cdr target)))
(mapcar (lambda (f) (list f)) target))
((listp target)
(list target))
(t
(list (list target)))))))
(defun rspec-compile-command (target &optional opts)
"Composes RSpec command line for the compile function"
(rspec--vagrant-wrapper
(rspec--docker-wrapper
(mapconcat 'identity `(,(rspec-runner)
,(rspec-runner-options opts)
,(rspec-runner-target target)) " "))))
(defvar rspec-compilation-mode-font-lock-keywords
'((compilation--ensure-parse)
("^\\(Pending\\|Failures\\):$"
(0 font-lock-function-name-face))
("^[0-9]+ examples?, 0 failures.*$"
(0 compilation-info-face))
("^[0-9]+ examples?, \\([0-9]+ failures?\\)"
(1 compilation-error-face))))
(defvar rspec-compilation-error-regexp-alist-alist
'((rspec-capybara-html "^ +HTML screenshot: \\([0-9A-Za-z@_./\:-]+\\.html\\)" 1 nil nil 0 1)
(rspec-capybara-screenshot "^ +\\(Image \\)?\\[?[sS]creenshot\\]?: \\(.+\\.png\\)" 2 nil nil 0 2)
(rspec "^ +# \\([0-9A-Za-z@_./:-]+\\.rb\\):\\([0-9]+\\):in" 1 2 nil 2 1)
(rspec-pendings "^ +# \\([0-9A-Za-z@_./:-]+\\.rb\\):\\([0-9]+\\)" 1 2 nil 1 1)
(rspec-summary "^rspec \\([0-9A-Za-z@_./:-]+\\.rb\\):\\([0-9]+\\)" 1 2 nil 2 1)))
(defvar rspec-compilation-error-regexp-alist
(mapcar 'car rspec-compilation-error-regexp-alist-alist))
(define-compilation-mode rspec-compilation-mode "RSpec Compilation"
"Compilation mode for RSpec output."
(add-hook 'compilation-start-hook 'rspec-run-before-verification-hooks nil t)
(add-hook 'compilation-filter-hook 'rspec-colorize-compilation-buffer nil t)
(add-hook 'compilation-finish-functions 'rspec-store-failures nil t)
(add-hook 'compilation-finish-functions 'rspec-handle-error nil t)
(add-hook 'compilation-finish-functions 'rspec-run-after-verification-hooks t t))
(defun rspec-store-failures (&rest _)
"Store the file and line number of the failed examples from this run."
(let (failures)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^rspec \\([0-9A-Za-z@_./:-]+\\.rb:[0-9]+\\)" nil t)
(push (match-string-no-properties 1) failures)))
(setq rspec-last-failed-specs (reverse failures))))
(defun rspec-colorize-compilation-buffer ()
(ansi-color-apply-on-region compilation-filter-start (point)))
(defun rspec-handle-error (&rest _)
(save-excursion
(goto-char (point-max))
(when (save-excursion
(forward-line -10)
(search-forward "`+' for LL():Rake::Scope::EmptyScope" nil t))
(let ((inhibit-modification-hooks t)
(inhibit-read-only t)
(url "https://github.com/pezra/rspec-mode/issues/84"))
(insert (format "\n%s\n"
(propertize
"You seem to be using Rake 0.9. Rake 10 is recommended."
'font-lock-face 'error)))
(insert "See ")
(insert-text-button url 'type 'help-url 'help-args (list url))
(insert ".\n")))))
(defun rspec-run-after-verification-hooks (&rest _)
"Executes any functions in `rspec-after-verification-hook'"
(run-hooks 'rspec-after-verification-hook))
(defun rspec-run-before-verification-hooks (&rest _)
"Executes any functions in `rspec-before-verification-hook'"
(run-hooks 'rspec-before-verification-hook))
(defun rspec-project-root-directory-p (directory)
(or (file-regular-p (expand-file-name "Rakefile" directory))
(file-regular-p (expand-file-name "Gemfile" directory))
(file-regular-p (expand-file-name "Berksfile" directory))))
(defun rspec-project-root (&optional directory)
"Find the root directory of the project.
Walk the directory tree until it finds a rake file."
(let ((directory (file-name-as-directory (or directory default-directory))))
(cond ((rspec-root-directory-p directory)
(error "Could not determine the project root."))
((rspec-project-root-directory-p directory) (expand-file-name directory))