xref: /vim-8.2.3635/src/testdir/test_cd.vim (revision beae4084)
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), 'E472:')
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
61func Test_cd_with_cpo_chdir()
62  e Xfoo
63  call setline(1, 'foo')
64  let path = getcwd()
65  set cpo+=.
66
67  " :cd should fail when buffer is modified and 'cpo' contains dot.
68  call assert_fails('cd ..', 'E747:')
69  call assert_equal(path, getcwd())
70
71  " :cd with exclamation mark should succeed.
72  cd! ..
73  call assert_notequal(path, getcwd())
74
75  " :cd should succeed when buffer has been written.
76  w!
77  exe 'cd ' .. fnameescape(path)
78  call assert_equal(path, getcwd())
79
80  call delete('Xfoo')
81  set cpo&
82  bw!
83endfunc
84
85" Test for chdir()
86func Test_chdir_func()
87  let topdir = getcwd()
88  call mkdir('Xdir/y/z', 'p')
89
90  " Create a few tabpages and windows with different directories
91  new
92  cd Xdir
93  tabnew
94  tcd y
95  below new
96  below new
97  lcd z
98
99  tabfirst
100  call chdir('..')
101  call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
102  call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
103  tabnext | wincmd t
104  eval '..'->chdir()
105  call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
106  call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
107  call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
108  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
109  3wincmd w
110  call chdir('..')
111  call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
112  call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
113  call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
114  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
115
116  " Error case
117  call assert_fails("call chdir('dir-abcd')", 'E472:')
118  silent! let d = chdir("dir_abcd")
119  call assert_equal("", d)
120  " Should not crash
121  call chdir(d)
122
123  only | tabonly
124  call chdir(topdir)
125  call delete('Xdir', 'rf')
126endfunc
127
128func Test_cd_completion()
129  call mkdir('XComplDir1', 'p')
130  call mkdir('XComplDir2', 'p')
131  call writefile([], 'XComplFile')
132
133  for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
134    call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
135    call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
136  endfor
137
138  call delete('XComplDir1', 'd')
139  call delete('XComplDir2', 'd')
140  call delete('XComplFile')
141endfunc
142