blob: 53f9c1081ee77290877543612b45689273c57d66 [file] [log] [blame]
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001" Test for :cd and chdir()
Bram Moolenaar15618fa2017-03-19 21:37:13 +01002
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003source shared.vim
4
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005func 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 ..
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020013 call assert_notequal(path, getcwd())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010014 exe 'cd ' .. fnameescape(path)
Bram Moolenaar15618fa2017-03-19 21:37:13 +010015 call assert_equal(path, getcwd())
16endfunc
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020017
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())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010025 exe 'cd ' .. fnameescape(path)
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020026 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())
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010045
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')
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020059endfunc
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!
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010077 exe 'cd ' .. fnameescape(path)
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020078 call assert_equal(path, getcwd())
79
80 call delete('Xfoo')
81 set cpo&
82 bw!
83endfunc
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020084
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
Bram Moolenaar95058722020-06-01 16:26:19 +0200100 call assert_match('^\[global\] .*/Xdir$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200101 call chdir('..')
102 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200103 call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200104 tabnext | wincmd t
Bram Moolenaar95058722020-06-01 16:26:19 +0200105 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200106 eval '..'->chdir()
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200107 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
108 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
109 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
110 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
111 3wincmd w
Bram Moolenaar95058722020-06-01 16:26:19 +0200112 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200113 call chdir('..')
114 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
115 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
116 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
117 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
118
119 " Error case
120 call assert_fails("call chdir('dir-abcd')", 'E472:')
121 silent! let d = chdir("dir_abcd")
122 call assert_equal("", d)
Bram Moolenaar7cc96922020-01-31 22:41:38 +0100123 " Should not crash
124 call chdir(d)
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200125 call assert_equal('', chdir([]))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200126
127 only | tabonly
Bram Moolenaar3503d7c2019-11-09 20:10:17 +0100128 call chdir(topdir)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200129 call delete('Xdir', 'rf')
130endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100131
Bram Moolenaar002bc792020-06-05 22:33:42 +0200132" Test for changing to the previous directory '-'
133func Test_prev_dir()
134 let topdir = getcwd()
135 call mkdir('Xdir/a/b/c', 'p')
136
137 " Create a few tabpages and windows with different directories
138 new | only
139 tabnew | new
140 tabnew
141 tabfirst
142 cd Xdir
143 tabnext | wincmd t
144 tcd a
145 wincmd w
146 lcd b
147 tabnext
148 tcd a/b/c
149
150 " Change to the previous directory twice in all the windows.
151 tabfirst
152 cd - | cd -
153 tabnext | wincmd t
154 tcd - | tcd -
155 wincmd w
156 lcd - | lcd -
157 tabnext
158 tcd - | tcd -
159
160 " Check the directory of all the windows
161 tabfirst
162 call assert_equal('Xdir', fnamemodify(getcwd(), ':t'))
163 tabnext | wincmd t
164 call assert_equal('a', fnamemodify(getcwd(), ':t'))
165 wincmd w
166 call assert_equal('b', fnamemodify(getcwd(), ':t'))
167 tabnext
168 call assert_equal('c', fnamemodify(getcwd(), ':t'))
169
170 " Change to the previous directory using chdir()
171 tabfirst
172 call chdir("-") | call chdir("-")
173 tabnext | wincmd t
174 call chdir("-") | call chdir("-")
175 wincmd w
176 call chdir("-") | call chdir("-")
177 tabnext
178 call chdir("-") | call chdir("-")
179
180 " Check the directory of all the windows
181 tabfirst
182 call assert_equal('Xdir', fnamemodify(getcwd(), ':t'))
183 tabnext | wincmd t
184 call assert_equal('a', fnamemodify(getcwd(), ':t'))
185 wincmd w
186 call assert_equal('b', fnamemodify(getcwd(), ':t'))
187 tabnext
188 call assert_equal('c', fnamemodify(getcwd(), ':t'))
189
190 only | tabonly
191 call chdir(topdir)
192 call delete('Xdir', 'rf')
193endfunc
194
Bram Moolenaar297610b2019-12-27 17:20:55 +0100195func Test_cd_completion()
196 call mkdir('XComplDir1', 'p')
197 call mkdir('XComplDir2', 'p')
198 call writefile([], 'XComplFile')
199
200 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
201 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
202 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
203 endfor
204
205 call delete('XComplDir1', 'd')
206 call delete('XComplDir2', 'd')
207 call delete('XComplFile')
208endfunc