blob: da517515ae3502add383a01abc8b0c8755e5eeee [file] [log] [blame]
Bram Moolenaar5e66b422019-01-24 21:58:10 +01001" Tests for when a file was changed outside of Vim.
2
3func Test_FileChangedShell_reload()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004 CheckUnix
5
Bram Moolenaar5e66b422019-01-24 21:58:10 +01006 augroup testreload
7 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
8 augroup END
9 new Xchanged_r
10 call setline(1, 'reload this')
11 write
zeertzjq83cd2c72024-04-05 20:05:11 +020012 " Need to wait until the timestamp would change.
13 if has('nanotime')
14 sleep 10m
15 else
16 sleep 2
17 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010018 silent !echo 'extra line' >>Xchanged_r
19 checktime
20 call assert_equal('changed', g:reason)
21 call assert_equal(2, line('$'))
22 call assert_equal('extra line', getline(2))
23
24 " Only triggers once
25 let g:reason = ''
26 checktime
27 call assert_equal('', g:reason)
28
29 " When deleted buffer is not reloaded
30 silent !rm Xchanged_r
31 let g:reason = ''
32 checktime
33 call assert_equal('deleted', g:reason)
34 call assert_equal(2, line('$'))
35 call assert_equal('extra line', getline(2))
36
37 " When recreated buffer is reloaded
38 call setline(1, 'buffer is changed')
39 silent !echo 'new line' >>Xchanged_r
40 let g:reason = ''
41 checktime
42 call assert_equal('conflict', g:reason)
43 call assert_equal(1, line('$'))
44 call assert_equal('new line', getline(1))
45
46 " Only mode changed
47 silent !chmod +x Xchanged_r
48 let g:reason = ''
49 checktime
50 call assert_equal('mode', g:reason)
51 call assert_equal(1, line('$'))
52 call assert_equal('new line', getline(1))
53
54 " Only time changed
zeertzjq83cd2c72024-04-05 20:05:11 +020055 if has('nanotime')
56 sleep 10m
57 else
58 sleep 2
59 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010060 silent !touch Xchanged_r
61 let g:reason = ''
62 checktime
63 call assert_equal('time', g:reason)
64 call assert_equal(1, line('$'))
65 call assert_equal('new line', getline(1))
66
67 if has('persistent_undo')
68 " With an undo file the reload can be undone and a change before the
69 " reload.
70 set undofile
71 call setline(2, 'before write')
72 write
73 call setline(2, 'after write')
zeertzjq83cd2c72024-04-05 20:05:11 +020074 if has('nanotime')
75 sleep 10m
76 else
77 sleep 2
78 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010079 silent !echo 'different line' >>Xchanged_r
80 let g:reason = ''
81 checktime
82 call assert_equal('conflict', g:reason)
83 call assert_equal(3, line('$'))
84 call assert_equal('before write', getline(2))
85 call assert_equal('different line', getline(3))
86 " undo the reload
87 undo
88 call assert_equal(2, line('$'))
89 call assert_equal('after write', getline(2))
90 " undo the change before reload
91 undo
92 call assert_equal(2, line('$'))
93 call assert_equal('before write', getline(2))
94
95 set noundofile
96 endif
97
98 au! testreload
99 bwipe!
Bram Moolenaar137c14b2019-04-18 20:30:55 +0200100 call delete(undofile('Xchanged_r'))
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100101 call delete('Xchanged_r')
102endfunc
103
Rob Pilling8196e942022-02-11 15:12:10 +0000104func Test_FileChangedShell_edit()
105 CheckUnix
106
107 new Xchanged_r
108 call setline(1, 'reload this')
109 set fileformat=unix
110 write
111
112 " File format changed, reload (content only, no 'ff' etc)
113 augroup testreload
114 au!
115 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
116 augroup END
117 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100118 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000119 let g:reason = ''
120 checktime
121 call assert_equal('changed', g:reason)
122 call assert_equal(&fileformat, 'unix')
123 call assert_equal("line1\r", getline(1))
124 call assert_equal("line2\r", getline(2))
125 %s/\r
126 write
127
128 " File format changed, reload with 'ff', etc
129 augroup testreload
130 au!
131 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
132 augroup END
133 call assert_equal(&fileformat, 'unix')
134 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
135 let g:reason = ''
136 checktime
137 call assert_equal('changed', g:reason)
138 call assert_equal(&fileformat, 'dos')
139 call assert_equal('line1', getline(1))
140 call assert_equal('line2', getline(2))
141 set fileformat=unix
142 write
143
144 au! testreload
145 bwipe!
146 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000147endfunc
148
149func Test_FileChangedShell_edit_dialog()
150 CheckNotGui
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100151 CheckUnix " Using low level feedkeys() does not work on MS-Windows.
Rob Pilling8196e942022-02-11 15:12:10 +0000152
153 new Xchanged_r
154 call setline(1, 'reload this')
155 set fileformat=unix
156 write
157
158 " File format changed, reload (content only) via prompt
159 augroup testreload
160 au!
161 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
162 augroup END
163 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100164 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000165 let g:reason = ''
166 call feedkeys('L', 'L') " load file content only
167 checktime
168 call assert_equal('changed', g:reason)
169 call assert_equal(&fileformat, 'unix')
170 call assert_equal("line1\r", getline(1))
171 call assert_equal("line2\r", getline(2))
172 %s/\r
173 write
174
175 " File format changed, reload (file and options) via prompt
176 augroup testreload
177 au!
178 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
179 augroup END
180 call assert_equal(&fileformat, 'unix')
181 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
182 let g:reason = ''
183 call feedkeys('a', 'L') " load file content and options
184 checktime
185 call assert_equal('changed', g:reason)
186 call assert_equal(&fileformat, 'dos')
187 call assert_equal("line1", getline(1))
188 call assert_equal("line2", getline(2))
189 set fileformat=unix
190 write
191
192 au! testreload
193 bwipe!
194 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000195endfunc
196
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100197func Test_file_changed_dialog()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200198 CheckUnix
199 CheckNotGui
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100200 au! FileChangedShell
201
202 new Xchanged_d
203 call setline(1, 'reload this')
204 write
zeertzjq83cd2c72024-04-05 20:05:11 +0200205 " Need to wait until the timestamp would change.
206 if has('nanotime')
207 sleep 10m
208 else
209 sleep 2
210 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100211 silent !echo 'extra line' >>Xchanged_d
212 call feedkeys('L', 'L')
213 checktime
214 call assert_match('W11:', v:warningmsg)
215 call assert_equal(2, line('$'))
216 call assert_equal('reload this', getline(1))
217 call assert_equal('extra line', getline(2))
218
219 " delete buffer, only shows an error, no prompt
220 silent !rm Xchanged_d
221 checktime
222 call assert_match('E211:', v:warningmsg)
223 call assert_equal(2, line('$'))
224 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200225 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100226
Bram Moolenaar8239c622019-05-24 16:46:01 +0200227 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100228 call setline(1, 'buffer is changed')
229 silent !echo 'new line' >Xchanged_d
230 call feedkeys('L', 'L')
231 checktime
232 call assert_match('W12:', v:warningmsg)
233 call assert_equal(1, line('$'))
234 call assert_equal('new line', getline(1))
235
236 " Only mode changed, reload
237 silent !chmod +x Xchanged_d
238 call feedkeys('L', 'L')
239 checktime
240 call assert_match('W16:', v:warningmsg)
241 call assert_equal(1, line('$'))
242 call assert_equal('new line', getline(1))
243
244 " Only time changed, no prompt
zeertzjq83cd2c72024-04-05 20:05:11 +0200245 if has('nanotime')
246 sleep 10m
247 else
248 sleep 2
249 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100250 silent !touch Xchanged_d
251 let v:warningmsg = ''
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100252 checktime Xchanged_d
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100253 call assert_equal('', v:warningmsg)
254 call assert_equal(1, line('$'))
255 call assert_equal('new line', getline(1))
256
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200257 " File created after starting to edit it
258 call delete('Xchanged_d')
259 new Xchanged_d
Bram Moolenaar70e67252022-09-27 19:34:35 +0100260 call writefile(['one'], 'Xchanged_d', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200261 call feedkeys('L', 'L')
262 checktime Xchanged_d
263 call assert_equal(['one'], getline(1, '$'))
264 close!
265
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100266 bwipe!
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100267endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100268
269" Test for editing a new buffer from a FileChangedShell autocmd
270func Test_FileChangedShell_newbuf()
Bram Moolenaar70e67252022-09-27 19:34:35 +0100271 call writefile(['one', 'two'], 'Xchfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100272 new Xchfile
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100273 augroup testnewbuf
274 autocmd FileChangedShell * enew
275 augroup END
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100276 call writefile(['red'], 'Xchfile')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100277 call assert_fails('checktime', 'E811:')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100278
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100279 au! testnewbuf
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100280endfunc
281
282" vim: shiftwidth=2 sts=2 expandtab