2023-04-04 12:59:06 -07:00
|
|
|
local luv = require('luv')
|
2017-03-11 03:28:18 -07:00
|
|
|
local helpers = require('test.unit.helpers')(after_each)
|
2017-03-04 18:02:45 -07:00
|
|
|
local itp = helpers.gen_itp(it)
|
2014-08-30 07:42:45 -07:00
|
|
|
|
|
|
|
local cimport = helpers.cimport
|
|
|
|
local eq = helpers.eq
|
|
|
|
local neq = helpers.neq
|
|
|
|
local ffi = helpers.ffi
|
|
|
|
local cstr = helpers.cstr
|
|
|
|
local to_cstr = helpers.to_cstr
|
|
|
|
local NULL = helpers.NULL
|
|
|
|
local OK = helpers.OK
|
|
|
|
local FAIL = helpers.FAIL
|
2023-04-04 12:59:06 -07:00
|
|
|
local mkdir = helpers.mkdir
|
2014-08-30 07:42:45 -07:00
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
cimport('string.h')
|
2017-04-24 13:45:03 -07:00
|
|
|
local cimp = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
|
2014-08-30 07:42:45 -07:00
|
|
|
|
2015-11-17 07:49:12 -07:00
|
|
|
local length = 0
|
2014-09-06 15:36:00 -07:00
|
|
|
local buffer = nil
|
|
|
|
|
2017-05-13 09:17:21 -07:00
|
|
|
describe('path.c', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
describe('path_full_dir_name', function()
|
|
|
|
setup(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
mkdir('unit-test-directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
luv.fs_rmdir('unit-test-directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2015-11-17 07:49:12 -07:00
|
|
|
local function path_full_dir_name(directory, buf, len)
|
2014-08-30 07:42:45 -07:00
|
|
|
directory = to_cstr(directory)
|
2017-04-24 13:45:03 -07:00
|
|
|
return cimp.path_full_dir_name(directory, buf, len)
|
2014-08-30 07:42:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
-- Create empty string buffer which will contain the resulting path.
|
2023-04-04 12:59:06 -07:00
|
|
|
length = string.len(luv.cwd()) + 22
|
2015-11-17 07:49:12 -07:00
|
|
|
buffer = cstr(length, '')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the absolute directory name of a given relative one', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local result = path_full_dir_name('..', buffer, length)
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
2023-04-04 12:59:06 -07:00
|
|
|
local old_dir = luv.cwd()
|
|
|
|
luv.chdir('..')
|
|
|
|
local expected = luv.cwd()
|
|
|
|
luv.chdir(old_dir)
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(expected, (ffi.string(buffer)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the current directory name if the given string is empty', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
eq(OK, (path_full_dir_name('', buffer, length)))
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(luv.cwd(), (ffi.string(buffer)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('works with a normal relative dir', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local result = path_full_dir_name('unit-test-directory', buffer, length)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(luv.cwd() .. '/unit-test-directory', (ffi.string(buffer)))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
2021-11-19 05:07:04 -07:00
|
|
|
|
|
|
|
itp('works with a non-existing relative dir', function()
|
|
|
|
local result = path_full_dir_name('does-not-exist', buffer, length)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(luv.cwd() .. '/does-not-exist', (ffi.string(buffer)))
|
2021-11-19 05:07:04 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
|
|
|
itp('fails with a non-existing absolute dir', function()
|
|
|
|
eq(FAIL, path_full_dir_name('/does_not_exist', buffer, length))
|
|
|
|
end)
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('path_full_compare', function()
|
2019-10-05 20:35:48 -07:00
|
|
|
local function path_full_compare(s1, s2, cn, ee)
|
2014-08-30 07:42:45 -07:00
|
|
|
s1 = to_cstr(s1)
|
|
|
|
s2 = to_cstr(s2)
|
2019-10-05 20:35:48 -07:00
|
|
|
return cimp.path_full_compare(s1, s2, cn or 0, ee or 1)
|
2014-08-30 07:42:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local f1 = 'f1.o'
|
|
|
|
local f2 = 'f2.o'
|
|
|
|
before_each(function()
|
|
|
|
-- create the three files that will be used in this spec
|
2022-12-22 07:17:01 -07:00
|
|
|
io.open(f1, 'w'):close()
|
|
|
|
io.open(f2, 'w'):close()
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(f1)
|
|
|
|
os.remove(f2)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns kEqualFiles when passed the same file', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(cimp.kEqualFiles, (path_full_compare(f1, f1)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns kEqualFileNames when files that dont exist and have same name', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(cimp.kEqualFileNames, (path_full_compare('null.txt', 'null.txt', true)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns kBothFilesMissing when files that dont exist', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(cimp.kBothFilesMissing, (path_full_compare('null.txt', 'null.txt')))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns kDifferentFiles when passed different files', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(cimp.kDifferentFiles, (path_full_compare(f1, f2)))
|
|
|
|
eq(cimp.kDifferentFiles, (path_full_compare(f2, f1)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns kOneFileMissing if only one does not exist', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(cimp.kOneFileMissing, (path_full_compare(f1, 'null.txt')))
|
|
|
|
eq(cimp.kOneFileMissing, (path_full_compare('null.txt', f1)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('path_tail', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local function path_tail(file)
|
2017-04-24 13:45:03 -07:00
|
|
|
local res = cimp.path_tail((to_cstr(file)))
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, res)
|
|
|
|
return ffi.string(res)
|
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the tail of a given file path', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('file.txt', path_tail('directory/file.txt'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns an empty string if file ends in a slash', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('', path_tail('directory/'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('path_tail_with_sep', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local function path_tail_with_sep(file)
|
2017-04-24 13:45:03 -07:00
|
|
|
local res = cimp.path_tail_with_sep((to_cstr(file)))
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, res)
|
|
|
|
return ffi.string(res)
|
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the tail of a file together with its separator', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('///file.txt', path_tail_with_sep('directory///file.txt'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns an empty string when given an empty file name', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('', path_tail_with_sep(''))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns only the separator if there is a trailing separator', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('/', path_tail_with_sep('some/directory/'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('cuts a leading separator', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('file.txt', path_tail_with_sep('/file.txt'))
|
|
|
|
eq('', path_tail_with_sep('/'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the whole file name if there is no separator', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('file.txt', path_tail_with_sep('file.txt'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('invocation_path_tail', function()
|
|
|
|
-- Returns the path tail and length (out param) of the tail.
|
|
|
|
-- Does not convert the tail from C-pointer to lua string for use with
|
|
|
|
-- strcmp.
|
2015-11-17 07:49:12 -07:00
|
|
|
local function invocation_path_tail(invk)
|
2014-08-30 07:42:45 -07:00
|
|
|
local plen = ffi.new('size_t[?]', 1)
|
2017-04-24 13:45:03 -07:00
|
|
|
local ptail = cimp.invocation_path_tail((to_cstr(invk)), plen)
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, ptail)
|
|
|
|
|
|
|
|
-- it does not change the output if len==NULL
|
2017-04-24 13:45:03 -07:00
|
|
|
local tail2 = cimp.invocation_path_tail((to_cstr(invk)), NULL)
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, tail2)
|
|
|
|
eq((ffi.string(ptail)), (ffi.string(tail2)))
|
|
|
|
return ptail, plen[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- This test mimics the intended use in C.
|
2015-11-17 07:49:12 -07:00
|
|
|
local function compare(base, pinvk, len)
|
2014-08-30 07:42:45 -07:00
|
|
|
return eq(0, (ffi.C.strncmp((to_cstr(base)), pinvk, len)))
|
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the executable name of an invocation given a relative invocation', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local invk, len = invocation_path_tail('directory/exe a b c')
|
|
|
|
compare("exe a b c", invk, len)
|
|
|
|
eq(3, len)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the executable name of an invocation given an absolute invocation', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
if ffi.os == 'Windows' then
|
|
|
|
local invk, len = invocation_path_tail('C:\\Users\\anyone\\Program Files\\z a b')
|
|
|
|
compare('z a b', invk, len)
|
|
|
|
eq(1, len)
|
|
|
|
else
|
|
|
|
local invk, len = invocation_path_tail('/usr/bin/z a b')
|
|
|
|
compare('z a b', invk, len)
|
|
|
|
eq(1, len)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does not count arguments to the executable as part of its path', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local invk, len = invocation_path_tail('exe a/b\\c')
|
|
|
|
compare("exe a/b\\c", invk, len)
|
|
|
|
eq(3, len)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('only accepts whitespace as a terminator for the executable name', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local invk, _ = invocation_path_tail('exe-a+b_c[]()|#!@$%^&*')
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('exe-a+b_c[]()|#!@$%^&*', (ffi.string(invk)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('is equivalent to path_tail when args do not contain a path separator', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
local ptail = cimp.path_tail(to_cstr("a/b/c x y z"))
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, ptail)
|
|
|
|
local tail = ffi.string(ptail)
|
2015-11-17 07:49:12 -07:00
|
|
|
local invk, _ = invocation_path_tail("a/b/c x y z")
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(tail, ffi.string(invk))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('is not equivalent to path_tail when args contain a path separator', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
local ptail = cimp.path_tail(to_cstr("a/b/c x y/z"))
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, ptail)
|
2015-11-17 07:49:12 -07:00
|
|
|
local invk, _ = invocation_path_tail("a/b/c x y/z")
|
2014-08-30 07:42:45 -07:00
|
|
|
neq((ffi.string(ptail)), (ffi.string(invk)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('path_next_component', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local function path_next_component(file)
|
2017-04-24 13:45:03 -07:00
|
|
|
local res = cimp.path_next_component((to_cstr(file)))
|
2014-08-30 07:42:45 -07:00
|
|
|
neq(NULL, res)
|
|
|
|
return ffi.string(res)
|
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('directory/file.txt', path_next_component('some/directory/file.txt'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns empty string if given file contains no separator', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('', path_next_component('file.txt'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('path_shorten_fname', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NULL if `full_path` is NULL', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local dir = to_cstr('some/directory/file.txt')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(NULL, (cimp.path_shorten_fname(NULL, dir)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NULL if the path and dir does not match', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local dir = to_cstr('not/the/same')
|
|
|
|
local full = to_cstr('as/this.txt')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(NULL, (cimp.path_shorten_fname(full, dir)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NULL if the path is not separated properly', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local dir = to_cstr('some/very/long/')
|
|
|
|
local full = to_cstr('some/very/long/directory/file.txt')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(NULL, (cimp.path_shorten_fname(full, dir)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('shortens the filename if `dir_name` is the start of `full_path`', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local full = to_cstr('some/very/long/directory/file.txt')
|
|
|
|
local dir = to_cstr('some/very/long')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2018-03-24 05:56:55 -07:00
|
|
|
describe('path_try_shorten_fname', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local cwd = luv.cwd()
|
2014-08-30 07:42:45 -07:00
|
|
|
|
|
|
|
before_each(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
mkdir('ut_directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
luv.chdir(cwd)
|
|
|
|
luv.fs_rmdir('ut_directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2018-03-24 05:56:55 -07:00
|
|
|
describe('path_try_shorten_fname', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns shortened path if possible', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
luv.chdir('ut_directory')
|
|
|
|
local full = to_cstr(luv.cwd() .. '/subdir/file.txt')
|
2018-03-24 05:56:55 -07:00
|
|
|
eq('subdir/file.txt', (ffi.string(cimp.path_try_shorten_fname(full))))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns `full_path` if a shorter version is not possible', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local old = luv.cwd()
|
|
|
|
luv.chdir('ut_directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
local full = old .. '/subdir/file.txt'
|
2018-03-24 05:56:55 -07:00
|
|
|
eq(full, (ffi.string(cimp.path_try_shorten_fname(to_cstr(full)))))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NULL if `full_path` is NULL', function()
|
2018-03-24 05:56:55 -07:00
|
|
|
eq(NULL, (cimp.path_try_shorten_fname(NULL)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2017-05-13 09:17:21 -07:00
|
|
|
describe('path.c path_guess_exepath', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local cwd = luv.cwd()
|
2017-05-13 09:17:21 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-04-24 13:45:03 -07:00
|
|
|
describe('path.c', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
setup(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
mkdir('unit-test-directory');
|
2022-12-22 07:17:01 -07:00
|
|
|
io.open('unit-test-directory/test.file', 'w'):close()
|
2014-08-30 07:42:45 -07:00
|
|
|
|
|
|
|
-- Since the tests are executed, they are called by an executable. We use
|
|
|
|
-- that executable for several asserts.
|
2015-11-17 07:49:12 -07:00
|
|
|
local absolute_executable = arg[0]
|
2014-08-30 07:42:45 -07:00
|
|
|
|
|
|
|
-- Split absolute_executable into a directory and the actual file name for
|
|
|
|
-- later usage.
|
2015-11-17 07:49:12 -07:00
|
|
|
local directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$') -- luacheck: ignore
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
|
|
|
os.remove('unit-test-directory/test.file')
|
2023-04-04 12:59:06 -07:00
|
|
|
luv.fs_rmdir('unit-test-directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('vim_FullName', function()
|
2017-11-12 18:28:07 -07:00
|
|
|
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
|
2014-08-30 07:42:45 -07:00
|
|
|
end
|
|
|
|
|
2017-11-12 18:28:07 -07:00
|
|
|
local function get_buf_len(s, t)
|
|
|
|
return math.max(string.len(s), string.len(t)) + 1
|
|
|
|
end
|
2014-08-30 07:42:45 -07:00
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails if given filename is NULL', function()
|
2017-11-12 18:28:07 -07:00
|
|
|
local do_expand = 1
|
|
|
|
local buflen = 10
|
|
|
|
local buf = cstr(buflen, '')
|
|
|
|
local result = cimp.vim_FullName(NULL, buf, buflen, do_expand)
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(FAIL, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails safely if given length is wrong #5737', function()
|
2016-12-11 15:44:54 -07:00
|
|
|
local filename = 'foo/bar/bazzzzzzz/buz/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/a'
|
|
|
|
local too_short_len = 8
|
|
|
|
local buf = cstr(too_short_len, '')
|
2017-11-12 18:28:07 -07:00
|
|
|
local do_expand = 1
|
|
|
|
local result = cimp.vim_FullName(filename, buf, too_short_len, do_expand)
|
2016-12-11 15:44:54 -07:00
|
|
|
local expected = string.sub(filename, 1, (too_short_len - 1))
|
2017-11-12 18:28:07 -07:00
|
|
|
eq(expected, ffi.string(buf))
|
2016-12-11 15:44:54 -07:00
|
|
|
eq(FAIL, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('uses the filename if the filename is a URL', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local filename = 'http://www.neovim.org'
|
2017-11-12 18:28:07 -07:00
|
|
|
local buflen = string.len(filename) + 1
|
|
|
|
local do_expand = 1
|
|
|
|
local buf, result = vim_FullName(filename, buflen, do_expand)
|
|
|
|
eq(filename, ffi.string(buf))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails and uses filename if given filename contains non-existing directory', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local filename = 'non_existing_dir/test.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
local buflen = string.len(filename) + 1
|
|
|
|
local do_expand = 1
|
|
|
|
local buf, result = vim_FullName(filename, buflen, do_expand)
|
|
|
|
eq(filename, ffi.string(buf))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(FAIL, result)
|
|
|
|
end)
|
|
|
|
|
2017-04-24 13:45:03 -07:00
|
|
|
itp('concatenates filename if it does not contain a slash', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/test.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-04-24 13:45:03 -07:00
|
|
|
itp('concatenates directory name if it does not contain a slash', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/..'
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('enters given directory (instead of just concatenating the strings) if possible and if path contains a slash', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local old_dir = luv.cwd()
|
|
|
|
luv.chdir('..')
|
|
|
|
local expected = luv.cwd() .. '/test.file'
|
|
|
|
luv.chdir(old_dir)
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('just copies the path if it is already absolute and force=0', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local absolute_path = '/absolute/path'
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails and uses filename when the path is relative to HOME', function()
|
2019-09-16 15:39:33 -07:00
|
|
|
eq(false, cimp.os_isdir('~'), 'sanity check: no literal "~" directory')
|
2014-08-30 07:42:45 -07:00
|
|
|
local absolute_path = '~/home.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(FAIL, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('works with some "normal" relative path with directories', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/unit-test-directory/test.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does not modify the given filename', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/unit-test-directory/test.file'
|
2014-08-30 07:42:45 -07:00
|
|
|
local filename = to_cstr('unit-test-directory/test.file')
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
2015-09-08 07:13:42 -07:00
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('works with directories that have one path component', function()
|
2017-11-12 18:28:07 -07:00
|
|
|
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))
|
2015-09-08 07:13:42 -07:00
|
|
|
eq(OK, result)
|
|
|
|
end)
|
2017-09-18 11:06:55 -07:00
|
|
|
|
2022-10-29 16:10:04 -07:00
|
|
|
itp('does not remove trailing slash from non-existing relative directory #20847', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/non_existing_dir/'
|
2022-10-29 16:10:04 -07:00
|
|
|
local filename = 'non_existing_dir/'
|
|
|
|
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)
|
|
|
|
|
2017-10-01 15:24:19 -07:00
|
|
|
itp('expands "./" to the current directory #7117', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/unit-test-directory/test.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
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)
|
2017-09-18 11:06:55 -07:00
|
|
|
eq(OK, result)
|
2017-11-12 18:28:07 -07:00
|
|
|
eq(expected, ffi.string(buf))
|
2017-09-18 11:06:55 -07:00
|
|
|
end)
|
|
|
|
|
2017-10-01 15:24:19 -07:00
|
|
|
itp('collapses "foo/../foo" to "foo" #7117', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
local expected = luv.cwd() .. '/unit-test-directory/test.file'
|
2017-11-12 18:28:07 -07:00
|
|
|
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)
|
2017-09-18 11:06:55 -07:00
|
|
|
eq(OK, result)
|
2017-11-12 18:28:07 -07:00
|
|
|
eq(expected, ffi.string(buf))
|
2017-09-18 11:06:55 -07:00
|
|
|
end)
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2015-03-26 20:01:07 -07:00
|
|
|
describe('path_fix_case', function()
|
2015-11-17 07:49:12 -07:00
|
|
|
local function fix_case(file)
|
|
|
|
local c_file = to_cstr(file)
|
2017-04-24 13:45:03 -07:00
|
|
|
cimp.path_fix_case(c_file)
|
2015-03-26 20:01:07 -07:00
|
|
|
return ffi.string(c_file)
|
|
|
|
end
|
|
|
|
|
2023-04-04 12:59:06 -07:00
|
|
|
before_each(function() mkdir('CamelCase') end)
|
|
|
|
after_each(function() luv.fs_rmdir('CamelCase') end)
|
2015-04-10 13:23:01 -07:00
|
|
|
|
2015-03-26 20:01:07 -07:00
|
|
|
if ffi.os == 'Windows' or ffi.os == 'OSX' then
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('Corrects the case of file names in Mac and Windows', function()
|
2015-03-26 20:01:07 -07:00
|
|
|
eq('CamelCase', fix_case('camelcase'))
|
|
|
|
eq('CamelCase', fix_case('cAMELcASE'))
|
|
|
|
end)
|
|
|
|
else
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does nothing on Linux', function()
|
2015-03-26 20:01:07 -07:00
|
|
|
eq('camelcase', fix_case('camelcase'))
|
|
|
|
eq('cAMELcASE', fix_case('cAMELcASE'))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-08-30 07:42:45 -07:00
|
|
|
describe('append_path', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('joins given paths with a slash', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(100, 'path1')
|
|
|
|
local to_append = to_cstr('path2')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(OK, (cimp.append_path(path1, to_append, 100)))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq("path1/path2", (ffi.string(path1)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('joins given paths without adding an unnecessary slash', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(100, 'path1/')
|
|
|
|
local to_append = to_cstr('path2')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(OK, cimp.append_path(path1, to_append, 100))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq("path1/path2", (ffi.string(path1)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails and uses filename if there is not enough space left for to_append', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(11, 'path1/')
|
|
|
|
local to_append = to_cstr('path2')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(FAIL, (cimp.append_path(path1, to_append, 11)))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does not append a slash if to_append is empty', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(6, 'path1')
|
|
|
|
local to_append = to_cstr('')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(OK, (cimp.append_path(path1, to_append, 6)))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('path1', (ffi.string(path1)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does not append unnecessary dots', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(6, 'path1')
|
|
|
|
local to_append = to_cstr('.')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(OK, (cimp.append_path(path1, to_append, 6)))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('path1', (ffi.string(path1)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('copies to_append to path, if path is empty', function()
|
2014-08-30 07:42:45 -07:00
|
|
|
local path1 = cstr(7, '')
|
|
|
|
local to_append = to_cstr('/path2')
|
2017-04-24 13:45:03 -07:00
|
|
|
eq(OK, (cimp.append_path(path1, to_append, 7)))
|
2014-08-30 07:42:45 -07:00
|
|
|
eq('/path2', (ffi.string(path1)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2018-03-24 03:21:20 -07:00
|
|
|
describe('path_is_absolute', function()
|
|
|
|
local function path_is_absolute(filename)
|
2014-08-30 07:42:45 -07:00
|
|
|
filename = to_cstr(filename)
|
2018-03-24 03:21:20 -07:00
|
|
|
return cimp.path_is_absolute(filename)
|
2014-08-30 07:42:45 -07:00
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if filename starts with a slash', function()
|
2018-03-24 03:21:20 -07:00
|
|
|
eq(OK, path_is_absolute('/some/directory/'))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if filename starts with a tilde', function()
|
2018-03-24 03:21:20 -07:00
|
|
|
eq(OK, path_is_absolute('~/in/my/home~/directory'))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if filename starts not with slash nor tilde', function()
|
2018-03-24 03:21:20 -07:00
|
|
|
eq(FAIL, path_is_absolute('not/in/my/home~/directory'))
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|
|
|
|
end)
|
2020-05-03 13:46:55 -07:00
|
|
|
|
|
|
|
describe('path_with_extension', function()
|
|
|
|
local function path_with_extension(filename, extension)
|
|
|
|
local c_filename = to_cstr(filename)
|
|
|
|
local c_extension = to_cstr(extension)
|
|
|
|
return cimp.path_with_extension(c_filename, c_extension)
|
|
|
|
end
|
|
|
|
|
|
|
|
itp('returns true if filename includes a provided extension', function()
|
|
|
|
eq(true, path_with_extension('/some/path/file.lua', 'lua'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
itp('returns false if filename does not include a provided extension', function()
|
|
|
|
eq(false, path_with_extension('/some/path/file.vim', 'lua'))
|
|
|
|
eq(false, path_with_extension('/some/path/file', 'lua'))
|
|
|
|
end)
|
|
|
|
end)
|
2022-01-23 22:01:35 -07:00
|
|
|
|
|
|
|
describe('path_with_url', function()
|
|
|
|
itp('scheme is alpha and inner hyphen only', function()
|
|
|
|
local function path_with_url(fname)
|
|
|
|
return cimp.path_with_url(to_cstr(fname))
|
|
|
|
end
|
|
|
|
eq(1, path_with_url([[test://xyz/foo/b0]]))
|
|
|
|
eq(2, path_with_url([[test:\\xyz\foo\b0]]))
|
|
|
|
eq(0, path_with_url([[test+abc://xyz/foo/b1]]))
|
|
|
|
eq(0, path_with_url([[test_abc://xyz/foo/b2]]))
|
|
|
|
eq(1, path_with_url([[test-abc://xyz/foo/b3]]))
|
|
|
|
eq(2, path_with_url([[test-abc:\\xyz\foo\b3]]))
|
|
|
|
eq(0, path_with_url([[-test://xyz/foo/b4]]))
|
|
|
|
eq(0, path_with_url([[test-://xyz/foo/b5]]))
|
2022-08-23 23:38:06 -07:00
|
|
|
eq(1, path_with_url([[test-C:/xyz/foo/b5]]))
|
|
|
|
eq(1, path_with_url([[test-custom:/xyz/foo/b5]]))
|
|
|
|
eq(0, path_with_url([[c:/xyz/foo/b5]]))
|
|
|
|
eq(0, path_with_url([[C:/xyz/foo/b5]]))
|
2022-01-23 22:01:35 -07:00
|
|
|
end)
|
|
|
|
end)
|
2014-08-30 07:42:45 -07:00
|
|
|
end)
|