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. 8 call assert_fails('cd ' . repeat('x', 5000), 'E344:') 9endfunc 10 11func Test_cd_up_and_down() 12 let path = getcwd() 13 cd .. 14 call assert_notequal(path, getcwd()) 15 exe 'cd ' .. fnameescape(path) 16 call assert_equal(path, getcwd()) 17endfunc 18 19func Test_cd_no_arg() 20 if has('unix') 21 " Test that cd without argument goes to $HOME directory on Unix systems. 22 let path = getcwd() 23 cd 24 call assert_equal($HOME, getcwd()) 25 call assert_notequal(path, getcwd()) 26 exe 'cd ' .. fnameescape(path) 27 call assert_equal(path, getcwd()) 28 else 29 " Test that cd without argument echoes cwd on non-Unix systems. 30 call assert_match(getcwd(), execute('cd')) 31 endif 32endfunc 33 34func Test_cd_minus() 35 " Test the :cd - goes back to the previous directory. 36 let path = getcwd() 37 cd .. 38 let path_dotdot = getcwd() 39 call assert_notequal(path, path_dotdot) 40 cd - 41 call assert_equal(path, getcwd()) 42 cd - 43 call assert_equal(path_dotdot, getcwd()) 44 cd - 45 call assert_equal(path, getcwd()) 46 47 " Test for :cd - without a previous directory 48 let lines =<< trim [SCRIPT] 49 call assert_fails('cd -', 'E186:') 50 call assert_fails('call chdir("-")', 'E186:') 51 call writefile(v:errors, 'Xresult') 52 qall! 53 [SCRIPT] 54 call writefile(lines, 'Xscript') 55 if RunVim([], [], '--clean -S Xscript') 56 call assert_equal([], readfile('Xresult')) 57 endif 58 call delete('Xscript') 59 call delete('Xresult') 60endfunc 61 62" Test for chdir() 63func Test_chdir_func() 64 let topdir = getcwd() 65 call mkdir('Xdir/y/z', 'p') 66 67 " Create a few tabpages and windows with different directories 68 new 69 cd Xdir 70 tabnew 71 tcd y 72 below new 73 below new 74 lcd z 75 76 tabfirst 77 call assert_match('^\[global\] .*/Xdir$', trim(execute('verbose pwd'))) 78 call chdir('..') 79 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t')) 80 call assert_equal('z', fnamemodify(3->getcwd(2), ':t')) 81 tabnext | wincmd t 82 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd'))) 83 eval '..'->chdir() 84 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t')) 85 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t')) 86 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t')) 87 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t')) 88 3wincmd w 89 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd'))) 90 call chdir('..') 91 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t')) 92 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t')) 93 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t')) 94 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t')) 95 96 " Error case 97 call assert_fails("call chdir('dir-abcd')", 'E344:') 98 silent! let d = chdir("dir_abcd") 99 call assert_equal("", d) 100 " Should not crash 101 call chdir(d) 102 call assert_equal('', chdir([])) 103 104 only | tabonly 105 call chdir(topdir) 106 call delete('Xdir', 'rf') 107endfunc 108 109" Test for changing to the previous directory '-' 110func Test_prev_dir() 111 let topdir = getcwd() 112 call mkdir('Xdir/a/b/c', 'p') 113 114 " Create a few tabpages and windows with different directories 115 new | only 116 tabnew | new 117 tabnew 118 tabfirst 119 cd Xdir 120 tabnext | wincmd t 121 tcd a 122 wincmd w 123 lcd b 124 tabnext 125 tcd a/b/c 126 127 " Change to the previous directory twice in all the windows. 128 tabfirst 129 cd - | cd - 130 tabnext | wincmd t 131 tcd - | tcd - 132 wincmd w 133 lcd - | lcd - 134 tabnext 135 tcd - | tcd - 136 137 " Check the directory of all the windows 138 tabfirst 139 call assert_equal('Xdir', fnamemodify(getcwd(), ':t')) 140 tabnext | wincmd t 141 call assert_equal('a', fnamemodify(getcwd(), ':t')) 142 wincmd w 143 call assert_equal('b', fnamemodify(getcwd(), ':t')) 144 tabnext 145 call assert_equal('c', fnamemodify(getcwd(), ':t')) 146 147 " Change to the previous directory using chdir() 148 tabfirst 149 call chdir("-") | call chdir("-") 150 tabnext | wincmd t 151 call chdir("-") | call chdir("-") 152 wincmd w 153 call chdir("-") | call chdir("-") 154 tabnext 155 call chdir("-") | call chdir("-") 156 157 " Check the directory of all the windows 158 tabfirst 159 call assert_equal('Xdir', fnamemodify(getcwd(), ':t')) 160 tabnext | wincmd t 161 call assert_equal('a', fnamemodify(getcwd(), ':t')) 162 wincmd w 163 call assert_equal('b', fnamemodify(getcwd(), ':t')) 164 tabnext 165 call assert_equal('c', fnamemodify(getcwd(), ':t')) 166 167 only | tabonly 168 call chdir(topdir) 169 call delete('Xdir', 'rf') 170endfunc 171 172func Test_lcd_split() 173 let curdir = getcwd() 174 lcd .. 175 split 176 lcd - 177 call assert_equal(curdir, getcwd()) 178 quit! 179endfunc 180 181func Test_cd_from_non_existing_dir() 182 CheckNotMSWindows 183 184 let saveddir = getcwd() 185 call mkdir('Xdeleted_dir') 186 cd Xdeleted_dir 187 call delete(saveddir .. '/Xdeleted_dir', 'd') 188 189 " Expect E187 as the current directory was deleted. 190 call assert_fails('pwd', 'E187:') 191 call assert_equal('', getcwd()) 192 cd - 193 call assert_equal(saveddir, getcwd()) 194endfunc 195 196func Test_cd_completion() 197 call mkdir('XComplDir1', 'p') 198 call mkdir('XComplDir2', 'p') 199 call writefile([], 'XComplFile') 200 201 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir'] 202 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx') 203 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:) 204 endfor 205 206 call delete('XComplDir1', 'd') 207 call delete('XComplDir2', 'd') 208 call delete('XComplFile') 209endfunc 210 211func Test_cd_unknown_dir() 212 call mkdir('Xa') 213 cd Xa 214 call writefile(['text'], 'Xb.txt') 215 edit Xa/Xb.txt 216 let first_buf = bufnr() 217 cd .. 218 edit 219 call assert_equal(first_buf, bufnr()) 220 edit Xa/Xb.txt 221 call assert_notequal(first_buf, bufnr()) 222 223 bwipe! 224 exe "bwipe! " .. first_buf 225 call delete('Xa', 'rf') 226endfunc 227 228func Test_getcwd_actual_dir() 229 let startdir = getcwd() 230 call mkdir('Xactual') 231 call test_autochdir() 232 set autochdir 233 edit Xactual/file.txt 234 call assert_match('testdir.Xactual$', getcwd()) 235 lcd .. 236 call assert_match('testdir$', getcwd()) 237 edit 238 call assert_match('testdir.Xactual$', getcwd()) 239 call assert_match('testdir$', getcwd(win_getid())) 240 241 set noautochdir 242 bwipe! 243 call chdir(startdir) 244 call delete('Xactual', 'rf') 245endfunc 246 247" vim: shiftwidth=2 sts=2 expandtab 248