path_spec.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. local uv = vim.uv
  2. local t = require('test.unit.testutil')
  3. local itp = t.gen_itp(it)
  4. local cimport = t.cimport
  5. local eq = t.eq
  6. local neq = t.neq
  7. local ffi = t.ffi
  8. local cstr = t.cstr
  9. local to_cstr = t.to_cstr
  10. local NULL = t.NULL
  11. local OK = t.OK
  12. local FAIL = t.FAIL
  13. local mkdir = t.mkdir
  14. cimport('string.h')
  15. local cimp = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
  16. local options = cimport('./src/nvim/option_vars.h')
  17. local length = 0
  18. local buffer = nil
  19. describe('path.c', function()
  20. describe('path_full_dir_name', function()
  21. local old_dir
  22. setup(function()
  23. old_dir = uv.cwd()
  24. mkdir('unit-test-directory')
  25. uv.fs_symlink(old_dir .. '/unit-test-directory', 'unit-test-symlink')
  26. end)
  27. teardown(function()
  28. uv.fs_unlink('unit-test-symlink')
  29. uv.fs_rmdir('unit-test-directory')
  30. end)
  31. local function path_full_dir_name(directory, buf, len)
  32. directory = to_cstr(directory)
  33. return cimp.path_full_dir_name(directory, buf, len)
  34. end
  35. before_each(function()
  36. -- Create empty string buffer which will contain the resulting path.
  37. length = string.len(old_dir) + 22
  38. buffer = cstr(length, '')
  39. end)
  40. after_each(function()
  41. uv.chdir(old_dir)
  42. end)
  43. itp('returns the absolute directory name of a given relative one', function()
  44. eq(OK, path_full_dir_name('..', buffer, length))
  45. uv.chdir('..')
  46. local expected = uv.cwd()
  47. uv.chdir(old_dir)
  48. eq(expected, ffi.string(buffer))
  49. end)
  50. itp('returns the current directory name if the given string is empty', function()
  51. eq(OK, path_full_dir_name('', buffer, length))
  52. eq(old_dir, ffi.string(buffer))
  53. end)
  54. local function test_full_dir_absolute()
  55. itp('works with a normal absolute dir', function()
  56. eq(OK, path_full_dir_name(old_dir .. '/unit-test-directory', buffer, length))
  57. eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
  58. end)
  59. itp('works with a symlinked absolute dir', function()
  60. eq(OK, path_full_dir_name(old_dir .. '/unit-test-symlink', buffer, length))
  61. eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
  62. end)
  63. end
  64. test_full_dir_absolute()
  65. describe('when cwd does not exist #28786', function()
  66. before_each(function()
  67. mkdir('dir-to-remove')
  68. uv.chdir('dir-to-remove')
  69. uv.fs_rmdir(old_dir .. '/dir-to-remove')
  70. end)
  71. test_full_dir_absolute()
  72. end)
  73. itp('works with a normal relative dir', function()
  74. eq(OK, path_full_dir_name('unit-test-directory', buffer, length))
  75. eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
  76. end)
  77. itp('works with a symlinked relative dir', function()
  78. eq(OK, path_full_dir_name('unit-test-symlink', buffer, length))
  79. eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
  80. end)
  81. itp('works with a non-existing relative dir', function()
  82. eq(OK, path_full_dir_name('does-not-exist', buffer, length))
  83. eq(old_dir .. '/does-not-exist', ffi.string(buffer))
  84. end)
  85. itp('fails with a non-existing absolute dir', function()
  86. eq(FAIL, path_full_dir_name('/does_not_exist', buffer, length))
  87. end)
  88. end)
  89. describe('path_full_compare', function()
  90. local function path_full_compare(s1, s2, cn, ee)
  91. s1 = to_cstr(s1)
  92. s2 = to_cstr(s2)
  93. return cimp.path_full_compare(s1, s2, cn or 0, ee or 1)
  94. end
  95. local f1 = 'f1.o'
  96. local f2 = 'f2.o'
  97. before_each(function()
  98. -- create the three files that will be used in this spec
  99. io.open(f1, 'w'):close()
  100. io.open(f2, 'w'):close()
  101. end)
  102. after_each(function()
  103. os.remove(f1)
  104. os.remove(f2)
  105. end)
  106. itp('returns kEqualFiles when passed the same file', function()
  107. eq(cimp.kEqualFiles, (path_full_compare(f1, f1)))
  108. end)
  109. itp('returns kEqualFileNames when files that dont exist and have same name', function()
  110. eq(cimp.kEqualFileNames, (path_full_compare('null.txt', 'null.txt', true)))
  111. end)
  112. itp('returns kBothFilesMissing when files that dont exist', function()
  113. eq(cimp.kBothFilesMissing, (path_full_compare('null.txt', 'null.txt')))
  114. end)
  115. itp('returns kDifferentFiles when passed different files', function()
  116. eq(cimp.kDifferentFiles, (path_full_compare(f1, f2)))
  117. eq(cimp.kDifferentFiles, (path_full_compare(f2, f1)))
  118. end)
  119. itp('returns kOneFileMissing if only one does not exist', function()
  120. eq(cimp.kOneFileMissing, (path_full_compare(f1, 'null.txt')))
  121. eq(cimp.kOneFileMissing, (path_full_compare('null.txt', f1)))
  122. end)
  123. end)
  124. describe('path_tail', function()
  125. local function path_tail(file)
  126. local res = cimp.path_tail((to_cstr(file)))
  127. neq(NULL, res)
  128. return ffi.string(res)
  129. end
  130. itp('returns the tail of a given file path', function()
  131. eq('file.txt', path_tail('directory/file.txt'))
  132. end)
  133. itp('returns an empty string if file ends in a slash', function()
  134. eq('', path_tail('directory/'))
  135. end)
  136. end)
  137. describe('path_tail_with_sep', function()
  138. local function path_tail_with_sep(file)
  139. local res = cimp.path_tail_with_sep((to_cstr(file)))
  140. neq(NULL, res)
  141. return ffi.string(res)
  142. end
  143. itp('returns the tail of a file together with its separator', function()
  144. eq('///file.txt', path_tail_with_sep('directory///file.txt'))
  145. end)
  146. itp('returns an empty string when given an empty file name', function()
  147. eq('', path_tail_with_sep(''))
  148. end)
  149. itp('returns only the separator if there is a trailing separator', function()
  150. eq('/', path_tail_with_sep('some/directory/'))
  151. end)
  152. itp('cuts a leading separator', function()
  153. eq('file.txt', path_tail_with_sep('/file.txt'))
  154. eq('', path_tail_with_sep('/'))
  155. end)
  156. itp('returns the whole file name if there is no separator', function()
  157. eq('file.txt', path_tail_with_sep('file.txt'))
  158. end)
  159. end)
  160. describe('invocation_path_tail', function()
  161. -- Returns the path tail and length (out param) of the tail.
  162. -- Does not convert the tail from C-pointer to lua string for use with
  163. -- strcmp.
  164. local function invocation_path_tail(invk)
  165. local plen = ffi.new('size_t[?]', 1)
  166. local ptail = cimp.invocation_path_tail((to_cstr(invk)), plen)
  167. neq(NULL, ptail)
  168. -- it does not change the output if len==NULL
  169. local tail2 = cimp.invocation_path_tail((to_cstr(invk)), NULL)
  170. neq(NULL, tail2)
  171. eq((ffi.string(ptail)), (ffi.string(tail2)))
  172. return ptail, plen[0]
  173. end
  174. -- This test mimics the intended use in C.
  175. local function compare(base, pinvk, len)
  176. return eq(0, (ffi.C.strncmp((to_cstr(base)), pinvk, len)))
  177. end
  178. itp('returns the executable name of an invocation given a relative invocation', function()
  179. local invk, len = invocation_path_tail('directory/exe a b c')
  180. compare('exe a b c', invk, len)
  181. eq(3, len)
  182. end)
  183. itp('returns the executable name of an invocation given an absolute invocation', function()
  184. if ffi.os == 'Windows' then
  185. local invk, len = invocation_path_tail('C:\\Users\\anyone\\Program Files\\z a b')
  186. compare('z a b', invk, len)
  187. eq(1, len)
  188. else
  189. local invk, len = invocation_path_tail('/usr/bin/z a b')
  190. compare('z a b', invk, len)
  191. eq(1, len)
  192. end
  193. end)
  194. itp('does not count arguments to the executable as part of its path', function()
  195. local invk, len = invocation_path_tail('exe a/b\\c')
  196. compare('exe a/b\\c', invk, len)
  197. eq(3, len)
  198. end)
  199. itp('only accepts whitespace as a terminator for the executable name', function()
  200. local invk, _ = invocation_path_tail('exe-a+b_c[]()|#!@$%^&*')
  201. eq('exe-a+b_c[]()|#!@$%^&*', (ffi.string(invk)))
  202. end)
  203. itp('is equivalent to path_tail when args do not contain a path separator', function()
  204. local ptail = cimp.path_tail(to_cstr('a/b/c x y z'))
  205. neq(NULL, ptail)
  206. local tail = ffi.string(ptail)
  207. local invk, _ = invocation_path_tail('a/b/c x y z')
  208. eq(tail, ffi.string(invk))
  209. end)
  210. itp('is not equivalent to path_tail when args contain a path separator', function()
  211. local ptail = cimp.path_tail(to_cstr('a/b/c x y/z'))
  212. neq(NULL, ptail)
  213. local invk, _ = invocation_path_tail('a/b/c x y/z')
  214. neq((ffi.string(ptail)), (ffi.string(invk)))
  215. end)
  216. end)
  217. describe('path_next_component', function()
  218. local function path_next_component(file)
  219. local res = cimp.path_next_component((to_cstr(file)))
  220. neq(NULL, res)
  221. return ffi.string(res)
  222. end
  223. itp('returns', function()
  224. eq('directory/file.txt', path_next_component('some/directory/file.txt'))
  225. end)
  226. itp('returns empty string if given file contains no separator', function()
  227. eq('', path_next_component('file.txt'))
  228. end)
  229. end)
  230. describe('path_shorten_fname', function()
  231. itp('returns NULL if `full_path` is NULL', function()
  232. local dir = to_cstr('some/directory/file.txt')
  233. eq(NULL, (cimp.path_shorten_fname(NULL, dir)))
  234. end)
  235. itp('returns NULL if the path and dir does not match', function()
  236. local dir = to_cstr('not/the/same')
  237. local full = to_cstr('as/this.txt')
  238. eq(NULL, (cimp.path_shorten_fname(full, dir)))
  239. end)
  240. itp('returns NULL if the path is not separated properly', function()
  241. local dir = to_cstr('some/very/long/')
  242. local full = to_cstr('some/very/long/directory/file.txt')
  243. eq(NULL, (cimp.path_shorten_fname(full, dir)))
  244. end)
  245. itp('shortens the filename if `dir_name` is the start of `full_path`', function()
  246. local full = to_cstr('some/very/long/directory/file.txt')
  247. local dir = to_cstr('some/very/long')
  248. eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
  249. end)
  250. end)
  251. end)
  252. describe('path_try_shorten_fname', function()
  253. local cwd = uv.cwd()
  254. before_each(function()
  255. mkdir('ut_directory')
  256. end)
  257. after_each(function()
  258. uv.chdir(cwd)
  259. uv.fs_rmdir('ut_directory')
  260. end)
  261. describe('path_try_shorten_fname', function()
  262. itp('returns shortened path if possible', function()
  263. uv.chdir('ut_directory')
  264. local full = to_cstr(uv.cwd() .. '/subdir/file.txt')
  265. eq('subdir/file.txt', (ffi.string(cimp.path_try_shorten_fname(full))))
  266. end)
  267. itp('returns `full_path` if a shorter version is not possible', function()
  268. local old = uv.cwd()
  269. uv.chdir('ut_directory')
  270. local full = old .. '/subdir/file.txt'
  271. eq(full, (ffi.string(cimp.path_try_shorten_fname(to_cstr(full)))))
  272. end)
  273. itp('returns NULL if `full_path` is NULL', function()
  274. eq(NULL, (cimp.path_try_shorten_fname(NULL)))
  275. end)
  276. end)
  277. end)
  278. describe('path.c path_guess_exepath', function()
  279. local cwd = uv.cwd()
  280. for _, name in ipairs({ './nvim', '.nvim', 'foo/nvim' }) do
  281. itp('"' .. name .. '" returns name catenated with CWD', function()
  282. local bufsize = 255
  283. local buf = cstr(bufsize, '')
  284. cimp.path_guess_exepath(name, buf, bufsize)
  285. eq(cwd .. '/' .. name, ffi.string(buf))
  286. end)
  287. end
  288. itp('absolute path returns the name unmodified', function()
  289. local name = '/foo/bar/baz'
  290. local bufsize = 255
  291. local buf = cstr(bufsize, '')
  292. cimp.path_guess_exepath(name, buf, bufsize)
  293. eq(name, ffi.string(buf))
  294. end)
  295. itp('returns the name unmodified if not found in $PATH', function()
  296. local name = '23u0293_not_in_path'
  297. local bufsize = 255
  298. local buf = cstr(bufsize, '')
  299. cimp.path_guess_exepath(name, buf, bufsize)
  300. eq(name, ffi.string(buf))
  301. end)
  302. itp('does not crash if $PATH item exceeds MAXPATHL', function()
  303. local orig_path_env = os.getenv('PATH')
  304. local name = 'cat' -- Some executable in $PATH.
  305. local bufsize = 255
  306. local buf = cstr(bufsize, '')
  307. local insane_path = orig_path_env .. ':' .. (('x/'):rep(4097))
  308. cimp.os_setenv('PATH', insane_path, true)
  309. cimp.path_guess_exepath(name, buf, bufsize)
  310. eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
  311. -- Restore $PATH.
  312. cimp.os_setenv('PATH', orig_path_env, true)
  313. end)
  314. itp('returns full path found in $PATH', function()
  315. local name = 'cat' -- Some executable in $PATH.
  316. local bufsize = 255
  317. local buf = cstr(bufsize, '')
  318. cimp.path_guess_exepath(name, buf, bufsize)
  319. -- Usually "/bin/cat" on unix, "/path/to/nvim/cat" on Windows.
  320. eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
  321. end)
  322. end)
  323. describe('path.c', function()
  324. setup(function()
  325. mkdir('unit-test-directory')
  326. io.open('unit-test-directory/test.file', 'w'):close()
  327. -- Since the tests are executed, they are called by an executable. We use
  328. -- that executable for several asserts.
  329. local absolute_executable = arg[0]
  330. -- Split absolute_executable into a directory and the actual file name for
  331. -- later usage.
  332. local directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$') -- luacheck: ignore
  333. end)
  334. teardown(function()
  335. os.remove('unit-test-directory/test.file')
  336. uv.fs_rmdir('unit-test-directory')
  337. end)
  338. describe('vim_FullName', function()
  339. local function vim_FullName(filename, buflen, do_expand)
  340. local buf = cstr(buflen, '')
  341. local result = cimp.vim_FullName(to_cstr(filename), buf, buflen, do_expand)
  342. return buf, result
  343. end
  344. local function get_buf_len(s, q)
  345. return math.max(string.len(s), string.len(q)) + 1
  346. end
  347. itp('fails if given filename is NULL', function()
  348. local do_expand = 1
  349. local buflen = 10
  350. local buf = cstr(buflen, '')
  351. local result = cimp.vim_FullName(NULL, buf, buflen, do_expand)
  352. eq(FAIL, result)
  353. end)
  354. itp('fails safely if given length is wrong #5737', function()
  355. local filename = 'foo/bar/bazzzzzzz/buz/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/a'
  356. local too_short_len = 8
  357. local buf = cstr(too_short_len, '')
  358. local do_expand = 1
  359. local result = cimp.vim_FullName(filename, buf, too_short_len, do_expand)
  360. local expected = string.sub(filename, 1, (too_short_len - 1))
  361. eq(expected, ffi.string(buf))
  362. eq(FAIL, result)
  363. end)
  364. itp('uses the filename if the filename is a URL', function()
  365. local filename = 'http://www.neovim.org'
  366. local buflen = string.len(filename) + 1
  367. local do_expand = 1
  368. local buf, result = vim_FullName(filename, buflen, do_expand)
  369. eq(filename, ffi.string(buf))
  370. eq(OK, result)
  371. end)
  372. itp('fails and uses filename if given filename contains non-existing directory', function()
  373. -- test with different filename lengths
  374. for rep = 1, 10 do
  375. local filename = ('non_existing_'):rep(rep) .. 'dir/test.file'
  376. local buflen = string.len(filename) + 1
  377. local do_expand = 1
  378. local buf, result = vim_FullName(filename, buflen, do_expand)
  379. eq(filename, ffi.string(buf))
  380. eq(FAIL, result)
  381. end
  382. end)
  383. itp('concatenates filename if it does not contain a slash', function()
  384. local expected = uv.cwd() .. '/test.file'
  385. local filename = 'test.file'
  386. local buflen = get_buf_len(expected, filename)
  387. local do_expand = 1
  388. local buf, result = vim_FullName(filename, buflen, do_expand)
  389. eq(expected, ffi.string(buf))
  390. eq(OK, result)
  391. end)
  392. itp('produces absolute path for .. without a slash', function()
  393. local old_dir = uv.cwd()
  394. uv.chdir('..')
  395. local expected = uv.cwd()
  396. uv.chdir(old_dir)
  397. local filename = '..'
  398. local buflen = get_buf_len(expected, filename)
  399. local do_expand = 1
  400. local buf, result = vim_FullName(filename, buflen, do_expand)
  401. eq(expected, ffi.string(buf))
  402. eq(OK, result)
  403. end)
  404. itp('produces absolute path if possible and if path contains a slash', function()
  405. local old_dir = uv.cwd()
  406. uv.chdir('..')
  407. local expected = uv.cwd() .. '/test.file'
  408. uv.chdir(old_dir)
  409. local filename = '../test.file'
  410. local buflen = get_buf_len(expected, filename)
  411. local do_expand = 1
  412. local buf, result = vim_FullName(filename, buflen, do_expand)
  413. eq(expected, ffi.string(buf))
  414. eq(OK, result)
  415. end)
  416. itp('just copies the path if it is already absolute and force=0', function()
  417. local absolute_path = '/absolute/path'
  418. local buflen = string.len(absolute_path) + 1
  419. local do_expand = 0
  420. local buf, result = vim_FullName(absolute_path, buflen, do_expand)
  421. eq(absolute_path, ffi.string(buf))
  422. eq(OK, result)
  423. end)
  424. itp('fails and uses filename when the path is relative to HOME', function()
  425. eq(false, cimp.os_isdir('~'), 'sanity check: no literal "~" directory')
  426. local absolute_path = '~/home.file'
  427. local buflen = string.len(absolute_path) + 1
  428. local do_expand = 1
  429. local buf, result = vim_FullName(absolute_path, buflen, do_expand)
  430. eq(absolute_path, ffi.string(buf))
  431. eq(FAIL, result)
  432. end)
  433. itp('works with some "normal" relative path with directories', function()
  434. local expected = uv.cwd() .. '/unit-test-directory/test.file'
  435. local filename = 'unit-test-directory/test.file'
  436. local buflen = get_buf_len(expected, filename)
  437. local do_expand = 1
  438. local buf, result = vim_FullName(filename, buflen, do_expand)
  439. eq(expected, ffi.string(buf))
  440. eq(OK, result)
  441. end)
  442. itp('does not modify the given filename', function()
  443. local expected = uv.cwd() .. '/unit-test-directory/test.file'
  444. local filename = to_cstr('unit-test-directory/test.file')
  445. local buflen = string.len(expected) + 1
  446. local buf = cstr(buflen, '')
  447. local do_expand = 1
  448. -- Don't use the wrapper but pass a cstring directly to the c function.
  449. eq('unit-test-directory/test.file', ffi.string(filename))
  450. local result = cimp.vim_FullName(filename, buf, buflen, do_expand)
  451. eq(expected, ffi.string(buf))
  452. eq(OK, result)
  453. end)
  454. itp('works with directories that have one path component', function()
  455. local filename = '/tmp'
  456. local expected = filename
  457. local buflen = get_buf_len(expected, filename)
  458. local do_expand = 1
  459. local buf, result = vim_FullName(filename, buflen, do_expand)
  460. eq('/tmp', ffi.string(buf))
  461. eq(OK, result)
  462. end)
  463. itp('does not remove trailing slash from non-existing relative directory #20847', function()
  464. local expected = uv.cwd() .. '/non_existing_dir/'
  465. local filename = 'non_existing_dir/'
  466. local buflen = get_buf_len(expected, filename)
  467. local do_expand = 1
  468. local buf, result = vim_FullName(filename, buflen, do_expand)
  469. eq(expected, ffi.string(buf))
  470. eq(OK, result)
  471. end)
  472. itp('expands "./" to the current directory #7117', function()
  473. local expected = uv.cwd() .. '/unit-test-directory/test.file'
  474. local filename = './unit-test-directory/test.file'
  475. local buflen = get_buf_len(expected, filename)
  476. local do_expand = 1
  477. local buf, result = vim_FullName(filename, buflen, do_expand)
  478. eq(OK, result)
  479. eq(expected, ffi.string(buf))
  480. end)
  481. itp('collapses "foo/../foo" to "foo" #7117', function()
  482. local expected = uv.cwd() .. '/unit-test-directory/test.file'
  483. local filename = 'unit-test-directory/../unit-test-directory/test.file'
  484. local buflen = get_buf_len(expected, filename)
  485. local do_expand = 1
  486. local buf, result = vim_FullName(filename, buflen, do_expand)
  487. eq(OK, result)
  488. eq(expected, ffi.string(buf))
  489. end)
  490. end)
  491. describe('path_fix_case', function()
  492. local function fix_case(file)
  493. local c_file = to_cstr(file)
  494. cimp.path_fix_case(c_file)
  495. return ffi.string(c_file)
  496. end
  497. before_each(function()
  498. mkdir('CamelCase')
  499. end)
  500. after_each(function()
  501. uv.fs_rmdir('CamelCase')
  502. end)
  503. if ffi.os == 'Windows' or ffi.os == 'OSX' then
  504. itp('Corrects the case of file names in Mac and Windows', function()
  505. eq('CamelCase', fix_case('camelcase'))
  506. eq('CamelCase', fix_case('cAMELcASE'))
  507. end)
  508. else
  509. itp('does nothing on Linux', function()
  510. eq('camelcase', fix_case('camelcase'))
  511. eq('cAMELcASE', fix_case('cAMELcASE'))
  512. end)
  513. end
  514. end)
  515. describe('append_path', function()
  516. itp('joins given paths with a slash', function()
  517. local path1 = cstr(100, 'path1')
  518. local to_append = to_cstr('path2')
  519. eq(OK, (cimp.append_path(path1, to_append, 100)))
  520. eq('path1/path2', (ffi.string(path1)))
  521. end)
  522. itp('joins given paths without adding an unnecessary slash', function()
  523. local path1 = cstr(100, 'path1/')
  524. local to_append = to_cstr('path2')
  525. eq(OK, cimp.append_path(path1, to_append, 100))
  526. eq('path1/path2', (ffi.string(path1)))
  527. end)
  528. itp('fails and uses filename if there is not enough space left for to_append', function()
  529. local path1 = cstr(11, 'path1/')
  530. local to_append = to_cstr('path2')
  531. eq(FAIL, (cimp.append_path(path1, to_append, 11)))
  532. end)
  533. itp('does not append a slash if to_append is empty', function()
  534. local path1 = cstr(6, 'path1')
  535. local to_append = to_cstr('')
  536. eq(OK, (cimp.append_path(path1, to_append, 6)))
  537. eq('path1', (ffi.string(path1)))
  538. end)
  539. itp('does not append unnecessary dots', function()
  540. local path1 = cstr(6, 'path1')
  541. local to_append = to_cstr('.')
  542. eq(OK, (cimp.append_path(path1, to_append, 6)))
  543. eq('path1', (ffi.string(path1)))
  544. end)
  545. itp('copies to_append to path, if path is empty', function()
  546. local path1 = cstr(7, '')
  547. local to_append = to_cstr('/path2')
  548. eq(OK, (cimp.append_path(path1, to_append, 7)))
  549. eq('/path2', (ffi.string(path1)))
  550. end)
  551. end)
  552. describe('path_is_absolute', function()
  553. local function path_is_absolute(filename)
  554. filename = to_cstr(filename)
  555. return cimp.path_is_absolute(filename)
  556. end
  557. itp('returns true if filename starts with a slash', function()
  558. eq(true, path_is_absolute('/some/directory/'))
  559. end)
  560. itp('returns true if filename starts with a tilde', function()
  561. eq(true, path_is_absolute('~/in/my/home~/directory'))
  562. end)
  563. itp('returns false if filename starts not with slash nor tilde', function()
  564. eq(false, path_is_absolute('not/in/my/home~/directory'))
  565. end)
  566. end)
  567. describe('path_with_extension', function()
  568. local function path_with_extension(filename, extension)
  569. local c_filename = to_cstr(filename)
  570. local c_extension = to_cstr(extension)
  571. return cimp.path_with_extension(c_filename, c_extension)
  572. end
  573. itp('returns true if filename includes a provided extension', function()
  574. eq(true, path_with_extension('/some/path/file.lua', 'lua'))
  575. end)
  576. itp('returns false if filename does not include a provided extension', function()
  577. eq(false, path_with_extension('/some/path/file.vim', 'lua'))
  578. eq(false, path_with_extension('/some/path/file', 'lua'))
  579. end)
  580. itp("respects 'fileignorecase' option", function()
  581. options.p_fic = false
  582. eq(false, path_with_extension('/some/path/file.VIM', 'vim'))
  583. eq(false, path_with_extension('/some/path/file.LUA', 'lua'))
  584. options.p_fic = true
  585. eq(true, path_with_extension('/some/path/file.VIM', 'vim'))
  586. eq(true, path_with_extension('/some/path/file.LUA', 'lua'))
  587. end)
  588. end)
  589. describe('path_with_url', function()
  590. itp('scheme is alpha and inner hyphen only', function()
  591. local function path_with_url(fname)
  592. return cimp.path_with_url(to_cstr(fname))
  593. end
  594. eq(1, path_with_url([[test://xyz/foo/b0]]))
  595. eq(2, path_with_url([[test:\\xyz\foo\b0]]))
  596. eq(0, path_with_url([[test+abc://xyz/foo/b1]]))
  597. eq(0, path_with_url([[test_abc://xyz/foo/b2]]))
  598. eq(1, path_with_url([[test-abc://xyz/foo/b3]]))
  599. eq(2, path_with_url([[test-abc:\\xyz\foo\b3]]))
  600. eq(0, path_with_url([[-test://xyz/foo/b4]]))
  601. eq(0, path_with_url([[test-://xyz/foo/b5]]))
  602. eq(1, path_with_url([[test-C:/xyz/foo/b5]]))
  603. eq(1, path_with_url([[test-custom:/xyz/foo/b5]]))
  604. eq(0, path_with_url([[c:/xyz/foo/b5]]))
  605. eq(0, path_with_url([[C:/xyz/foo/b5]]))
  606. end)
  607. end)
  608. end)