robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesfeat(cwd)!: :lcd! (bang), rearrange :bcd/:lcd/… scope precedence #41194a4a544032a

master
11.6 KiB434 linesraw
1" Test for :cd and chdir()
2
3source shared.vim
4source check.vim
5
6func Test_cd_large_path()
7  " This used to crash with a heap write overflow.
8  call assert_fails('cd ' . repeat('x', 5000), 'E344:')
9endfunc
10
11func Test_cd_up_and_down()
12  let path = getcwd()
13  cd ..
14  call assert_notequal(path, getcwd())
15  exe 'cd ' .. fnameescape(path)
16  call assert_equal(path, getcwd())
17endfunc
18
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())
26    exe 'cd ' .. fnameescape(path)
27    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())
46
47  " 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
54  " 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', 'D')
62  if RunVim([], [], '--clean -S Xscript')
63    call assert_equal([], readfile('Xresult'))
64  endif
65  call delete('Xresult')
66endfunc
67
68" Test for chdir()
69func Test_chdir_func()
70  let topdir = getcwd()
71  call mkdir('Xchdir/y/z', 'pR')
72
73  " Create a few tabpages and windows with different directories
74  new
75  cd Xchdir
76  tabnew
77  tcd y
78  below new
79  below new
80  lcd z
81
82  tabfirst
83  call assert_match('^\[global\] .*/Xchdir$', trim(execute('verbose pwd')))
84  call chdir('..')
85  call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
86  call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
87  tabnext | wincmd t
88  call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
89  eval '..'->chdir()
90  call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
91  call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
92  call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
93  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
94  3wincmd w
95  call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
96  call chdir('..')
97  call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
98  call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
99  call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
100  call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
101
102  " Forcing scope
103  call chdir('.', 'global')
104  call assert_match('^\[global\]', trim(execute('verbose pwd')))
105  call chdir('.', 'tabpage')
106  call assert_match('^\[tabpage\]', trim(execute('verbose pwd')))
107  call chdir('.', 'window')
108  call assert_match('^\[window\]', trim(execute('verbose pwd')))
109  " Nvim: buffer is the narrowest scope and shadows the others, so it goes last.
110  call chdir('.', 'buffer')
111  call assert_match('^\[buffer\]', trim(execute('verbose pwd')))
112
113  " Error case
114  call assert_fails("call chdir('dir-abcd')", 'E344:')
115  silent! let d = chdir("dir_abcd")
116  call assert_equal("", d)
117  call assert_fails("call chdir('.', v:_null_string)", 'E475:')
118  call assert_fails("call chdir('.', [])", 'E730:')
119  " Should not crash
120  call chdir(d)
121  call assert_equal('', chdir([]))
122
123  only | tabonly
124  call chdir(topdir)
125endfunc
126
127" Test for changing to the previous directory '-'
128func Test_prev_dir()
129  let topdir = getcwd()
130  call mkdir('Xprevdir/a/b/c', 'pR')
131
132  " Create a few tabpages and windows with different directories
133  new | only
134  tabnew | new
135  tabnew
136  tabfirst
137  cd Xprevdir
138  tabnext | wincmd t
139  tcd a
140  wincmd w
141  lcd b
142  tabnext
143  tcd a/b/c
144
145  " Change to the previous directory twice in all the windows.
146  tabfirst
147  cd - | cd -
148  tabnext | wincmd t
149  tcd - | tcd -
150  wincmd w
151  lcd - | lcd -
152  tabnext
153  tcd - | tcd -
154
155  " Check the directory of all the windows
156  tabfirst
157  call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
158  tabnext | wincmd t
159  call assert_equal('a', fnamemodify(getcwd(), ':t'))
160  wincmd w
161  call assert_equal('b', fnamemodify(getcwd(), ':t'))
162  tabnext
163  call assert_equal('c', fnamemodify(getcwd(), ':t'))
164
165  " Change to the previous directory using chdir()
166  tabfirst
167  call chdir("-") | call chdir("-")
168  tabnext | wincmd t
169  call chdir("-") | call chdir("-")
170  wincmd w
171  call chdir("-") | call chdir("-")
172  tabnext
173  call chdir("-") | call chdir("-")
174
175  " Check the directory of all the windows
176  tabfirst
177  call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
178  tabnext | wincmd t
179  call assert_equal('a', fnamemodify(getcwd(), ':t'))
180  wincmd w
181  call assert_equal('b', fnamemodify(getcwd(), ':t'))
182  tabnext
183  call assert_equal('c', fnamemodify(getcwd(), ':t'))
184
185  only | tabonly
186  call chdir(topdir)
187endfunc
188
189func Test_lcd_split()
190  let curdir = getcwd()
191  lcd ..
192  split
193  lcd -
194  call assert_equal(curdir, getcwd())
195  quit!
196endfunc
197
198" Test that a temporary override of 'autochdir' via :lcd isn't clobbered by win_execute() in a split window.
199func Test_lcd_win_execute()
200  CheckFunction test_autochdir
201  CheckOption autochdir
202
203  let startdir = getcwd()
204  call mkdir('Xsubdir', 'R')
205  call test_autochdir()
206  set autochdir
207  edit Xsubdir/file
208  call assert_match('testdir.Xsubdir.file$', expand('%:p'))
209  split
210  lcd ..
211  call assert_match('testdir.Xsubdir.file$', expand('%:p'))
212  call win_execute(win_getid(2), "")
213  call assert_match('testdir.Xsubdir.file$', expand('%:p'))
214
215  set noautochdir
216  bwipe!
217  call chdir(startdir)
218endfunc
219
220func Test_cd_from_non_existing_dir()
221  if has('sun')
222    throw 'Skipped: Solaris does not allow deleting the current working directory'
223  endif
224  CheckNotMSWindows
225
226  let saveddir = getcwd()
227  call mkdir('Xdeleted_dir')
228  cd Xdeleted_dir
229  call delete(saveddir .. '/Xdeleted_dir', 'd')
230
231  " Expect E187 as the current directory was deleted.
232  call assert_fails('pwd', 'E187:')
233  call assert_equal('', getcwd())
234  cd -
235  call assert_equal(saveddir, getcwd())
236endfunc
237
238func Test_cd_completion()
239  call mkdir('XComplDir1', 'D')
240  call mkdir('XComplDir2', 'D')
241  call mkdir('sub/XComplDir3', 'pD')
242  call writefile([], 'XComplFile', 'D')
243
244  for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
245    call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
246    call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
247  endfor
248
249  set cdpath+=sub
250  for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
251    call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
252    call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/ XComplDir3/', @:)
253  endfor
254  set cdpath&
255
256  if has('win32')
257    " Test Windows absolute path completion
258    let saved_cwd = getcwd()
259
260    " Retrieve a suitable dir in the current drive
261    for d in readdir('/', 'isdirectory("/" .. v:val) && len(v:val) > 2')
262      " Paths containing '$' such as "$RECYCLE.BIN" are skipped because
263      " they are considered environment variables and completion does not
264      " work.
265      if d =~ '\V$'
266        continue
267      endif
268      " Skip directories that we don't have permission to "cd" into by
269      " actually "cd"ing into them and making sure they don't fail.
270      " Directory "System Volume Information" is an example of this.
271      try
272        call chdir('/' .. d)
273        let dir = d
274        " Yay! We found a suitable dir!
275        break
276      catch /:\(E472\|E344\):/
277        " Just skip directories where "cd" fails
278        continue
279      finally
280        call chdir(saved_cwd)
281      endtry
282    endfor
283    if !exists('dir')
284      throw 'Skipped: no testable directories found in the current drive root'
285    endif
286
287    " Get partial path
288    let partial = dir[0:-2]
289    " Get the current drive letter and full path of the target dir
290    call chdir('/' .. dir)
291    let full = getcwd()
292    let drive = full[0]
293    call chdir(saved_cwd)
294
295    " Spaces are escaped in command line completion. Next, in assert_match(),
296    " the backslash added by the first escape also needs to be escaped
297    " separately, so the escape is doubled.
298    let want_full = escape(escape(full, ' '), '\')
299    let want_dir = escape(escape(dir, ' '), '\')
300
301    for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
302      for sep in [ '/', '\']
303
304        " Explicit drive letter
305        call feedkeys(':' .. cmd .. ' ' .. drive .. ':' .. sep ..
306                     \  partial .. "\<C-A>\<C-B>\"\<CR>", 'tx')
307        call assert_match(want_full, @:)
308
309        " Implicit drive letter
310        call feedkeys(':' .. cmd .. ' ' .. sep .. partial .. "\<C-A>\<C-B>\"\<CR>", 'tx')
311        call assert_match('/' .. want_dir .. '/', @:)
312
313        " UNC path
314        call feedkeys(':' .. cmd .. ' ' .. sep .. sep .. $COMPUTERNAME .. sep ..
315                     \ drive .. '$' .. sep .. partial .."\<C-A>\<C-B>\"\<CR>", 'tx')
316        call assert_match('//' .. $COMPUTERNAME .. '/' .. drive .. '$/' .. want_dir .. '/' , @:)
317
318      endfor
319    endfor
320  endif
321endfunc
322
323func Test_cd_unknown_dir()
324  call mkdir('Xa', 'R')
325  cd Xa
326  call writefile(['text'], 'Xb.txt')
327  edit Xa/Xb.txt
328  let first_buf = bufnr()
329  cd ..
330  edit
331  call assert_equal(first_buf, bufnr())
332  edit Xa/Xb.txt
333  call assert_notequal(first_buf, bufnr())
334
335  bwipe!
336  exe "bwipe! " .. first_buf
337endfunc
338
339func Test_getcwd_actual_dir()
340  CheckFunction test_autochdir
341  CheckOption autochdir
342
343  let startdir = getcwd()
344  call mkdir('Xactual', 'R')
345  call test_autochdir()
346  set autochdir
347  edit Xactual/file.txt
348  call assert_match('testdir.Xactual$', getcwd())
349  lcd ..
350  call assert_match('testdir$', getcwd())
351  edit
352  call assert_match('testdir.Xactual$', getcwd())
353  call assert_match('testdir$', getcwd(win_getid()))
354
355  set noautochdir
356  bwipe!
357  call chdir(startdir)
358endfunc
359
360func Test_cd_preserve_symlinks()
361  " Test new behavior: preserve symlinks when cpo-=~
362  set cpoptions+=~
363
364  let savedir = getcwd()
365  call mkdir('Xsource', 'R')
366  call writefile(['abc'], 'Xsource/foo.txt', 'D')
367
368  if has("win32")
369    silent !mklink /D Xdest Xsource
370  else
371    silent !ln -s Xsource Xdest
372  endif
373  if v:shell_error
374    call delete('Xsource', 'rf')
375    throw 'Skipped: cannot create symlinks'
376  endif
377
378  edit Xdest/foo.txt
379  let path_before = expand('%')
380  call assert_match('Xdest[/\\]foo\.txt$', path_before)
381
382  cd .
383  let path_after = expand('%')
384  call assert_equal(path_before, path_after)
385  call assert_match('Xdest[/\\]foo\.txt$', path_after)
386
387  bwipe!
388  set cpoptions&
389  call delete('Xdest', 'rf')
390  call delete('Xsource', 'rf')
391  call chdir(savedir)
392endfunc
393
394func Test_cd_symlinks()
395  CheckNotMSWindows
396
397  let savedir = getcwd()
398  call mkdir('Xsource', 'R')
399  call writefile(['abc'], 'Xsource/foo.txt', 'D')
400
401  silent !ln -s Xsource Xdest
402  if v:shell_error
403    call delete('Xsource', 'rf')
404    throw 'Skipped: cannot create symlinks'
405  endif
406
407  edit Xdest/foo.txt
408  let path_before = expand('%')
409  call assert_match('Xdest[/\\]foo\.txt$', path_before)
410
411  cd .
412  let path_after = expand('%')
413  call assert_match('Xsource[/\\]foo\.txt$', path_after)
414  call assert_notequal(path_before, path_after)
415
416  bwipe!
417  set cpoptions&
418  call delete('Xdest', 'rf')
419  call delete('Xsource', 'rf')
420  call chdir(savedir)
421endfunc
422
423func Test_cd_shorten_bufname_with_duplicate_slashes()
424  let savedir = getcwd()
425  call mkdir('Xexistingdir', 'R')
426  new Xexistingdir//foo/bar
427  cd Xexistingdir
428  call assert_equal('foo/bar', bufname('%'))
429
430  call chdir(savedir)
431  bwipe!
432endfunc
433
434" vim: shiftwidth=2 sts=2 expandtab