robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local n =require ( 'test.functional.testnvim' )() 2local t =require ( 'test.testutil' ) 3 4local describe , it , after_each , pending , finally = 5t . describe , t . it , t . after_each , t . pending , t . finally 6local api =n . api 7local command =n . command 8local eq =t . eq 9local exec_capture =n . exec_capture 10local exec_lua =n . exec_lua 11local feed =n . feed 12local fn =n . fn 13local ok =t . ok 14local poke_eventloop =n . poke_eventloop 15 16local function lines () 17return api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 18end 19 20local function edit ( path ) 21api . nvim_cmd ( { cmd ='edit' , args ={ path } , magic ={ file =false , bar =false } } , {} ) 22end 23 24local function cd ( path ) 25api . nvim_cmd ( { cmd ='cd' , args ={ path } , magic ={ file =false , bar =false } } , {} ) 26end 27 28local function line_of ( text ) 29for i , line in ipairs ( lines ()) do 30if line == text then 31return i 32end 33end 34error (( 'missing line %q in %s' ): format ( text , vim . inspect ( lines ()))) 35end 36 37local function bufopt ( name ) 38return api . nvim_get_option_value ( name , { buf =0 } ) 39end 40 41local function has_syntax_group ( name ) 42return exec_capture ( 'syntax list ' .. name ): find ( name , 1 , true ) ~= nil 43end 44 45local function assert_directory ( path ) 46local ffname =path : sub ( - 1 ) == '/' and path or path .. '/' 47eq ( ffname , api . nvim_buf_get_name ( 0 )) 48eq ( path , vim . fs . normalize ( fn . fnamemodify ( fn . bufname ( '%' ), ':p' ))) 49eq ( 'directory' , bufopt ( 'filetype' )) 50eq ( true , bufopt ( 'buflisted' )) 51end 52 53local function filesystem_root ( path ) 54local root =vim . fs . normalize ( vim . fs . abspath ( path )) 55for parent in vim . fs . parents ( root ) do 56root =parent 57end 58return root 59end 60 61local function write_config_plugin ( path , text ) 62local plugin_file =vim . fs . joinpath ( vim . fn . stdpath ( 'config' ), path ) 63vim . fs . mkdir ( vim . fs . dirname ( plugin_file ), { parents =true } ) 64finally ( function () 65os . remove ( plugin_file ) 66end ) 67t . write_file ( plugin_file , text ) 68end 69 70---@param args? string[] 71---@return string[] 72local function with_buftype_optionset ( args ) 73return vim . list_extend ( { 74'--clean' , 75'--cmd' , 76'let g:nvim_dir_events = []' , 77'--cmd' , 78[[autocmd OptionSet buftype call add(g:nvim_dir_events, v:option_new)]] , 79} , args or {} ) 80end 81 82local function expect_buftype_optionset ( path ) 83assert_directory ( path ) 84eq ( { 'nowrite' } , exec_lua ( 'return vim.g.nvim_dir_events' )) 85end 86 87describe ( 'nvim.dir' , function () 88local root 89local subdir 90local file 91 92local function make_fixture () 93root =vim . fs . normalize ( t . tmpname ( false ) .. ' space%#' ) 94subdir =root .. '/subdir' 95file =root .. '/alpha.txt' 96t . mkdir ( root ) 97t . mkdir ( subdir ) 98t . write_file ( file , 'alpha' , true ) 99t . write_file ( root .. '/.hidden' , 'hidden' , true ) 100end 101 102after_each ( function () 103if root then 104n . rmdir ( root ) 105end 106root =nil 107end ) 108 109it ( 'opens a startup directory argument' , function () 110make_fixture () 111n . clear ( { args ={ '--clean' , root } } ) 112 113assert_directory ( root ) 114eq ( false , vim . tbl_contains ( lines (), '../' )) 115eq ( 'subdir/' , lines ()[ 1 ]) 116line_of ( '.hidden' ) 117line_of ( 'alpha.txt' ) 118end ) 119 120it ( '3P dir-browser can handle `FileType directory` event and rename buf' , function () 121make_fixture () 122n . clear ( { args ={ '--clean' } } ) 123exec_lua ( function () 124_G . nvim_dir_loaded_in_filetype =nil 125vim . api . nvim_create_autocmd ( 'FileType' , { 126pattern ='directory' , 127callback =function ( ev ) 128_G . nvim_dir_loaded_in_filetype =package . loaded [ 'nvim.dir' ] ~= nil 129local dir =vim . api . nvim_buf_get_name ( ev . buf ) 130local browser =vim . api . nvim_create_buf ( false , true ) 131vim . api . nvim_buf_set_name ( browser , 'example://' .. dir ) 132vim . api . nvim_buf_set_lines ( browser , 0 , - 1 , false , { 133'plugin browser for: ' .. dir , 134} ) 135vim . api . nvim_set_current_buf ( browser ) 136vim . api . nvim_buf_delete ( ev . buf , { force =true } ) 137end , 138} ) 139end ) 140 141edit ( root ) 142 143eq ( false , exec_lua ( 'return _G.nvim_dir_loaded_in_filetype' )) 144eq ( 'example://' .. root .. '/' , api . nvim_buf_get_name ( 0 )) 145eq ( { 'plugin browser for: ' .. root .. '/' } , lines ()) 146eq ( false , exec_lua ( [[return package.loaded['nvim.dir'] ~= nil]] )) 147eq ( '' , exec_capture ( 'messages' )) 148end ) 149 150it ( 'triggers nested autocmds when opening directory buffers' , function () 151make_fixture () 152 153n . clear ( { 154args =with_buftype_optionset ( { root } ), 155} ) 156expect_buftype_optionset ( root ) 157 158n . clear ( { 159args =with_buftype_optionset (), 160} ) 161edit ( root ) 162expect_buftype_optionset ( root ) 163end ) 164 165it ( 'handles nested autocmds deleting the directory buffer' , function () 166make_fixture () 167n . clear ( { 168args ={ 169'--clean' , 170'--cmd' , 171'let g:nvim_dir_wiped = 0' , 172'--cmd' , 173[[autocmd OptionSet buftype let g:nvim_dir_wiped = 1 | bwipeout!]] , 174root , 175} , 176} ) 177 178eq ( 1 , exec_lua ( 'return vim.g.nvim_dir_wiped' )) 179eq ( '' , exec_capture ( 'messages' )) 180end ) 181 182it ( 'does not load the module until opening a directory' , function () 183make_fixture () 184n . clear ( { args ={ '--clean' } } ) 185 186eq ( false , exec_lua ( [[return package.loaded['nvim.dir'] ~= nil]] )) 187edit ( root ) 188eq ( true , exec_lua ( [[return package.loaded['nvim.dir'] ~= nil]] )) 189assert_directory ( root ) 190end ) 191 192it ( 'opens a custom listing provider' , function () 193n . clear ( { args ={ '--clean' } } ) 194 195exec_lua ( function () 196require ( 'nvim.dir' ). open ( 0 , 'custom://root' , { 197list =function ( _ , name , cb ) 198vim . g . nvim_dir_list_name =name 199cb ( nil , { 200{ name ='child' , dir =true } , 201{ name ='file.txt' , dir =false } , 202} ) 203end , 204open =function ( _ , name , entry ) 205vim . g . nvim_dir_opened =entry . name .. ':' .. name 206end , 207open_parent =function ( _ , name ) 208vim . g . nvim_dir_parent =name 209end , 210init =function ( buf ) 211vim . bo [ buf ]. filetype ='customdir' 212end , 213} ) 214end ) 215 216eq ( 'custom://root' , api . nvim_buf_get_name ( 0 )) 217eq ( 'customdir' , bufopt ( 'filetype' )) 218eq ( { 'child/' , 'file.txt' } , lines ()) 219eq ( 'custom://root' , exec_lua ( 'return vim.g.nvim_dir_list_name' )) 220 221api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 222api . nvim_set_option_value ( 'modifiable' , true , { buf =0 } ) 223api . nvim_set_current_line ( 'renamed.txt' ) 224feed ( '<CR>' ) 225poke_eventloop () 226eq ( 'renamed.txt:custom://root' , exec_lua ( 'return vim.g.nvim_dir_opened' )) 227 228feed ( '-' ) 229poke_eventloop () 230eq ( 'custom://root' , exec_lua ( 'return vim.g.nvim_dir_parent' )) 231end ) 232 233it ( 'reports custom listing provider errors' , function () 234n . clear ( { args ={ '--clean' } } ) 235 236exec_lua ( function () 237require ( 'nvim.dir' ). open ( 0 , 'custom://error' , { 238list =function ( buf , _ , cb ) 239vim . b [ buf ]. custom_fail =( vim . b [ buf ]. custom_fail or 0 ) + 1 240if vim . b [ buf ]. custom_fail == 1 then 241cb ( 'simulated error' ) 242else 243cb ( nil , { { name ='recovered.txt' , dir =false } } ) 244end 245end , 246open =function () end , 247open_parent =function () end , 248} ) 249end ) 250 251ok ( exec_capture ( 'messages' ): find ( 'simulated error' , 1 , true ) ~= nil ) 252-- A failed initial load renders an empty listing that "R" can retry. 253eq ( 'simulated error' , exec_lua ( [[return vim.b.nvim_dir.err]] )) 254eq ( { '' } , lines ()) 255eq ( 'nowrite' , bufopt ( 'buftype' )) 256eq ( false , bufopt ( 'modifiable' )) 257feed ( 'R' ) 258poke_eventloop () 259eq ( { 'recovered.txt' } , lines ()) 260eq ( vim . NIL , exec_lua ( [[return vim.b.nvim_dir.err]] )) 261end ) 262 263it ( 'failed directory open does not repeat the error on every BufEnter' , function () 264t . skip ( t . is_os ( 'win' ), 'directory permissions are POSIX-only' ) 265t . skip ( vim . uv . getuid () == 0 , 'root ignores directory permissions' ) 266n . clear ( { args ={ '--clean' } } ) 267make_fixture () 268local locked =root .. '/locked' 269t . mkdir ( locked ) 270finally ( function () 271vim . uv . fs_chmod ( locked , tonumber ( '755' , 8 )) 272end ) 273vim . uv . fs_chmod ( locked , 0 ) 274 275edit ( root ) 276api . nvim_win_set_cursor ( 0 , { line_of ( 'locked/' ), 0 } ) 277feed ( '<CR>' ) 278poke_eventloop () 279local function count_errors () 280local _ , n_errors =exec_capture ( 'messages' ): gsub ( 'EACCES' , '' ) 281return n_errors 282end 283eq ( 1 , count_errors ()) 284ok ( exec_lua ( [[return vim.b.nvim_dir.err]] ): find ( 'EACCES' , 1 , true ) ~= nil ) 285eq ( 'nowrite' , bufopt ( 'buftype' )) 286edit ( file ) 287feed ( '<C-^>' ) 288poke_eventloop () 289eq ( 1 , count_errors ()) 290vim . uv . fs_chmod ( locked , tonumber ( '755' , 8 )) 291t . write_file ( locked .. '/inside.txt' , 'inside' , true ) 292feed ( 'R' ) 293poke_eventloop () 294eq ( { 'inside.txt' } , lines ()) 295end ) 296 297it ( 'does not swallow errors raised after the list callback' , function () 298n . clear ( { args ={ '--clean' } } ) 299 300local err =exec_lua ( function () 301local ok_open , e =pcall ( require ( 'nvim.dir' ). open , 0 , 'custom://late-error' , { 302list =function ( _ , _ , cb ) 303cb ( nil , { { name ='file.txt' , dir =false } } ) 304error ( 'late provider error' ) 305end , 306open =function () end , 307open_parent =function () end , 308} ) 309return not ok_open and tostring ( e ) or nil 310end ) 311ok ( err ~= nil and err : find ( 'late provider error' , 1 , true ) ~= nil ) 312eq ( { 'file.txt' } , lines ()) 313end ) 314 315it ( 'reloads custom listing providers' , function () 316n . clear ( { args ={ '--clean' } } ) 317 318exec_lua ( function () 319require ( 'nvim.dir' ). open ( 0 , 'custom://root' , { 320list =function ( buf , _ , cb ) 321vim . b [ buf ]. custom_count =( vim . b [ buf ]. custom_count or 0 ) + 1 322cb ( nil , { { name ='file' .. vim . b [ buf ]. custom_count .. '.txt' , dir =false } } ) 323end , 324open =function () end , 325open_parent =function () end , 326} ) 327end ) 328 329eq ( { 'file1.txt' } , lines ()) 330feed ( 'R' ) 331poke_eventloop () 332eq ( { 'file2.txt' } , lines ()) 333end ) 334 335it ( 'fires DirReadPost for every listing' , function () 336make_fixture () 337-- Entry order is locale-dependent: PUC Lua compares strings with strcoll(). 338n . clear ( { args ={ '--clean' } , env ={ LC_ALL ='C' } } ) 339exec_lua ( function () 340vim . g . renders =0 341vim . api . nvim_create_autocmd ( 'User' , { 342pattern ='DirReadPost' , 343callback =function ( args ) 344vim . g . renders =vim . g . renders + 1 345vim . g . render_lines =vim . api . nvim_buf_get_lines ( args . buf , 0 , - 1 , true ) 346vim . g . render_writable =vim . bo [ args . buf ]. modifiable and not vim . bo [ args . buf ]. readonly 347end , 348} ) 349end ) 350 351edit ( root ) 352eq ( 1 , exec_lua ( 'return vim.g.renders' )) 353eq ( { 'subdir/' , '.hidden' , 'alpha.txt' } , exec_lua ( 'return vim.g.render_lines' )) 354-- Writable while handlers run, locked again afterwards. 355eq ( true , exec_lua ( 'return vim.g.render_writable' )) 356eq ( false , bufopt ( 'modifiable' )) 357eq ( true , bufopt ( 'readonly' )) 358eq ( false , bufopt ( 'modified' )) 359 360feed ( 'R' ) 361poke_eventloop () 362eq ( 2 , exec_lua ( 'return vim.g.renders' )) 363 364edit ( root ) 365eq ( 3 , exec_lua ( 'return vim.g.renders' )) 366 367api . nvim_win_set_cursor ( 0 , { line_of ( 'subdir/' ), 0 } ) 368feed ( '<CR>' ) 369poke_eventloop () 370assert_directory ( subdir ) 371eq ( 4 , exec_lua ( 'return vim.g.renders' )) 372end ) 373 374it ( 'keeps the listing readonly when a handler errors' , function () 375make_fixture () 376-- Entry order is locale-dependent: PUC Lua compares strings with strcoll(). 377n . clear ( { args ={ '--clean' } , env ={ LC_ALL ='C' } } ) 378exec_lua ( function () 379vim . api . nvim_create_autocmd ( 'User' , { 380pattern ='DirReadPost' , 381callback =function () 382error ( 'handler error' ) 383end , 384} ) 385end ) 386 387edit ( root ) 388 389eq ( { 'subdir/' , '.hidden' , 'alpha.txt' } , lines ()) 390eq ( false , bufopt ( 'modifiable' )) 391eq ( true , bufopt ( 'readonly' )) 392end ) 393 394it ( 'selects the origin entry after a handler reorders' , function () 395make_fixture () 396n . clear ( { args ={ '--clean' } } ) 397exec_lua ( function () 398vim . api . nvim_create_autocmd ( 'User' , { 399pattern ='DirReadPost' , 400callback =function () 401vim . cmd ( 'silent keeppatterns sort!' ) 402end , 403} ) 404end ) 405 406edit ( subdir ) 407feed ( '-' ) 408poke_eventloop () 409 410eq ( { 'subdir/' , 'alpha.txt' , '.hidden' } , lines ()) 411eq ( 'subdir/' , api . nvim_get_current_line ()) 412end ) 413 414it ( 'ignores callbacks from replaced listings' , function () 415n . clear ( { args ={ '--clean' } } ) 416 417exec_lua ( function () 418local dir =require ( 'nvim.dir' ) 419dir . open ( 0 , 'custom://old' , { 420list =function ( _ , _ , cb ) 421_G . nvim_dir_stale_callback =cb 422end , 423open =function () end , 424open_parent =function () end , 425} ) 426dir . open ( 0 , 'custom://new' , { 427list =function ( _ , _ , cb ) 428cb ( nil , { { name ='current.txt' , dir =false } } ) 429end , 430open =function () end , 431open_parent =function () end , 432} ) 433end ) 434 435eq ( 'custom://new' , api . nvim_buf_get_name ( 0 )) 436eq ( { 'current.txt' } , lines ()) 437 438exec_lua ( function () 439_G . nvim_dir_stale_callback ( nil , { { name ='stale.txt' , dir =false } } ) 440end ) 441 442eq ( 'custom://new' , api . nvim_buf_get_name ( 0 )) 443eq ( { 'current.txt' } , lines ()) 444end ) 445 446it ( 'replaces listing providers' , function () 447n . clear ( { args ={ '--clean' } } ) 448 449exec_lua ( function () 450local dir =require ( 'nvim.dir' ) 451local function provider ( label ) 452return { 453list =function ( buf , _ , cb ) 454local key ='nvim_dir_' .. label .. '_lists' 455vim . b [ buf ][ key ] =( vim . b [ buf ][ key ] or 0 ) + 1 456vim . g . nvim_dir_provider_list =label .. ':' .. vim . b [ buf ][ key ] 457cb ( nil , { { name =label .. '.txt' , dir =false } } ) 458end , 459open =function ( _ , _ , entry ) 460vim . g . nvim_dir_provider_open =label .. ':' .. entry . name 461end , 462open_parent =function () 463vim . g . nvim_dir_provider_parent =label 464end , 465} 466end 467dir . open ( 0 , 'custom://old' , provider ( 'old' )) 468dir . open ( 0 , 'custom://new' , provider ( 'new' )) 469end ) 470 471eq ( { 'new.txt' } , lines ()) 472eq ( 'new:1' , exec_lua ( 'return vim.g.nvim_dir_provider_list' )) 473for _ , plug in ipairs ( { 474'<Plug>(nvim-dir-open)' , 475'<Plug>(nvim-dir-up)' , 476'<Plug>(nvim-dir-reload)' , 477} ) do 478eq ( 0 , fn . maparg ( plug , 'n' , false , true ). buffer ) 479end 480 481feed ( '<CR>' ) 482poke_eventloop () 483eq ( 'new:new.txt' , exec_lua ( 'return vim.g.nvim_dir_provider_open' )) 484 485feed ( '-' ) 486poke_eventloop () 487eq ( 'new' , exec_lua ( 'return vim.g.nvim_dir_provider_parent' )) 488 489feed ( 'R' ) 490poke_eventloop () 491eq ( 'new:2' , exec_lua ( 'return vim.g.nvim_dir_provider_list' )) 492end ) 493 494it ( 'maps [count]- to open directories' , function () 495make_fixture () 496n . clear ( { args_rm ={ '--cmd' } , args ={ '--clean' } } ) 497local cwd =vim . fs . normalize ( fn . getcwd ()) 498 499edit ( file ) 500feed ( '-' ) 501poke_eventloop () 502 503assert_directory ( root ) 504line_of ( 'alpha.txt' ) 505 506-- Ensure the cursor stays on the entry we navigated up from. 507eq ( 'alpha.txt' , api . nvim_get_current_line ()) 508 509edit ( file ) 510feed ( '1-' ) 511poke_eventloop () 512 513assert_directory ( cwd ) 514 515edit ( file ) 516feed ( '-' ) 517poke_eventloop () 518feed ( '1-' ) 519poke_eventloop () 520assert_directory ( cwd ) 521 522edit ( file ) 523feed ( '2-' ) 524poke_eventloop () 525 526assert_directory ( vim . fs . dirname ( root )) 527eq ( vim . fs . basename ( root ) .. '/' , api . nvim_get_current_line ()) 528end ) 529 530it ( 'maps - to open the current directory from a buffer with no path' , function () 531make_fixture () 532n . clear ( { args_rm ={ '--cmd' } , args ={ '--clean' } } ) 533local cwd =fn . getcwd () 534command ( 'terminal' ) 535eq ( 'terminal' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 536cd ( root ) 537finally ( function () 538cd ( cwd ) 539end ) 540 541feed ( '-' ) 542poke_eventloop () 543 544assert_directory ( root ) 545 546command ( 'enew' ) 547eq ( '' , api . nvim_buf_get_name ( 0 )) 548feed ( '-' ) 549poke_eventloop () 550 551assert_directory ( root ) 552end ) 553 554it ( 'preserves a modified unnamed buffer when opening the current directory' , function () 555make_fixture () 556n . clear ( { args_rm ={ '--cmd' } , args ={ '--clean' } } ) 557local cwd =fn . getcwd () 558cd ( root ) 559finally ( function () 560cd ( cwd ) 561end ) 562local old_buf =api . nvim_get_current_buf () 563api . nvim_buf_set_lines ( old_buf , 0 , - 1 , false , { 'unsaved' } ) 564 565feed ( '-' ) 566poke_eventloop () 567 568assert_directory ( root ) 569eq ( old_buf , fn . bufnr ( '#' )) 570eq ( true , api . nvim_buf_is_valid ( old_buf )) 571eq ( true , api . nvim_get_option_value ( 'modified' , { buf =old_buf } )) 572eq ( { 'unsaved' } , api . nvim_buf_get_lines ( old_buf , 0 , - 1 , false )) 573end ) 574 575it ( 'does not shadow startup plugin mappings in directory buffers' , function () 576make_fixture () 577write_config_plugin ( 578'plugin/dirvish.lua' , 579[[ 580vim.g.dirvish_up = 0 581vim.keymap.set('n', '-', function() 582vim.g.dirvish_up = vim.g.dirvish_up + 1 583end) 584vim.keymap.set('n', 'a', '<Nop>') 585vim.keymap.set('n', 's', '<Plug>(nvim-dir-split)') 586]] 587) 588 589-- Remove -u NONE instead of --clean, which excludes the config dir from 'runtimepath'. 590n . clear ( { args_rm ={ '-u' , '--cmd' } } ) 591 592eq ( 1 , fn . exists ( 'g:loaded_nvim_dir_plugin' )) -- Avoid false negatives. 593edit ( root ) 594assert_directory ( root ) 595eq ( 0 , fn . maparg ( '-' , 'n' , false , true ). buffer ) 596eq ( '<Nop>' , fn . maparg ( 'a' , 'n' )) 597eq ( '' , fn . maparg ( 'o' , 'n' )) 598 599feed ( '-as' ) 600poke_eventloop () 601 602eq ( 1 , exec_lua ( 'return vim.g.dirvish_up' )) 603assert_directory ( root ) 604eq ( 2 , # api . nvim_list_wins ()) 605feed ( '<C-W>p' ) 606assert_directory ( subdir ) 607end ) 608 609it ( 'preserves alternate buffer when opening a parent directory' , function () 610make_fixture () 611n . clear ( { args_rm ={ '--cmd' } , args ={ '--clean' } } ) 612 613edit ( file ) 614feed ( '-' ) 615poke_eventloop () 616 617assert_directory ( root ) 618-- Keep the alternate buffer on the file we navigated up from. 619eq ( file , api . nvim_buf_get_name ( fn . bufnr ( '#' ))) 620 621feed ( '-' ) 622poke_eventloop () 623assert_directory ( vim . fs . dirname ( root )) 624eq ( file , api . nvim_buf_get_name ( fn . bufnr ( '#' ))) 625 626feed ( '<CR>' ) 627poke_eventloop () 628assert_directory ( root ) 629eq ( file , api . nvim_buf_get_name ( fn . bufnr ( '#' ))) 630 631feed ( '1-' ) 632poke_eventloop () 633eq ( file , api . nvim_buf_get_name ( fn . bufnr ( '#' ))) 634end ) 635 636it ( 'uses an absolute buffer name for a relative startup directory argument' , function () 637make_fixture () 638local cwd =assert ( vim . uv . cwd ()) 639assert ( vim . uv . chdir ( root )) 640n . clear ( { args ={ '--clean' , '.' } } ) 641assert ( vim . uv . chdir ( cwd )) 642 643assert_directory ( root ) 644end ) 645 646it ( 'normalizes edited directory names' , function () 647make_fixture () 648local literal =root .. '/$HOME' 649t . mkdir ( literal ) 650n . clear ( { args ={ '--clean' } } ) 651 652edit ( literal .. '///' ) 653 654eq ( literal .. '/' , api . nvim_buf_get_name ( 0 )) 655eq ( 'directory' , bufopt ( 'filetype' )) 656eq ( { '' } , lines ()) 657end ) 658 659it ( 'does not show a parent entry at the filesystem root' , function () 660n . clear ( { args ={ '--clean' } } ) 661local root_dir =filesystem_root ( fn . getcwd ()) 662 663edit ( root_dir ) 664 665assert_directory ( root_dir ) 666eq ( false , vim . tbl_contains ( lines (), '../' )) 667end ) 668 669it ( 'navigates entries and refreshes the listing' , function () 670make_fixture () 671n . clear ( { args ={ '--clean' } } ) 672local cwd =fn . getcwd () 673 674edit ( root ) 675assert_directory ( root ) 676eq ( vim . uv . fs_realpath ( root ), vim . uv . fs_realpath ( fn . getcwd ())) 677eq ( 'alpha.txt' , fn . findfile ( 'alpha.txt' )) 678 679local win =api . nvim_get_current_win () 680for key , entry in pairs ( { a ='alpha.txt' , o ='subdir/' } ) do 681local cursor ={ line_of ( entry ), 0 } 682api . nvim_win_set_cursor ( 0 , cursor ) 683feed ( key ) 684poke_eventloop () 685eq ( win , api . nvim_get_current_win ()) 686assert_directory ( root ) 687eq ( cursor , api . nvim_win_get_cursor ( 0 )) 688eq ( 2 , # api . nvim_list_wins ()) 689eq ( key == 'a' and 'row' or 'col' , fn . winlayout ()[ 1 ]) 690feed ( '<C-W>p' ) 691eq ( root .. '/' .. entry , api . nvim_buf_get_name ( 0 )) 692command ( 'close' ) 693end 694 695api . nvim_win_set_cursor ( 0 , { line_of ( 'alpha.txt' ), 0 } ) 696feed ( '<CR>' ) 697poke_eventloop () 698eq ( file , api . nvim_buf_get_name ( 0 )) 699eq ( { 'alpha' } , lines ()) 700eq ( cwd , fn . getcwd ()) 701 702edit ( subdir ) 703assert_directory ( subdir ) 704eq ( { '' } , lines ()) 705eq ( vim . uv . fs_realpath ( subdir ), vim . uv . fs_realpath ( fn . getcwd ())) 706feed ( 'ao' ) 707poke_eventloop () 708eq ( 1 , # api . nvim_list_wins ()) 709 710feed ( '-' ) 711poke_eventloop () 712assert_directory ( root ) 713 714t . write_file ( root .. '/beta.txt' , 'beta' , true ) 715feed ( 'R' ) 716poke_eventloop () 717line_of ( 'beta.txt' ) 718end ) 719 720it ( "follows global 'hidden' when abandoned" , function () 721make_fixture () 722n . clear ( { args ={ '--clean' } } ) 723 724command ( 'set hidden' ) 725 726edit ( root ) 727local root_buf =api . nvim_get_current_buf () 728eq ( '' , bufopt ( 'bufhidden' )) 729 730edit ( subdir ) 731eq ( true , api . nvim_buf_is_loaded ( root_buf )) 732eq ( 1 , fn . getbufinfo ( root_buf )[ 1 ]. hidden ) 733 734n . clear ( { args ={ '--clean' } } ) 735command ( 'set nohidden' ) 736 737edit ( root ) 738root_buf =api . nvim_get_current_buf () 739eq ( '' , bufopt ( 'bufhidden' )) 740 741edit ( subdir ) 742eq ( false , api . nvim_buf_is_loaded ( root_buf )) 743end ) 744 745it ( 'reloads directory buffers' , function () 746make_fixture () 747n . clear ( { 748args ={ '--clean' , '--cmd' , [[autocmd FileType directory ++once setlocal bufhidden=delete]] } , 749} ) 750 751edit ( root ) 752assert_directory ( root ) 753eq ( 'delete' , bufopt ( 'bufhidden' )) 754eq ( true , has_syntax_group ( 'directoryDirectory' )) 755local buf =api . nvim_get_current_buf () 756 757t . write_file ( root .. '/beta.txt' , 'beta' , true ) 758feed ( 'R' ) 759poke_eventloop () 760eq ( 'delete' , bufopt ( 'bufhidden' )) 761eq ( true , has_syntax_group ( 'directoryDirectory' )) 762line_of ( 'beta.txt' ) 763 764t . write_file ( root .. '/gamma.txt' , 'gamma' , true ) 765command ( 'edit' ) 766eq ( buf , api . nvim_get_current_buf ()) 767assert_directory ( root ) 768eq ( true , has_syntax_group ( 'directoryDirectory' )) 769line_of ( 'subdir/' ) 770line_of ( 'alpha.txt' ) 771line_of ( 'gamma.txt' ) 772end ) 773 774it ( 'reports an error and keeps the buffer when reloading a removed directory' , function () 775make_fixture () 776n . clear ( { args ={ '--clean' } } ) 777 778edit ( subdir ) 779assert_directory ( subdir ) 780 781n . rmdir ( subdir ) 782feed ( 'R' ) 783poke_eventloop () 784 785ok ( exec_capture ( 'messages' ): find ( 'ENOENT' , 1 , true ) ~= nil ) 786ok ( exec_lua ( [[return vim.b.nvim_dir.err]] ): find ( 'ENOENT' , 1 , true ) ~= nil ) 787assert_directory ( subdir ) 788end ) 789 790it ( 'refreshes a directory when navigated into again' , function () 791make_fixture () 792n . clear ( { args ={ '--clean' } } ) 793 794edit ( root ) 795api . nvim_win_set_cursor ( 0 , { line_of ( 'subdir/' ), 0 } ) 796feed ( '<CR>' ) 797poke_eventloop () 798assert_directory ( subdir ) 799eq ( { '' } , lines ()) 800 801t . write_file ( subdir .. '/new.txt' , 'new' , true ) 802feed ( '-' ) 803poke_eventloop () 804assert_directory ( root ) 805 806api . nvim_win_set_cursor ( 0 , { line_of ( 'subdir/' ), 0 } ) 807feed ( '<CR>' ) 808poke_eventloop () 809assert_directory ( subdir ) 810line_of ( 'new.txt' ) 811end ) 812 813it ( 'displays filenames as buffer text and opens them from the buffer' , function () 814make_fixture () 815n . clear ( { args ={ '--clean' } } ) 816 817edit ( root ) 818line_of ( '.hidden' ) 819line_of ( 'subdir/' ) 820api . nvim_win_set_cursor ( 0 , { line_of ( 'alpha.txt' ), 0 } ) 821feed ( '<CR>' ) 822poke_eventloop () 823 824eq ( file , api . nvim_buf_get_name ( 0 )) 825eq ( { 'alpha' } , lines ()) 826end ) 827 828it ( 'encodes special filename characters in directory buffers' , function () 829-- Windows reserves backslash as a separator and disallows control characters in filenames. 830-- https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file 831t . skip ( t . is_os ( 'win' ), 'N/A: Windows filenames cannot contain these characters' ) 832make_fixture () 833n . clear ( { args ={ '--clean' } } ) 834 835local name ='line\nbreak.txt' 836local raw_names ={ 837'back\\slash.txt' , 838'tab\tname.txt' , 839'carriage\rreturn.txt' , 840'ctrl\1name.txt' , 841'del\127name.txt' , 842} 843for _ , raw_name in ipairs ( raw_names ) do 844t . write_file ( root .. '/' .. raw_name , 'raw' , true ) 845end 846t . write_file ( root .. '/' .. name , 'newline' , true ) 847 848edit ( root ) 849for _ , raw_name in ipairs ( raw_names ) do 850line_of ( raw_name ) 851end 852api . nvim_win_set_cursor ( 0 , { line_of ( 'line\0break.txt' ), 0 } ) 853feed ( 'a<C-W>p' ) 854poke_eventloop () 855 856eq ( root .. '/' .. name , api . nvim_buf_get_name ( 0 )) 857eq ( { 'newline' } , lines ()) 858end ) 859 860it ( 'leaves existing special buffers alone' , function () 861make_fixture () 862n . clear ( { args ={ '--clean' } } ) 863 864api . nvim_set_option_value ( 'buftype' , 'nofile' , { buf =0 } ) 865api . nvim_buf_set_name ( 0 , root ) 866command ( 'doautocmd BufEnter' ) 867 868eq ( 'nofile' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 869eq ( '' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 870end ) 871 872it ( 'coexists with netrw and can be disabled' , function () 873make_fixture () 874n . clear ( { args ={ '--clean' } } ) 875local cwd =fn . getcwd () 876 877ok ( fn . exists ( ':Explore' ) > 0 ) 878edit ( root ) 879eq ( 'directory' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 880 881cd ( root ) 882command ( 'Explore .' ) 883cd ( cwd ) 884eq ( 'netrw' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 885 886n . clear ( { 887args ={ '--clean' , '--cmd' , 'let g:loaded_nvim_dir_plugin = 1' } , 888} ) 889edit ( root ) 890eq ( 'netrw' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 891end ) 892 893for _ , case in ipairs ( { 894{ command ='edit' , windows =1 , tabs =1 } , 895{ command ='split' , windows =2 , tabs =1 } , 896{ command ='vsplit' , windows =2 , tabs =1 } , 897{ command ='tabedit' , windows =1 , tabs =2 } , 898{ command ='tabnew' , windows =1 , tabs =2 } , 899} ) do 900it (( '":browse %s"' ): format ( case . command ), function () 901if t . is_zig_build () then 902return pending ( 'broken with build.zig: TMPDIR relative cwd' ) 903end 904make_fixture () 905n . clear ( { args ={ '--clean' } } ) 906local cwd =fn . getcwd () 907 908cd ( root ) 909command ( 'browse ' .. case . command ) 910cd ( cwd ) 911 912eq ( case . windows , # api . nvim_tabpage_list_wins ( 0 )) 913eq ( case . tabs , # api . nvim_list_tabpages ()) 914assert_directory ( root ) 915line_of ( 'alpha.txt' ) 916end ) 917end 918end )