Skip to content

Instantly share code, notes, and snippets.

@gabrielelana
Created May 9, 2013 12:54
Show Gist options
  • Save gabrielelana/5547281 to your computer and use it in GitHub Desktop.
Save gabrielelana/5547281 to your computer and use it in GitHub Desktop.
Local .vimrc for a php project, run/open current test
" autoload the local .vimrc file you need to have
" https://github.com/MarcWeber/vim-addon-local-vimrc
" plugin installed
map <Leader>t :call RunCurrentTest()<CR>
map <Leader>o :call OpenCurrentTest()<CR>
function! RunCurrentTest()
let l:current_file=expand("%:p")
let l:test_file="nothing"
if match(l:current_file, "Test\.php$") != -1
let l:test_file=l:current_file
else
let l:test_file=SearchForRelatedTestFile(l:current_file)
endif
if l:test_file != "nothing"
exec "!phpunit" l:test_file
else
echo "sorry, nothing to run :-("
endif
endfunction
function! OpenCurrentTest()
let l:current_file=expand("%:p")
let l:test_file="nothing"
let l:test_file=SearchForRelatedTestFile(l:current_file)
if l:test_file != "nothing"
exec ":belowright :split " l:test_file
else
echo "sorry, nothing to open :-("
endif
endfunction
function! SearchForRelatedTestFile(file_path)
let l:file_name=fnamemodify(a:file_path, ":t")
let l:test_file_name=fnamemodify(l:file_name, ":r") . "Test.php"
let l:project_root_path=ProjectRootGuess(a:file_path)
let l:found=system("find '".l:project_root_path."/spec' -name '".l:test_file_name."'")
let l:number_of_file_founds=strlen(substitute(l:found, "[^\n]", "", "g"))
if l:number_of_file_founds == 1
return l:found
endif
endfunction
function! ProjectRootGuess(file_path)
for l:marker in [".git", ".vimrc"]
let l:result=""
let l:pivot=a:file_path
while l:pivot!=fnamemodify(l:pivot, ":h")
let l:pivot=fnamemodify(l:pivot, ":h")
if len(glob(l:pivot."/".l:marker))
let l:result=l:pivot
endif
endwhile
if len(l:result)
return l:result
endif
endfor
return filereadable(a:file_path) ? fnamemodify(a:file_path, ":h") : a:file_path
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment