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