blob: 9fb5958fe66cff940767e864b8d9fddb6cfbc15a [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
Dominique Pellefe3418a2021-07-10 17:59:48 +02004source check.vim
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01005
Bram Moolenaar15618fa2017-03-19 21:37:13 +01006func Test_cd_large_path()
7 " This used to crash with a heap write overflow.
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02008 call assert_fails('cd ' . repeat('x', 5000), 'E344:')
Bram Moolenaar15618fa2017-03-19 21:37:13 +01009endfunc
10
11func Test_cd_up_and_down()
12 let path = getcwd()
13 cd ..
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020014 call assert_notequal(path, getcwd())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010015 exe 'cd ' .. fnameescape(path)
Bram Moolenaar15618fa2017-03-19 21:37:13 +010016 call assert_equal(path, getcwd())
17endfunc
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020018
19func Test_cd_no_arg()
20 if has('unix')
21 " Test that cd without argument goes to $HOME directory on Unix systems.
22 let path = getcwd()
23 cd
24 call assert_equal($HOME, getcwd())
25 call assert_notequal(path, getcwd())
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010026 exe 'cd ' .. fnameescape(path)
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020027 call assert_equal(path, getcwd())
28 else
29 " Test that cd without argument echoes cwd on non-Unix systems.
30 call assert_match(getcwd(), execute('cd'))
31 endif
32endfunc
33
34func Test_cd_minus()
35 " Test the :cd - goes back to the previous directory.
36 let path = getcwd()
37 cd ..
38 let path_dotdot = getcwd()
39 call assert_notequal(path, path_dotdot)
40 cd -
41 call assert_equal(path, getcwd())
42 cd -
43 call assert_equal(path_dotdot, getcwd())
44 cd -
45 call assert_equal(path, getcwd())
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010046
Richard Doty3d0abad2021-12-29 14:39:08 +000047 " Test for :cd - after a failed :cd
48 call assert_fails('cd /nonexistent', 'E344:')
49 call assert_equal(path, getcwd())
50 cd -
51 call assert_equal(path_dotdot, getcwd())
52 cd -
53
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010054 " Test for :cd - without a previous directory
55 let lines =<< trim [SCRIPT]
56 call assert_fails('cd -', 'E186:')
57 call assert_fails('call chdir("-")', 'E186:')
58 call writefile(v:errors, 'Xresult')
59 qall!
60 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010061 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010062 if RunVim([], [], '--clean -S Xscript')
63 call assert_equal([], readfile('Xresult'))
64 endif
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010065 call delete('Xresult')
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020066endfunc
67
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020068" Test for chdir()
69func Test_chdir_func()
70 let topdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010071 call mkdir('Xchdir/y/z', 'pR')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020072
73 " Create a few tabpages and windows with different directories
74 new
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010075 cd Xchdir
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020076 tabnew
77 tcd y
78 below new
79 below new
80 lcd z
81
82 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010083 call assert_match('^\[global\] .*/Xchdir$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020084 call chdir('..')
85 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020086 call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020087 tabnext | wincmd t
Bram Moolenaar95058722020-06-01 16:26:19 +020088 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020089 eval '..'->chdir()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010090 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
91 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020092 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
93 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
94 3wincmd w
Bram Moolenaar95058722020-06-01 16:26:19 +020095 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020096 call chdir('..')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010097 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
98 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020099 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
100 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
101
102 " Error case
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200103 call assert_fails("call chdir('dir-abcd')", 'E344:')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200104 silent! let d = chdir("dir_abcd")
105 call assert_equal("", d)
Bram Moolenaar7cc96922020-01-31 22:41:38 +0100106 " Should not crash
107 call chdir(d)
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200108 call assert_equal('', chdir([]))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200109
110 only | tabonly
Bram Moolenaar3503d7c2019-11-09 20:10:17 +0100111 call chdir(topdir)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200112endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100113
Bram Moolenaar002bc792020-06-05 22:33:42 +0200114" Test for changing to the previous directory '-'
115func Test_prev_dir()
116 let topdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100117 call mkdir('Xprevdir/a/b/c', 'pR')
Bram Moolenaar002bc792020-06-05 22:33:42 +0200118
119 " Create a few tabpages and windows with different directories
120 new | only
121 tabnew | new
122 tabnew
123 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100124 cd Xprevdir
Bram Moolenaar002bc792020-06-05 22:33:42 +0200125 tabnext | wincmd t
126 tcd a
127 wincmd w
128 lcd b
129 tabnext
130 tcd a/b/c
131
132 " Change to the previous directory twice in all the windows.
133 tabfirst
134 cd - | cd -
135 tabnext | wincmd t
136 tcd - | tcd -
137 wincmd w
138 lcd - | lcd -
139 tabnext
140 tcd - | tcd -
141
142 " Check the directory of all the windows
143 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100144 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200145 tabnext | wincmd t
146 call assert_equal('a', fnamemodify(getcwd(), ':t'))
147 wincmd w
148 call assert_equal('b', fnamemodify(getcwd(), ':t'))
149 tabnext
150 call assert_equal('c', fnamemodify(getcwd(), ':t'))
151
152 " Change to the previous directory using chdir()
153 tabfirst
154 call chdir("-") | call chdir("-")
155 tabnext | wincmd t
156 call chdir("-") | call chdir("-")
157 wincmd w
158 call chdir("-") | call chdir("-")
159 tabnext
160 call chdir("-") | call chdir("-")
161
162 " Check the directory of all the windows
163 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100164 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200165 tabnext | wincmd t
166 call assert_equal('a', fnamemodify(getcwd(), ':t'))
167 wincmd w
168 call assert_equal('b', fnamemodify(getcwd(), ':t'))
169 tabnext
170 call assert_equal('c', fnamemodify(getcwd(), ':t'))
171
172 only | tabonly
173 call chdir(topdir)
Bram Moolenaar002bc792020-06-05 22:33:42 +0200174endfunc
175
Bram Moolenaara9a47d12020-08-09 21:45:52 +0200176func Test_lcd_split()
177 let curdir = getcwd()
178 lcd ..
179 split
180 lcd -
181 call assert_equal(curdir, getcwd())
182 quit!
183endfunc
184
Dominique Pellefe3418a2021-07-10 17:59:48 +0200185func Test_cd_from_non_existing_dir()
186 CheckNotMSWindows
187
188 let saveddir = getcwd()
189 call mkdir('Xdeleted_dir')
190 cd Xdeleted_dir
191 call delete(saveddir .. '/Xdeleted_dir', 'd')
192
193 " Expect E187 as the current directory was deleted.
194 call assert_fails('pwd', 'E187:')
195 call assert_equal('', getcwd())
196 cd -
197 call assert_equal(saveddir, getcwd())
198endfunc
199
Bram Moolenaar297610b2019-12-27 17:20:55 +0100200func Test_cd_completion()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100201 call mkdir('XComplDir1', 'D')
202 call mkdir('XComplDir2', 'D')
203 call writefile([], 'XComplFile', 'D')
Bram Moolenaar297610b2019-12-27 17:20:55 +0100204
205 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
206 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
207 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
208 endfor
Bram Moolenaar297610b2019-12-27 17:20:55 +0100209endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200210
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100211func Test_cd_unknown_dir()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100212 call mkdir('Xa', 'R')
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100213 cd Xa
214 call writefile(['text'], 'Xb.txt')
215 edit Xa/Xb.txt
216 let first_buf = bufnr()
217 cd ..
218 edit
219 call assert_equal(first_buf, bufnr())
220 edit Xa/Xb.txt
221 call assert_notequal(first_buf, bufnr())
222
223 bwipe!
224 exe "bwipe! " .. first_buf
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100225endfunc
226
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000227func Test_getcwd_actual_dir()
Dominique Pelle8dea1452021-12-04 15:12:40 +0000228 CheckOption autochdir
229
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000230 let startdir = getcwd()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100231 call mkdir('Xactual', 'R')
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000232 call test_autochdir()
233 set autochdir
234 edit Xactual/file.txt
235 call assert_match('testdir.Xactual$', getcwd())
236 lcd ..
237 call assert_match('testdir$', getcwd())
238 edit
239 call assert_match('testdir.Xactual$', getcwd())
240 call assert_match('testdir$', getcwd(win_getid()))
241
242 set noautochdir
243 bwipe!
244 call chdir(startdir)
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000245endfunc
246
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200247" vim: shiftwidth=2 sts=2 expandtab