robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

taorefactor(path): pathcmp() #41035f0a0182285

master
26.6 KiB790 linesraw
1local uv = vim.uv
2local t = require('test.unit.testutil')
3local describe, before_each, after_each, setup, teardown =
4  t.describe, t.before_each, t.after_each, t.setup, t.teardown
5local itp = t.gen_itp(t.it)
6
7local cimport = t.cimport
8local eq = t.eq
9local neq = t.neq
10local ffi = t.ffi
11local cstr = t.cstr
12local to_cstr = t.to_cstr
13local NULL = t.NULL
14local OK = t.OK
15local FAIL = t.FAIL
16local mkdir = t.mkdir
17
18cimport('string.h')
19local cimp = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
20local options = cimport('./src/nvim/option_vars.h')
21
22local length = 0
23local buffer = nil
24
25describe('path.c', function()
26  describe('path_full_dir_name', function()
27    local old_dir
28
29    setup(function()
30      old_dir = uv.cwd()
31      mkdir('unit-test-directory')
32      uv.fs_symlink(old_dir .. '/unit-test-directory', 'unit-test-symlink')
33    end)
34
35    teardown(function()
36      uv.fs_unlink('unit-test-symlink')
37      uv.fs_rmdir('unit-test-directory')
38    end)
39
40    local function path_full_dir_name(directory, buf, len)
41      directory = to_cstr(directory)
42      return cimp.path_full_dir_name(directory, buf, len)
43    end
44
45    before_each(function()
46      -- Create empty string buffer which will contain the resulting path.
47      length = string.len(old_dir) + 22
48      buffer = cstr(length, '')
49    end)
50
51    after_each(function()
52      uv.chdir(old_dir)
53    end)
54
55    itp('returns the absolute directory name of a given relative one', function()
56      eq(OK, path_full_dir_name('..', buffer, length))
57      uv.chdir('..')
58      local expected = uv.cwd()
59      uv.chdir(old_dir)
60      eq(expected, ffi.string(buffer))
61    end)
62
63    itp('returns the current directory name if the given string is empty', function()
64      eq(OK, path_full_dir_name('', buffer, length))
65      eq(old_dir, ffi.string(buffer))
66    end)
67
68    local function test_full_dir_absolute()
69      itp('works with a normal absolute dir', function()
70        eq(OK, path_full_dir_name(old_dir .. '/unit-test-directory', buffer, length))
71        eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
72      end)
73
74      itp('works with a symlinked absolute dir', function()
75        eq(OK, path_full_dir_name(old_dir .. '/unit-test-symlink', buffer, length))
76        eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
77      end)
78    end
79
80    test_full_dir_absolute()
81
82    describe('when cwd does not exist #28786', function()
83      before_each(function()
84        mkdir('dir-to-remove')
85        uv.chdir('dir-to-remove')
86        uv.fs_rmdir(old_dir .. '/dir-to-remove')
87      end)
88
89      test_full_dir_absolute()
90    end)
91
92    itp('works with a normal relative dir', function()
93      eq(OK, path_full_dir_name('unit-test-directory', buffer, length))
94      eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
95    end)
96
97    itp('works with a symlinked relative dir', function()
98      eq(OK, path_full_dir_name('unit-test-symlink', buffer, length))
99      eq(old_dir .. '/unit-test-directory', ffi.string(buffer))
100    end)
101
102    itp('works with a non-existing relative dir', function()
103      eq(OK, path_full_dir_name('does-not-exist', buffer, length))
104      eq(old_dir .. '/does-not-exist', ffi.string(buffer))
105    end)
106
107    itp('fails with a non-existing absolute dir', function()
108      eq(FAIL, path_full_dir_name('/does_not_exist', buffer, length))
109    end)
110  end)
111
112  describe('path_equal', function()
113    local function path_equal(s1, s2, flags)
114      s1 = to_cstr(s1)
115      s2 = to_cstr(s2)
116      return cimp.path_equal(s1, s2, flags or cimp.kPathCmpExpand)
117    end
118
119    local f1 = 'f1.o'
120    local f2 = 'f2.o'
121    before_each(function()
122      -- create the three files that will be used in this spec
123      io.open(f1, 'w'):close()
124      io.open(f2, 'w'):close()
125    end)
126
127    after_each(function()
128      os.remove(f1)
129      os.remove(f2)
130    end)
131
132    itp('returns true when passed the same existing file', function()
133      eq(true, path_equal(f1, f1))
134      eq(true, path_equal(f1, ('%s/%s'):format(uv.fs_realpath('.'), f1)))
135    end)
136
137    itp(
138      'returns true for nonexistent same-name files via fullname fallback (kPathCmpFull)',
139      function()
140        eq(true, path_equal('null.txt', 'null.txt', cimp.kPathCmpFull))
141      end
142    )
143
144    itp('returns false for nonexistent same-name files without fullname fallback', function()
145      eq(false, path_equal('null.txt', 'null.txt'))
146    end)
147
148    itp('returns false when passed different files', function()
149      eq(false, path_equal(f1, f2))
150      eq(false, path_equal(f2, f1))
151    end)
152
153    itp('returns false if only one does not exist', function()
154      eq(false, path_equal(f1, 'null.txt'))
155      eq(false, path_equal('null.txt', f1))
156    end)
157
158    itp('returns false if two files differ literally', function()
159      eq(false, path_equal('null1.txt', 'null2.txt', cimp.kPathCmpLiteral))
160    end)
161
162    itp("respects 'fileignorecase' option", function()
163      options.p_fic = false
164      eq(false, path_equal('Foo', 'foo', cimp.kPathCmpLiteral))
165      options.p_fic = true
166      eq(true, path_equal('Foo', 'foo', cimp.kPathCmpLiteral))
167      -- mb_toupper considers ß and ẞ equal, but not İ and i.
168      -- utf_fold keeps them as they are.
169      eq(false, path_equal('foß', 'foẞ', cimp.kPathCmpLiteral))
170      eq(false, path_equal('foİ', 'foi', cimp.kPathCmpLiteral))
171    end)
172  end)
173
174  describe('path_cmp', function()
175    local function path_cmp(a, b, maxlen)
176      return cimp.path_cmp(options.p_fic, to_cstr(a), to_cstr(b), maxlen)
177    end
178
179    itp('returns 0 when passed same paths', function()
180      eq(0, path_cmp('foo/bar', 'foo/bar', 7))
181    end)
182
183    itp('returns non-zero when passed different paths', function()
184      eq(1, path_cmp('foobar', 'foo/bar', 7))
185      eq(-1, path_cmp('foo/bar', 'foobar', 7))
186      neq(0, path_cmp('foo/bar', 'foo/baz', 7))
187    end)
188
189    itp('returns 0 when maxlen truncates to a common prefix', function()
190      eq(0, path_cmp('foo/bar', 'foo/baz', 6))
191    end)
192
193    itp('ignores a single trailing path sep', function()
194      eq(0, path_cmp('foo', 'foo/', 4))
195      neq(0, path_cmp('/', '//', 2))
196      neq(0, path_cmp('foo', 'foo//', 5))
197      neq(0, path_cmp('foo/', 'foo//', 5))
198    end)
199  end)
200
201  describe('path_tail', function()
202    local function path_tail(file)
203      local res = cimp.path_tail((to_cstr(file)))
204      neq(NULL, res)
205      return ffi.string(res)
206    end
207
208    itp('returns the tail of a given file path', function()
209      eq('file.txt', path_tail('directory/file.txt'))
210    end)
211
212    itp('returns an empty string if file ends in a slash', function()
213      eq('', path_tail('directory/'))
214    end)
215  end)
216
217  describe('path_tail_with_sep', function()
218    local function path_tail_with_sep(file)
219      local res = cimp.path_tail_with_sep((to_cstr(file)))
220      neq(NULL, res)
221      return ffi.string(res)
222    end
223
224    itp('returns the tail of a file together with its separator', function()
225      eq('///file.txt', path_tail_with_sep('directory///file.txt'))
226    end)
227
228    itp('returns an empty string when given an empty file name', function()
229      eq('', path_tail_with_sep(''))
230    end)
231
232    itp('returns only the separator if there is a trailing separator', function()
233      eq('/', path_tail_with_sep('some/directory/'))
234    end)
235
236    itp('cuts a leading separator', function()
237      eq('file.txt', path_tail_with_sep('/file.txt'))
238      eq('', path_tail_with_sep('/'))
239    end)
240
241    itp('returns the whole file name if there is no separator', function()
242      eq('file.txt', path_tail_with_sep('file.txt'))
243    end)
244  end)
245
246  describe('invocation_path_tail', function()
247    -- Returns the path tail and length (out param) of the tail.
248    -- Does not convert the tail from C-pointer to lua string for use with
249    -- strcmp.
250    local function invocation_path_tail(invk)
251      local plen = ffi.new('size_t[?]', 1)
252      local ptail = cimp.invocation_path_tail((to_cstr(invk)), plen)
253      neq(NULL, ptail)
254
255      -- it does not change the output if len==NULL
256      local tail2 = cimp.invocation_path_tail((to_cstr(invk)), NULL)
257      neq(NULL, tail2)
258      eq((ffi.string(ptail)), (ffi.string(tail2)))
259      return ptail, plen[0]
260    end
261
262    -- This test mimics the intended use in C.
263    local function compare(base, pinvk, len)
264      return eq(0, (ffi.C.strncmp((to_cstr(base)), pinvk, len)))
265    end
266
267    itp('returns the executable name of an invocation given a relative invocation', function()
268      local invk, len = invocation_path_tail('directory/exe a b c')
269      compare('exe a b c', invk, len)
270      eq(3, len)
271    end)
272
273    itp('returns the executable name of an invocation given an absolute invocation', function()
274      if ffi.os == 'Windows' then
275        local invk, len = invocation_path_tail('C:\\Users\\anyone\\Program Files\\z a b')
276        compare('z a b', invk, len)
277        eq(1, len)
278      else
279        local invk, len = invocation_path_tail('/usr/bin/z a b')
280        compare('z a b', invk, len)
281        eq(1, len)
282      end
283    end)
284
285    itp('does not count arguments to the executable as part of its path', function()
286      local invk, len = invocation_path_tail('exe a/b\\c')
287      compare('exe a/b\\c', invk, len)
288      eq(3, len)
289    end)
290
291    itp('only accepts whitespace as a terminator for the executable name', function()
292      local invk, _ = invocation_path_tail('exe-a+b_c[]()|#!@$%^&*')
293      eq('exe-a+b_c[]()|#!@$%^&*', (ffi.string(invk)))
294    end)
295
296    itp('is equivalent to path_tail when args do not contain a path separator', function()
297      local ptail = cimp.path_tail(to_cstr('a/b/c x y z'))
298      neq(NULL, ptail)
299      local tail = ffi.string(ptail)
300      local invk, _ = invocation_path_tail('a/b/c x y z')
301      eq(tail, ffi.string(invk))
302    end)
303
304    itp('is not equivalent to path_tail when args contain a path separator', function()
305      local ptail = cimp.path_tail(to_cstr('a/b/c x y/z'))
306      neq(NULL, ptail)
307      local invk, _ = invocation_path_tail('a/b/c x y/z')
308      neq((ffi.string(ptail)), (ffi.string(invk)))
309    end)
310  end)
311
312  describe('path_next_component', function()
313    local function path_next_component(file)
314      local res = cimp.path_next_component((to_cstr(file)))
315      neq(NULL, res)
316      return ffi.string(res)
317    end
318
319    itp('returns', function()
320      eq('directory/file.txt', path_next_component('some/directory/file.txt'))
321    end)
322
323    itp('returns empty string if given file contains no separator', function()
324      eq('', path_next_component('file.txt'))
325    end)
326  end)
327
328  describe('path_shorten_fname', function()
329    itp('returns NULL if `full_path` is NULL', function()
330      local dir = to_cstr('some/directory/file.txt')
331      eq(NULL, (cimp.path_shorten_fname(NULL, dir)))
332    end)
333
334    itp('returns NULL if the path and dir does not match', function()
335      local dir = to_cstr('not/the/same')
336      local full = to_cstr('as/this.txt')
337      eq(NULL, (cimp.path_shorten_fname(full, dir)))
338    end)
339
340    itp('returns NULL if the path is not separated properly', function()
341      local dir = to_cstr('some/very/long/')
342      local full = to_cstr('some/very/long/directory/file.txt')
343      eq(NULL, (cimp.path_shorten_fname(full, dir)))
344    end)
345
346    itp('shortens the filename if `dir_name` is the start of `full_path`', function()
347      local full = to_cstr('some/very/long/directory/file.txt')
348      local dir = to_cstr('some/very/long')
349      eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
350      -- Also works with duplicate slashes. #37080
351      full = to_cstr('some/very/long//directory/file.txt')
352      eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
353      full = to_cstr('some/very/long///directory/file.txt')
354      eq('directory/file.txt', (ffi.string(cimp.path_shorten_fname(full, dir))))
355    end)
356  end)
357end)
358
359describe('path_try_shorten_fname', function()
360  local cwd = uv.cwd()
361
362  before_each(function()
363    mkdir('ut_directory')
364  end)
365
366  after_each(function()
367    uv.chdir(cwd)
368    uv.fs_rmdir('ut_directory')
369  end)
370
371  describe('path_try_shorten_fname', function()
372    itp('returns shortened path if possible', function()
373      uv.chdir('ut_directory')
374      local full = to_cstr(uv.cwd() .. '/subdir/file.txt')
375      eq('subdir/file.txt', (ffi.string(cimp.path_try_shorten_fname(full))))
376    end)
377
378    itp('returns `full_path` if a shorter version is not possible', function()
379      local old = uv.cwd()
380      uv.chdir('ut_directory')
381      local full = old .. '/subdir/file.txt'
382      eq(full, (ffi.string(cimp.path_try_shorten_fname(to_cstr(full)))))
383    end)
384
385    itp('returns NULL if `full_path` is NULL', function()
386      eq(NULL, (cimp.path_try_shorten_fname(NULL)))
387    end)
388  end)
389end)
390
391describe('path.c path_guess_exepath', function()
392  local cwd = uv.cwd()
393
394  for _, name in ipairs({ './nvim', '.nvim', 'foo/nvim' }) do
395    itp('"' .. name .. '" returns name catenated with CWD', function()
396      local bufsize = 255
397      local buf = cstr(bufsize, '')
398      cimp.path_guess_exepath(name, buf, bufsize)
399      eq(cwd .. '/' .. name, ffi.string(buf))
400    end)
401  end
402
403  itp('absolute path returns the name unmodified', function()
404    local name = '/foo/bar/baz'
405    local bufsize = 255
406    local buf = cstr(bufsize, '')
407    cimp.path_guess_exepath(name, buf, bufsize)
408    eq(name, ffi.string(buf))
409  end)
410
411  itp('returns the name unmodified if not found in $PATH', function()
412    local name = '23u0293_not_in_path'
413    local bufsize = 255
414    local buf = cstr(bufsize, '')
415    cimp.path_guess_exepath(name, buf, bufsize)
416    eq(name, ffi.string(buf))
417  end)
418
419  itp('does not crash if $PATH item exceeds MAXPATHL', function()
420    local orig_path_env = os.getenv('PATH')
421    local name = 'cat' -- Some executable in $PATH.
422    local bufsize = 255
423    local buf = cstr(bufsize, '')
424    local insane_path = orig_path_env .. ':' .. (('x/'):rep(4097))
425
426    cimp.os_setenv('PATH', insane_path, true)
427    cimp.path_guess_exepath(name, buf, bufsize)
428    eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
429
430    -- Restore $PATH.
431    cimp.os_setenv('PATH', orig_path_env, true)
432  end)
433
434  itp('returns full path found in $PATH', function()
435    local name = 'cat' -- Some executable in $PATH.
436    local bufsize = 255
437    local buf = cstr(bufsize, '')
438    cimp.path_guess_exepath(name, buf, bufsize)
439    -- Usually "/bin/cat" on unix, "/path/to/nvim/cat" on Windows.
440    eq('bin/' .. name, ffi.string(buf):sub(-#('bin/' .. name), -1))
441  end)
442end)
443
444describe('path.c', function()
445  setup(function()
446    mkdir('unit-test-directory')
447    io.open('unit-test-directory/test.file', 'w'):close()
448  end)
449
450  teardown(function()
451    os.remove('unit-test-directory/test.file')
452    uv.fs_rmdir('unit-test-directory')
453  end)
454
455  describe('vim_FullName', function()
456    local function vim_FullName(filename, buflen, do_expand)
457      local buf = cstr(buflen, '')
458      local result = cimp.vim_FullName(to_cstr(filename), buf, buflen, do_expand)
459      return buf, result
460    end
461
462    local function get_buf_len(s, q)
463      return math.max(string.len(s), string.len(q)) + 1
464    end
465
466    itp('fails if given filename is NULL', function()
467      local do_expand = 1
468      local buflen = 10
469      local buf = cstr(buflen, '')
470      local result = cimp.vim_FullName(NULL, buf, buflen, do_expand)
471      eq(FAIL, result)
472    end)
473
474    itp('fails safely if given length is wrong #5737', function()
475      local filename = 'foo/bar/bazzzzzzz/buz/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/a'
476      local too_short_len = 8
477      local buf = cstr(too_short_len, '')
478      local do_expand = 1
479      local result = cimp.vim_FullName(filename, buf, too_short_len, do_expand)
480      local expected = string.sub(filename, 1, (too_short_len - 1))
481      eq(expected, ffi.string(buf))
482      eq(FAIL, result)
483    end)
484
485    itp('uses the filename if the filename is a URL', function()
486      local filename = 'http://www.neovim.org'
487      local buflen = string.len(filename) + 1
488      local do_expand = 1
489      local buf, result = vim_FullName(filename, buflen, do_expand)
490      eq(filename, ffi.string(buf))
491      eq(OK, result)
492    end)
493
494    itp('fails and uses filename if given filename contains non-existing directory', function()
495      -- test with different filename lengths
496      for rep = 1, 10 do
497        local filename = ('non_existing_'):rep(rep) .. 'dir/test.file'
498        local buflen = string.len(filename) + 1
499        local do_expand = 1
500        local buf, result = vim_FullName(filename, buflen, do_expand)
501        eq(filename, ffi.string(buf))
502        eq(FAIL, result)
503      end
504    end)
505
506    itp('concatenates filename if it does not contain a slash', function()
507      local expected = uv.cwd() .. '/test.file'
508      local filename = 'test.file'
509      local buflen = get_buf_len(expected, filename)
510      local do_expand = 1
511      local buf, result = vim_FullName(filename, buflen, do_expand)
512      eq(expected, ffi.string(buf))
513      eq(OK, result)
514    end)
515
516    itp('produces absolute path for .. without a slash', function()
517      local old_dir = uv.cwd()
518      uv.chdir('..')
519      local expected = uv.cwd()
520      uv.chdir(old_dir)
521      local filename = '..'
522      local buflen = get_buf_len(expected, filename)
523      local do_expand = 1
524      local buf, result = vim_FullName(filename, buflen, do_expand)
525      eq(expected, ffi.string(buf))
526      eq(OK, result)
527    end)
528
529    itp('produces absolute path if possible and if path contains a slash', function()
530      local old_dir = uv.cwd()
531      uv.chdir('..')
532      local expected = uv.cwd() .. '/test.file'
533      uv.chdir(old_dir)
534      local filename = '../test.file'
535      local buflen = get_buf_len(expected, filename)
536      local do_expand = 1
537      local buf, result = vim_FullName(filename, buflen, do_expand)
538      eq(expected, ffi.string(buf))
539      eq(OK, result)
540    end)
541
542    itp('just copies the path if it is already absolute and force=0', function()
543      local absolute_path = '/absolute/path'
544      local buflen = string.len(absolute_path) + 1
545      local do_expand = 0
546      local buf, result = vim_FullName(absolute_path, buflen, do_expand)
547      eq(absolute_path, ffi.string(buf))
548      eq(OK, result)
549    end)
550
551    itp('fails and uses filename when the path is relative to HOME', function()
552      eq(false, cimp.os_isdir('~'), 'sanity check: no literal "~" directory')
553      local absolute_path = '~/home.file'
554      local buflen = string.len(absolute_path) + 1
555      local do_expand = 1
556      local buf, result = vim_FullName(absolute_path, buflen, do_expand)
557      eq(absolute_path, ffi.string(buf))
558      eq(FAIL, result)
559    end)
560
561    itp('works with some "normal" relative path with directories', function()
562      local expected = uv.cwd() .. '/unit-test-directory/test.file'
563      local filename = 'unit-test-directory/test.file'
564      local buflen = get_buf_len(expected, filename)
565      local do_expand = 1
566      local buf, result = vim_FullName(filename, buflen, do_expand)
567      eq(expected, ffi.string(buf))
568      eq(OK, result)
569    end)
570
571    itp('does not modify the given filename', function()
572      local expected = uv.cwd() .. '/unit-test-directory/test.file'
573      local filename = to_cstr('unit-test-directory/test.file')
574      local buflen = string.len(expected) + 1
575      local buf = cstr(buflen, '')
576      local do_expand = 1
577      -- Don't use the wrapper but pass a cstring directly to the c function.
578      eq('unit-test-directory/test.file', ffi.string(filename))
579      local result = cimp.vim_FullName(filename, buf, buflen, do_expand)
580      eq(expected, ffi.string(buf))
581      eq(OK, result)
582    end)
583
584    itp('works with directories that have one path component', function()
585      local filename = '/tmp'
586      local expected = filename
587      local buflen = get_buf_len(expected, filename)
588      local do_expand = 1
589      local buf, result = vim_FullName(filename, buflen, do_expand)
590      eq('/tmp', ffi.string(buf))
591      eq(OK, result)
592    end)
593
594    itp('does not remove trailing slash from non-existing relative directory #20847', function()
595      local expected = uv.cwd() .. '/non_existing_dir/'
596      local filename = 'non_existing_dir/'
597      local buflen = get_buf_len(expected, filename)
598      local do_expand = 1
599      local buf, result = vim_FullName(filename, buflen, do_expand)
600      eq(expected, ffi.string(buf))
601      eq(OK, result)
602    end)
603
604    itp('expands "./" to the current directory #7117', function()
605      local expected = uv.cwd() .. '/unit-test-directory/test.file'
606      local filename = './unit-test-directory/test.file'
607      local buflen = get_buf_len(expected, filename)
608      local do_expand = 1
609      local buf, result = vim_FullName(filename, buflen, do_expand)
610      eq(OK, result)
611      eq(expected, ffi.string(buf))
612    end)
613
614    itp('collapses "foo/../foo" to "foo" #7117', function()
615      local expected = uv.cwd() .. '/unit-test-directory/test.file'
616      local filename = 'unit-test-directory/../unit-test-directory/test.file'
617      local buflen = get_buf_len(expected, filename)
618      local do_expand = 1
619      local buf, result = vim_FullName(filename, buflen, do_expand)
620      eq(OK, result)
621      eq(expected, ffi.string(buf))
622    end)
623  end)
624
625  describe('path_fix_case', function()
626    local function fix_case(file)
627      local c_file = to_cstr(file)
628      cimp.path_fix_case(c_file)
629      return ffi.string(c_file)
630    end
631
632    before_each(function()
633      mkdir('CamelCase')
634    end)
635    after_each(function()
636      uv.fs_rmdir('CamelCase')
637    end)
638
639    if ffi.os == 'Windows' or ffi.os == 'OSX' then
640      itp('Corrects the case of file names in Mac and Windows', function()
641        eq('CamelCase', fix_case('camelcase'))
642        eq('CamelCase', fix_case('cAMELcASE'))
643      end)
644    else
645      itp('does nothing on Linux', function()
646        eq('camelcase', fix_case('camelcase'))
647        eq('cAMELcASE', fix_case('cAMELcASE'))
648      end)
649    end
650  end)
651
652  describe('append_path', function()
653    itp('joins given paths with a slash', function()
654      local path1 = cstr(100, 'path1')
655      local to_append = to_cstr('path2')
656      eq(OK, (cimp.append_path(path1, to_append, 100)))
657      eq('path1/path2', (ffi.string(path1)))
658    end)
659
660    itp('joins given paths without adding an unnecessary slash', function()
661      local path1 = cstr(100, 'path1/')
662      local to_append = to_cstr('path2')
663      eq(OK, cimp.append_path(path1, to_append, 100))
664      eq('path1/path2', (ffi.string(path1)))
665    end)
666
667    itp('fails and uses filename if there is not enough space left for to_append', function()
668      local path1 = cstr(11, 'path1/')
669      local to_append = to_cstr('path2')
670      eq(FAIL, (cimp.append_path(path1, to_append, 11)))
671    end)
672
673    itp('does not append a slash if to_append is empty', function()
674      local path1 = cstr(6, 'path1')
675      local to_append = to_cstr('')
676      eq(OK, (cimp.append_path(path1, to_append, 6)))
677      eq('path1', (ffi.string(path1)))
678    end)
679
680    itp('does not append unnecessary dots', function()
681      local path1 = cstr(6, 'path1')
682      local to_append = to_cstr('.')
683      eq(OK, (cimp.append_path(path1, to_append, 6)))
684      eq('path1', (ffi.string(path1)))
685    end)
686
687    itp('copies to_append to path, if path is empty', function()
688      local path1 = cstr(7, '')
689      local to_append = to_cstr('/path2')
690      eq(OK, (cimp.append_path(path1, to_append, 7)))
691      eq('/path2', (ffi.string(path1)))
692    end)
693  end)
694
695  describe('path_is_absolute', function()
696    local function path_is_absolute(filename)
697      filename = to_cstr(filename)
698      return cimp.path_is_absolute(filename)
699    end
700
701    itp('returns true if filename starts with a slash', function()
702      eq(true, path_is_absolute('/some/directory/'))
703    end)
704
705    itp('returns true if filename starts with a tilde', function()
706      eq(true, path_is_absolute('~/in/my/home~/directory'))
707    end)
708
709    itp('returns false if filename starts not with slash nor tilde', function()
710      eq(false, path_is_absolute('not/in/my/home~/directory'))
711    end)
712  end)
713
714  describe('path_with_extension', function()
715    local function path_with_extension(filename, extension)
716      local c_filename = to_cstr(filename)
717      local c_extension = to_cstr(extension)
718      return cimp.path_with_extension(c_filename, c_extension)
719    end
720
721    itp('returns true if filename includes a provided extension', function()
722      eq(true, path_with_extension('/some/path/file.lua', 'lua'))
723    end)
724
725    itp('returns false if filename does not include a provided extension', function()
726      eq(false, path_with_extension('/some/path/file.vim', 'lua'))
727      eq(false, path_with_extension('/some/path/file', 'lua'))
728    end)
729
730    itp("respects 'fileignorecase' option", function()
731      options.p_fic = false
732      eq(false, path_with_extension('/some/path/file.VIM', 'vim'))
733      eq(false, path_with_extension('/some/path/file.LUA', 'lua'))
734      options.p_fic = true
735      eq(true, path_with_extension('/some/path/file.VIM', 'vim'))
736      eq(true, path_with_extension('/some/path/file.LUA', 'lua'))
737    end)
738  end)
739
740  describe('path_with_url', function()
741    itp('scheme is alpha and inner numeric, "+", "-", "." only', function()
742      local function path_with_url(fname)
743        return cimp.path_with_url(to_cstr(fname))
744      end
745
746      -- Check normal scheme with just alphabetic
747      eq(1, path_with_url([[test://xyz/foo/b0]]))
748      eq(2, path_with_url([[test:\\xyz\foo\b0]]))
749
750      -- Check valid scheme with just alphanumeric
751      eq(1, path_with_url([[test123://xyz/foo/b0]]))
752      eq(2, path_with_url([[test123:\\xyz\foo\b0]]))
753
754      -- Check invalid scheme (contains invalid character)
755      eq(0, path_with_url([[test_abc://xyz/foo/b2]]))
756
757      -- Check valid scheme containing '+', '-', or '.'
758      eq(1, path_with_url([[test+abc://xyz/foo/b1]]))
759      eq(2, path_with_url([[test+abc:\\xyz\foo\b1]]))
760      eq(1, path_with_url([[test-abc://xyz/foo/b3]]))
761      eq(2, path_with_url([[test-abc:\\xyz\foo\b3]]))
762      eq(1, path_with_url([[test.abc://xyz/foo/b1]]))
763      eq(2, path_with_url([[test.abc:\\xyz\foo\b1]]))
764
765      -- Check valid scheme with full suite of allowed characters
766      eq(1, path_with_url([[test+abc-123.ghi://xyz/foo/b1]]))
767      eq(2, path_with_url([[test+abc-123.ghi:\\xyz\foo\b1]]))
768
769      -- Check invalid scheme starting or ending with '+', '-', or '.'
770      eq(0, path_with_url([[-test://xyz/foo/b4]]))
771      eq(0, path_with_url([[test-://xyz/foo/b5]]))
772      eq(0, path_with_url([[+test://xyz/foo/b4]]))
773      eq(0, path_with_url([[test+://xyz/foo/b5]]))
774      eq(0, path_with_url([[.test://xyz/foo/b4]]))
775      eq(0, path_with_url([[test.://xyz/foo/b5]]))
776
777      -- Check additional valid scheme containing '+', '-', or '.'
778      eq(1, path_with_url([[test-C:/xyz/foo/b5]]))
779      eq(1, path_with_url([[test-custom:/xyz/foo/b5]]))
780      eq(1, path_with_url([[test+C:/xyz/foo/b5]]))
781      eq(1, path_with_url([[test+custom:/xyz/foo/b5]]))
782      eq(1, path_with_url([[test.C:/xyz/foo/b5]]))
783      eq(1, path_with_url([[test.custom:/xyz/foo/b5]]))
784
785      -- Check invalid scheme representing drive letter
786      eq(0, path_with_url([[c:/xyz/foo/b5]]))
787      eq(0, path_with_url([[C:/xyz/foo/b5]]))
788    end)
789  end)
790end)