xref: /vim-8.2.3635/src/testdir/test_cd.vim (revision 8024f936)
1" Test for :cd and chdir()
2
3func Test_cd_large_path()
4  " This used to crash with a heap write overflow.
5  call assert_fails('cd ' . repeat('x', 5000), 'E472:')
6endfunc
7
8func Test_cd_up_and_down()
9  let path = getcwd()
10  cd ..
11  call assert_notequal(path, getcwd())
12  exe 'cd ' .. fnameescape(path)
13  call assert_equal(path, getcwd())
14endfunc
15
16func Test_cd_no_arg()
17  if has('unix')
18    " Test that cd without argument goes to $HOME directory on Unix systems.
19    let path = getcwd()
20    cd
21    call assert_equal($HOME, getcwd())
22    call assert_notequal(path, getcwd())
23    exe 'cd ' .. fnameescape(path)
24    call assert_equal(path, getcwd())
25  else
26    " Test that cd without argument echoes cwd on non-Unix systems.
27    call assert_match(getcwd(), execute('cd'))
28  endif
29endfunc
30
31func Test_cd_minus()
32  " Test the  :cd -  goes back to the previous directory.
33  let path = getcwd()
34  cd ..
35  let path_dotdot = getcwd()
36  call assert_notequal(path, path_dotdot)
37  cd -
38  call assert_equal(path, getcwd())
39  cd -
40  call assert_equal(path_dotdot, getcwd())
41  cd -
42  call assert_equal(path, getcwd())
43endfunc
44
45func Test_cd_with_cpo_chdir()
46  e Xfoo
47  call setline(1, 'foo')
48  let path = getcwd()
49  set cpo+=.
50
51  " :cd should fail when buffer is modified and 'cpo' contains dot.
52  call assert_fails('cd ..', 'E747:')
53  call assert_equal(path, getcwd())
54
55  " :cd with exclamation mark should succeed.
56  cd! ..
57  call assert_notequal(path, getcwd())
58
59  " :cd should succeed when buffer has been written.
60  w!
61  exe 'cd ' .. fnameescape(path)
62  call assert_equal(path, getcwd())
63
64  call delete('Xfoo')
65  set cpo&
66  bw!
67endfunc
68
69" Test for chdir()
70func Test_chdir_func()
71  let topdir = getcwd()
72  call mkdir('Xdir/y/z', 'p')
73
74  " Create a few tabpages and windows with different directories
75  new
76  cd Xdir
77  tabnew
78  tcd y
79  below new
80  below new
81  lcd z
82
83  tabfirst
84  call chdir('..')
85  call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
86  call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
87  tabnext | wincmd t
88  eval '..'->chdir()
89  call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
90  call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
91  call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
92  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
93  3wincmd w
94  call chdir('..')
95  call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
96  call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
97  call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
98  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
99
100  " Error case
101  call assert_fails("call chdir('dir-abcd')", 'E472:')
102  silent! let d = chdir("dir_abcd")
103  call assert_equal("", d)
104
105  only | tabonly
106  call chdir(topdir)
107  call delete('Xdir', 'rf')
108endfunc
109
110func Test_cd_completion()
111  call mkdir('XComplDir1', 'p')
112  call mkdir('XComplDir2', 'p')
113  call writefile([], 'XComplFile')
114
115  for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
116    call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
117    call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
118  endfor
119
120  call delete('XComplDir1', 'd')
121  call delete('XComplDir2', 'd')
122  call delete('XComplFile')
123endfunc
124