aboutsummaryrefslogtreecommitdiff
path: root/test/unit/path_spec.lua
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-12-03 16:49:30 +0300
committerZyX <kp-pav@yandex.ru>2017-12-03 16:49:30 +0300
commitc49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57 (patch)
treeb7e59c416d1435725c65f8952b6e55c70544d97e /test/unit/path_spec.lua
parent62108c3b0be46936c83f6d4c98b44ceb5e6f77fd (diff)
parent27a577586eace687c47e7398845178208cae524a (diff)
downloadrneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.tar.gz
rneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.tar.bz2
rneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.zip
Merge branch 'master' into s-dash-stdin
Diffstat (limited to 'test/unit/path_spec.lua')
-rw-r--r--test/unit/path_spec.lua260
1 files changed, 175 insertions, 85 deletions
diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua
index 470f971e68..ed597eaed7 100644
--- a/test/unit/path_spec.lua
+++ b/test/unit/path_spec.lua
@@ -13,12 +13,12 @@ local OK = helpers.OK
local FAIL = helpers.FAIL
cimport('string.h')
-local path = cimport('./src/nvim/path.h')
+local cimp = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
local length = 0
local buffer = nil
-describe('path function', function()
+describe('path.c', function()
describe('path_full_dir_name', function()
setup(function()
lfs.mkdir('unit-test-directory')
@@ -30,7 +30,7 @@ describe('path function', function()
local function path_full_dir_name(directory, buf, len)
directory = to_cstr(directory)
- return path.path_full_dir_name(directory, buf, len)
+ return cimp.path_full_dir_name(directory, buf, len)
end
before_each(function()
@@ -69,7 +69,7 @@ describe('path function', function()
local function path_full_compare(s1, s2, cn)
s1 = to_cstr(s1)
s2 = to_cstr(s2)
- return path.path_full_compare(s1, s2, cn or 0)
+ return cimp.path_full_compare(s1, s2, cn or 0)
end
local f1 = 'f1.o'
@@ -86,31 +86,31 @@ describe('path function', function()
end)
itp('returns kEqualFiles when passed the same file', function()
- eq(path.kEqualFiles, (path_full_compare(f1, f1)))
+ eq(cimp.kEqualFiles, (path_full_compare(f1, f1)))
end)
itp('returns kEqualFileNames when files that dont exist and have same name', function()
- eq(path.kEqualFileNames, (path_full_compare('null.txt', 'null.txt', true)))
+ eq(cimp.kEqualFileNames, (path_full_compare('null.txt', 'null.txt', true)))
end)
itp('returns kBothFilesMissing when files that dont exist', function()
- eq(path.kBothFilesMissing, (path_full_compare('null.txt', 'null.txt')))
+ eq(cimp.kBothFilesMissing, (path_full_compare('null.txt', 'null.txt')))
end)
itp('returns kDifferentFiles when passed different files', function()
- eq(path.kDifferentFiles, (path_full_compare(f1, f2)))
- eq(path.kDifferentFiles, (path_full_compare(f2, f1)))
+ eq(cimp.kDifferentFiles, (path_full_compare(f1, f2)))
+ eq(cimp.kDifferentFiles, (path_full_compare(f2, f1)))
end)
itp('returns kOneFileMissing if only one does not exist', function()
- eq(path.kOneFileMissing, (path_full_compare(f1, 'null.txt')))
- eq(path.kOneFileMissing, (path_full_compare('null.txt', f1)))
+ eq(cimp.kOneFileMissing, (path_full_compare(f1, 'null.txt')))
+ eq(cimp.kOneFileMissing, (path_full_compare('null.txt', f1)))
end)
end)
describe('path_tail', function()
local function path_tail(file)
- local res = path.path_tail((to_cstr(file)))
+ local res = cimp.path_tail((to_cstr(file)))
neq(NULL, res)
return ffi.string(res)
end
@@ -126,7 +126,7 @@ describe('path function', function()
describe('path_tail_with_sep', function()
local function path_tail_with_sep(file)
- local res = path.path_tail_with_sep((to_cstr(file)))
+ local res = cimp.path_tail_with_sep((to_cstr(file)))
neq(NULL, res)
return ffi.string(res)
end
@@ -159,11 +159,11 @@ describe('path function', function()
-- strcmp.
local function invocation_path_tail(invk)
local plen = ffi.new('size_t[?]', 1)
- local ptail = path.invocation_path_tail((to_cstr(invk)), plen)
+ local ptail = cimp.invocation_path_tail((to_cstr(invk)), plen)
neq(NULL, ptail)
-- it does not change the output if len==NULL
- local tail2 = path.invocation_path_tail((to_cstr(invk)), NULL)
+ local tail2 = cimp.invocation_path_tail((to_cstr(invk)), NULL)
neq(NULL, tail2)
eq((ffi.string(ptail)), (ffi.string(tail2)))
return ptail, plen[0]
@@ -204,7 +204,7 @@ describe('path function', function()
end)
itp('is equivalent to path_tail when args do not contain a path separator', function()
- local ptail = path.path_tail(to_cstr("a/b/c x y z"))
+ local ptail = cimp.path_tail(to_cstr("a/b/c x y z"))
neq(NULL, ptail)
local tail = ffi.string(ptail)
local invk, _ = invocation_path_tail("a/b/c x y z")
@@ -212,7 +212,7 @@ describe('path function', function()
end)
itp('is not equivalent to path_tail when args contain a path separator', function()
- local ptail = path.path_tail(to_cstr("a/b/c x y/z"))
+ local ptail = cimp.path_tail(to_cstr("a/b/c x y/z"))
neq(NULL, ptail)
local invk, _ = invocation_path_tail("a/b/c x y/z")
neq((ffi.string(ptail)), (ffi.string(invk)))
@@ -221,7 +221,7 @@ describe('path function', function()
describe('path_next_component', function()
local function path_next_component(file)
- local res = path.path_next_component((to_cstr(file)))
+ local res = cimp.path_next_component((to_cstr(file)))
neq(NULL, res)
return ffi.string(res)
end
@@ -238,25 +238,25 @@ describe('path function', function()
describe('path_shorten_fname', function()
itp('returns NULL if `full_path` is NULL', function()
local dir = to_cstr('some/directory/file.txt')
- eq(NULL, (path.path_shorten_fname(NULL, dir)))
+ eq(NULL, (cimp.path_shorten_fname(NULL, dir)))
end)
itp('returns NULL if the path and dir does not match', function()
local dir = to_cstr('not/the/same')
local full = to_cstr('as/this.txt')
- eq(NULL, (path.path_shorten_fname(full, dir)))
+ eq(NULL, (cimp.path_shorten_fname(full, dir)))
end)
itp('returns NULL if the path is not separated properly', function()
local dir = to_cstr('some/very/long/')
local full = to_cstr('some/very/long/directory/file.txt')
- eq(NULL, (path.path_shorten_fname(full, dir)))
+ eq(NULL, (cimp.path_shorten_fname(full, dir)))
end)
itp('shortens the filename if `dir_name` is the start of `full_path`', function()
local full = to_cstr('some/very/long/directory/file.txt')
local dir = to_cstr('some/very/long')
- eq('directory/file.txt', (ffi.string(path.path_shorten_fname(full, dir))))
+ eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
end)
end)
end)
@@ -277,23 +277,76 @@ describe('path_shorten_fname_if_possible', function()
itp('returns shortened path if possible', function()
lfs.chdir('ut_directory')
local full = to_cstr(lfs.currentdir() .. '/subdir/file.txt')
- eq('subdir/file.txt', (ffi.string(path.path_shorten_fname_if_possible(full))))
+ eq('subdir/file.txt', (ffi.string(cimp.path_shorten_fname_if_possible(full))))
end)
itp('returns `full_path` if a shorter version is not possible', function()
local old = lfs.currentdir()
lfs.chdir('ut_directory')
local full = old .. '/subdir/file.txt'
- eq(full, (ffi.string(path.path_shorten_fname_if_possible(to_cstr(full)))))
+ eq(full, (ffi.string(cimp.path_shorten_fname_if_possible(to_cstr(full)))))
end)
itp('returns NULL if `full_path` is NULL', function()
- eq(NULL, (path.path_shorten_fname_if_possible(NULL)))
+ eq(NULL, (cimp.path_shorten_fname_if_possible(NULL)))
end)
end)
end)
-describe('more path function', function()
+describe('path.c path_guess_exepath', function()
+ local cwd = lfs.currentdir()
+
+ for _,name in ipairs({'./nvim', '.nvim', 'foo/nvim'}) do
+ itp('"'..name..'" returns name catenated with CWD', function()
+ local bufsize = 255
+ local buf = cstr(bufsize, '')
+ cimp.path_guess_exepath(name, buf, bufsize)
+ eq(cwd..'/'..name, ffi.string(buf))
+ end)
+ end
+
+ itp('absolute path returns the name unmodified', function()
+ local name = '/foo/bar/baz'
+ local bufsize = 255
+ local buf = cstr(bufsize, '')
+ cimp.path_guess_exepath(name, buf, bufsize)
+ eq(name, ffi.string(buf))
+ end)
+
+ itp('returns the name unmodified if not found in $PATH', function()
+ local name = '23u0293_not_in_path'
+ local bufsize = 255
+ local buf = cstr(bufsize, '')
+ cimp.path_guess_exepath(name, buf, bufsize)
+ eq(name, ffi.string(buf))
+ end)
+
+ itp('does not crash if $PATH item exceeds MAXPATHL', function()
+ local orig_path_env = os.getenv('PATH')
+ local name = 'cat' -- Some executable in $PATH.
+ local bufsize = 255
+ local buf = cstr(bufsize, '')
+ local insane_path = orig_path_env..':'..(("x/"):rep(4097))
+
+ cimp.os_setenv('PATH', insane_path, true)
+ cimp.path_guess_exepath(name, buf, bufsize)
+ eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
+
+ -- Restore $PATH.
+ cimp.os_setenv('PATH', orig_path_env, true)
+ end)
+
+ itp('returns full path found in $PATH', function()
+ local name = 'cat' -- Some executable in $PATH.
+ local bufsize = 255
+ local buf = cstr(bufsize, '')
+ cimp.path_guess_exepath(name, buf, bufsize)
+ -- Usually "/bin/cat" on unix, "/path/to/nvim/cat" on Windows.
+ eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
+ end)
+end)
+
+describe('path.c', function()
setup(function()
lfs.mkdir('unit-test-directory');
io.open('unit-test-directory/test.file', 'w').close()
@@ -313,126 +366,163 @@ describe('more path function', function()
end)
describe('vim_FullName', function()
- local function vim_FullName(filename, buf, len, force)
- filename = to_cstr(filename)
- return path.vim_FullName(filename, buf, len, force)
+ local function vim_FullName(filename, buflen, do_expand)
+ local buf = cstr(buflen, '')
+ local result = cimp.vim_FullName(to_cstr(filename), buf, buflen, do_expand)
+ return buf, result
end
- before_each(function()
- -- Create empty string buffer which will contain the resulting path.
- length = (string.len(lfs.currentdir())) + 33
- buffer = cstr(length, '')
- end)
+ local function get_buf_len(s, t)
+ return math.max(string.len(s), string.len(t)) + 1
+ end
itp('fails if given filename is NULL', function()
- local force_expansion = 1
- local result = path.vim_FullName(NULL, buffer, length, force_expansion)
+ local do_expand = 1
+ local buflen = 10
+ local buf = cstr(buflen, '')
+ local result = cimp.vim_FullName(NULL, buf, buflen, do_expand)
eq(FAIL, result)
end)
itp('fails safely if given length is wrong #5737', function()
- local force_expansion = 1
local filename = 'foo/bar/bazzzzzzz/buz/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/a'
local too_short_len = 8
local buf = cstr(too_short_len, '')
- local result = path.vim_FullName(filename, buf, too_short_len, force_expansion)
+ local do_expand = 1
+ local result = cimp.vim_FullName(filename, buf, too_short_len, do_expand)
local expected = string.sub(filename, 1, (too_short_len - 1))
- eq(expected, (ffi.string(buf)))
+ eq(expected, ffi.string(buf))
eq(FAIL, result)
end)
itp('uses the filename if the filename is a URL', function()
- local force_expansion = 1
local filename = 'http://www.neovim.org'
- local result = vim_FullName(filename, buffer, length, force_expansion)
- eq(filename, (ffi.string(buffer)))
+ local buflen = string.len(filename) + 1
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(filename, ffi.string(buf))
eq(OK, result)
end)
itp('fails and uses filename if given filename contains non-existing directory', function()
- local force_expansion = 1
local filename = 'non_existing_dir/test.file'
- local result = vim_FullName(filename, buffer, length, force_expansion)
- eq(filename, (ffi.string(buffer)))
+ local buflen = string.len(filename) + 1
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(filename, ffi.string(buf))
eq(FAIL, result)
end)
- itp('concatenates given filename if it does not contain a slash', function()
- local force_expansion = 1
- local result = vim_FullName('test.file', buffer, length, force_expansion)
+ itp('concatenates filename if it does not contain a slash', function()
local expected = lfs.currentdir() .. '/test.file'
- eq(expected, (ffi.string(buffer)))
+ local filename = 'test.file'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(expected, ffi.string(buf))
eq(OK, result)
end)
- itp('concatenates given filename if it is a directory but does not contain a\n slash', function()
- local force_expansion = 1
- local result = vim_FullName('..', buffer, length, force_expansion)
+ itp('concatenates directory name if it does not contain a slash', function()
local expected = lfs.currentdir() .. '/..'
- eq(expected, (ffi.string(buffer)))
+ local filename = '..'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(expected, ffi.string(buf))
eq(OK, result)
end)
- -- Is it possible for every developer to enter '..' directory while running
- -- the unit tests? Which other directory would be better?
itp('enters given directory (instead of just concatenating the strings) if possible and if path contains a slash', function()
- local force_expansion = 1
- local result = vim_FullName('../test.file', buffer, length, force_expansion)
local old_dir = lfs.currentdir()
lfs.chdir('..')
local expected = lfs.currentdir() .. '/test.file'
lfs.chdir(old_dir)
- eq(expected, (ffi.string(buffer)))
+ local filename = '../test.file'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(expected, ffi.string(buf))
eq(OK, result)
end)
itp('just copies the path if it is already absolute and force=0', function()
- local force_expansion = 0
local absolute_path = '/absolute/path'
- local result = vim_FullName(absolute_path, buffer, length, force_expansion)
- eq(absolute_path, (ffi.string(buffer)))
+ local buflen = string.len(absolute_path) + 1
+ local do_expand = 0
+ local buf, result = vim_FullName(absolute_path, buflen, do_expand)
+ eq(absolute_path, ffi.string(buf))
eq(OK, result)
end)
itp('fails and uses filename when the path is relative to HOME', function()
- local force_expansion = 1
+ eq(false, cimp.os_isdir('~')) -- sanity check: no literal "~" directory.
local absolute_path = '~/home.file'
- local result = vim_FullName(absolute_path, buffer, length, force_expansion)
- eq(absolute_path, (ffi.string(buffer)))
+ local buflen = string.len(absolute_path) + 1
+ local do_expand = 1
+ local buf, result = vim_FullName(absolute_path, buflen, do_expand)
+ eq(absolute_path, ffi.string(buf))
eq(FAIL, result)
end)
itp('works with some "normal" relative path with directories', function()
- local force_expansion = 1
- local result = vim_FullName('unit-test-directory/test.file', buffer, length, force_expansion)
+ local expected = lfs.currentdir() .. '/unit-test-directory/test.file'
+ local filename = 'unit-test-directory/test.file'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(expected, ffi.string(buf))
eq(OK, result)
- eq(lfs.currentdir() .. '/unit-test-directory/test.file', (ffi.string(buffer)))
end)
itp('does not modify the given filename', function()
- local force_expansion = 1
+ local expected = lfs.currentdir() .. '/unit-test-directory/test.file'
local filename = to_cstr('unit-test-directory/test.file')
- -- Don't use the wrapper here but pass a cstring directly to the c
- -- function.
- local result = path.vim_FullName(filename, buffer, length, force_expansion)
- eq(lfs.currentdir() .. '/unit-test-directory/test.file', (ffi.string(buffer)))
- eq('unit-test-directory/test.file', (ffi.string(filename)))
+ local buflen = string.len(expected) + 1
+ local buf = cstr(buflen, '')
+ local do_expand = 1
+ -- Don't use the wrapper but pass a cstring directly to the c function.
+ eq('unit-test-directory/test.file', ffi.string(filename))
+ local result = cimp.vim_FullName(filename, buf, buflen, do_expand)
+ eq(expected, ffi.string(buf))
eq(OK, result)
end)
itp('works with directories that have one path component', function()
- local force_expansion = 1
- local filename = to_cstr('/tmp')
- local result = path.vim_FullName(filename, buffer, length, force_expansion)
- eq('/tmp', ffi.string(buffer))
+ local filename = '/tmp'
+ local expected = filename
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq('/tmp', ffi.string(buf))
+ eq(OK, result)
+ end)
+
+ itp('expands "./" to the current directory #7117', function()
+ local expected = lfs.currentdir() .. '/unit-test-directory/test.file'
+ local filename = './unit-test-directory/test.file'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
+ eq(OK, result)
+ eq(expected, ffi.string(buf))
+ end)
+
+ itp('collapses "foo/../foo" to "foo" #7117', function()
+ local expected = lfs.currentdir() .. '/unit-test-directory/test.file'
+ local filename = 'unit-test-directory/../unit-test-directory/test.file'
+ local buflen = get_buf_len(expected, filename)
+ local do_expand = 1
+ local buf, result = vim_FullName(filename, buflen, do_expand)
eq(OK, result)
+ eq(expected, ffi.string(buf))
end)
end)
describe('path_fix_case', function()
local function fix_case(file)
local c_file = to_cstr(file)
- path.path_fix_case(c_file)
+ cimp.path_fix_case(c_file)
return ffi.string(c_file)
end
@@ -456,41 +546,41 @@ describe('more path function', function()
itp('joins given paths with a slash', function()
local path1 = cstr(100, 'path1')
local to_append = to_cstr('path2')
- eq(OK, (path.append_path(path1, to_append, 100)))
+ eq(OK, (cimp.append_path(path1, to_append, 100)))
eq("path1/path2", (ffi.string(path1)))
end)
itp('joins given paths without adding an unnecessary slash', function()
local path1 = cstr(100, 'path1/')
local to_append = to_cstr('path2')
- eq(OK, path.append_path(path1, to_append, 100))
+ eq(OK, cimp.append_path(path1, to_append, 100))
eq("path1/path2", (ffi.string(path1)))
end)
itp('fails and uses filename if there is not enough space left for to_append', function()
local path1 = cstr(11, 'path1/')
local to_append = to_cstr('path2')
- eq(FAIL, (path.append_path(path1, to_append, 11)))
+ eq(FAIL, (cimp.append_path(path1, to_append, 11)))
end)
itp('does not append a slash if to_append is empty', function()
local path1 = cstr(6, 'path1')
local to_append = to_cstr('')
- eq(OK, (path.append_path(path1, to_append, 6)))
+ eq(OK, (cimp.append_path(path1, to_append, 6)))
eq('path1', (ffi.string(path1)))
end)
itp('does not append unnecessary dots', function()
local path1 = cstr(6, 'path1')
local to_append = to_cstr('.')
- eq(OK, (path.append_path(path1, to_append, 6)))
+ eq(OK, (cimp.append_path(path1, to_append, 6)))
eq('path1', (ffi.string(path1)))
end)
itp('copies to_append to path, if path is empty', function()
local path1 = cstr(7, '')
local to_append = to_cstr('/path2')
- eq(OK, (path.append_path(path1, to_append, 7)))
+ eq(OK, (cimp.append_path(path1, to_append, 7)))
eq('/path2', (ffi.string(path1)))
end)
end)
@@ -498,7 +588,7 @@ describe('more path function', function()
describe('path_is_absolute_path', function()
local function path_is_absolute_path(filename)
filename = to_cstr(filename)
- return path.path_is_absolute_path(filename)
+ return cimp.path_is_absolute_path(filename)
end
itp('returns true if filename starts with a slash', function()