Permalink
Browse files

Run specs directly from vim

* Run current spec file.
* Run spec nearest cursor.
* Run last spec that was run.
* Convert s alias to s script, use Zeus if present.
  • Loading branch information...
croaky committed Sep 29, 2012
1 parent 126344c commit 80b77cdefc75d271c7c41f82118a8cfea6642755
Showing with 49 additions and 1 deletion.
  1. +0 −1 aliases
  2. +10 −0 bin/s
  3. +39 −0 vimrc
View
@@ -20,7 +20,6 @@ alias b="bundle"
# Tests and Specs
alias t="ruby -I test"
-alias s="bundle exec rspec"
alias cuc="bundle exec cucumber"
# Rubygems
View
10 bin/s
@@ -0,0 +1,10 @@
+#!/bin/sh
+#
+# Run specs with great speed and daring-do.
+#
+
+if [ -f zeus.json ]; then

This comment has been minimized.

Show comment Hide comment
@doitian

doitian Jan 29, 2013

checking .zeus.sock is better, which is created by zeus start

@doitian

doitian Jan 29, 2013

checking .zeus.sock is better, which is created by zeus start

This comment has been minimized.

Show comment Hide comment
@doitian

doitian Jan 29, 2013

[ -S .zeus.sock ]

@doitian

doitian Jan 29, 2013

[ -S .zeus.sock ]

This comment has been minimized.

Show comment Hide comment
@jferris

jferris Jan 29, 2013

Member

Right you are. We've since replaced this with an rspec shell function and an s alias. The shell function checks for the socket: https://github.com/thoughtbot/dotfiles/blob/master/zsh/functions/rspec

@jferris

jferris Jan 29, 2013

Member

Right you are. We've since replaced this with an rspec shell function and an s alias. The shell function checks for the socket: https://github.com/thoughtbot/dotfiles/blob/master/zsh/functions/rspec

This comment has been minimized.

Show comment Hide comment
@jc00ke

jc00ke Jan 29, 2013

I was using the presence of the zeus command but I'll give detecting .zeus.sock a whirl.

@jc00ke

jc00ke Jan 29, 2013

I was using the presence of the zeus command but I'll give detecting .zeus.sock a whirl.

+ zeus rspec $1
+else
+ bundle exec rspec $1
+fi
View
39 vimrc
@@ -129,3 +129,42 @@ let g:html_indent_tags = 'li\|p'
" Markdown files end in .md
au BufRead,BufNewFile *.md set filetype=markdown
+
+" rspec mappings
+map <Leader>t :call RunCurrentSpecFile()<CR>
+map <Leader>s :call RunNearestSpec()<CR>
+map <Leader>l :call RunLastSpec()<CR>
+
+function! RunCurrentSpecFile()
+ if InSpecFile()
+ let l:command = "s " . @% . " -f documentation"

This comment has been minimized.

Show comment Hide comment
@jc00ke

jc00ke Jan 12, 2013

This works for you? I had to change this to just

let l:command = "s " . @%

Also had to change RunNearestSpec vvv

@jc00ke

jc00ke Jan 12, 2013

This works for you? I had to change this to just

let l:command = "s " . @%

Also had to change RunNearestSpec vvv

This comment has been minimized.

Show comment Hide comment
@croaky

croaky Feb 24, 2013

Contributor

This one does work for me the RunNearestSpec one appears to be broken.

@croaky

croaky Feb 24, 2013

Contributor

This one does work for me the RunNearestSpec one appears to be broken.

This comment has been minimized.

Show comment Hide comment
@gylaz

gylaz Feb 25, 2013

Contributor

with the latest zeus gem, i had to provide the flags before the file, like:

zeus rspec -f documentation spec/models/user.rb

Thus, to be compatible, this line could be:

let l:command = "s -fd " . @%

Note that -fd is the short-hand for -f documentation

@gylaz

gylaz Feb 25, 2013

Contributor

with the latest zeus gem, i had to provide the flags before the file, like:

zeus rspec -f documentation spec/models/user.rb

Thus, to be compatible, this line could be:

let l:command = "s -fd " . @%

Note that -fd is the short-hand for -f documentation

