test_cd.vim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. " Test for :cd and chdir()
  2. source shared.vim
  3. source check.vim
  4. func Test_cd_large_path()
  5. " This used to crash with a heap write overflow.
  6. call assert_fails('cd ' . repeat('x', 5000), 'E344:')
  7. endfunc
  8. func Test_cd_up_and_down()
  9. let path = getcwd()
  10. cd ..
  11. call assert_notequal(path, getcwd())
  12. exe 'cd ' .. fnameescape(path)
  13. call assert_equal(path, getcwd())
  14. endfunc
  15. func Test_cd_no_arg()
  16. if has('unix')
  17. " Test that cd without argument goes to $HOME directory on Unix systems.
  18. let path = getcwd()
  19. cd
  20. call assert_equal($HOME, getcwd())
  21. call assert_notequal(path, getcwd())
  22. exe 'cd ' .. fnameescape(path)
  23. call assert_equal(path, getcwd())
  24. else
  25. " Test that cd without argument echoes cwd on non-Unix systems.
  26. call assert_match(getcwd(), execute('cd'))
  27. endif
  28. endfunc
  29. func Test_cd_minus()
  30. " Test the :cd - goes back to the previous directory.
  31. let path = getcwd()
  32. cd ..
  33. let path_dotdot = getcwd()
  34. call assert_notequal(path, path_dotdot)
  35. cd -
  36. call assert_equal(path, getcwd())
  37. cd -
  38. call assert_equal(path_dotdot, getcwd())
  39. cd -
  40. call assert_equal(path, getcwd())
  41. " Test for :cd - after a failed :cd
  42. call assert_fails('cd /nonexistent', 'E344:')
  43. call assert_equal(path, getcwd())
  44. cd -
  45. call assert_equal(path_dotdot, getcwd())
  46. cd -
  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', 'D')
  55. if RunVim([], [], '--clean -S Xscript')
  56. call assert_equal([], readfile('Xresult'))
  57. endif
  58. call delete('Xresult')
  59. endfunc
  60. " Test for chdir()
  61. func Test_chdir_func()
  62. let topdir = getcwd()
  63. call mkdir('Xchdir/y/z', 'pR')
  64. " Create a few tabpages and windows with different directories
  65. new
  66. cd Xchdir
  67. tabnew
  68. tcd y
  69. below new
  70. below new
  71. lcd z
  72. tabfirst
  73. call assert_match('^\[global\] .*/Xchdir$', trim(execute('verbose pwd')))
  74. call chdir('..')
  75. call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
  76. call assert_equal('z', fnamemodify(3->getcwd(2), ':t'))
  77. tabnext | wincmd t
  78. call assert_match('^\[tabpage\] .*/y$', trim(execute('verbose pwd')))
  79. eval '..'->chdir()
  80. call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
  81. call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
  82. call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
  83. call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
  84. 3wincmd w
  85. call assert_match('^\[window\] .*/z$', trim(execute('verbose pwd')))
  86. call chdir('..')
  87. call assert_equal('Xchdir', fnamemodify(getcwd(1, 2), ':t'))
  88. call assert_equal('Xchdir', fnamemodify(getcwd(2, 2), ':t'))
  89. call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
  90. call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
  91. " Error case
  92. call assert_fails("call chdir('dir-abcd')", 'E344:')
  93. silent! let d = chdir("dir_abcd")
  94. call assert_equal("", d)
  95. " Should not crash
  96. call chdir(d)
  97. call assert_equal('', chdir([]))
  98. only | tabonly
  99. call chdir(topdir)
  100. endfunc
  101. " Test for changing to the previous directory '-'
  102. func Test_prev_dir()
  103. let topdir = getcwd()
  104. call mkdir('Xprevdir/a/b/c', 'pR')
  105. " Create a few tabpages and windows with different directories
  106. new | only
  107. tabnew | new
  108. tabnew
  109. tabfirst
  110. cd Xprevdir
  111. tabnext | wincmd t
  112. tcd a
  113. wincmd w
  114. lcd b
  115. tabnext
  116. tcd a/b/c
  117. " Change to the previous directory twice in all the windows.
  118. tabfirst
  119. cd - | cd -
  120. tabnext | wincmd t
  121. tcd - | tcd -
  122. wincmd w
  123. lcd - | lcd -
  124. tabnext
  125. tcd - | tcd -
  126. " Check the directory of all the windows
  127. tabfirst
  128. call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
  129. tabnext | wincmd t
  130. call assert_equal('a', fnamemodify(getcwd(), ':t'))
  131. wincmd w
  132. call assert_equal('b', fnamemodify(getcwd(), ':t'))
  133. tabnext
  134. call assert_equal('c', fnamemodify(getcwd(), ':t'))
  135. " Change to the previous directory using chdir()
  136. tabfirst
  137. call chdir("-") | call chdir("-")
  138. tabnext | wincmd t
  139. call chdir("-") | call chdir("-")
  140. wincmd w
  141. call chdir("-") | call chdir("-")
  142. tabnext
  143. call chdir("-") | call chdir("-")
  144. " Check the directory of all the windows
  145. tabfirst
  146. call assert_equal('Xprevdir', fnamemodify(getcwd(), ':t'))
  147. 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. only | tabonly
  154. call chdir(topdir)
  155. endfunc
  156. func Test_lcd_split()
  157. let curdir = getcwd()
  158. lcd ..
  159. split
  160. lcd -
  161. call assert_equal(curdir, getcwd())
  162. quit!
  163. endfunc
  164. func Test_cd_from_non_existing_dir()
  165. CheckNotMSWindows
  166. let saveddir = getcwd()
  167. call mkdir('Xdeleted_dir')
  168. cd Xdeleted_dir
  169. call delete(saveddir .. '/Xdeleted_dir', 'd')
  170. " Expect E187 as the current directory was deleted.
  171. call assert_fails('pwd', 'E187:')
  172. call assert_equal('', getcwd())
  173. cd -
  174. call assert_equal(saveddir, getcwd())
  175. endfunc
  176. func Test_cd_completion()
  177. call mkdir('XComplDir1', 'D')
  178. call mkdir('XComplDir2', 'D')
  179. call mkdir('sub/XComplDir3', 'pD')
  180. call writefile([], 'XComplFile', 'D')
  181. for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
  182. call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
  183. call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/', @:)
  184. endfor
  185. set cdpath+=sub
  186. for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir']
  187. call feedkeys(':' .. cmd .. " XCompl\<C-A>\<C-B>\"\<CR>", 'tx')
  188. call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/ XComplDir3/', @:)
  189. endfor
  190. set cdpath&
  191. endfunc
  192. func Test_cd_unknown_dir()
  193. call mkdir('Xa', 'R')
  194. cd Xa
  195. call writefile(['text'], 'Xb.txt')
  196. edit Xa/Xb.txt
  197. let first_buf = bufnr()
  198. cd ..
  199. edit
  200. call assert_equal(first_buf, bufnr())
  201. edit Xa/Xb.txt
  202. call assert_notequal(first_buf, bufnr())
  203. bwipe!
  204. exe "bwipe! " .. first_buf
  205. endfunc
  206. func Test_getcwd_actual_dir()
  207. CheckFunction test_autochdir
  208. CheckOption autochdir
  209. let startdir = getcwd()
  210. call mkdir('Xactual', 'R')
  211. call test_autochdir()
  212. set autochdir
  213. edit Xactual/file.txt
  214. call assert_match('testdir.Xactual$', getcwd())
  215. lcd ..
  216. call assert_match('testdir$', getcwd())
  217. edit
  218. call assert_match('testdir.Xactual$', getcwd())
  219. call assert_match('testdir$', getcwd(win_getid()))
  220. set noautochdir
  221. bwipe!
  222. call chdir(startdir)
  223. endfunc
  224. " vim: shiftwidth=2 sts=2 expandtab