blob: c3a5664aedfec17085eec330451113a747c9df83 [file] [log] [blame]
Bram Moolenaar5e66b422019-01-24 21:58:10 +01001" Tests for when a file was changed outside of Vim.
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
4
Bram Moolenaar5e66b422019-01-24 21:58:10 +01005func Test_FileChangedShell_reload()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02006 CheckUnix
7
Bram Moolenaar5e66b422019-01-24 21:58:10 +01008 augroup testreload
9 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
10 augroup END
11 new Xchanged_r
12 call setline(1, 'reload this')
13 write
14 " Need to wait until the timestamp would change by at least a second.
15 sleep 2
16 silent !echo 'extra line' >>Xchanged_r
17 checktime
18 call assert_equal('changed', g:reason)
19 call assert_equal(2, line('$'))
20 call assert_equal('extra line', getline(2))
21
22 " Only triggers once
23 let g:reason = ''
24 checktime
25 call assert_equal('', g:reason)
26
27 " When deleted buffer is not reloaded
28 silent !rm Xchanged_r
29 let g:reason = ''
30 checktime
31 call assert_equal('deleted', g:reason)
32 call assert_equal(2, line('$'))
33 call assert_equal('extra line', getline(2))
34
35 " When recreated buffer is reloaded
36 call setline(1, 'buffer is changed')
37 silent !echo 'new line' >>Xchanged_r
38 let g:reason = ''
39 checktime
40 call assert_equal('conflict', g:reason)
41 call assert_equal(1, line('$'))
42 call assert_equal('new line', getline(1))
43
44 " Only mode changed
45 silent !chmod +x Xchanged_r
46 let g:reason = ''
47 checktime
48 call assert_equal('mode', g:reason)
49 call assert_equal(1, line('$'))
50 call assert_equal('new line', getline(1))
51
52 " Only time changed
53 sleep 2
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
61 if has('persistent_undo')
62 " With an undo file the reload can be undone and a change before the
63 " reload.
64 set undofile
65 call setline(2, 'before write')
66 write
67 call setline(2, 'after write')
68 sleep 2
69 silent !echo 'different line' >>Xchanged_r
70 let g:reason = ''
71 checktime
72 call assert_equal('conflict', g:reason)
73 call assert_equal(3, line('$'))
74 call assert_equal('before write', getline(2))
75 call assert_equal('different line', getline(3))
76 " undo the reload
77 undo
78 call assert_equal(2, line('$'))
79 call assert_equal('after write', getline(2))
80 " undo the change before reload
81 undo
82 call assert_equal(2, line('$'))
83 call assert_equal('before write', getline(2))
84
85 set noundofile
86 endif
87
88 au! testreload
89 bwipe!
Bram Moolenaar137c14b2019-04-18 20:30:55 +020090 call delete(undofile('Xchanged_r'))
Bram Moolenaar5e66b422019-01-24 21:58:10 +010091 call delete('Xchanged_r')
92endfunc
93
Rob Pilling8196e942022-02-11 15:12:10 +000094func Test_FileChangedShell_edit()
95 CheckUnix
96
97 new Xchanged_r
98 call setline(1, 'reload this')
99 set fileformat=unix
100 write
101
102 " File format changed, reload (content only, no 'ff' etc)
103 augroup testreload
104 au!
105 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
106 augroup END
107 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100108 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000109 let g:reason = ''
110 checktime
111 call assert_equal('changed', g:reason)
112 call assert_equal(&fileformat, 'unix')
113 call assert_equal("line1\r", getline(1))
114 call assert_equal("line2\r", getline(2))
115 %s/\r
116 write
117
118 " File format changed, reload with 'ff', etc
119 augroup testreload
120 au!
121 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
122 augroup END
123 call assert_equal(&fileformat, 'unix')
124 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
125 let g:reason = ''
126 checktime
127 call assert_equal('changed', g:reason)
128 call assert_equal(&fileformat, 'dos')
129 call assert_equal('line1', getline(1))
130 call assert_equal('line2', getline(2))
131 set fileformat=unix
132 write
133
134 au! testreload
135 bwipe!
136 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000137endfunc
138
139func Test_FileChangedShell_edit_dialog()
140 CheckNotGui
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100141 CheckUnix " Using low level feedkeys() does not work on MS-Windows.
Rob Pilling8196e942022-02-11 15:12:10 +0000142
143 new Xchanged_r
144 call setline(1, 'reload this')
145 set fileformat=unix
146 write
147
148 " File format changed, reload (content only) via prompt
149 augroup testreload
150 au!
151 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
152 augroup END
153 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100154 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000155 let g:reason = ''
156 call feedkeys('L', 'L') " load file content only
157 checktime
158 call assert_equal('changed', g:reason)
159 call assert_equal(&fileformat, 'unix')
160 call assert_equal("line1\r", getline(1))
161 call assert_equal("line2\r", getline(2))
162 %s/\r
163 write
164
165 " File format changed, reload (file and options) via prompt
166 augroup testreload
167 au!
168 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
169 augroup END
170 call assert_equal(&fileformat, 'unix')
171 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
172 let g:reason = ''
173 call feedkeys('a', 'L') " load file content and options
174 checktime
175 call assert_equal('changed', g:reason)
176 call assert_equal(&fileformat, 'dos')
177 call assert_equal("line1", getline(1))
178 call assert_equal("line2", getline(2))
179 set fileformat=unix
180 write
181
182 au! testreload
183 bwipe!
184 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000185endfunc
186
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100187func Test_file_changed_dialog()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200188 CheckUnix
189 CheckNotGui
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100190 au! FileChangedShell
191
192 new Xchanged_d
193 call setline(1, 'reload this')
194 write
195 " Need to wait until the timestamp would change by at least a second.
196 sleep 2
197 silent !echo 'extra line' >>Xchanged_d
198 call feedkeys('L', 'L')
199 checktime
200 call assert_match('W11:', v:warningmsg)
201 call assert_equal(2, line('$'))
202 call assert_equal('reload this', getline(1))
203 call assert_equal('extra line', getline(2))
204
205 " delete buffer, only shows an error, no prompt
206 silent !rm Xchanged_d
207 checktime
208 call assert_match('E211:', v:warningmsg)
209 call assert_equal(2, line('$'))
210 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200211 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100212
Bram Moolenaar8239c622019-05-24 16:46:01 +0200213 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100214 call setline(1, 'buffer is changed')
215 silent !echo 'new line' >Xchanged_d
216 call feedkeys('L', 'L')
217 checktime
218 call assert_match('W12:', v:warningmsg)
219 call assert_equal(1, line('$'))
220 call assert_equal('new line', getline(1))
221
222 " Only mode changed, reload
223 silent !chmod +x Xchanged_d
224 call feedkeys('L', 'L')
225 checktime
226 call assert_match('W16:', v:warningmsg)
227 call assert_equal(1, line('$'))
228 call assert_equal('new line', getline(1))
229
230 " Only time changed, no prompt
231 sleep 2
232 silent !touch Xchanged_d
233 let v:warningmsg = ''
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100234 checktime Xchanged_d
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100235 call assert_equal('', v:warningmsg)
236 call assert_equal(1, line('$'))
237 call assert_equal('new line', getline(1))
238
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200239 " File created after starting to edit it
240 call delete('Xchanged_d')
241 new Xchanged_d
Bram Moolenaar70e67252022-09-27 19:34:35 +0100242 call writefile(['one'], 'Xchanged_d', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200243 call feedkeys('L', 'L')
244 checktime Xchanged_d
245 call assert_equal(['one'], getline(1, '$'))
246 close!
247
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100248 bwipe!
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100249endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100250
251" Test for editing a new buffer from a FileChangedShell autocmd
252func Test_FileChangedShell_newbuf()
Bram Moolenaar70e67252022-09-27 19:34:35 +0100253 call writefile(['one', 'two'], 'Xchfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100254 new Xchfile
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100255 augroup testnewbuf
256 autocmd FileChangedShell * enew
257 augroup END
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100258 call writefile(['red'], 'Xchfile')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100259 call assert_fails('checktime', 'E811:')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100260
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100261 au! testnewbuf
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100262endfunc
263
264" vim: shiftwidth=2 sts=2 expandtab