test_filechanged.vim 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. " Tests for when a file was changed outside of Vim.
  2. source check.vim
  3. func Test_FileChangedShell_reload()
  4. CheckUnix
  5. augroup testreload
  6. au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
  7. augroup END
  8. new Xchanged_r
  9. call setline(1, 'reload this')
  10. write
  11. " Need to wait until the timestamp would change.
  12. if has('nanotime')
  13. sleep 10m
  14. else
  15. sleep 2
  16. endif
  17. silent !echo 'extra line' >>Xchanged_r
  18. checktime
  19. call assert_equal('changed', g:reason)
  20. call assert_equal(2, line('$'))
  21. call assert_equal('extra line', getline(2))
  22. " Only triggers once
  23. let g:reason = ''
  24. checktime
  25. call assert_equal('', g:reason)
  26. " When deleted buffer is not reloaded
  27. silent !rm Xchanged_r
  28. let g:reason = ''
  29. checktime
  30. call assert_equal('deleted', g:reason)
  31. call assert_equal(2, line('$'))
  32. call assert_equal('extra line', getline(2))
  33. " When recreated buffer is reloaded
  34. call setline(1, 'buffer is changed')
  35. silent !echo 'new line' >>Xchanged_r
  36. let g:reason = ''
  37. checktime
  38. call assert_equal('conflict', g:reason)
  39. call assert_equal(1, line('$'))
  40. call assert_equal('new line', getline(1))
  41. " Only mode changed
  42. silent !chmod +x Xchanged_r
  43. let g:reason = ''
  44. checktime
  45. call assert_equal('mode', g:reason)
  46. call assert_equal(1, line('$'))
  47. call assert_equal('new line', getline(1))
  48. " Only time changed
  49. if has('nanotime')
  50. sleep 10m
  51. else
  52. sleep 2
  53. endif
  54. silent !touch Xchanged_r
  55. let g:reason = ''
  56. checktime
  57. call assert_equal('time', g:reason)
  58. call assert_equal(1, line('$'))
  59. call assert_equal('new line', getline(1))
  60. if has('persistent_undo')
  61. " With an undo file the reload can be undone and a change before the
  62. " reload.
  63. set undofile
  64. call setline(2, 'before write')
  65. write
  66. call setline(2, 'after write')
  67. if has('nanotime')
  68. sleep 10m
  69. else
  70. sleep 2
  71. endif
  72. silent !echo 'different line' >>Xchanged_r
  73. let g:reason = ''
  74. checktime
  75. call assert_equal('conflict', g:reason)
  76. call assert_equal(3, line('$'))
  77. call assert_equal('before write', getline(2))
  78. call assert_equal('different line', getline(3))
  79. " undo the reload
  80. undo
  81. call assert_equal(2, line('$'))
  82. call assert_equal('after write', getline(2))
  83. " undo the change before reload
  84. undo
  85. call assert_equal(2, line('$'))
  86. call assert_equal('before write', getline(2))
  87. set noundofile
  88. endif
  89. au! testreload
  90. bwipe!
  91. call delete(undofile('Xchanged_r'))
  92. call delete('Xchanged_r')
  93. endfunc
  94. func Test_FileChangedShell_edit()
  95. CheckUnix
  96. new Xchanged_r
  97. call setline(1, 'reload this')
  98. set fileformat=unix
  99. write
  100. " File format changed, reload (content only, no 'ff' etc)
  101. augroup testreload
  102. au!
  103. au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
  104. augroup END
  105. call assert_equal(&fileformat, 'unix')
  106. sleep 10m " make the test less flaky in Nvim
  107. call writefile(["line1\r", "line2\r"], 'Xchanged_r')
  108. let g:reason = ''
  109. checktime
  110. call assert_equal('changed', g:reason)
  111. call assert_equal(&fileformat, 'unix')
  112. call assert_equal("line1\r", getline(1))
  113. call assert_equal("line2\r", getline(2))
  114. %s/\r
  115. write
  116. " File format changed, reload with 'ff', etc
  117. augroup testreload
  118. au!
  119. au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
  120. augroup END
  121. call assert_equal(&fileformat, 'unix')
  122. sleep 10m " make the test less flaky in Nvim
  123. call writefile(["line1\r", "line2\r"], 'Xchanged_r')
  124. let g:reason = ''
  125. checktime
  126. call assert_equal('changed', g:reason)
  127. call assert_equal(&fileformat, 'dos')
  128. call assert_equal('line1', getline(1))
  129. call assert_equal('line2', getline(2))
  130. set fileformat=unix
  131. write
  132. au! testreload
  133. bwipe!
  134. call delete(undofile('Xchanged_r'))
  135. call delete('Xchanged_r')
  136. endfunc
  137. func Test_FileChangedShell_edit_dialog()
  138. CheckNotGui
  139. CheckUnix " Using low level feedkeys() does not work on MS-Windows.
  140. new Xchanged_r
  141. call setline(1, 'reload this')
  142. set fileformat=unix
  143. write
  144. " File format changed, reload (content only) via prompt
  145. augroup testreload
  146. au!
  147. au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
  148. augroup END
  149. call assert_equal(&fileformat, 'unix')
  150. sleep 10m " make the test less flaky in Nvim
  151. call writefile(["line1\r", "line2\r"], 'Xchanged_r')
  152. let g:reason = ''
  153. call feedkeys('L', 'L') " load file content only
  154. checktime
  155. call assert_equal('changed', g:reason)
  156. call assert_equal(&fileformat, 'unix')
  157. call assert_equal("line1\r", getline(1))
  158. call assert_equal("line2\r", getline(2))
  159. %s/\r
  160. write
  161. " File format changed, reload (file and options) via prompt
  162. augroup testreload
  163. au!
  164. au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
  165. augroup END
  166. call assert_equal(&fileformat, 'unix')
  167. sleep 10m " make the test less flaky in Nvim
  168. call writefile(["line1\r", "line2\r"], 'Xchanged_r')
  169. let g:reason = ''
  170. call feedkeys('a', 'L') " load file content and options
  171. checktime
  172. call assert_equal('changed', g:reason)
  173. call assert_equal(&fileformat, 'dos')
  174. call assert_equal("line1", getline(1))
  175. call assert_equal("line2", getline(2))
  176. set fileformat=unix
  177. write
  178. au! testreload
  179. bwipe!
  180. call delete(undofile('Xchanged_r'))
  181. call delete('Xchanged_r')
  182. endfunc
  183. func Test_file_changed_dialog()
  184. CheckUnix
  185. CheckNotGui
  186. au! FileChangedShell
  187. new Xchanged_d
  188. call setline(1, 'reload this')
  189. write
  190. " Need to wait until the timestamp would change.
  191. if has('nanotime')
  192. sleep 10m
  193. else
  194. sleep 2
  195. endif
  196. silent !echo 'extra line' >>Xchanged_d
  197. call feedkeys('L', 'L')
  198. checktime
  199. call assert_match('W11:', v:warningmsg)
  200. call assert_equal(2, line('$'))
  201. call assert_equal('reload this', getline(1))
  202. call assert_equal('extra line', getline(2))
  203. " delete buffer, only shows an error, no prompt
  204. silent !rm Xchanged_d
  205. checktime
  206. call assert_match('E211:', v:warningmsg)
  207. call assert_equal(2, line('$'))
  208. call assert_equal('extra line', getline(2))
  209. let v:warningmsg = 'empty'
  210. " change buffer, recreate the file and reload
  211. call setline(1, 'buffer is changed')
  212. silent !echo 'new line' >Xchanged_d
  213. call feedkeys('L', 'L')
  214. checktime
  215. call assert_match('W12:', v:warningmsg)
  216. call assert_equal(1, line('$'))
  217. call assert_equal('new line', getline(1))
  218. " Only mode changed, reload
  219. silent !chmod +x Xchanged_d
  220. call feedkeys('L', 'L')
  221. checktime
  222. call assert_match('W16:', v:warningmsg)
  223. call assert_equal(1, line('$'))
  224. call assert_equal('new line', getline(1))
  225. " Only time changed, no prompt
  226. if has('nanotime')
  227. sleep 10m
  228. else
  229. sleep 2
  230. endif
  231. silent !touch Xchanged_d
  232. let v:warningmsg = ''
  233. checktime Xchanged_d
  234. call assert_equal('', v:warningmsg)
  235. call assert_equal(1, line('$'))
  236. call assert_equal('new line', getline(1))
  237. " File created after starting to edit it
  238. call delete('Xchanged_d')
  239. new Xchanged_d
  240. call writefile(['one'], 'Xchanged_d')
  241. call feedkeys('L', 'L')
  242. checktime Xchanged_d
  243. call assert_equal(['one'], getline(1, '$'))
  244. close!
  245. bwipe!
  246. call delete('Xchanged_d')
  247. endfunc
  248. " Test for editing a new buffer from a FileChangedShell autocmd
  249. func Test_FileChangedShell_newbuf()
  250. call writefile(['one', 'two'], 'Xfile')
  251. new Xfile
  252. augroup testnewbuf
  253. autocmd FileChangedShell * enew
  254. augroup END
  255. sleep 10m " make the test less flaky in Nvim
  256. call writefile(['red'], 'Xfile')
  257. call assert_fails('checktime', 'E811:')
  258. au! testnewbuf
  259. call delete('Xfile')
  260. endfunc
  261. " vim: shiftwidth=2 sts=2 expandtab