Detect .git directories for finding the project root for C projects

This commit is contained in:
w0rp 2017-06-25 21:33:15 +01:00
parent 8b557f346c
commit 3828ea5b26
4 changed files with 71 additions and 42 deletions

View File

@ -2,11 +2,18 @@
" Description: Functions for integrating with C-family linters. " Description: Functions for integrating with C-family linters.
function! ale#c#FindProjectRoot(buffer) abort function! ale#c#FindProjectRoot(buffer) abort
for l:project_filename in ['configure', 'Makefile', 'CMakeLists.txt'] for l:project_filename in ['.git/HEAD', 'configure', 'Makefile', 'CMakeLists.txt']
let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename) let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename)
if !empty(l:full_path) if !empty(l:full_path)
return fnamemodify(l:full_path, ':h') let l:path = fnamemodify(l:full_path, ':h')
" Correct .git path detection.
if fnamemodify(l:path, ':t') ==# '.git'
let l:path = fnamemodify(l:path, ':h')
endif
return l:path
endif endif
endfor endfor

View File

@ -19,11 +19,24 @@ After:
unlet! g:dir unlet! g:dir
call ale#linter#Reset() call ale#linter#Reset()
" Run this only once for this series of tests. The cleanup Execute step
" will run at the bottom of this file.
"
" We need to move .git/HEAD away so we don't match it, as we need to test
" functions which look for .git/HEAD.
Execute(Move .git/HEAD to a temp dir):
let g:temp_head_filename = tempname()
let g:head_filename = findfile('.git/HEAD', ';')
if !empty(g:head_filename)
call writefile(readfile(g:head_filename, 'b'), g:temp_head_filename, 'b')
call delete(g:head_filename)
endif
Execute(The C GCC handler should include 'include' directories for projects with a Makefile): Execute(The C GCC handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
cd test_c_projects/makefile_project/subdir call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'gcc -S -x c -fsyntax-only ' \ 'gcc -S -x c -fsyntax-only '
@ -35,8 +48,7 @@ Execute(The C GCC handler should include 'include' directories for projects with
Execute(The C GCC handler should include 'include' directories for projects with a configure file): Execute(The C GCC handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
cd test_c_projects/configure_project/subdir call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'gcc -S -x c -fsyntax-only ' \ 'gcc -S -x c -fsyntax-only '
@ -48,8 +60,7 @@ Execute(The C GCC handler should include 'include' directories for projects with
Execute(The C GCC handler should include root directories for projects with .h files in them): Execute(The C GCC handler should include root directories for projects with .h files in them):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
cd test_c_projects/h_file_project/subdir call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'gcc -S -x c -fsyntax-only ' \ 'gcc -S -x c -fsyntax-only '
@ -61,8 +72,7 @@ Execute(The C GCC handler should include root directories for projects with .h f
Execute(The C GCC handler should include root directories for projects with .hpp files in them): Execute(The C GCC handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
cd test_c_projects/hpp_file_project/subdir call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'gcc -S -x c -fsyntax-only ' \ 'gcc -S -x c -fsyntax-only '
@ -74,8 +84,7 @@ Execute(The C GCC handler should include root directories for projects with .hpp
Execute(The C Clang handler should include 'include' directories for projects with a Makefile): Execute(The C Clang handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
cd test_c_projects/makefile_project/subdir call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'clang -S -x c -fsyntax-only ' \ 'clang -S -x c -fsyntax-only '
@ -87,8 +96,7 @@ Execute(The C Clang handler should include 'include' directories for projects wi
Execute(The C Clang handler should include 'include' directories for projects with a configure file): Execute(The C Clang handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
cd test_c_projects/h_file_project/subdir call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'clang -S -x c -fsyntax-only ' \ 'clang -S -x c -fsyntax-only '
@ -100,8 +108,7 @@ Execute(The C Clang handler should include 'include' directories for projects wi
Execute(The C Clang handler should include root directories for projects with .h files in them): Execute(The C Clang handler should include root directories for projects with .h files in them):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
cd test_c_projects/h_file_project/subdir call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'clang -S -x c -fsyntax-only ' \ 'clang -S -x c -fsyntax-only '
@ -113,8 +120,7 @@ Execute(The C Clang handler should include root directories for projects with .h
Execute(The C Clang handler should include root directories for projects with .hpp files in them): Execute(The C Clang handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
cd test_c_projects/hpp_file_project/subdir call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.c')
silent noautocmd file file.c
AssertEqual AssertEqual
\ 'clang -S -x c -fsyntax-only ' \ 'clang -S -x c -fsyntax-only '
@ -126,8 +132,7 @@ Execute(The C Clang handler should include root directories for projects with .h
Execute(The C++ GCC handler should include 'include' directories for projects with a Makefile): Execute(The C++ GCC handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/cpp/gcc.vim runtime! ale_linters/cpp/gcc.vim
cd test_c_projects/makefile_project/subdir call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'gcc -S -x c++ -fsyntax-only ' \ 'gcc -S -x c++ -fsyntax-only '
@ -139,8 +144,7 @@ Execute(The C++ GCC handler should include 'include' directories for projects wi
Execute(The C++ GCC handler should include 'include' directories for projects with a configure file): Execute(The C++ GCC handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/cpp/gcc.vim runtime! ale_linters/cpp/gcc.vim
cd test_c_projects/configure_project/subdir call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'gcc -S -x c++ -fsyntax-only ' \ 'gcc -S -x c++ -fsyntax-only '
@ -152,8 +156,7 @@ Execute(The C++ GCC handler should include 'include' directories for projects wi
Execute(The C++ GCC handler should include root directories for projects with .h files in them): Execute(The C++ GCC handler should include root directories for projects with .h files in them):
runtime! ale_linters/cpp/gcc.vim runtime! ale_linters/cpp/gcc.vim
cd test_c_projects/h_file_project/subdir call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'gcc -S -x c++ -fsyntax-only ' \ 'gcc -S -x c++ -fsyntax-only '
@ -165,8 +168,7 @@ Execute(The C++ GCC handler should include root directories for projects with .h
Execute(The C++ GCC handler should include root directories for projects with .hpp files in them): Execute(The C++ GCC handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/cpp/gcc.vim runtime! ale_linters/cpp/gcc.vim
cd test_c_projects/hpp_file_project/subdir call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'gcc -S -x c++ -fsyntax-only ' \ 'gcc -S -x c++ -fsyntax-only '
@ -178,8 +180,7 @@ Execute(The C++ GCC handler should include root directories for projects with .h
Execute(The C++ Clang handler should include 'include' directories for projects with a Makefile): Execute(The C++ Clang handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/cpp/clang.vim runtime! ale_linters/cpp/clang.vim
cd test_c_projects/makefile_project/subdir call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'clang++ -S -x c++ -fsyntax-only ' \ 'clang++ -S -x c++ -fsyntax-only '
@ -191,8 +192,7 @@ Execute(The C++ Clang handler should include 'include' directories for projects
Execute(The C++ Clang handler should include 'include' directories for projects with a configure file): Execute(The C++ Clang handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/cpp/clang.vim runtime! ale_linters/cpp/clang.vim
cd test_c_projects/configure_project/subdir call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'clang++ -S -x c++ -fsyntax-only ' \ 'clang++ -S -x c++ -fsyntax-only '
@ -204,8 +204,7 @@ Execute(The C++ Clang handler should include 'include' directories for projects
Execute(The C++ Clang handler should include root directories for projects with .h files in them): Execute(The C++ Clang handler should include root directories for projects with .h files in them):
runtime! ale_linters/cpp/clang.vim runtime! ale_linters/cpp/clang.vim
cd test_c_projects/h_file_project/subdir call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'clang++ -S -x c++ -fsyntax-only ' \ 'clang++ -S -x c++ -fsyntax-only '
@ -217,8 +216,7 @@ Execute(The C++ Clang handler should include root directories for projects with
Execute(The C++ Clang handler should include root directories for projects with .hpp files in them): Execute(The C++ Clang handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/cpp/clang.vim runtime! ale_linters/cpp/clang.vim
cd test_c_projects/hpp_file_project/subdir call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.cpp')
silent noautocmd file file.cpp
AssertEqual AssertEqual
\ 'clang++ -S -x c++ -fsyntax-only ' \ 'clang++ -S -x c++ -fsyntax-only '
@ -227,17 +225,41 @@ Execute(The C++ Clang handler should include root directories for projects with
\ . ' -' \ . ' -'
\ , ale_linters#cpp#clang#GetCommand(bufnr('')) \ , ale_linters#cpp#clang#GetCommand(bufnr(''))
Execute(The C++ Clang handler shoud use the include directory based on the .git location):
runtime! ale_linters/cpp/clang.vim
if !isdirectory(g:dir . '/test_c_projects/git_and_nested_makefiles/.git')
call mkdir(g:dir . '/test_c_projects/git_and_nested_makefiles/.git')
endif
if !filereadable(g:dir . '/test_c_projects/git_and_nested_makefiles/.git/HEAD')
call writefile([], g:dir . '/test_c_projects/git_and_nested_makefiles/.git/HEAD')
endif
call ale#test#SetFilename('test_c_projects/git_and_nested_makefiles/src/file.cpp')
AssertEqual
\ 'clang++ -S -x c++ -fsyntax-only '
\ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/git_and_nested_makefiles/src') . ' '
\ . ' -I' . ale#Escape(g:dir . '/test_c_projects/git_and_nested_makefiles/include') . ' '
\ . ' -'
\ , ale_linters#cpp#clang#GetCommand(bufnr(''))
Execute(The C++ ClangTidy handler should include json folders for projects with suitable build directory in them): Execute(The C++ ClangTidy handler should include json folders for projects with suitable build directory in them):
runtime! ale_linters/cpp/clangtidy.vim runtime! ale_linters/cpp/clangtidy.vim
cd test_c_projects/json_project/subdir call ale#test#SetFilename('test_c_projects/json_project/subdir/file.cpp')
silent noautocmd file file.cpp
" TODO Test to move to C-family tools tests
" AssertEqual
" \ '/testplugin/test/test_c_projects/json_project/build'
" \ , ale#c#FindCompileCommands(bufnr(''))
AssertEqual AssertEqual
\ 'clang-tidy -checks=''*'' %s -p ''/testplugin/test/test_c_projects/json_project/build''' \ 'clang-tidy -checks=''*'' %s '
\ . '-p ' . ale#Escape(g:dir . '/test_c_projects/json_project/build')
\ , ale_linters#cpp#clangtidy#GetCommand(bufnr('')) \ , ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(Move .git/HEAD back):
if !empty(g:head_filename)
call writefile(readfile(g:temp_head_filename, 'b'), g:head_filename, 'b')
call delete(g:temp_head_filename)
endif
unlet! g:temp_head_filename
unlet! g:head_filename