1" Test for :cd and chdir() 2 3source shared.vim 4 5func Test_cd_large_path() 6 " This used to crash with a heap write overflow. 7 call assert_fails('cd ' . repeat('x', 5000), 'E344:') 8endfunc 9 10func Test_cd_up_and_down() 11 let path = getcwd() 12 cd .. 13 call assert_notequal(path, getcwd()) 14 exe 'cd ' .. fnameescape(path) 15 call assert_equal(path, getcwd()) 16endfunc 17 18func Test_cd_no_arg() 19 if has('unix') 20 " Test that cd without argument goes to $HOME directory on Unix systems. 21 let path = getcwd() 22 cd 23 call assert_equal($HOME, getcwd()) 24 call assert_notequal(path, getcwd()) 25 exe 'cd ' .. fnameescape(path) 26 call assert_equal(path, getcwd()) 27 else 28 " Test that cd without argument echoes cwd on non-Unix systems. 29 call assert_match(getcwd(), execute('cd')) 30 endif 31endfunc 32 33func Test_cd_minus() 34 " Test the :cd - goes back to the previous directory. 35 let path = getcwd() 36 cd .. 37 let path_dotdot = getcwd() 38 call assert_notequal(path, path_dotdot) 39 cd - 40 call assert_equal(path, getcwd()) 41 cd - 42 call assert_equal(path_dotdot, getcwd()) 43 cd - 44 call assert_equal(path, getcwd()) 45 46 " Test for :cd - without a previous directory 47 let lines =<< trim [SCRIPT] 48 call assert_fails('cd -', 'E186:') 49 call assert_fails('call chdir("-")', 'E186:') 50 call writefile(v:errors, 'Xresult') 51 qall! 52 [SCRIPT] 53 call writefile(lines, 'Xscript') 54 if RunVim([], [], '--clean -S Xscript') 55 call assert_equal([], readfile('Xresult')) 56 endif 57 call delete('Xscript') 58 call delete('Xresult') 59endfunc 60 61" Test for chdir() 62func Test_chdir_func() 63 let topdir = getcwd() 64 call mkdir('Xdir/y/z', 'p') 65 66 " Create a few tabpages and windows with different directories 67 new 68 cd Xdir 69 tabnew 70 tcd y 71 below new 72 below new 73 lcd z 74 75 tabfirst 76 call assert_match('^\[global\] .*/Xdir$', trim(execute('verbose pwd'))) 77 call chdir('..') 78 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t')) 79 call assert_equal('z', fnamemodify(3->getcwd(2), ':t')) 80 tabnext | wincmd t 81 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd'))) 82 eval '..'->chdir() 83 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t')) 84 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t')) 85 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t')) 86 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t')) 87 3wincmd w 88 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd'))) 89 call chdir('..') 90 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t')) 91 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t')) 92 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t')) 93 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t')) 94 95 " Error case 96 call assert_fails("call chdir('dir-abcd')", 'E344:') 97 silent! let d = chdir("dir_abcd") 98 call assert_equal("", d) 99 " Should not crash 100 call chdir(d) 101 call assert_equal('', chdir([])) 102 103 only | tabonly 104 call chdir(topdir) 105 call delete('Xdir', 'rf') 106endfunc 107 108" Test for changing to the previous directory '-' 109func Test_prev_dir() 110 let topdir = getcwd() 111 call mkdir('Xdir/a/b/c', 'p') 112 113 " Create a few tabpages and windows with different directories 114 new | only 115 tabnew | new 116 tabnew 117 tabfirst 118 cd Xdir 119 tabnext | wincmd t 120 tcd a 121 wincmd w 122 lcd b 123 tabnext 124 tcd a/b/c 125 126 " Change to the previous directory twice in all the windows. 127 tabfirst 128 cd - | cd - 129 tabnext | wincmd t 130 tcd - | tcd - 131 wincmd w 132 lcd - | lcd - 133 tabnext 134 tcd - | tcd - 135 136 " Check the directory of all the windows 137 tabfirst 138 call assert_equal('Xdir', fnamemodify(getcwd(), ':t')) 139 tabnext | wincmd t 140 call assert_equal('a', fnamemodify(getcwd(), ':t')) 141 wincmd w 142 call assert_equal('b', fnamemodify(getcwd(), ':t')) 143 tabnext 144 call assert_equal('c', fnamemodify(getcwd(), ':t')) 145 146 " Change to the previous directory using chdir() 147 tabfirst 148 call chdir("-") | call chdir("-") 149 tabnext | wincmd t 150 call chdir("-") | call chdir("-") 151 wincmd w 152 call chdir("-") | call chdir("-") 153 tabnext 154 call chdir("-") | call chdir("-") 155 156 " Check the directory of all the windows 157 tabfirst 158 call assert_equal('Xdir', fnamemodify(getcwd(), ':t')) 159 tabnext | wincmd t 160 call assert_equal('a', fnamemodify(getcwd(), ':t')) 161 wincmd w 162 call assert_equal('b', fnamemodify(getcwd(), ':t')) 163 tabnext 164 call assert_equal('c', fnamemodify(getcwd(), ':t')) 165 166 only | tabonly 167 call chdir(topdir) 168 call delete('Xdir', 'rf') 169endfunc 170 171func Test_lcd_split() 172 let curdir = getcwd() 173 lcd .. 174 split 175 lcd - 176 call assert_equal(curdir, getcwd()) 177 quit! 178endfunc 179 180func Test_cd_completion() 181 call mkdir('XComplDir1', 'p') 182 call mkdir('XComplDir2', 'p') 183 call writefile([], 'XComplFile') 184 185 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir'] 186 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx') 187 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:) 188 endfor 189 190 call delete('XComplDir1', 'd') 191 call delete('XComplDir2', 'd') 192 call delete('XComplFile') 193endfunc 194 195" vim: shiftwidth=2 sts=2 expandtab 196