blob: 878a2aeb5f51d85907311a5aab8bc55d39bfe1c0 [file] [log] [blame]
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001" Test for :cd and chdir()
Bram Moolenaar15618fa2017-03-19 21:37:13 +01002
3func Test_cd_large_path()
4 " This used to crash with a heap write overflow.
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005 call assert_fails('cd ' . repeat('x', 5000), 'E344:')
Bram Moolenaar15618fa2017-03-19 21:37:13 +01006endfunc
7
8func Test_cd_up_and_down()
9 let path = getcwd()
10 cd ..
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020011 call assert_notequal(path, getcwd())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010012 exe 'cd ' .. fnameescape(path)
Bram Moolenaar15618fa2017-03-19 21:37:13 +010013 call assert_equal(path, getcwd())
14endfunc
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020015
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())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010023 exe 'cd ' .. fnameescape(path)
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020024 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())
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010043
Richard Doty3d0abad2021-12-29 14:39:08 +000044 " Test for :cd - after a failed :cd
45 call assert_fails('cd /nonexistent', 'E344:')
46 call assert_equal(path, getcwd())
47 cd -
48 call assert_equal(path_dotdot, getcwd())
49 cd -
50
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010051 " Test for :cd - without a previous directory
52 let lines =<< trim [SCRIPT]
53 call assert_fails('cd -', 'E186:')
54 call assert_fails('call chdir("-")', 'E186:')
55 call writefile(v:errors, 'Xresult')
56 qall!
57 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010058 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010059 if RunVim([], [], '--clean -S Xscript')
60 call assert_equal([], readfile('Xresult'))
61 endif
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010062 call delete('Xresult')
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020063endfunc
64
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020065" Test for chdir()
66func Test_chdir_func()
67 let topdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010068 call mkdir('Xchdir/y/z', 'pR')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020069
70 " Create a few tabpages and windows with different directories
71 new
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010072 cd Xchdir
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020073 tabnew
74 tcd y
75 below new
76 below new
77 lcd z
78
79 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010080 call assert_match('^\[global\] .*/Xchdir$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020081 call chdir('..')
82 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020083 call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020084 tabnext | wincmd t
Bram Moolenaar95058722020-06-01 16:26:19 +020085 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020086 eval '..'->chdir()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010087 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
88 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020089 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
90 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
91 3wincmd w
Bram Moolenaar95058722020-06-01 16:26:19 +020092 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020093 call chdir('..')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010094 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
95 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020096 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
97 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
98
99 " Error case
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200100 call assert_fails("call chdir('dir-abcd')", 'E344:')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200101 silent! let d = chdir("dir_abcd")
102 call assert_equal("", d)
Bram Moolenaar7cc96922020-01-31 22:41:38 +0100103 " Should not crash
104 call chdir(d)
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200105 call assert_equal('', chdir([]))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200106
107 only | tabonly
Bram Moolenaar3503d7c2019-11-09 20:10:17 +0100108 call chdir(topdir)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200109endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100110
Bram Moolenaar002bc792020-06-05 22:33:42 +0200111" Test for changing to the previous directory '-'
112func Test_prev_dir()
113 let topdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100114 call mkdir('Xprevdir/a/b/c', 'pR')
Bram Moolenaar002bc792020-06-05 22:33:42 +0200115
116 " Create a few tabpages and windows with different directories
117 new | only
118 tabnew | new
119 tabnew
120 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100121 cd Xprevdir
Bram Moolenaar002bc792020-06-05 22:33:42 +0200122 tabnext | wincmd t
123 tcd a
124 wincmd w
125 lcd b
126 tabnext
127 tcd a/b/c
128
129 " Change to the previous directory twice in all the windows.
130 tabfirst
131 cd - | cd -
132 tabnext | wincmd t
133 tcd - | tcd -
134 wincmd w
135 lcd - | lcd -
136 tabnext
137 tcd - | tcd -
138
139 " Check the directory of all the windows
140 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100141 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200142 tabnext | wincmd t
143 call assert_equal('a', fnamemodify(getcwd(), ':t'))
144 wincmd w
145 call assert_equal('b', fnamemodify(getcwd(), ':t'))
146 tabnext
147 call assert_equal('c', fnamemodify(getcwd(), ':t'))
148
149 " Change to the previous directory using chdir()
150 tabfirst
151 call chdir("-") | call chdir("-")
152 tabnext | wincmd t
153 call chdir("-") | call chdir("-")
154 wincmd w
155 call chdir("-") | call chdir("-")
156 tabnext
157 call chdir("-") | call chdir("-")
158
159 " Check the directory of all the windows
160 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100161 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200162 tabnext | wincmd t
163 call assert_equal('a', fnamemodify(getcwd(), ':t'))
164 wincmd w
165 call assert_equal('b', fnamemodify(getcwd(), ':t'))
166 tabnext
167 call assert_equal('c', fnamemodify(getcwd(), ':t'))
168
169 only | tabonly
170 call chdir(topdir)
Bram Moolenaar002bc792020-06-05 22:33:42 +0200171endfunc
172
Bram Moolenaara9a47d12020-08-09 21:45:52 +0200173func Test_lcd_split()
174 let curdir = getcwd()
175 lcd ..
176 split
177 lcd -
178 call assert_equal(curdir, getcwd())
179 quit!
180endfunc
181
Dominique Pellefe3418a2021-07-10 17:59:48 +0200182func Test_cd_from_non_existing_dir()
183 CheckNotMSWindows
184
185 let saveddir = getcwd()
186 call mkdir('Xdeleted_dir')
187 cd Xdeleted_dir
188 call delete(saveddir .. '/Xdeleted_dir', 'd')
189
190 " Expect E187 as the current directory was deleted.
191 call assert_fails('pwd', 'E187:')
192 call assert_equal('', getcwd())
193 cd -
194 call assert_equal(saveddir, getcwd())
195endfunc
196
Bram Moolenaar297610b2019-12-27 17:20:55 +0100197func Test_cd_completion()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100198 call mkdir('XComplDir1', 'D')
199 call mkdir('XComplDir2', 'D')
LemonBoya20bf692024-07-11 22:35:53 +0200200 call mkdir('sub/XComplDir3', 'pD')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100201 call writefile([], 'XComplFile', 'D')
Bram Moolenaar297610b2019-12-27 17:20:55 +0100202
203 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
204 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
205 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
206 endfor
LemonBoya20bf692024-07-11 22:35:53 +0200207
208 set cdpath+=sub
209 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
210 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
211 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/ XComplDir3/', @:)
212 endfor
213 set cdpath&
Bram Moolenaar297610b2019-12-27 17:20:55 +0100214endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200215
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100216func Test_cd_unknown_dir()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100217 call mkdir('Xa', 'R')
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100218 cd Xa
219 call writefile(['text'], 'Xb.txt')
220 edit Xa/Xb.txt
221 let first_buf = bufnr()
222 cd ..
223 edit
224 call assert_equal(first_buf, bufnr())
225 edit Xa/Xb.txt
226 call assert_notequal(first_buf, bufnr())
227
228 bwipe!
229 exe "bwipe! " .. first_buf
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100230endfunc
231
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000232func Test_getcwd_actual_dir()
Dominique Pelle8dea1452021-12-04 15:12:40 +0000233 CheckOption autochdir
234
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000235 let startdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100236 call mkdir('Xactual', 'R')
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000237 call test_autochdir()
238 set autochdir
239 edit Xactual/file.txt
240 call assert_match('testdir.Xactual$', getcwd())
241 lcd ..
242 call assert_match('testdir$', getcwd())
243 edit
244 call assert_match('testdir.Xactual$', getcwd())
245 call assert_match('testdir$', getcwd(win_getid()))
246
247 set noautochdir
248 bwipe!
249 call chdir(startdir)
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000250endfunc
251
glepnir4ade6682025-07-03 20:41:23 +0200252func Test_cd_preserve_symlinks()
253 " Test new behavior: preserve symlinks when cpo-=~
254 set cpoptions+=~
255
256 let savedir = getcwd()
257 call mkdir('Xsource', 'R')
258 call writefile(['abc'], 'Xsource/foo.txt', 'D')
259
260 if has("win32")
261 silent !mklink /D Xdest Xsource
262 else
263 silent !ln -s Xsource Xdest
264 endif
265 if v:shell_error
266 call delete('Xsource', 'rf')
267 throw 'Skipped: cannot create symlinks'
268 endif
269
270 edit Xdest/foo.txt
271 let path_before = expand('%')
272 call assert_match('Xdest[/\\]foo\.txt$', path_before)
273
274 cd .
275 let path_after = expand('%')
276 call assert_equal(path_before, path_after)
277 call assert_match('Xdest[/\\]foo\.txt$', path_after)
278
279 bwipe!
280 set cpoptions&
281 call delete('Xdest', 'rf')
282 call delete('Xsource', 'rf')
283 call chdir(savedir)
284endfunc
285
286func Test_cd_symlinks()
287 CheckNotMSWindows
288
289 let savedir = getcwd()
290 call mkdir('Xsource', 'R')
291 call writefile(['abc'], 'Xsource/foo.txt', 'D')
292
293 silent !ln -s Xsource Xdest
294 if v:shell_error
295 call delete('Xsource', 'rf')
296 throw 'Skipped: cannot create symlinks'
297 endif
298
299 edit Xdest/foo.txt
300 let path_before = expand('%')
301 call assert_match('Xdest[/\\]foo\.txt$', path_before)
302
303 cd .
304 let path_after = expand('%')
305 call assert_match('Xsource[/\\]foo\.txt$', path_after)
306 call assert_notequal(path_before, path_after)
307
308 bwipe!
309 set cpoptions&
310 call delete('Xdest', 'rf')
311 call delete('Xsource', 'rf')
312 call chdir(savedir)
313endfunc
314
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200315" vim: shiftwidth=2 sts=2 expandtab