robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
a4a544032a
master
1" Test for :cd and chdir() 2 3source shared.vim 4source check.vim 5 6func Test_cd_large_path () 7" This used to crash with a heap write overflow. 8call assert_fails ( 'cd ' . repeat ( 'x' , 5000 ), 'E344:' ) 9endfunc 10 11func Test_cd_up_and_down () 12let path = getcwd () 13cd .. 14call assert_notequal ( path , getcwd ()) 15exe 'cd ' .. fnameescape ( path ) 16call assert_equal ( path , getcwd ()) 17endfunc 18 19func Test_cd_no_arg () 20if has ( 'unix' ) 21" Test that cd without argument goes to $HOME directory on Unix systems. 22let path = getcwd () 23cd 24call assert_equal ( $ HOME , getcwd ()) 25call assert_notequal ( path , getcwd ()) 26exe 'cd ' .. fnameescape ( path ) 27call assert_equal ( path , getcwd ()) 28else 29" Test that cd without argument echoes cwd on non-Unix systems. 30call assert_match ( getcwd (), execute ( 'cd' )) 31endif 32endfunc 33 34func Test_cd_minus () 35" Test the :cd - goes back to the previous directory. 36let path = getcwd () 37cd .. 38let path_dotdot = getcwd () 39call assert_notequal ( path , path_dotdot ) 40cd - 41call assert_equal ( path , getcwd ()) 42cd - 43call assert_equal ( path_dotdot , getcwd ()) 44cd - 45call assert_equal ( path , getcwd ()) 46 47" Test for :cd - after a failed :cd 48call assert_fails ( 'cd /nonexistent' , 'E344:' ) 49call assert_equal ( path , getcwd ()) 50cd - 51call assert_equal ( path_dotdot , getcwd ()) 52cd - 53 54" Test for :cd - without a previous directory 55let lines =<< trim [SCRIPT] 56call assert_fails('cd -', 'E186:') 57call assert_fails('call chdir("-")', 'E186:') 58call writefile(v:errors, 'Xresult') 59qall! 60[SCRIPT] 61call writefile ( lines , 'Xscript' , 'D' ) 62if RunVim ([], [], '--clean -S Xscript' ) 63call assert_equal ([], readfile ( 'Xresult' )) 64endif 65call delete ( 'Xresult' ) 66endfunc 67 68" Test for chdir() 69func Test_chdir_func () 70let topdir = getcwd () 71call mkdir ( 'Xchdir/y/z' , 'pR' ) 72 73" Create a few tabpages and windows with different directories 74new 75cd Xchdir 76tabnew 77tcd y 78below new 79below new 80lcd z 81 82tabfirst 83call assert_match ( '^\[global\] .*/Xchdir$' , trim ( execute ( 'verbose pwd' ))) 84call chdir ( '..' ) 85call assert_equal ( 'y' , fnamemodify ( getcwd ( 1 , 2 ), ':t' )) 86call assert_equal ( 'z' , fnamemodify ( 3 ->getcwd ( 2 ), ':t' )) 87tabnext |wincmd t 88call assert_match ( '^\[tabpage\] .*/y$' , trim ( execute ( 'verbose pwd' ))) 89eval '..' ->chdir () 90call assert_equal ( 'Xchdir' , fnamemodify ( getcwd ( 1 , 2 ), ':t' )) 91call assert_equal ( 'Xchdir' , fnamemodify ( getcwd ( 2 , 2 ), ':t' )) 92call assert_equal ( 'z' , fnamemodify ( getcwd ( 3 , 2 ), ':t' )) 93call assert_equal ( 'testdir' , fnamemodify ( getcwd ( 1 , 1 ), ':t' )) 943 wincmd w 95call assert_match ( '^\[window\] .*/z$' , trim ( execute ( 'verbose pwd' ))) 96call chdir ( '..' ) 97call assert_equal ( 'Xchdir' , fnamemodify ( getcwd ( 1 , 2 ), ':t' )) 98call assert_equal ( 'Xchdir' , fnamemodify ( getcwd ( 2 , 2 ), ':t' )) 99call assert_equal ( 'y' , fnamemodify ( getcwd ( 3 , 2 ), ':t' )) 100call assert_equal ( 'testdir' , fnamemodify ( getcwd ( 1 , 1 ), ':t' )) 101 102" Forcing scope 103call chdir ( '.' , 'global' ) 104call assert_match ( '^\[global\]' , trim ( execute ( 'verbose pwd' ))) 105call chdir ( '.' , 'tabpage' ) 106call assert_match ( '^\[tabpage\]' , trim ( execute ( 'verbose pwd' ))) 107call chdir ( '.' , 'window' ) 108call assert_match ( '^\[window\]' , trim ( execute ( 'verbose pwd' ))) 109" Nvim: buffer is the narrowest scope and shadows the others, so it goes last. 110call chdir ( '.' , 'buffer' ) 111call assert_match ( '^\[buffer\]' , trim ( execute ( 'verbose pwd' ))) 112 113" Error case 114call assert_fails ( "call chdir('dir-abcd')" , 'E344:' ) 115silent ! let d = chdir ( "dir_abcd" ) 116call assert_equal ( "" , d ) 117call assert_fails ( "call chdir('.', v:_null_string)" , 'E475:' ) 118call assert_fails ( "call chdir('.', [])" , 'E730:' ) 119" Should not crash 120call chdir ( d ) 121call assert_equal ( '' , chdir ([])) 122 123only |tabonly 124call chdir ( topdir ) 125endfunc 126 127" Test for changing to the previous directory '-' 128func Test_prev_dir () 129let topdir = getcwd () 130call mkdir ( 'Xprevdir/a/b/c' , 'pR' ) 131 132" Create a few tabpages and windows with different directories 133new |only 134tabnew |new 135tabnew 136tabfirst 137cd Xprevdir 138tabnext |wincmd t 139tcd a 140wincmd w 141lcd b 142tabnext 143tcd a/b/c 144 145" Change to the previous directory twice in all the windows. 146tabfirst 147cd - |cd - 148tabnext |wincmd t 149tcd - |tcd - 150wincmd w 151lcd - |lcd - 152tabnext 153tcd - |tcd - 154 155" Check the directory of all the windows 156tabfirst 157call assert_equal ( 'Xprevdir' , fnamemodify ( getcwd (), ':t' )) 158tabnext |wincmd t 159call assert_equal ( 'a' , fnamemodify ( getcwd (), ':t' )) 160wincmd w 161call assert_equal ( 'b' , fnamemodify ( getcwd (), ':t' )) 162tabnext 163call assert_equal ( 'c' , fnamemodify ( getcwd (), ':t' )) 164 165" Change to the previous directory using chdir() 166tabfirst 167call chdir ( "-" ) |call chdir ( "-" ) 168tabnext |wincmd t 169call chdir ( "-" ) |call chdir ( "-" ) 170wincmd w 171call chdir ( "-" ) |call chdir ( "-" ) 172tabnext 173call chdir ( "-" ) |call chdir ( "-" ) 174 175" Check the directory of all the windows 176tabfirst 177call assert_equal ( 'Xprevdir' , fnamemodify ( getcwd (), ':t' )) 178tabnext |wincmd t 179call assert_equal ( 'a' , fnamemodify ( getcwd (), ':t' )) 180wincmd w 181call assert_equal ( 'b' , fnamemodify ( getcwd (), ':t' )) 182tabnext 183call assert_equal ( 'c' , fnamemodify ( getcwd (), ':t' )) 184 185only |tabonly 186call chdir ( topdir ) 187endfunc 188 189func Test_lcd_split () 190let curdir = getcwd () 191lcd .. 192split 193lcd - 194call assert_equal ( curdir , getcwd ()) 195quit ! 196endfunc 197 198" Test that a temporary override of 'autochdir' via :lcd isn't clobbered by win_execute() in a split window. 199func Test_lcd_win_execute () 200CheckFunction test_autochdir 201CheckOption autochdir 202 203let startdir = getcwd () 204call mkdir ( 'Xsubdir' , 'R' ) 205call test_autochdir () 206set autochdir 207edit Xsubdir/file 208call assert_match ( 'testdir.Xsubdir.file$' , expand ( '%:p' )) 209split 210lcd .. 211call assert_match ( 'testdir.Xsubdir.file$' , expand ( '%:p' )) 212call win_execute ( win_getid ( 2 ), "" ) 213call assert_match ( 'testdir.Xsubdir.file$' , expand ( '%:p' )) 214 215set noautochdir 216bwipe ! 217call chdir ( startdir ) 218endfunc 219 220func Test_cd_from_non_existing_dir () 221if has ( 'sun' ) 222throw 'Skipped: Solaris does not allow deleting the current working directory' 223endif 224CheckNotMSWindows 225 226let saveddir = getcwd () 227call mkdir ( 'Xdeleted_dir' ) 228cd Xdeleted_dir 229call delete ( saveddir .. '/Xdeleted_dir' , 'd' ) 230 231" Expect E187 as the current directory was deleted. 232call assert_fails ( 'pwd' , 'E187:' ) 233call assert_equal ( '' , getcwd ()) 234cd - 235call assert_equal ( saveddir , getcwd ()) 236endfunc 237 238func Test_cd_completion () 239call mkdir ( 'XComplDir1' , 'D' ) 240call mkdir ( 'XComplDir2' , 'D' ) 241call mkdir ( 'sub/XComplDir3' , 'pD' ) 242call writefile ([], 'XComplFile' , 'D' ) 243 244for cmd in [ 'cd' , 'chdir' , 'lcd' , 'lchdir' , 'tcd' , 'tchdir' ] 245call feedkeys ( ':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>" , 'tx' ) 246call assert_equal ( '"' .. cmd .. ' XComplDir1/ XComplDir2/' , @:) 247endfor 248 249set cdpath += sub 250for cmd in [ 'cd' , 'chdir' , 'lcd' , 'lchdir' , 'tcd' , 'tchdir' ] 251call feedkeys ( ':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>" , 'tx' ) 252call assert_equal ( '"' .. cmd .. ' XComplDir1/ XComplDir2/ XComplDir3/' , @:) 253endfor 254set cdpath & 255 256if has ( 'win32' ) 257" Test Windows absolute path completion 258let saved_cwd = getcwd () 259 260" Retrieve a suitable dir in the current drive 261for d in readdir ( '/' , 'isdirectory("/" .. v:val) && len(v:val) > 2' ) 262" Paths containing '$' such as "$RECYCLE.BIN" are skipped because 263" they are considered environment variables and completion does not 264" work. 265if d =~ '\V$' 266continue 267endif 268" Skip directories that we don't have permission to "cd" into by 269" actually "cd"ing into them and making sure they don't fail. 270" Directory "System Volume Information" is an example of this. 271try 272call chdir ( '/' .. d ) 273let dir = d 274" Yay! We found a suitable dir! 275break 276catch /:\(E472\|E344\):/ 277" Just skip directories where "cd" fails 278continue 279finally 280call chdir ( saved_cwd ) 281endtry 282endfor 283if ! exists ( 'dir' ) 284throw 'Skipped: no testable directories found in the current drive root' 285endif 286 287" Get partial path 288let partial = dir [ 0 : -2 ] 289" Get the current drive letter and full path of the target dir 290call chdir ( '/' .. dir ) 291let full = getcwd () 292let drive = full [ 0 ] 293call chdir ( saved_cwd ) 294 295" Spaces are escaped in command line completion. Next, in assert_match(), 296" the backslash added by the first escape also needs to be escaped 297" separately, so the escape is doubled. 298let want_full = escape ( escape ( full , ' ' ), '\' ) 299let want_dir = escape ( escape ( dir , ' ' ), '\' ) 300 301for cmd in [ 'cd' , 'chdir' , 'lcd' , 'lchdir' , 'tcd' , 'tchdir' ] 302for sep in [ '/' , '\' ] 303 304" Explicit drive letter 305call feedkeys ( ':' .. cmd .. ' ' .. drive .. ':' .. sep .. 306 \partial .. "\<C-A>\<C-B>\"\<CR>" , 'tx' ) 307call assert_match ( want_full , @:) 308 309" Implicit drive letter 310call feedkeys ( ':' .. cmd .. ' ' .. sep .. partial .. "\<C-A>\<C-B>\"\<CR>" , 'tx' ) 311call assert_match ( '/' .. want_dir .. '/' , @:) 312 313" UNC path 314call feedkeys ( ':' .. cmd .. ' ' .. sep .. sep .. $ COMPUTERNAME .. sep .. 315 \drive .. '$' .. sep .. partial .. "\<C-A>\<C-B>\"\<CR>" , 'tx' ) 316call assert_match ( '//' .. $ COMPUTERNAME .. '/' .. drive .. '$/' .. want_dir .. '/' , @:) 317 318endfor 319endfor 320endif 321endfunc 322 323func Test_cd_unknown_dir () 324call mkdir ( 'Xa' , 'R' ) 325cd Xa 326call writefile ([ 'text' ], 'Xb.txt' ) 327edit Xa/Xb.txt 328let first_buf = bufnr () 329cd .. 330edit 331call assert_equal ( first_buf , bufnr ()) 332edit Xa/Xb.txt 333call assert_notequal ( first_buf , bufnr ()) 334 335bwipe ! 336exe "bwipe! " .. first_buf 337endfunc 338 339func Test_getcwd_actual_dir () 340CheckFunction test_autochdir 341CheckOption autochdir 342 343let startdir = getcwd () 344call mkdir ( 'Xactual' , 'R' ) 345call test_autochdir () 346set autochdir 347edit Xactual/file.txt 348call assert_match ( 'testdir.Xactual$' , getcwd ()) 349lcd .. 350call assert_match ( 'testdir$' , getcwd ()) 351edit 352call assert_match ( 'testdir.Xactual$' , getcwd ()) 353call assert_match ( 'testdir$' , getcwd ( win_getid ())) 354 355set noautochdir 356bwipe ! 357call chdir ( startdir ) 358endfunc 359 360func Test_cd_preserve_symlinks () 361" Test new behavior: preserve symlinks when cpo-=~ 362set cpoptions += ~ 363 364let savedir = getcwd () 365call mkdir ( 'Xsource' , 'R' ) 366call writefile ([ 'abc' ], 'Xsource/foo.txt' , 'D' ) 367 368if has ( "win32" ) 369silent !mklink /D Xdest Xsource 370else 371silent !ln -sXsource Xdest 372endif 373if v: shell_error 374call delete ( 'Xsource' , 'rf' ) 375throw 'Skipped: cannot create symlinks' 376endif 377 378edit Xdest/foo.txt 379let path_before = expand ( '%' ) 380call assert_match ( 'Xdest[/\\]foo\.txt$' , path_before ) 381 382cd . 383let path_after = expand ( '%' ) 384call assert_equal ( path_before , path_after ) 385call assert_match ( 'Xdest[/\\]foo\.txt$' , path_after ) 386 387bwipe ! 388set cpoptions & 389call delete ( 'Xdest' , 'rf' ) 390call delete ( 'Xsource' , 'rf' ) 391call chdir ( savedir ) 392endfunc 393 394func Test_cd_symlinks () 395CheckNotMSWindows 396 397let savedir = getcwd () 398call mkdir ( 'Xsource' , 'R' ) 399call writefile ([ 'abc' ], 'Xsource/foo.txt' , 'D' ) 400 401silent !ln -sXsource Xdest 402if v: shell_error 403call delete ( 'Xsource' , 'rf' ) 404throw 'Skipped: cannot create symlinks' 405endif 406 407edit Xdest/foo.txt 408let path_before = expand ( '%' ) 409call assert_match ( 'Xdest[/\\]foo\.txt$' , path_before ) 410 411cd . 412let path_after = expand ( '%' ) 413call assert_match ( 'Xsource[/\\]foo\.txt$' , path_after ) 414call assert_notequal ( path_before , path_after ) 415 416bwipe ! 417set cpoptions & 418call delete ( 'Xdest' , 'rf' ) 419call delete ( 'Xsource' , 'rf' ) 420call chdir ( savedir ) 421endfunc 422 423func Test_cd_shorten_bufname_with_duplicate_slashes () 424let savedir = getcwd () 425call mkdir ( 'Xexistingdir' , 'R' ) 426new Xexistingdir//foo/bar 427cd Xexistingdir 428call assert_equal ( 'foo/bar' , bufname ( '%' )) 429 430call chdir ( savedir ) 431bwipe ! 432endfunc 433 434" vim: shiftwidth=2 sts=2 expandtab