From 654a1724730e181c80cd262a781055c5a9f0ca05 Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 17 Oct 2016 21:14:34 +0100 Subject: [PATCH] Make the nearest file search result absolute paths, and add tests to cover it. --- autoload/ale/util.vim | 10 +++++++++- test/test_nearest_file_search.vader | 11 +++++++++++ test/top/example.ini | 0 test/top/middle/bottom/dummy.txt | 0 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/test_nearest_file_search.vader create mode 100644 test/top/example.ini create mode 100644 test/top/middle/bottom/dummy.txt diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index c218f09..82bed85 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -33,7 +33,15 @@ endfunction " Given a buffer and a filename, find the nearest file by searching upwards " through the paths relative to the given buffer. function! ale#util#FindNearestFile(buffer, filename) abort - return findfile(a:filename, fnamemodify(bufname(a:buffer), ':p') . ';') + let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') + + let l:relative_path = findfile(a:filename, l:buffer_filename . ';') + + if !empty(l:relative_path) + return fnamemodify(l:relative_path, ':p') + endif + + return '' endfunction function! ale#util#GetFunction(string_or_ref) abort diff --git a/test/test_nearest_file_search.vader b/test/test_nearest_file_search.vader new file mode 100644 index 0000000..c59bfce --- /dev/null +++ b/test/test_nearest_file_search.vader @@ -0,0 +1,11 @@ +Execute(Open a file some directory down): + silent! cd /testplugin/test + :e! top/middle/bottom/dummy.txt + +Then(We should be able to find a configuration file further up): + AssertEqual expand('%:p:h:h:h:h') . '/top/example.ini', ale#util#FindNearestFile(bufnr('%'), 'example.ini') + +Execute(Do nothing): + +Then(We shouldn't find anything for files which don't match): + AssertEqual '', ale#util#FindNearestFile(bufnr('%'), 'cantfindthis') diff --git a/test/top/example.ini b/test/top/example.ini new file mode 100644 index 0000000..e69de29 diff --git a/test/top/middle/bottom/dummy.txt b/test/top/middle/bottom/dummy.txt new file mode 100644 index 0000000..e69de29