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