2021-07-31 15:28:36 -07:00
|
|
|
" Test for :cd and chdir()
|
|
|
|
|
|
|
|
source shared.vim
|
|
|
|
source check.vim
|
2018-07-22 13:18:51 -07:00
|
|
|
|
|
|
|
func Test_cd_large_path()
|
|
|
|
" This used to crash with a heap write overflow.
|
|
|
|
call assert_fails('cd ' . repeat('x', 5000), 'E472:')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_cd_up_and_down()
|
|
|
|
let path = getcwd()
|
|
|
|
cd ..
|
2018-08-26 06:56:26 -07:00
|
|
|
call assert_notequal(path, getcwd())
|
2021-10-17 07:04:53 -07:00
|
|
|
exe 'cd ' .. fnameescape(path)
|
2018-07-22 13:18:51 -07:00
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
endfunc
|
2018-08-26 06:56:26 -07:00
|
|
|
|
|
|
|
func Test_cd_no_arg()
|
|
|
|
if has('unix')
|
|
|
|
" Test that cd without argument goes to $HOME directory on Unix systems.
|
|
|
|
let path = getcwd()
|
|
|
|
cd
|
|
|
|
call assert_equal($HOME, getcwd())
|
|
|
|
call assert_notequal(path, getcwd())
|
2021-10-17 07:04:53 -07:00
|
|
|
exe 'cd ' .. fnameescape(path)
|
2018-08-26 06:56:26 -07:00
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
else
|
|
|
|
" Test that cd without argument echoes cwd on non-Unix systems.
|
|
|
|
call assert_match(getcwd(), execute('cd'))
|
|
|
|
endif
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_cd_minus()
|
|
|
|
" Test the :cd - goes back to the previous directory.
|
|
|
|
let path = getcwd()
|
|
|
|
cd ..
|
|
|
|
let path_dotdot = getcwd()
|
|
|
|
call assert_notequal(path, path_dotdot)
|
|
|
|
cd -
|
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
cd -
|
|
|
|
call assert_equal(path_dotdot, getcwd())
|
|
|
|
cd -
|
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_cd_with_cpo_chdir()
|
|
|
|
e Xfoo
|
|
|
|
call setline(1, 'foo')
|
|
|
|
let path = getcwd()
|
2018-08-26 11:41:45 -07:00
|
|
|
" set cpo+=.
|
2018-08-26 06:56:26 -07:00
|
|
|
|
|
|
|
" :cd should fail when buffer is modified and 'cpo' contains dot.
|
2018-08-26 11:41:45 -07:00
|
|
|
" call assert_fails('cd ..', 'E747:')
|
2018-08-26 06:56:26 -07:00
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
|
|
|
|
" :cd with exclamation mark should succeed.
|
|
|
|
cd! ..
|
|
|
|
call assert_notequal(path, getcwd())
|
|
|
|
|
|
|
|
" :cd should succeed when buffer has been written.
|
|
|
|
w!
|
2021-10-17 07:04:53 -07:00
|
|
|
exe 'cd ' .. fnameescape(path)
|
2018-08-26 06:56:26 -07:00
|
|
|
call assert_equal(path, getcwd())
|
|
|
|
|
|
|
|
call delete('Xfoo')
|
|
|
|
set cpo&
|
|
|
|
bw!
|
|
|
|
endfunc
|
2021-07-31 15:28:36 -07:00
|
|
|
|
2021-10-17 07:04:53 -07:00
|
|
|
" Test for chdir()
|
|
|
|
func Test_chdir_func()
|
|
|
|
let topdir = getcwd()
|
|
|
|
call mkdir('Xdir/y/z', 'p')
|
|
|
|
|
|
|
|
" Create a few tabpages and windows with different directories
|
|
|
|
new
|
|
|
|
cd Xdir
|
|
|
|
tabnew
|
|
|
|
tcd y
|
|
|
|
below new
|
|
|
|
below new
|
|
|
|
lcd z
|
|
|
|
|
|
|
|
tabfirst
|
2021-10-17 07:04:53 -07:00
|
|
|
call assert_match('^\[global\] .*/Xdir$', trim(execute('verbose pwd')))
|
2021-10-17 07:04:53 -07:00
|
|
|
call chdir('..')
|
|
|
|
call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
|
|
|
|
call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
|
|
|
|
tabnext | wincmd t
|
2021-10-17 07:04:53 -07:00
|
|
|
call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
|
2021-10-17 07:04:53 -07:00
|
|
|
call chdir('..')
|
|
|
|
call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
|
|
|
|
call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
|
|
|
|
call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
|
|
|
|
call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
|
|
|
|
3wincmd w
|
2021-10-17 07:04:53 -07:00
|
|
|
call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
|
2021-10-17 07:04:53 -07:00
|
|
|
call chdir('..')
|
|
|
|
call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
|
|
|
|
call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
|
|
|
|
call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
|
|
|
|
call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
|
|
|
|
|
|
|
|
" Error case
|
|
|
|
call assert_fails("call chdir('dir-abcd')", 'E472:')
|
|
|
|
silent! let d = chdir("dir_abcd")
|
|
|
|
call assert_equal("", d)
|
2021-10-17 07:04:53 -07:00
|
|
|
" Should not crash
|
|
|
|
call chdir(d)
|
2021-10-17 07:04:53 -07:00
|
|
|
|
|
|
|
only | tabonly
|
2021-10-17 07:04:53 -07:00
|
|
|
call chdir(topdir)
|
2021-10-17 07:04:53 -07:00
|
|
|
call delete('Xdir', 'rf')
|
|
|
|
endfunc
|
|
|
|
|
2021-07-31 15:28:36 -07:00
|
|
|
func Test_cd_from_non_existing_dir()
|
|
|
|
CheckNotMSWindows
|
|
|
|
|
|
|
|
let saveddir = getcwd()
|
|
|
|
call mkdir('Xdeleted_dir')
|
|
|
|
cd Xdeleted_dir
|
|
|
|
call delete(saveddir .. '/Xdeleted_dir', 'd')
|
|
|
|
|
|
|
|
" Expect E187 as the current directory was deleted.
|
|
|
|
call assert_fails('pwd', 'E187:')
|
|
|
|
call assert_equal('', getcwd())
|
|
|
|
cd -
|
|
|
|
call assert_equal(saveddir, getcwd())
|
|
|
|
endfunc
|