blob: 8eda44aff265761ab506a4303e643d6b004ac952 [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
47 " Test for :cd - without a previous directory
48 let lines =<< trim [SCRIPT]
49 call assert_fails('cd -', 'E186:')
50 call assert_fails('call chdir("-")', 'E186:')
51 call writefile(v:errors, 'Xresult')
52 qall!
53 [SCRIPT]
54 call writefile(lines, 'Xscript')
55 if RunVim([], [], '--clean -S Xscript')
56 call assert_equal([], readfile('Xresult'))
57 endif
58 call delete('Xscript')
59 call delete('Xresult')
Bram Moolenaarb2e0c942018-07-03 18:36:27 +020060endfunc
61
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020062" Test for chdir()
63func Test_chdir_func()
64 let topdir = getcwd()
65 call mkdir('Xdir/y/z', 'p')
66
67 " Create a few tabpages and windows with different directories
68 new
69 cd Xdir
70 tabnew
71 tcd y
72 below new
73 below new
74 lcd z
75
76 tabfirst
Bram Moolenaar95058722020-06-01 16:26:19 +020077 call assert_match('^\[global\] .*/Xdir$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020078 call chdir('..')
79 call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020080 call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020081 tabnext | wincmd t
Bram Moolenaar95058722020-06-01 16:26:19 +020082 call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020083 eval '..'->chdir()
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020084 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
85 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
86 call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
87 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
88 3wincmd w
Bram Moolenaar95058722020-06-01 16:26:19 +020089 call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020090 call chdir('..')
91 call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
92 call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
93 call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
94 call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
95
96 " Error case
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020097 call assert_fails("call chdir('dir-abcd')", 'E344:')
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020098 silent! let d = chdir("dir_abcd")
99 call assert_equal("", d)
Bram Moolenaar7cc96922020-01-31 22:41:38 +0100100 " Should not crash
101 call chdir(d)
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200102 call assert_equal('', chdir([]))
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200103
104 only | tabonly
Bram Moolenaar3503d7c2019-11-09 20:10:17 +0100105 call chdir(topdir)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200106 call delete('Xdir', 'rf')
107endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100108
Bram Moolenaar002bc792020-06-05 22:33:42 +0200109" Test for changing to the previous directory '-'
110func Test_prev_dir()
111 let topdir = getcwd()
112 call mkdir('Xdir/a/b/c', 'p')
113
114 " Create a few tabpages and windows with different directories
115 new | only
116 tabnew | new
117 tabnew
118 tabfirst
119 cd Xdir
120 tabnext | wincmd t
121 tcd a
122 wincmd w
123 lcd b
124 tabnext
125 tcd a/b/c
126
127 " Change to the previous directory twice in all the windows.
128 tabfirst
129 cd - | cd -
130 tabnext | wincmd t
131 tcd - | tcd -
132 wincmd w
133 lcd - | lcd -
134 tabnext
135 tcd - | tcd -
136
137 " Check the directory of all the windows
138 tabfirst
139 call assert_equal('Xdir', fnamemodify(getcwd(), ':t'))
140 tabnext | wincmd t
141 call assert_equal('a', fnamemodify(getcwd(), ':t'))
142 wincmd w
143 call assert_equal('b', fnamemodify(getcwd(), ':t'))
144 tabnext
145 call assert_equal('c', fnamemodify(getcwd(), ':t'))
146
147 " Change to the previous directory using chdir()
148 tabfirst
149 call chdir("-") | call chdir("-")
150 tabnext | wincmd t
151 call chdir("-") | call chdir("-")
152 wincmd w
153 call chdir("-") | call chdir("-")
154 tabnext
155 call chdir("-") | call chdir("-")
156
157 " Check the directory of all the windows
158 tabfirst
159 call assert_equal('Xdir', fnamemodify(getcwd(), ':t'))
160 tabnext | wincmd t
161 call assert_equal('a', fnamemodify(getcwd(), ':t'))
162 wincmd w
163 call assert_equal('b', fnamemodify(getcwd(), ':t'))
164 tabnext
165 call assert_equal('c', fnamemodify(getcwd(), ':t'))
166
167 only | tabonly
168 call chdir(topdir)
169 call delete('Xdir', 'rf')
170endfunc
171
Bram Moolenaara9a47d12020-08-09 21:45:52 +0200172func Test_lcd_split()
173 let curdir = getcwd()
174 lcd ..
175 split
176 lcd -
177 call assert_equal(curdir, getcwd())
178 quit!
179endfunc
180
Dominique Pellefe3418a2021-07-10 17:59:48 +0200181func Test_cd_from_non_existing_dir()
182 CheckNotMSWindows
183
184 let saveddir = getcwd()
185 call mkdir('Xdeleted_dir')
186 cd Xdeleted_dir
187 call delete(saveddir .. '/Xdeleted_dir', 'd')
188
189 " Expect E187 as the current directory was deleted.
190 call assert_fails('pwd', 'E187:')
191 call assert_equal('', getcwd())
192 cd -
193 call assert_equal(saveddir, getcwd())
194endfunc
195
Bram Moolenaar297610b2019-12-27 17:20:55 +0100196func Test_cd_completion()
197 call mkdir('XComplDir1', 'p')
198 call mkdir('XComplDir2', 'p')
199 call writefile([], 'XComplFile')
200
201 for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
202 call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
203 call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
204 endfor
205
206 call delete('XComplDir1', 'd')
207 call delete('XComplDir2', 'd')
208 call delete('XComplFile')
209endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200210
211" vim: shiftwidth=2 sts=2 expandtab