+ call SetLastSpecCommand(l:command)
+ call RunSpecs(l:command)
+ endif
+endfunction
+
+function! RunNearestSpec()
+ if InSpecFile()
+ let l:command = "s " . @% . " -l " . line(".") . " -f documentation"

This comment has been minimized.

Show comment Hide comment
@jc00ke

jc00ke Jan 12, 2013

Had to use what was documented in zeus source

let l:command = "s " . @% . ":" . line(".")
@jc00ke

jc00ke Jan 12, 2013

Had to use what was documented in zeus source

let l:command = "s " . @% . ":" . line(".")

This comment has been minimized.

Show comment Hide comment
@croaky

croaky Feb 24, 2013

Contributor

The semicolon is an improvement but there's another issue with the full path. I thought @% was the name relative to the current directory but I'm getting an absolute path:

s /Users/croaky/dev/bluesky/spec/models/category_spec.rb:19

No tests found on line 19.

Changing it manually to a relative path fixes it but now I need to translate that to vimscript:

s spec/models/category_spec.rb:19

Category.alphabetical
  sorts accounts by name
@croaky

croaky Feb 24, 2013

Contributor

The semicolon is an improvement but there's another issue with the full path. I thought @% was the name relative to the current directory but I'm getting an absolute path:

s /Users/croaky/dev/bluesky/spec/models/category_spec.rb:19

No tests found on line 19.

Changing it manually to a relative path fixes it but now I need to translate that to vimscript:

s spec/models/category_spec.rb:19

Category.alphabetical
  sorts accounts by name
+ call SetLastSpecCommand(l:command)
+ call RunSpecs(l:command)
+ endif
+endfunction
+
+function! RunLastSpec()
+ if exists("t:last_spec_command")
+ call RunSpecs(t:last_spec_command)
+ endif
+endfunction
+
+function! InSpecFile()
+ return match(expand("%"), "_spec.rb$") != -1
+endfunction
+
+function! SetLastSpecCommand(command)
+ let t:last_spec_command = a:command
+endfunction
+
+function! RunSpecs(command)
+ execute ":w\|!clear && echo " . a:command . " && echo && " . a:command
+endfunction

2 comments on commit 80b77cd

@datatrxfr

This comment has been minimized.

Show comment Hide comment
@datatrxfr

datatrxfr Mar 2, 2013

I imitated your guy's work here and it's working beautifully on two different machines, however one of them returns back to the file without waiting at the Press ENTER or type command to continue prompt. They both have the exact same .vimrc file. Any thoughts on what could be causing this? I'm not sure if these things matter or not but the other differences are Ubuntu 10.04 vs 12.04, and rspec 2.12.0 vs 2.13.0
In both environments running :! ls from inside vim does present and wait at the Press ENTER or type command to continue prompt.

I imitated your guy's work here and it's working beautifully on two different machines, however one of them returns back to the file without waiting at the Press ENTER or type command to continue prompt. They both have the exact same .vimrc file. Any thoughts on what could be causing this? I'm not sure if these things matter or not but the other differences are Ubuntu 10.04 vs 12.04, and rspec 2.12.0 vs 2.13.0
In both environments running :! ls from inside vim does present and wait at the Press ENTER or type command to continue prompt.

@mecampbellsoup

This comment has been minimized.

Show comment Hide comment
@mecampbellsoup

mecampbellsoup Aug 20, 2015

@croaky Sorry this comment is probably out of place in this conversation... Have you ever come across a 'load order' bug when using Zeus with a Rails 3.x project? Recently I was running tests in Zeus that threw an exception seemingly because ActiveRecord hadn't yet been loaded, so one of my model factories was expecting a method from the ActiveRecord::Base superclass but instead the user.superclass returned Object.

@croaky Sorry this comment is probably out of place in this conversation... Have you ever come across a 'load order' bug when using Zeus with a Rails 3.x project? Recently I was running tests in Zeus that threw an exception seemingly because ActiveRecord hadn't yet been loaded, so one of my model factories was expecting a method from the ActiveRecord::Base superclass but instead the user.superclass returned Object.

Please sign in to comment.