blob: c82cefc2775f15f0cc19e4c42e1998ab9464710f [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]
61 call writefile(lines, 'Xscript')
62 if RunVim([], [], '--clean -S Xscript')
63 call assert_equal([], readfile('Xresult'))
64 endif
65 call delete('Xscript')
66 call delete('Xresult')
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020067endfunc
68
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020069" Test for chdir()
70func Test_chdir_func()
71 let topdir = getcwd()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010072 call mkdir('Xchdir/y/z', 'p')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020073
74 " Create a few tabpages and windows with different directories
75 new
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010076 cd Xchdir
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020077 tabnew
78 tcd y
79 below new
80 below new
81 lcd z
82
83 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010084 call assert_match('^\[global\] .*/Xchdir$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020085 call chdir('..')
86 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020087 call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020088 tabnext | wincmd t
Bram Moolenaar95058722020-06-01 16:26:19 +020089 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020090 eval '..'->chdir()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010091 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
92 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020093 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
94 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
95 3wincmd w
Bram Moolenaar95058722020-06-01 16:26:19 +020096 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020097 call chdir('..')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010098 call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
99 call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200100 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
101 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
102
103 " Error case
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200104 call assert_fails("call chdir('dir-abcd')", 'E344:')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200105 silent! let d = chdir("dir_abcd")
106 call assert_equal("", d)
Bram Moolenaar7cc96922020-01-31 22:41:38 +0100107 " Should not crash
108 call chdir(d)
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200109 call assert_equal('', chdir([]))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200110
111 only | tabonly
Bram Moolenaar3503d7c2019-11-09 20:10:17 +0100112 call chdir(topdir)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100113 call delete('Xchdir', 'rf')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200114endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100115
Bram Moolenaar002bc792020-06-05 22:33:42 +0200116" Test for changing to the previous directory '-'
117func Test_prev_dir()
118 let topdir = getcwd()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100119 call mkdir('Xprevdir/a/b/c', 'p')
Bram Moolenaar002bc792020-06-05 22:33:42 +0200120
121 " Create a few tabpages and windows with different directories
122 new | only
123 tabnew | new
124 tabnew
125 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100126 cd Xprevdir
Bram Moolenaar002bc792020-06-05 22:33:42 +0200127 tabnext | wincmd t
128 tcd a
129 wincmd w
130 lcd b
131 tabnext
132 tcd a/b/c
133
134 " Change to the previous directory twice in all the windows.
135 tabfirst
136 cd - | cd -
137 tabnext | wincmd t
138 tcd - | tcd -
139 wincmd w
140 lcd - | lcd -
141 tabnext
142 tcd - | tcd -
143
144 " Check the directory of all the windows
145 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100146 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200147 tabnext | wincmd t
148 call assert_equal('a', fnamemodify(getcwd(), ':t'))
149 wincmd w
150 call assert_equal('b', fnamemodify(getcwd(), ':t'))
151 tabnext
152 call assert_equal('c', fnamemodify(getcwd(), ':t'))
153
154 " Change to the previous directory using chdir()
155 tabfirst
156 call chdir("-") | call chdir("-")
157 tabnext | wincmd t
158 call chdir("-") | call chdir("-")
159 wincmd w
160 call chdir("-") | call chdir("-")
161 tabnext
162 call chdir("-") | call chdir("-")
163
164 " Check the directory of all the windows
165 tabfirst
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100166 call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
Bram Moolenaar002bc792020-06-05 22:33:42 +0200167 tabnext | wincmd t
168 call assert_equal('a', fnamemodify(getcwd(), ':t'))
169 wincmd w
170 call assert_equal('b', fnamemodify(getcwd(), ':t'))
171 tabnext
172 call assert_equal('c', fnamemodify(getcwd(), ':t'))
173
174 only | tabonly
175 call chdir(topdir)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100176 call delete('Xprevdir', 'rf')
Bram Moolenaar002bc792020-06-05 22:33:42 +0200177endfunc
178
Bram Moolenaara9a47d12020-08-09 21:45:52 +0200179func Test_lcd_split()
180 let curdir = getcwd()
181 lcd ..
182 split
183 lcd -
184 call assert_equal(curdir, getcwd())
185 quit!
186endfunc
187
Dominique Pellefe3418a2021-07-10 17:59:48 +0200188func Test_cd_from_non_existing_dir()
189 CheckNotMSWindows
190
191 let saveddir = getcwd()
192 call mkdir('Xdeleted_dir')
193 cd Xdeleted_dir
194 call delete(saveddir .. '/Xdeleted_dir', 'd')
195
196 " Expect E187 as the current directory was deleted.
197 call assert_fails('pwd', 'E187:')
198 call assert_equal('', getcwd())
199 cd -
200 call assert_equal(saveddir, getcwd())
201endfunc
202
Bram Moolenaar297610b2019-12-27 17:20:55 +0100203func Test_cd_completion()
204 call mkdir('XComplDir1', 'p')
205 call mkdir('XComplDir2', 'p')
206 call writefile([], 'XComplFile')
207
208 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
209 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
210 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
211 endfor
212
213 call delete('XComplDir1', 'd')
214 call delete('XComplDir2', 'd')
215 call delete('XComplFile')
216endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200217
Bram Moolenaarc6376c72021-10-03 19:29:48 +0100218func Test_cd_unknown_dir()
219 call mkdir('Xa')
220 cd Xa
221 call writefile(['text'], 'Xb.txt')
222 edit Xa/Xb.txt
223 let first_buf = bufnr()
224 cd ..
225 edit
226 call assert_equal(first_buf, bufnr())
227 edit Xa/Xb.txt
228 call assert_notequal(first_buf, bufnr())
229
230 bwipe!
231 exe "bwipe! " .. first_buf
232 call delete('Xa', 'rf')
233endfunc
234
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000235func Test_getcwd_actual_dir()
Dominique Pelle8dea1452021-12-04 15:12:40 +0000236 CheckOption autochdir
237
Bram Moolenaar851c7a62021-11-18 20:47:31 +0000238 let startdir = getcwd()
239 call mkdir('Xactual')
240 call test_autochdir()
241 set autochdir
242 edit Xactual/file.txt
243 call assert_match('testdir.Xactual$', getcwd())
244 lcd ..
245 call assert_match('testdir$', getcwd())
246 edit
247 call assert_match('testdir.Xactual$', getcwd())
248 call assert_match('testdir$', getcwd(win_getid()))
249
250 set noautochdir
251 bwipe!
252 call chdir(startdir)
253 call delete('Xactual', 'rf')
254endfunc
255
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200256" vim: shiftwidth=2 sts=2 expandtab