robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each , setup , finally = 5t . describe , t . it , t . before_each , t . after_each , t . setup , t . finally 6local clear =n . clear 7local exec_lua =n . exec_lua 8local eq =t . eq 9local eq_paths =t . eq_paths 10local mkdir_p =n . mkdir_p 11local rmdir =n . rmdir 12local nvim_dir =n . nvim_dir 13local command =n . command 14local api =n . api 15local fn =n . fn 16local test_build_dir =t . paths . test_build_dir 17local test_source_path =t . paths . test_source_path 18local nvim_prog =n . nvim_prog 19local is_os =t . is_os 20local mkdir =t . mkdir 21 22local nvim_prog_basename =is_os ( 'win' ) and 'nvim.exe' or 'nvim' 23 24local link_limit =is_os ( 'win' ) and 64 or ( is_os ( 'mac' ) or is_os ( 'bsd' )) and 33 or 41 25 26local test_basename_dirname_eq ={ 27'~/foo/' , 28'~/foo' , 29'~/foo/bar.lua' , 30'foo.lua' , 31' ' , 32'' , 33'.' , 34'..' , 35'../' , 36'~' , 37'/usr/bin' , 38'/usr/bin/gcc' , 39'/' , 40'/usr/' , 41'/usr' , 42'c:/usr' , 43'c:/' , 44'c:' , 45'c:/users/foo' , 46'c:/users/foo/bar.lua' , 47'c:/users/foo/bar/../' , 48'~/foo/bar\\baz' , 49} 50 51local tests_windows_paths ={ 52'c:\\usr' , 53'c:\\' , 54'c:' , 55'c:\\users\\foo' , 56'c:\\users\\foo\\bar.lua' , 57'c:\\users\\foo\\bar\\..\\' , 58} 59 60setup ( clear ) 61 62describe ( 'vim.fs' , function () 63describe ( 'parents()' , function () 64it ( 'works' , function () 65local test_dir =nvim_dir .. '/test' 66mkdir_p ( test_dir ) 67local dirs ={} --- @type string[] 68for dir in vim . fs . parents ( test_dir .. '/foo.txt' ) do 69dirs [ # dirs + 1 ] =dir 70if dir == test_build_dir then 71break 72end 73end 74eq ( { test_dir , nvim_dir , test_build_dir } , dirs ) 75rmdir ( test_dir ) 76end ) 77end ) 78 79describe ( 'dirname()' , function () 80it ( 'works' , function () 81eq ( test_build_dir , vim . fs . dirname ( nvim_dir )) 82 83---@param paths string[] 84---@param is_win? boolean 85local function test_paths ( paths , is_win ) 86local gsub =is_win and [[:gsub('\\', '/')]] or '' 87local code =string . format ( 88[[ 89local path = ... 90return vim.fn.fnamemodify(path,':h')%s 91]] , 92gsub 93) 94 95for _ , path in ipairs ( paths ) do 96eq ( exec_lua ( code , path ), vim . fs . dirname ( path ), path ) 97end 98end 99 100test_paths ( test_basename_dirname_eq ) 101if is_os ( 'win' ) then 102test_paths ( tests_windows_paths , true ) 103end 104end ) 105 106it ( 'trims redundant slashes #37698' , function () 107eq ( '/name' , vim . fs . dirname ( '/name//////////' )) 108end ) 109end ) 110 111describe ( 'basename()' , function () 112it ( 'works' , function () 113eq ( nvim_prog_basename , vim . fs . basename ( nvim_prog )) 114 115---@param paths string[] 116---@param is_win? boolean 117local function test_paths ( paths , is_win ) 118local gsub =is_win and [[:gsub('\\', '/')]] or '' 119local code =string . format ( 120[[ 121local path = ... 122return vim.fn.fnamemodify(path,':t')%s 123]] , 124gsub 125) 126 127for _ , path in ipairs ( paths ) do 128eq ( exec_lua ( code , path ), vim . fs . basename ( path ), path ) 129end 130end 131 132test_paths ( test_basename_dirname_eq ) 133if is_os ( 'win' ) then 134test_paths ( tests_windows_paths , true ) 135end 136end ) 137 138it ( 'trims redundant slashes #37698' , function () 139-- XXX: for better or worse, this matches python's `os.path.basename`. 140-- https://github.com/neovim/neovim/issues/37698#issuecomment-3847866806 141eq ( '' , vim . fs . basename ( '/name//////////' )) 142end ) 143end ) 144 145describe ( 'dir()' , function () 146before_each ( function () 147mkdir ( 'testd' ) 148mkdir ( 'testd/a' ) 149mkdir ( 'testd/a/b' ) 150mkdir ( 'testd/a/b/c' ) 151end ) 152 153after_each ( function () 154rmdir ( 'testd' ) 155end ) 156 157it ( 'works' , function () 158eq ( 159true , 160exec_lua ( function () 161for name , type in vim . fs . dir ( nvim_dir ) do 162if name == nvim_prog_basename and type == 'file' then 163return true 164end 165end 166return false 167end ) 168) 169end ) 170 171it ( 'works with opts.depth, opts.skip and opts.follow' , function () 172io . open ( 'testd/a1' , 'w' ): close () 173io . open ( 'testd/b1' , 'w' ): close () 174io . open ( 'testd/c1' , 'w' ): close () 175io . open ( 'testd/a/a2' , 'w' ): close () 176io . open ( 'testd/a/b2' , 'w' ): close () 177io . open ( 'testd/a/c2' , 'w' ): close () 178io . open ( 'testd/a/b/a3' , 'w' ): close () 179io . open ( 'testd/a/b/b3' , 'w' ): close () 180io . open ( 'testd/a/b/c3' , 'w' ): close () 181io . open ( 'testd/a/b/c/a4' , 'w' ): close () 182io . open ( 'testd/a/b/c/b4' , 'w' ): close () 183io . open ( 'testd/a/b/c/c4' , 'w' ): close () 184 185local function run ( dir , depth , skip , follow ) 186return exec_lua ( function ( follow_ ) 187local r ={} --- @type table<string, string> 188local skip_f --- @type function 189if skip then 190skip_f =function ( n0 ) 191if vim . tbl_contains ( skip or {} , n0 ) then 192return false 193end 194end 195end 196for name , type_ in vim . fs . dir ( dir , { depth =depth , skip =skip_f , follow =follow_ } ) do 197r [ name ] =type_ 198end 199return r 200end , follow ) 201end 202 203local exp ={} 204 205exp [ 'a1' ] ='file' 206exp [ 'b1' ] ='file' 207exp [ 'c1' ] ='file' 208exp [ 'a' ] ='directory' 209 210eq ( exp , run ( 'testd' , 1 )) 211 212exp [ 'a/a2' ] ='file' 213exp [ 'a/b2' ] ='file' 214exp [ 'a/c2' ] ='file' 215exp [ 'a/b' ] ='directory' 216local lexp =vim . deepcopy ( exp ) 217 218eq ( exp , run ( 'testd' , 2 )) 219 220exp [ 'a/b/a3' ] ='file' 221exp [ 'a/b/b3' ] ='file' 222exp [ 'a/b/c3' ] ='file' 223exp [ 'a/b/c' ] ='directory' 224 225eq ( exp , run ( 'testd' , 3 )) 226eq ( exp , run ( 'testd' , 999 , { 'a/b/c' } )) 227 228exp [ 'a/b/c/a4' ] ='file' 229exp [ 'a/b/c/b4' ] ='file' 230exp [ 'a/b/c/c4' ] ='file' 231 232eq ( exp , run ( 'testd' , 999 )) 233 234vim . uv . fs_symlink ( vim . uv . fs_realpath ( 'testd/a' ), 'testd/l' , { junction =true , dir =true } ) 235lexp [ 'l' ] ='link' 236eq ( lexp , run ( 'testd' , 2 , nil , false )) 237 238lexp [ 'l/a2' ] ='file' 239lexp [ 'l/b2' ] ='file' 240lexp [ 'l/c2' ] ='file' 241lexp [ 'l/b' ] ='directory' 242eq ( lexp , run ( 'testd' , 2 , nil , true )) 243end ) 244 245it ( 'follow=true handles symlink loop' , function () 246local cwd ='testd/a/b/c' 247local symlink =cwd .. '/link_loop' ---@type string 248vim . uv . fs_symlink ( vim . uv . fs_realpath ( cwd ), symlink , { junction =true , dir =true } ) 249 250eq ( 251link_limit , 252exec_lua ( function () 253return # vim . iter ( vim . fs . dir ( cwd , { depth =math . huge , follow =true } )): totable () 254end ) 255) 256end ) 257 258describe ( 'fs_scandir_next fallback' , function () 259before_each ( function () 260mkdir ( 'testdir' ) 261t . write_file ( 'testdir/test.txt' , 'test file' ) 262end ) 263 264after_each ( function () 265rmdir ( 'testdir' ) 266end ) 267 268it ( 'falls back to fs_lstat when fs_scandir_next returns nil type' , function () 269local result =n . exec_lua ( [[ 270local orig = vim.uv.fs_scandir_next 271 272vim.uv.fs_scandir_next = function(fs) 273local name = orig(fs) 274if name then 275return name, nil 276end 277return name 278end 279 280local out = {} 281for name, etype in vim.fs.dir('testdir') do 282out[name] = etype 283end 284 285vim.uv.fs_scandir_next = orig 286return out 287]] ) 288 289eq ( 'file' , result [ 'test.txt' ]) 290end ) 291end ) 292 293it ( 'reports errors' , function () 294mkdir ( 'testdir' ) 295mkdir ( 'testdir/noaccess' ) 296mkdir ( 'testdir/a' ) 297mkdir ( 'testdir/a/noaccess' ) 298finally ( function () 299rmdir ( 'testdir' ) 300end ) 301 302-- With opts.err=false: errors are silent, unreadable root looks empty. 303eq ( 3040 , 305exec_lua ( function () 306local n0 =0 307for _ in vim . fs . dir ( 'does-not-exist' ) do 308n0 =n0 + 1 309end 310return n0 311end ) 312) 313 314-- With opts.err=true: unreadable root dir yields a single (name, nil, err). 315eq ( 316{ name ='does-not-exist' , err ='ENOENT: no such file or directory: does-not-exist' } , 317exec_lua ( function () 318for name , type , err in vim . fs . dir ( 'does-not-exist' , { err =true } ) do 319return { name =name , type =type , err =err } 320end 321end ) 322) 323 324-- With opts.err=true: unreadable child dir. 325local result =exec_lua ( function () 326-- Stub fs_scandir since chmod doesn't work reliably on Windows. 327local orig_scandir =vim . uv . fs_scandir 328vim . uv . fs_scandir =function ( path , ... ) 329if path == 'testdir/noaccess' then 330return nil , 'EACCES: permission denied: testdir/noaccess' 331end 332return orig_scandir ( path , ... ) 333end 334 335local errors ={} ---@type table<string, string> 336for f , _ , err in vim . fs . dir ( 'testdir' , { depth =2 , err =true } ) do 337errors [ f ] =err 338end 339 340vim . uv . fs_scandir =orig_scandir 341return errors 342end ) 343eq ( 'EACCES: permission denied: testdir/noaccess' , result [ 'noaccess' ]) 344-- nil: with depth=2 we don't scan testdir/a/noaccess. 345eq ( nil , result [ 'a/noaccess' ]) 346end ) 347 348it ( 'plain=true' , function () 349mkdir ( 'testdir' ) 350mkdir ( 'testdir/$XTEST_FS_DIR' ) 351mkdir ( 'testdir/expanded' ) 352t . write_file ( 'testdir/$XTEST_FS_DIR/literal.txt' , '' ) 353t . write_file ( 'testdir/expanded/expanded.txt' , '' ) 354finally ( function () 355rmdir ( 'testdir' ) 356end ) 357 358eq ( 359{ { [ 'expanded.txt' ] ='file' } , { [ 'literal.txt' ] ='file' } } , 360exec_lua ( function () 361vim . uv . os_setenv ( 'XTEST_FS_DIR' , 'expanded' ) 362local out ={} ---@type table<string, string>[] 363for i , plain in ipairs ( { false , true } ) do 364out [ i ] ={} 365for name , etype in vim . fs . dir ( 'testdir/$XTEST_FS_DIR' , { plain =plain } ) do 366out [ i ][ name ] =etype 367end 368end 369return out 370end ) 371) 372end ) 373end ) 374 375describe ( 'find()' , function () 376it ( 'works' , function () 377eq ( 378{ test_build_dir .. '/bin' } , 379vim . fs . find ( 'bin' , { path =nvim_dir , upward =true , type ='directory' } ) 380) 381eq ( { nvim_prog } , vim . fs . find ( nvim_prog_basename , { path =test_build_dir , type ='file' } )) 382 383local parent , name =nvim_dir : match ( '^(.*/)([^/]+)$' ) 384eq ( { nvim_dir } , vim . fs . find ( name , { path =parent , upward =true , type ='directory' } )) 385end ) 386 387-- Isolated scratch tree (not the repo) so the result is deterministic regardless of what else 388-- sits under the source path (e.g. extra "build*/" dirs). 389it ( 'follows symlinks' , function () 390local root =t . tmpname ( false ) 391mkdir ( root ) 392-- realpath() so a symlinked tmpdir (macOS /tmp -> /private/tmp) doesn't consume a hop. 393root =vim . fs . normalize ( assert ( vim . uv . fs_realpath ( root ))) 394mkdir ( root .. '/real' ) 395t . write_file ( root .. '/real/target' , '' ) 396-- Directory symlink: follow=true descends into it, follow=false does not. 397vim . uv . fs_symlink ( root .. '/real' , root .. '/lnk' , { junction =true , dir =true } ) 398 399local function find ( follow ) 400local r =vim . fs . find ( 'target' , { 401path =root , 402type ='file' , 403limit =math . huge , 404follow =follow , 405} ) 406table . sort ( r ) 407return r 408end 409 410eq ( { root .. '/lnk/target' , root .. '/real/target' } , find ( true )) 411eq ( { root .. '/real/target' } , find ( false )) 412end ) 413 414it ( 'follow=true handles symlink loop' , function () 415local root =t . tmpname ( false ) 416mkdir ( root ) 417root =assert ( vim . uv . fs_realpath ( root )) 418t . write_file ( root .. '/target' , '' ) 419-- Self-referential loop: follow=true must terminate at the OS symlink-follow limit, 420-- yielding one hit per loop level. 421vim . uv . fs_symlink ( root , root .. '/loop' , { junction =true , dir =true } ) 422 423eq ( link_limit , # vim . fs . find ( 'target' , { 424path =root , 425type ='file' , 426limit =math . huge , 427follow =true , 428} )) 429end ) 430 431it ( 'accepts predicate as names' , function () 432local opts ={ path =nvim_dir , upward =true , type ='directory' } 433eq ( 434{ test_build_dir .. '/bin' } , 435vim . fs . find ( function ( x ) 436return x == 'bin' 437end , opts ) 438) 439eq ( 440{ nvim_prog } , 441vim . fs . find ( function ( x ) 442return x == nvim_prog_basename 443end , { path =test_build_dir , type ='file' } ) 444) 445eq ( 446{} , 447vim . fs . find ( function ( x ) 448return x == 'no-match' 449end , opts ) 450) 451 452opts ={ path =test_source_path .. '/contrib' , limit =math . huge } 453eq ( 454exec_lua ( function () 455return vim . tbl_map ( 456vim . fs . basename , 457vim . fn . glob ( test_source_path .. '/contrib/*' , false , true ) 458) 459end ), 460vim . tbl_map ( 461vim . fs . basename , 462vim . fs . find ( function ( _ , d ) 463return d : match ( '[\\/]contrib$' ) 464end , opts ) 465) 466) 467end ) 468 469it ( 'reports errors' , function () 470mkdir ( 'testdir' ) 471mkdir ( 'testdir/noaccess' ) 472mkdir ( 'testdir/a' ) 473mkdir ( 'testdir/a/noaccess' ) 474t . write_file ( 'testdir/a/match.lua' , '' ) 475finally ( function () 476rmdir ( 'testdir' ) 477end ) 478 479-- Scenarios run in a shared setup(clear) so the fs_scandir/fs_access stubs (chmod is unreliable on 480-- Windows) are installed and restored. 481local res =exec_lua ( function () 482local orig_scandir =vim . uv . fs_scandir 483local orig_access =vim . uv . fs_access 484local function is_blocked ( path ) 485for _ , prefix in ipairs ( { 'testdir/noaccess' , 'testdir/a/noaccess' } ) do 486if path == prefix or vim . startswith ( path , prefix .. '/' ) then 487return true 488end 489end 490return false 491end 492vim . uv . fs_scandir =function ( path , ... ) 493if is_blocked ( path ) then 494return nil , 'EACCES: permission denied: ' .. path 495end 496return orig_scandir ( path , ... ) 497end 498vim . uv . fs_access =function ( path , ... ) 499if is_blocked ( path ) then 500return nil , 'EACCES: permission denied: ' .. path 501end 502return orig_access ( path , ... ) 503end 504 505local r ={} 506r . nonexistent ={ vim . fs . find ( 'foo' , { path ='does-not-exist' } ) } 507r . unreadable_root ={ vim . fs . find ( 'foo' , { path ='testdir/noaccess' } ) } 508local dmatches , derrors = 509vim . fs . find ( 'match.lua' , { path ='testdir' , limit =math . huge , type ='file' } ) 510table . sort ( derrors ) -- readdir order is not deterministic 511r . downward ={ dmatches , derrors } 512r . upward =select ( 5132 , 514vim . fs . find ( 'match.lua' , { 515path ='testdir/noaccess/x' , 516upward =true , 517stop ='testdir' , 518} ) 519) 520 521vim . uv . fs_scandir =orig_scandir 522vim . uv . fs_access =orig_access 523return r 524end ) 525 526-- Nonexistent / unreadable root path: no matches, one error. 527eq ( { {} , { 'ENOENT: no such file or directory: does-not-exist' } } , res . nonexistent ) 528eq ( { {} , { 'EACCES: permission denied: testdir/noaccess' } } , res . unreadable_root ) 529 530-- Downward search collects child errors, yet still returns the match found elsewhere. 531eq ( { 532{ 'testdir/a/match.lua' } , 533{ 534'EACCES: permission denied: testdir/a/noaccess' , 535'EACCES: permission denied: testdir/noaccess' , 536} , 537} , res . downward ) 538 539-- Upward search reports an error for each unreadable ancestor, in traversal order. 540eq ( { 541'EACCES: permission denied: testdir/noaccess/x' , 542'EACCES: permission denied: testdir/noaccess' , 543} , res . upward ) 544end ) 545end ) 546 547describe ( 'root()' , function () 548before_each ( function () 549command ( 'edit test/functional/fixtures/tty-test.c' ) 550end ) 551 552after_each ( function () 553command ( 'bwipe!' ) 554end ) 555 556it ( 'works with a single marker' , function () 557eq_paths ( test_source_path , exec_lua ( [[return vim.fs.root(0, 'CMakePresets.json')]] )) 558end ) 559 560it ( 'works with multiple markers' , function () 561local bufnr =api . nvim_get_current_buf () 562eq_paths ( 563vim . fs . joinpath ( test_source_path , 'test/functional/fixtures' ), 564exec_lua ( [[return vim.fs.root(..., {'CMakeLists.txt', 'CMakePresets.json'})]] , bufnr ) 565) 566end ) 567 568it ( 'nested markers have equal priority' , function () 569local bufnr =api . nvim_get_current_buf () 570eq_paths ( 571vim . fs . joinpath ( test_source_path , 'test/functional' ), 572exec_lua ( 573[[return vim.fs.root(..., { 'example_spec.lua', {'CMakeLists.txt', 'CMakePresets.json'}, '.luarc.json'})]] , 574bufnr 575) 576) 577eq_paths ( 578vim . fs . joinpath ( test_source_path , 'test/functional/fixtures' ), 579exec_lua ( 580[[return vim.fs.root(..., { {'CMakeLists.txt', 'CMakePresets.json'}, 'example_spec.lua', '.luarc.json'})]] , 581bufnr 582) 583) 584eq_paths ( 585vim . fs . joinpath ( test_source_path , 'test/functional/fixtures' ), 586exec_lua ( 587[[return vim.fs.root(..., { 588function(name, _) 589return name:match('%.txt$') 590end, 591'example_spec.lua', 592'.luarc.json' })]] , 593bufnr 594) 595) 596end ) 597 598it ( 'works with a function' , function () 599---@type string 600local result =exec_lua ( function () 601return vim . fs . root ( 0 , function ( name , _ ) 602return name : match ( '%.txt$' ) 603end ) 604end ) 605eq_paths ( vim . fs . joinpath ( test_source_path , 'test/functional/fixtures' ), result ) 606end ) 607 608it ( 'works with a filename argument' , function () 609eq ( test_source_path , exec_lua ( [[return vim.fs.root(..., 'CMakePresets.json')]] , nvim_prog )) 610end ) 611 612it ( 'works with a relative path' , function () 613eq_paths ( 614test_source_path , 615exec_lua ( [[return vim.fs.root(..., 'CMakePresets.json')]] , vim . fs . basename ( nvim_prog )) 616) 617end ) 618 619it ( 'returns CWD (absolute path) for unnamed buffers' , function () 620assert ( n . fn . isabsolutepath ( test_source_path ) == 1 ) 621command ( 'new' ) 622eq_paths ( test_source_path , exec_lua ( [[return vim.fs.root(0, 'CMakePresets.json')]] )) 623end ) 624 625it ( "returns CWD (absolute path) for buffers with non-empty 'buftype'" , function () 626assert ( n . fn . isabsolutepath ( test_source_path ) == 1 ) 627command ( 'new' ) 628command ( 'set buftype=nofile' ) 629command ( 'file lua://' ) 630eq_paths ( test_source_path , exec_lua ( [[return vim.fs.root(0, 'CMakePresets.json')]] )) 631end ) 632 633it ( 'returns CWD (absolute path) if no match is found' , function () 634assert ( n . fn . isabsolutepath ( test_source_path ) == 1 ) 635eq_paths ( 636test_source_path , 637exec_lua ( [[return vim.fs.root('file://bogus', 'CMakePresets.json')]] ) 638) 639end ) 640end ) 641 642describe ( 'joinpath()' , function () 643it ( 'works' , function () 644eq ( 'foo/bar/baz' , vim . fs . joinpath ( 'foo' , 'bar' , 'baz' )) 645eq ( 'foo/bar/baz' , vim . fs . joinpath ( 'foo' , '/bar/' , '/baz' )) 646end ) 647it ( 'rewrites backslashes on Windows' , function () 648if is_os ( 'win' ) then 649eq ( 'foo/bar/baz/zub/' , vim . fs . joinpath ( [[foo]] , [[\\bar\\\\baz]] , [[zub\]] )) 650else 651eq ( [[foo/\\bar\\\\baz/zub\]] , vim . fs . joinpath ( [[foo]] , [[\\bar\\\\baz]] , [[zub\]] )) 652end 653end ) 654it ( 'strips redundant slashes' , function () 655if is_os ( 'win' ) then 656eq ( 'foo/bar/baz/zub/' , vim . fs . joinpath ( [[foo//]] , [[\\bar\\\\baz]] , [[zub\]] )) 657else 658eq ( 'foo/bar/baz/zub/' , vim . fs . joinpath ( [[foo]] , [[//bar////baz]] , [[zub/]] )) 659end 660end ) 661it ( 'handles empty segments' , function () 662eq ( 'foo/bar' , vim . fs . joinpath ( '' , 'foo' , '' , 'bar' , '' )) 663eq ( 'foo/bar' , vim . fs . joinpath ( '' , '' , 'foo' , 'bar' , '' , '' )) 664eq ( '' , vim . fs . joinpath ( '' )) 665eq ( '' , vim . fs . joinpath ( '' , '' , '' , '' )) 666end ) 667end ) 668 669describe ( 'slug()' , function () 670it ( 'replaces unsafe characters with "-"' , function () 671-- `\` is normalized to `/` on Windows, so the hash differs per platform 672eq ( 673'a-b-c-d-e-f-g-h-i-j-k-l-' 674.. vim . fn . sha256 ( vim . fs . normalize ( 'a/b\\c:d*e?f"g<h>i|j k\tl' )): sub ( 1 , 8 ), 675vim . fs . slug ( 'a/b\\c:d*e?f"g<h>i|j k\tl' ) 676) 677eq ( 'a-16b8a9f5' , vim . fs . slug ( 'a/ ' )) 678eq ( 'a-ca978112' , vim . fs . slug ( 'a/.' )) 679end ) 680 681it ( 'works' , function () 682-- `=special` 683eq ( '=special-8a5edab2' , vim . fs . slug ( '/' )) 684eq ( '=special-ab5df625' , vim . fs . slug ( '...' )) 685eq ( '=special-11d925ec' , vim . fs . slug ( '-------' )) 686eq ( 'src-foo-init.lua-cf05d2fe' , vim . fs . slug ( '/src/foo/init.lua' )) 687eq ( 'C--src-project-main.c-3b0eb5f5' , vim . fs . slug ( 'C:/src/project/main.c' )) 688eq ( 'con.txt-d3bde286' , vim . fs . slug ( 'con.txt' )) 689-- Windows reserved names. 690eq ( 'con-1143da2b' , vim . fs . slug ( 'con' )) 691eq ( 'com¹-bdcbdc69' , vim . fs . slug ( 'com¹' )) 692eq ( 'NUL-ae6de182' , vim . fs . slug ( 'NUL' )) 693eq ( 'Prn-0d399452' , vim . fs . slug ( 'Prn' )) 694eq ( 'aux-321f6814' , vim . fs . slug ( 'aux' )) 695eq ( 'con.foo.bar-f386c405' , vim . fs . slug ( 'con.foo.bar' )) 696eq ( 'con-.-txt-48f98581' , vim . fs . slug ( 'con . txt' )) 697-- URIs are prefixed with `=uri-<scheme>-`. 698eq ( '=uri-term-foo-bar-123-bash-7fd6bb99' , vim . fs . slug ( 'term://foo/bar//123:bash' )) 699eq ( '=uri-http-example.com-2e152ec0' , vim . fs . slug ( 'http://example.com' )) 700eq ( '=uri-scp-host-path-5188b9a2' , vim . fs . slug ( 'scp://host/path' )) 701-- $HOME is replaced with `~` 702local p =vim . uv . os_homedir () .. '/my-project' 703local hash8_2 =vim . fn . sha256 ( vim . fs . normalize ( p )): sub ( 1 , 8 ) 704eq ( '~-my-project-' .. hash8_2 , vim . fs . slug ( p )) 705 706-- Windows-only cases. 707if is_os ( 'win' ) then 708eq ( '=unc-foo-dir-file-549fb6e7' , vim . fs . slug ( [[\\foo\dir\file]] )) 709-- `\\?\` and `\\.\` 710eq ( 711'---Volume{a1b2c3d4-aa00-4000-a111-1a2b3c4d5e6f}-dir-file-cfda6a98' , 712vim . fs . slug ( [[\\?\Volume{a1b2c3d4-aa00-4000-a111-1a2b3c4d5e6f}\dir\file]] ) 713) 714eq ( '---C--dir-file-e8f30888' , vim . fs . slug ( [[\\?\C:\dir\file]] )) 715eq ( '-.-COM1-e0e5710d' , vim . fs . slug ( [[\\.\COM1]] )) 716end 717end ) 718 719it ( '`opts.maxlen`' , function () 720-- maxlen < 8 is an error 721t . matches ( '`opts.maxlen` must be >= 8' , t . pcall_err ( vim . fs . slug , 'foo' , { maxlen =7 } )) 722 723eq ( '2c26b46b' , vim . fs . slug ( 'foo' , { maxlen =8 } )) 724eq ( '2c26b46b' , vim . fs . slug ( 'foo' , { maxlen =11 } )) 725eq ( 'foo-2c26b46b' , vim . fs . slug ( 'foo' , { maxlen =12 } )) 726eq ( 'ddab29ff' , vim . fs . slug ( 'foo.txt' , { maxlen =12 } )) 727eq ( 'foo-2c26b46b' , vim . fs . slug ( 'foo' , { maxlen =13 } )) 728 729-- truncates to "{head}~~~{tail}-{hash8}" 730eq ( 731'aaaa-~~~-ffff-gggg-file.txt-7bd52057' , 732vim . fs . slug ( '/aaaa/bbbb/cccc/dddd/eeee/ffff/gggg/file.txt' , { maxlen =40 } ) 733) 734eq ( '测~~~-测试3-58cf4a70' , vim . fs . slug ( '/测试1/测试测试2/测试3' , { maxlen =25 } )) 735eq ( 'ab~~~uvwxyz-71c480df' , vim . fs . slug ( 'abcdefghijklmnopqrstuvwxyz' , { maxlen =20 } )) 736eq ( 'dir~~~试.md-d4141f20' , vim . fs . slug (( 'dir/' ): rep ( 20 ) .. '测试.md' , { maxlen =22 } )) 737eq ( '~~~试abc-ab138cd6' , vim . fs . slug ( '测试abc测试abc' , { maxlen =18 } )) 738eq ( '~~~d-473a1da7' , vim . fs . slug ( 'foo/bar/longlonglong.md' , { maxlen =13 } )) 739eq ( 'f~~~md-473a1da7' , vim . fs . slug ( 'foo/bar/longlonglong.md' , { maxlen =15 } )) 740end ) 741end ) 742 743describe ( 'normalize()' , function () 744it ( 'removes trailing /' , function () 745eq ( '/home/user' , vim . fs . normalize ( '/home/user/' )) 746end ) 747it ( 'works with /' , function () 748eq ( '/' , vim . fs . normalize ( '/' )) 749end ) 750it ( 'works with ~' , function () 751eq ( vim . fs . normalize ( assert ( vim . uv . os_homedir ())) .. '/src/foo' , vim . fs . normalize ( '~/src/foo' )) 752eq ( '~/src/foo' , vim . fs . normalize ( '~/src/foo' , { plain =true } )) 753end ) 754it ( 'works with environment variables' , function () 755local xdg_config_home =test_build_dir .. '/.config' 756eq ( 757xdg_config_home .. '/nvim' , 758exec_lua ( function () 759return vim . _with ( { env ={ XDG_CONFIG_HOME =xdg_config_home } } , function () 760return vim . fs . normalize ( '$XDG_CONFIG_HOME/nvim' ) 761end ) 762end ) 763) 764eq ( '$XDG_CONFIG_HOME/nvim' , vim . fs . normalize ( '$XDG_CONFIG_HOME/nvim' , { plain =true } )) 765end ) 766 767-- Opts required for testing posix paths and win paths 768local posix_opts ={ win =false } 769local win_opts ={ win =true } 770 771it ( 'preserves leading double slashes in POSIX paths' , function () 772eq ( '//foo' , vim . fs . normalize ( '//foo' , posix_opts )) 773eq ( '//foo/bar' , vim . fs . normalize ( '//foo//bar////' , posix_opts )) 774eq ( '/foo' , vim . fs . normalize ( '///foo' , posix_opts )) 775eq ( '//' , vim . fs . normalize ( '//' , posix_opts )) 776eq ( '/' , vim . fs . normalize ( '///' , posix_opts )) 777eq ( '/foo/bar' , vim . fs . normalize ( '/foo//bar////' , posix_opts )) 778end ) 779 780it ( 'normalizes drive letter' , function () 781eq ( 'C:/' , vim . fs . normalize ( 'C:/' , win_opts )) 782eq ( 'C:/' , vim . fs . normalize ( 'c:/' , win_opts )) 783eq ( 'D:/' , vim . fs . normalize ( 'd:/' , win_opts )) 784eq ( 'C:' , vim . fs . normalize ( 'C:' , win_opts )) 785eq ( 'C:' , vim . fs . normalize ( 'c:' , win_opts )) 786eq ( 'D:' , vim . fs . normalize ( 'd:' , win_opts )) 787eq ( 'C:/foo/test' , vim . fs . normalize ( 'C:/foo/test/' , win_opts )) 788eq ( 'C:/foo/test' , vim . fs . normalize ( 'c:/foo/test/' , win_opts )) 789eq ( 'D:foo/test' , vim . fs . normalize ( 'D:foo/test/' , win_opts )) 790eq ( 'D:foo/test' , vim . fs . normalize ( 'd:foo/test/' , win_opts )) 791end ) 792 793it ( 'always treats paths as case-sensitive #31833' , function () 794eq ( 'TEST' , vim . fs . normalize ( 'TEST' , win_opts )) 795eq ( 'test' , vim . fs . normalize ( 'test' , win_opts )) 796eq ( 'C:/FOO/test' , vim . fs . normalize ( 'C:/FOO/test' , win_opts )) 797eq ( 'C:/foo/test' , vim . fs . normalize ( 'C:/foo/test' , win_opts )) 798eq ( '//SERVER/SHARE/FOO/BAR' , vim . fs . normalize ( '//SERVER/SHARE/FOO/BAR' , win_opts )) 799eq ( '//server/share/foo/bar' , vim . fs . normalize ( '//server/share/foo/bar' , win_opts )) 800eq ( 'C:/FOO/test' , vim . fs . normalize ( 'c:/FOO/test' , win_opts )) 801end ) 802 803it ( 'allows backslashes on unix-based os' , function () 804eq ( '/home/user/hello\\world' , vim . fs . normalize ( '/home/user/hello\\world' , posix_opts )) 805end ) 806 807it ( 'preserves / after drive letters' , function () 808eq ( 'C:/' , vim . fs . normalize ( [[C:\]] , win_opts )) 809end ) 810 811it ( 'works with UNC and DOS device paths' , function () 812eq ( '//server/share/foo/bar' , vim . fs . normalize ( [[\\server\\share\\\foo\bar\\\]] , win_opts )) 813eq ( '//system07/C$/' , vim . fs . normalize ( [[\\system07\C$\\\\]] , win_opts )) 814eq ( '//./C:/foo/bar' , vim . fs . normalize ( [[\\.\\C:\foo\\\\bar]] , win_opts )) 815eq ( '//?/C:/foo/bar' , vim . fs . normalize ( [[\\?\C:\\\foo\bar\\\\]] , win_opts )) 816eq ( 817'//?/UNC/server/share/foo/bar' , 818vim . fs . normalize ( [[\\?\UNC\server\\\share\\\\foo\\\bar]] , win_opts ) 819) 820eq ( '//./BootPartition/foo/bar' , vim . fs . normalize ( [[\\.\BootPartition\\foo\bar]] , win_opts )) 821eq ( 822'//./Volume{12345678-1234-1234-1234-1234567890AB}/foo/bar' , 823vim . fs . normalize ( [[\\.\Volume{12345678-1234-1234-1234-1234567890AB}\\\foo\bar\\]] , win_opts ) 824) 825end ) 826 827it ( 'handles invalid UNC and DOS device paths' , function () 828eq ( '//server/share' , vim . fs . normalize ( [[\\server\share]] , win_opts )) 829eq ( '//server/' , vim . fs . normalize ( [[\\server\]] , win_opts )) 830eq ( '//./UNC/server/share' , vim . fs . normalize ( [[\\.\UNC\server\share]] , win_opts )) 831eq ( '//?/UNC/server/' , vim . fs . normalize ( [[\\?\UNC\server\]] , win_opts )) 832eq ( '//?/UNC/server/..' , vim . fs . normalize ( [[\\?\UNC\server\..]] , win_opts )) 833eq ( '//./' , vim . fs . normalize ( [[\\.\]] , win_opts )) 834eq ( '//./foo' , vim . fs . normalize ( [[\\.\foo]] , win_opts )) 835eq ( '//./BootPartition' , vim . fs . normalize ( [[\\.\BootPartition]] , win_opts )) 836end ) 837 838it ( 'converts backward slashes' , function () 839eq ( 'C:/Users/jdoe' , vim . fs . normalize ( [[C:\Users\jdoe]] , win_opts )) 840end ) 841 842describe ( '. and .. component resolving' , function () 843it ( 'works' , function () 844-- Windows paths 845eq ( 'C:/Users' , vim . fs . normalize ( [[C:\Users\jdoe\Downloads\.\..\..\]] , win_opts )) 846eq ( 'C:/Users/jdoe' , vim . fs . normalize ( [[C:\Users\jdoe\Downloads\.\..\.\.\]] , win_opts )) 847eq ( 'C:/' , vim . fs . normalize ( 'C:/Users/jdoe/Downloads/./../../../' , win_opts )) 848eq ( 'C:foo' , vim . fs . normalize ( [[C:foo\bar\.\..\.]] , win_opts )) 849-- POSIX paths 850eq ( '/home' , vim . fs . normalize ( '/home/jdoe/Downloads/./../..' , posix_opts )) 851eq ( '/home/jdoe' , vim . fs . normalize ( '/home/jdoe/Downloads/./../././' , posix_opts )) 852eq ( '/' , vim . fs . normalize ( '/home/jdoe/Downloads/./../../../' , posix_opts )) 853-- OS-agnostic relative paths 854eq ( 'foo/bar/baz' , vim . fs . normalize ( 'foo/bar/foobar/../baz/./' )) 855eq ( 'foo/bar' , vim . fs . normalize ( 'foo/bar/foobar/../baz/./../../bar/./.' )) 856end ) 857 858it ( 'works when relative path reaches current directory' , function () 859eq ( 'C:' , vim . fs . normalize ( 'C:foo/bar/../../.' , win_opts )) 860 861eq ( '.' , vim . fs . normalize ( '.' )) 862eq ( '.' , vim . fs . normalize ( '././././' )) 863eq ( '.' , vim . fs . normalize ( 'foo/bar/../../.' )) 864end ) 865 866it ( 'works when relative path goes outside current directory' , function () 867eq ( '../../foo/bar' , vim . fs . normalize ( '../../foo/bar' )) 868eq ( '../foo' , vim . fs . normalize ( 'foo/bar/../../../foo' )) 869 870eq ( 'C:../foo' , vim . fs . normalize ( 'C:../foo' , win_opts )) 871eq ( 'C:../../foo/bar' , vim . fs . normalize ( 'C:foo/../../../foo/bar' , win_opts )) 872end ) 873 874it ( '.. in root directory resolves to itself' , function () 875eq ( 'C:/' , vim . fs . normalize ( 'C:/../../' , win_opts )) 876eq ( 'C:/foo' , vim . fs . normalize ( 'C:/foo/../../foo' , win_opts )) 877 878eq ( '//server/share/' , vim . fs . normalize ( [[\\server\share\..\..]] , win_opts )) 879eq ( '//server/share/foo' , vim . fs . normalize ( [[\\server\\share\foo\..\..\foo]] , win_opts )) 880 881eq ( '//./C:/' , vim . fs . normalize ( [[\\.\C:\..\..]] , win_opts )) 882eq ( '//?/C:/foo' , vim . fs . normalize ( [[\\?\C:\..\..\foo]] , win_opts )) 883 884eq ( '//./UNC/server/share/' , vim . fs . normalize ( [[\\.\UNC\\server\share\..\..\]] , win_opts )) 885eq ( 886'//?/UNC/server/share/foo' , 887vim . fs . normalize ( [[\\?\UNC\server\\share\..\..\foo]] , win_opts ) 888) 889 890eq ( '//?/BootPartition/' , vim . fs . normalize ( [[\\?\BootPartition\..\..]] , win_opts )) 891eq ( '//./BootPartition/foo' , vim . fs . normalize ( [[\\.\BootPartition\..\..\foo]] , win_opts )) 892 893eq ( '/' , vim . fs . normalize ( '/../../' , posix_opts )) 894eq ( '/foo' , vim . fs . normalize ( '/foo/../../foo' , posix_opts )) 895end ) 896end ) 897end ) 898 899describe ( 'abspath()' , function () 900local cwd =assert ( t . fix_slashes ( assert ( vim . uv . cwd ()))) 901local home =t . fix_slashes ( assert ( vim . uv . os_homedir ())) 902 903it ( 'expands relative paths' , function () 904assert ( n . fn . isabsolutepath ( cwd ) == 1 ) 905eq ( cwd , vim . fs . abspath ( '.' )) 906eq ( cwd .. '/foo' , vim . fs . abspath ( 'foo' )) 907eq ( cwd .. '/././foo' , vim . fs . abspath ( '././foo' )) 908eq ( cwd .. '/.././../foo' , vim . fs . abspath ( '.././../foo' )) 909end ) 910 911it ( 'works with absolute paths' , function () 912if is_os ( 'win' ) then 913eq ( [[C:/foo]] , vim . fs . abspath ( [[C:\foo]] )) 914eq ( [[C:/foo/../.]] , vim . fs . abspath ( [[C:\foo\..\.]] )) 915eq ( '//foo/bar' , vim . fs . abspath ( '\\\\foo\\bar' )) 916eq ( '//foo/bar' , vim . fs . abspath ( '\\\\foo\\bar' )) 917else 918eq ( '/foo/../.' , vim . fs . abspath ( '/foo/../.' )) 919eq ( '/foo/bar' , vim . fs . abspath ( '/foo/bar' )) 920end 921end ) 922 923it ( 'with `cwd`' , function () 924local parent_cwd =vim . fs . dirname ( cwd ) 925eq ( parent_cwd , vim . fs . abspath ( '.' , { cwd =parent_cwd } )) 926eq ( parent_cwd .. '/foo' , vim . fs . abspath ( 'foo' , { cwd =parent_cwd } )) 927eq ( parent_cwd .. '/.././../foo' , vim . fs . abspath ( '.././../foo' , { cwd =parent_cwd } )) 928eq ( '/foo/bar' , vim . fs . abspath ( '/foo/bar' , { cwd =parent_cwd } )) 929end ) 930 931it ( 'expands ~' , function () 932eq ( home .. '/foo' , vim . fs . abspath ( '~/foo' )) 933eq ( home .. '/./.././foo' , vim . fs . abspath ( '~/./.././foo' )) 934end ) 935 936it ( 'does not expand ~ if plain=true' , function () 937eq ( cwd .. '/~/foo' , vim . fs . abspath ( '~/foo' , { plain =true } )) 938eq ( cwd .. '/~/./.././foo' , vim . fs . abspath ( '~/./.././foo' , { plain =true } )) 939end ) 940 941if is_os ( 'win' ) then 942it ( 'works with drive-specific cwd on Windows' , function () 943local cwd_drive =cwd : match ( '^%w:' ) 944 945eq ( cwd .. '/foo' , vim . fs . abspath ( cwd_drive .. 'foo' )) 946end ) 947end 948end ) 949 950describe ( 'relpath()' , function () 951it ( 'works' , function () 952local cwd =assert ( t . fix_slashes ( assert ( vim . uv . cwd ()))) 953local my_dir =vim . fs . joinpath ( cwd , 'foo' ) 954 955eq ( nil , vim . fs . relpath ( '/var/lib' , '/var' )) 956eq ( nil , vim . fs . relpath ( '/var/lib' , '/bin' )) 957eq ( nil , vim . fs . relpath ( my_dir , 'bin' )) 958eq ( nil , vim . fs . relpath ( my_dir , './bin' )) 959eq ( nil , vim . fs . relpath ( my_dir , '././' )) 960eq ( nil , vim . fs . relpath ( my_dir , '../' )) 961eq ( nil , vim . fs . relpath ( '/var/lib' , '/' )) 962eq ( nil , vim . fs . relpath ( '/var/lib' , '//' )) 963eq ( nil , vim . fs . relpath ( ' ' , '/var' )) 964eq ( nil , vim . fs . relpath ( ' ' , '/var' )) 965eq ( '.' , vim . fs . relpath ( '/var/lib' , '/var/lib' )) 966eq ( 'lib' , vim . fs . relpath ( '/var/' , '/var/lib' )) 967eq ( 'var/lib' , vim . fs . relpath ( '/' , '/var/lib' )) 968eq ( 'bar/package.json' , vim . fs . relpath ( '/foo/test' , '/foo/test/bar/package.json' )) 969eq ( 'foo/bar' , vim . fs . relpath ( cwd , 'foo/bar' )) 970eq ( 'foo/bar' , vim . fs . relpath ( '.' , vim . fs . joinpath ( cwd , 'foo/bar' ))) 971eq ( 'bar' , vim . fs . relpath ( 'foo' , 'foo/bar' )) 972eq ( nil , vim . fs . relpath ( '/var/lib' , '/var/library/foo' )) 973 974if is_os ( 'win' ) then 975eq ( nil , vim . fs . relpath ( '/' , ' ' )) 976eq ( nil , vim . fs . relpath ( '/' , 'var' )) 977else 978local cwd_rel_root =cwd : sub ( 2 ) 979eq ( cwd_rel_root .. '/ ' , vim . fs . relpath ( '/' , ' ' )) 980eq ( cwd_rel_root .. '/var' , vim . fs . relpath ( '/' , 'var' )) 981end 982 983if is_os ( 'win' ) then 984eq ( nil , vim . fs . relpath ( 'c:/aaaa/' , '/aaaa/cccc' )) 985eq ( nil , vim . fs . relpath ( 'c:/aaaa/' , './aaaa/cccc' )) 986eq ( nil , vim . fs . relpath ( 'c:/aaaa/' , 'aaaa/cccc' )) 987eq ( nil , vim . fs . relpath ( 'c:/blah\\blah' , 'd:/games' )) 988eq ( nil , vim . fs . relpath ( 'c:/games' , 'd:/games' )) 989eq ( nil , vim . fs . relpath ( 'c:/games' , 'd:/games/foo' )) 990eq ( nil , vim . fs . relpath ( 'c:/aaaa/bbbb' , 'c:/aaaa' )) 991eq ( 'cccc' , vim . fs . relpath ( 'c:/aaaa/' , 'c:/aaaa/cccc' )) 992eq ( 'aaaa/bbbb' , vim . fs . relpath ( 'C:/' , 'c:\\aaaa\\bbbb' )) 993eq ( 'bar/package.json' , vim . fs . relpath ( 'C:\\foo\\test' , 'C:\\foo\\test\\bar\\package.json' )) 994eq ( 'baz' , vim . fs . relpath ( '\\\\foo\\bar' , '\\\\foo\\bar\\baz' )) 995eq ( nil , vim . fs . relpath ( 'a/b/c' , 'a\\b' )) 996eq ( 'd' , vim . fs . relpath ( 'a/b/c' , 'a\\b\\c\\d' )) 997eq ( '.' , vim . fs . relpath ( '\\\\foo\\bar\\baz' , '\\\\foo\\bar\\baz' )) 998eq ( nil , vim . fs . relpath ( 'C:\\foo\\test' , 'C:\\foo\\Test\\bar\\package.json' )) 999end 1000end ) 1001end ) 1002 1003describe ( 'rm()' , function () 1004before_each ( function () 1005t . mkdir ( 'Xtest_fs-rm' ) 1006t . write_file ( 'Xtest_fs-rm/file-to-link' , 'File to link' ) 1007t . mkdir ( 'Xtest_fs-rm/dir-to-link' ) 1008t . write_file ( 'Xtest_fs-rm/dir-to-link/file' , 'File in dir to link' ) 1009end ) 1010 1011after_each ( function () 1012vim . uv . fs_unlink ( 'Xtest_fs-rm/dir-to-link/file' ) 1013vim . uv . fs_rmdir ( 'Xtest_fs-rm/dir-to-link' ) 1014vim . uv . fs_unlink ( 'Xtest_fs-rm/file-to-link' ) 1015vim . uv . fs_rmdir ( 'Xtest_fs-rm' ) 1016end ) 1017 1018it ( 'symlink' , function () 1019-- File 1020vim . uv . fs_symlink ( 'Xtest_fs-rm/file-to-link' , 'Xtest_fs-rm/file-as-link' ) 1021vim . fs . rm ( 'Xtest_fs-rm/file-as-link' ) 1022eq ( nil , vim . uv . fs_stat ( 'Xtest_fs-rm/file-as-link' )) 1023eq ( { 'File to link' } , fn . readfile ( 'Xtest_fs-rm/file-to-link' )) 1024 1025-- Directory 1026local function assert_rm_symlinked_dir ( opts ) 1027vim . uv . fs_symlink ( 'Xtest_fs-rm/dir-to-link' , 'Xtest_fs-rm/dir-as-link' ) 1028vim . fs . rm ( 'Xtest_fs-rm/dir-as-link' , opts ) 1029eq ( nil , vim . uv . fs_stat ( 'Xtest_fs-rm/dir-as-link' )) 1030eq ( { 'File in dir to link' } , fn . readfile ( 'Xtest_fs-rm/dir-to-link/file' )) 1031end 1032 1033assert_rm_symlinked_dir ( {} ) 1034assert_rm_symlinked_dir ( { force =true } ) 1035assert_rm_symlinked_dir ( { recursive =true } ) 1036assert_rm_symlinked_dir ( { recursive =true , force =true } ) 1037end ) 1038end ) 1039 1040describe ( 'ext()' , function () 1041it ( 'works' , function () 1042-- See test/functional/vimscript/fnamemodify_spec.lua 1043end ) 1044end ) 1045end )