From 3828ea5b2655a03fe8de0332797386f42ae3b9ac Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 25 Jun 2017 21:33:15 +0100 Subject: Detect .git directories for finding the project root for C projects --- autoload/ale/c.vim | 11 ++- test/test_c_import_paths.vader | 102 +++++++++++++-------- .../git_and_nested_makefiles/include/test.h | 0 .../git_and_nested_makefiles/src/Makefile | 0 4 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 test/test_c_projects/git_and_nested_makefiles/include/test.h create mode 100644 test/test_c_projects/git_and_nested_makefiles/src/Makefile diff --git a/autoload/ale/c.vim b/autoload/ale/c.vim index 17d72605..4fe2f547 100644 --- a/autoload/ale/c.vim +++ b/autoload/ale/c.vim @@ -2,11 +2,18 @@ " Description: Functions for integrating with C-family linters. 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) 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 endfor diff --git a/test/test_c_import_paths.vader b/test/test_c_import_paths.vader index b867100a..a9206eda 100644 --- a/test/test_c_import_paths.vader +++ b/test/test_c_import_paths.vader @@ -19,11 +19,24 @@ After: unlet! g:dir 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): runtime! ale_linters/c/gcc.vim - cd test_c_projects/makefile_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/gcc.vim - cd test_c_projects/configure_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/gcc.vim - cd test_c_projects/h_file_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/gcc.vim - cd test_c_projects/hpp_file_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/clang.vim - cd test_c_projects/makefile_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/clang.vim - cd test_c_projects/h_file_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/clang.vim - cd test_c_projects/h_file_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/c/clang.vim - cd test_c_projects/hpp_file_project/subdir - silent noautocmd file file.c + call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.c') AssertEqual \ '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): runtime! ale_linters/cpp/gcc.vim - cd test_c_projects/makefile_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/gcc.vim - cd test_c_projects/configure_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/gcc.vim - cd test_c_projects/h_file_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/gcc.vim - cd test_c_projects/hpp_file_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/clang.vim - cd test_c_projects/makefile_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/clang.vim - cd test_c_projects/configure_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/configure_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/clang.vim - cd test_c_projects/h_file_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/h_file_project/subdir/file.cpp') AssertEqual \ '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): runtime! ale_linters/cpp/clang.vim - cd test_c_projects/hpp_file_project/subdir - silent noautocmd file file.cpp + call ale#test#SetFilename('test_c_projects/hpp_file_project/subdir/file.cpp') AssertEqual \ '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('')) +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): runtime! ale_linters/cpp/clangtidy.vim - cd test_c_projects/json_project/subdir - 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('')) + call ale#test#SetFilename('test_c_projects/json_project/subdir/file.cpp') 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('')) + +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 diff --git a/test/test_c_projects/git_and_nested_makefiles/include/test.h b/test/test_c_projects/git_and_nested_makefiles/include/test.h new file mode 100644 index 00000000..e69de29b diff --git a/test/test_c_projects/git_and_nested_makefiles/src/Makefile b/test/test_c_projects/git_and_nested_makefiles/src/Makefile new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3