Skip to content

Instantly share code, notes, and snippets.

@lkosak
Created May 15, 2012 07:15
Show Gist options
  • Save lkosak/2699716 to your computer and use it in GitHub Desktop.
Save lkosak/2699716 to your computer and use it in GitHub Desktop.
Run them rails tests from vim!
function! RunRailsTest()
let file = expand("%")
let basepath = getcwd()
let in_test_file = match(file, '\(.feature\|_spec.rb\|_test.rb\)$') != -1
if !in_test_file
" Come up with a best guess search path
let search_fname = fnamemodify(file, ":s?^app/??:s?.rb$?_spec.rb?")
" Search for that shit
let result = globpath(basepath."/spec", "**/".search_fname)
if empty(result)
echo "Couldn't locate test file"
return
end
" Get first match (globpath returns a \n separated list)
let file = split(result, "\n")[0]
" Make it relative to the current dir (for display purposes)
let file = fnamemodify(file, ":.")
end
exec ":!clear && echo 'Running ".file."' && ruby " . file
endfunction
nmap <leader>r :call RunRailsTest()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment