robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , setup , teardown = 5t . describe , t . it , t . before_each , t . setup , t . teardown 6local clear =n . clear 7local command =n . command 8local eq =t . eq 9local eval =n . eval 10local feed =n . feed 11local fn =n . fn 12local insert =n . insert 13local is_os =t . is_os 14local mkdir =t . mkdir 15local rmdir =n . rmdir 16local write_file =t . write_file 17local api =n . api 18 19local function join_path ( ... ) 20return table . concat ( { ... } , '/' ) 21end 22 23describe ( 'path' , function () 24local targetdir 25local expected_path 26 27before_each ( function () 28targetdir =join_path ( 'test' , 'functional' , 'fixtures' ) 29clear () 30command ( 'edit ' .. join_path ( targetdir , 'tty-test.c' )) 31expected_path =eval ( 'expand("%:p")' ) 32end ) 33 34it ( 'with /./ segment #7117' , function () 35command ( 'edit ' .. join_path ( targetdir , '.' , 'tty-test.c' )) 36eq ( expected_path , eval ( 'expand("%:p")' )) 37end ) 38 39it ( 'with ./ prefix #7117' , function () 40command ( 'edit ' .. join_path ( '.' , targetdir , 'tty-test.c' )) 41eq ( expected_path , eval ( 'expand("%:p")' )) 42end ) 43 44it ( 'with ./ prefix, after directory change #7117' , function () 45command ( 'edit ' .. join_path ( '.' , targetdir , 'tty-test.c' )) 46command ( 'cd test' ) 47eq ( expected_path , eval ( 'expand("%:p")' )) 48end ) 49 50it ( 'with /../ segment #7117' , function () 51command ( 'edit ' .. join_path ( targetdir , '..' , 'fixtures' , 'tty-test.c' )) 52eq ( expected_path , eval ( 'expand("%:p")' )) 53end ) 54 55it ( 'with ../ and different starting directory #7117' , function () 56command ( 'cd test' ) 57command ( 'edit ' .. join_path ( '..' , targetdir , 'tty-test.c' )) 58eq ( expected_path , eval ( 'expand("%:p")' )) 59end ) 60 61it ( 'with ./../ and different starting directory #7117' , function () 62command ( 'cd test' ) 63command ( 'edit ' .. join_path ( '.' , '..' , targetdir , 'tty-test.c' )) 64eq ( expected_path , eval ( 'expand("%:p")' )) 65end ) 66 67it ( 'with implicit drive letter #40013' , function () 68t . skip ( not is_os ( 'win' ), 'N/A: only works on Windows' ) 69command ( 'edit ' .. expected_path : sub ( 3 )) 70eq ( 1 , # fn . getbufinfo ()) 71eq ( expected_path , eval ( 'expand("%:p")' )) 72end ) 73end ) 74 75describe ( 'startup' , function () 76local home , foo , bar =vim . fs . normalize ( '~' ), 'Xtest-foo.lua' , 'Xtest-bar.lua' 77 78setup ( function () 79write_file (( '%s/%s' ): format ( home , foo ), 'local foo = 1;' ) 80write_file (( '%s/%s' ): format ( home , bar ), '' ) 81end ) 82 83teardown ( function () 84os . remove (( '%s/%s' ): format ( home , foo )) 85os . remove (( '%s/%s' ): format ( home , bar )) 86end ) 87 88it ( 'expands tilde-prefixed paths #29380' , function () 89t . skip ( not is_os ( 'win' ), 'N/A: shell expands ~ on Unix' ) 90clear { 91args ={ ( '~/%s' ): format ( foo ), ( '~\\%s' ): format ( bar ) } , 92} 93local expected ={ ( '%s/%s' ): format ( home , foo ), ( '%s/%s' ): format ( home , bar ) } 94eq ( expected , vim . tbl_map ( fn . bufname , api . nvim_list_bufs ())) 95eq ( expected , fn . argv ()) 96end ) 97 98it ( 'normalizes v:progpath #39382' , function () 99clear () 100eq ( 101n . nvim_prog , 102fn . system ( { 103is_os ( 'win' ) and n . nvim_prog : gsub ( '/' , '\\' ) or n . nvim_prog , 104'--clean' , 105'--headless' , 106'+echo v:progpath' , 107'+q' , 108} ) 109) 110end ) 111 112it ( 'normalizes -u arguments and triggers autocmd events #39382' , function () 113local file =( '%s/%s' ): format ( home , foo ) 114if is_os ( 'win' ) then 115file =file : gsub ( '/' , '\\' ) 116end 117clear { 118args_rm ={ '--cmd' , '-u' } , 119args ={ 120'--cmd' , 121( 'autocmd SourcePre */%s let g:matched = 1' ): format ( foo ), 122'-u' , 123file , 124} , 125} 126local scripts =fn . getscriptinfo ( { name =foo } ) 127eq ( 1 , # scripts ) 128eq (( '%s/%s' ): format ( home , foo ), scripts [ 1 ]. name ) 129t . ok ( fn . eval ( 'g:matched' )) 130end ) 131end ) 132 133describe ( 'expand wildcard' , function () 134before_each ( clear ) 135 136it ( 'with special characters #24421' , function () 137local folders =is_os ( 'win' ) and { 138'{folder}' , 139'folder$name' , 140} or { 141'folder-name' , 142'folder#name' , 143} 144for _ , folder in ipairs ( folders ) do 145mkdir ( folder ) 146local file =( '%s%sfile.txt' ): format ( folder , n . get_pathsep ()) 147write_file ( file , '' ) 148eq ( file , fn . expand (( '%s/*' ): format ( folder ))) 149rmdir ( folder ) 150end 151end ) 152end ) 153 154describe ( 'file search' , function () 155local testdir ='Xtest_path_spec' 156 157before_each ( clear ) 158 159setup ( function () 160mkdir ( testdir ) 161end ) 162 163teardown ( function () 164rmdir ( testdir ) 165end ) 166 167it ( 'gf finds multibyte filename in line #20517' , function () 168command ( 'cd test/functional/fixtures' ) 169insert ( 'filename_with_unicode_ααα' ) 170eq ( '' , eval ( 'expand("%")' )) 171feed ( 'gf' ) 172eq ( 'filename_with_unicode_ααα' , eval ( 'expand("%:t")' )) 173end ) 174 175it ( 'gf/<cfile> matches Windows drive-letter filepaths (without ":" in &isfname)' , function () 176local iswin =is_os ( 'win' ) 177local function test_cfile ( input , expected , expected_win ) 178expected =( iswin and expected_win or expected ) or input 179command ( '%delete' ) 180insert ( input ) 181command ( 'norm! 0' ) 182eq ( expected , eval ( 'expand("<cfile>")' )) 183end 184 185-- test_cfile([[c:/d:/e:/foo/bar.txt]], 'c:/d:/e') -- TODO(justinmk): should return "d:/foo/bar.txt" ? 186test_cfile ( [[c:/d:/foo/bar.txt]] ) -- TODO(justinmk): should return "d:/foo/bar.txt" ? 187test_cfile ( [[//share/c:/foo/bar/]] ) 188test_cfile ( [[file://c:/foo/bar]] ) 189test_cfile ( [[file://c:/foo/bar:42]] ) 190test_cfile ( [[file://c:/foo/bar:42:666]] ) 191test_cfile ( [[https://c:/foo/bar]] ) 192test_cfile ( [[\foo\bar]] , [[foo]] , [[\foo\bar]] ) 193test_cfile ( [[/foo/bar]] , [[/foo/bar]] ) 194test_cfile ( [[c:\foo\bar]] , [[c:]] , [[c:\foo\bar]] ) 195test_cfile ( [[c:\foo\bar:42:666]] , [[c:]] , [[c:\foo\bar]] ) 196test_cfile ( [[c:/foo/bar]] ) 197test_cfile ( [[c:/foo/bar:42]] , [[c:/foo/bar]] ) 198test_cfile ( [[c:/foo/bar:42:666]] , [[c:/foo/bar]] ) 199test_cfile ( [[c:foo\bar]] , [[c]] ) 200test_cfile ( [[c:foo/bar]] , [[c]] ) 201test_cfile ( [[c:foo]] , [[c]] ) 202-- Examples from: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#example-ways-to-refer-to-the-same-file 203test_cfile ( [[c:\temp\test-file.txt]] , [[c:]] , [[c:\temp\test-file.txt]] ) 204test_cfile ( 205[[\\127.0.0.1\c$\temp\test-file.txt]] , 206[[127.0.0.1]] , 207[[\\127.0.0.1\c$\temp\test-file.txt]] 208) 209test_cfile ( 210[[\\LOCALHOST\c$\temp\test-file.txt]] , 211[[LOCALHOST]] , 212[[\\LOCALHOST\c$\temp\test-file.txt]] 213) 214-- not supported yet 215test_cfile ( [[\\.\c:\temp\test-file.txt]] , [[.]] , [[\\.\c]] ) 216-- not supported yet 217test_cfile ( [[\\?\c:\temp\test-file.txt]] , [[c:]] , [[\\]] ) 218test_cfile ( 219[[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]] , 220[[.]] , 221[[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]] 222) 223test_cfile ( 224[[\\127.0.0.1\c$\temp\test-file.txt]] , 225[[127.0.0.1]] , 226[[\\127.0.0.1\c$\temp\test-file.txt]] 227) 228end ) 229 230it ( 'gf/<cfile> handles local file: paths' , function () 231local path =fn . fnamemodify ( join_path ( testdir , 'gf-target.txt' ), ':p' ): gsub ( '\\' , '/' ) 232local uri ='file:' .. ( is_os ( 'win' ) and '/' or '' ) .. path 233write_file ( path , '' ) 234insert ( uri ) 235command ( 'norm! 0' ) 236eq ( path , eval ( 'expand("<cfile>")' )) 237feed ( 'gf' ) 238eq ( path , fn . fnamemodify ( eval ( 'expand("%:p")' ), ':p' ): gsub ( '\\' , '/' )) 239end ) 240 241---@param funcname 'finddir' | 'findfile' 242local function test_find_func ( funcname , folder , item ) 243local d =join_path ( testdir , folder ) 244mkdir ( d ) 245local expected =join_path ( d , item ) 246if funcname == 'finddir' then 247mkdir ( expected ) 248else 249write_file ( expected , '' ) 250end 251eq ( expected , fn [ funcname ]( item , d : gsub ( ' ' , [[\ ]] ))) 252end 253 254it ( 'finddir()' , function () 255test_find_func ( 'finddir' , 'directory' , 'folder' ) 256test_find_func ( 'finddir' , 'directory' , 'folder name' ) 257test_find_func ( 'finddir' , 'fold#er name' , 'directory' ) 258end ) 259 260it ( 'findfile()' , function () 261test_find_func ( 'findfile' , 'directory' , 'file.txt' ) 262test_find_func ( 'findfile' , 'directory' , 'file name.txt' ) 263test_find_func ( 'findfile' , 'fold#er name' , 'file.txt' ) 264end ) 265end )