« Back to Index

Trying to run RSpec and Cucumber tests via Vim but found executing commands in different contexts means scope changes between what’s available in the $PATH to the installed Ruby version and it’s available gemsets.

View original Gist on GitHub

Run Tests via.vim

" Run currently open test file
" Bastardised from https://github.com/r00k/dotfiles/blob/master/vimrc#L245
function! SetTestFile()
    let g:bjo_test_file=@%
endfunction

function! SetTestFileLineNumber()
    let g:bjo_test_file_line=line(".")
endfunction

function! SetTestRunner(runner)
    let g:bjo_test_runner=a:runner
endfunction

function! RunCurrentTestFile()
    let in_a_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1

    if in_a_test_file
        call SetTestFile()

        if match(expand('%'), '\.feature$') != -1
            call SetTestRunner("!/Users/markmcdonnell/.gem/ruby/2.0.0/bin/cucumber")
            exec g:bjo_test_runner g:bjo_test_file
        elseif match(expand('%'), '_spec\.rb$') != -1
            call SetTestRunner("!/Users/markmcdonnell/.gem/ruby/2.0.0/bin/rspec")
        endif
    endif
endfunction

function! RunCurrentLineInTestFile()
    let in_a_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1

    if in_a_test_file
        call SetTestFileLineNumber()
    endif

    exec "!/Users/markmcdonnell/.gem/ruby/2.0.0/bin/rspec" g:bjo_test_file . ":" . g:bjo_test_file_line
endfunction