blob: 5dca43fd4ed9a403463dcdac013413c590325848 [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')
108 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
109 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'))
137 call delete('Xchanged_r')
138endfunc
139
140func Test_FileChangedShell_edit_dialog()
141 CheckNotGui
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100142 CheckUnix " Using low level feedkeys() does not work on MS-Windows.
Rob Pilling8196e942022-02-11 15:12:10 +0000143
144 new Xchanged_r
145 call setline(1, 'reload this')
146 set fileformat=unix
147 write
148
149 " File format changed, reload (content only) via prompt
150 augroup testreload
151 au!
152 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
153 augroup END
154 call assert_equal(&fileformat, 'unix')
155 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
156 let g:reason = ''
157 call feedkeys('L', 'L') " load file content only
158 checktime
159 call assert_equal('changed', g:reason)
160 call assert_equal(&fileformat, 'unix')
161 call assert_equal("line1\r", getline(1))
162 call assert_equal("line2\r", getline(2))
163 %s/\r
164 write
165
166 " File format changed, reload (file and options) via prompt
167 augroup testreload
168 au!
169 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
170 augroup END
171 call assert_equal(&fileformat, 'unix')
172 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
173 let g:reason = ''
174 call feedkeys('a', 'L') " load file content and options
175 checktime
176 call assert_equal('changed', g:reason)
177 call assert_equal(&fileformat, 'dos')
178 call assert_equal("line1", getline(1))
179 call assert_equal("line2", getline(2))
180 set fileformat=unix
181 write
182
183 au! testreload
184 bwipe!
185 call delete(undofile('Xchanged_r'))
186 call delete('Xchanged_r')
187endfunc
188
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100189func Test_file_changed_dialog()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200190 CheckUnix
191 CheckNotGui
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100192 au! FileChangedShell
193
194 new Xchanged_d
195 call setline(1, 'reload this')
196 write
197 " Need to wait until the timestamp would change by at least a second.
198 sleep 2
199 silent !echo 'extra line' >>Xchanged_d
200 call feedkeys('L', 'L')
201 checktime
202 call assert_match('W11:', v:warningmsg)
203 call assert_equal(2, line('$'))
204 call assert_equal('reload this', getline(1))
205 call assert_equal('extra line', getline(2))
206
207 " delete buffer, only shows an error, no prompt
208 silent !rm Xchanged_d
209 checktime
210 call assert_match('E211:', v:warningmsg)
211 call assert_equal(2, line('$'))
212 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200213 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100214
Bram Moolenaar8239c622019-05-24 16:46:01 +0200215 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100216 call setline(1, 'buffer is changed')
217 silent !echo 'new line' >Xchanged_d
218 call feedkeys('L', 'L')
219 checktime
220 call assert_match('W12:', v:warningmsg)
221 call assert_equal(1, line('$'))
222 call assert_equal('new line', getline(1))
223
224 " Only mode changed, reload
225 silent !chmod +x Xchanged_d
226 call feedkeys('L', 'L')
227 checktime
228 call assert_match('W16:', v:warningmsg)
229 call assert_equal(1, line('$'))
230 call assert_equal('new line', getline(1))
231
232 " Only time changed, no prompt
233 sleep 2
234 silent !touch Xchanged_d
235 let v:warningmsg = ''
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100236 checktime Xchanged_d
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100237 call assert_equal('', v:warningmsg)
238 call assert_equal(1, line('$'))
239 call assert_equal('new line', getline(1))
240
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200241 " File created after starting to edit it
242 call delete('Xchanged_d')
243 new Xchanged_d
244 call writefile(['one'], 'Xchanged_d')
245 call feedkeys('L', 'L')
246 checktime Xchanged_d
247 call assert_equal(['one'], getline(1, '$'))
248 close!
249
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100250 bwipe!
251 call delete('Xchanged_d')
252endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100253
254" Test for editing a new buffer from a FileChangedShell autocmd
255func Test_FileChangedShell_newbuf()
256 call writefile(['one', 'two'], 'Xfile')
257 new Xfile
258 augroup testnewbuf
259 autocmd FileChangedShell * enew
260 augroup END
261 call writefile(['red'], 'Xfile')
262 call assert_fails('checktime', 'E811:')
263 au! testnewbuf
264 call delete('Xfile')
265endfunc
266
267" vim: shiftwidth=2 sts=2 expandtab