blob: 12ac4ccde4c7e13ebda4c4e388c4ee6f5a30cfc2 [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
zeertzjq83cd2c72024-04-05 20:05:11 +020014 " Need to wait until the timestamp would change.
15 if has('nanotime')
16 sleep 10m
17 else
18 sleep 2
19 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010020 silent !echo 'extra line' >>Xchanged_r
21 checktime
22 call assert_equal('changed', g:reason)
23 call assert_equal(2, line('$'))
24 call assert_equal('extra line', getline(2))
25
26 " Only triggers once
27 let g:reason = ''
28 checktime
29 call assert_equal('', g:reason)
30
31 " When deleted buffer is not reloaded
32 silent !rm Xchanged_r
33 let g:reason = ''
34 checktime
35 call assert_equal('deleted', g:reason)
36 call assert_equal(2, line('$'))
37 call assert_equal('extra line', getline(2))
38
39 " When recreated buffer is reloaded
40 call setline(1, 'buffer is changed')
41 silent !echo 'new line' >>Xchanged_r
42 let g:reason = ''
43 checktime
44 call assert_equal('conflict', g:reason)
45 call assert_equal(1, line('$'))
46 call assert_equal('new line', getline(1))
47
48 " Only mode changed
49 silent !chmod +x Xchanged_r
50 let g:reason = ''
51 checktime
52 call assert_equal('mode', g:reason)
53 call assert_equal(1, line('$'))
54 call assert_equal('new line', getline(1))
55
56 " Only time changed
zeertzjq83cd2c72024-04-05 20:05:11 +020057 if has('nanotime')
58 sleep 10m
59 else
60 sleep 2
61 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010062 silent !touch Xchanged_r
63 let g:reason = ''
64 checktime
65 call assert_equal('time', g:reason)
66 call assert_equal(1, line('$'))
67 call assert_equal('new line', getline(1))
68
69 if has('persistent_undo')
70 " With an undo file the reload can be undone and a change before the
71 " reload.
72 set undofile
73 call setline(2, 'before write')
74 write
75 call setline(2, 'after write')
zeertzjq83cd2c72024-04-05 20:05:11 +020076 if has('nanotime')
77 sleep 10m
78 else
79 sleep 2
80 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +010081 silent !echo 'different line' >>Xchanged_r
82 let g:reason = ''
83 checktime
84 call assert_equal('conflict', g:reason)
85 call assert_equal(3, line('$'))
86 call assert_equal('before write', getline(2))
87 call assert_equal('different line', getline(3))
88 " undo the reload
89 undo
90 call assert_equal(2, line('$'))
91 call assert_equal('after write', getline(2))
92 " undo the change before reload
93 undo
94 call assert_equal(2, line('$'))
95 call assert_equal('before write', getline(2))
96
97 set noundofile
98 endif
99
100 au! testreload
101 bwipe!
Bram Moolenaar137c14b2019-04-18 20:30:55 +0200102 call delete(undofile('Xchanged_r'))
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100103 call delete('Xchanged_r')
104endfunc
105
Rob Pilling8196e942022-02-11 15:12:10 +0000106func Test_FileChangedShell_edit()
107 CheckUnix
108
109 new Xchanged_r
110 call setline(1, 'reload this')
111 set fileformat=unix
112 write
113
114 " File format changed, reload (content only, no 'ff' etc)
115 augroup testreload
116 au!
117 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
118 augroup END
119 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100120 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000121 let g:reason = ''
122 checktime
123 call assert_equal('changed', g:reason)
124 call assert_equal(&fileformat, 'unix')
125 call assert_equal("line1\r", getline(1))
126 call assert_equal("line2\r", getline(2))
127 %s/\r
128 write
129
130 " File format changed, reload with 'ff', etc
131 augroup testreload
132 au!
133 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
134 augroup END
135 call assert_equal(&fileformat, 'unix')
136 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
137 let g:reason = ''
138 checktime
139 call assert_equal('changed', g:reason)
140 call assert_equal(&fileformat, 'dos')
141 call assert_equal('line1', getline(1))
142 call assert_equal('line2', getline(2))
143 set fileformat=unix
144 write
145
146 au! testreload
147 bwipe!
148 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000149endfunc
150
151func Test_FileChangedShell_edit_dialog()
152 CheckNotGui
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100153 CheckUnix " Using low level feedkeys() does not work on MS-Windows.
Rob Pilling8196e942022-02-11 15:12:10 +0000154
155 new Xchanged_r
156 call setline(1, 'reload this')
157 set fileformat=unix
158 write
159
160 " File format changed, reload (content only) via prompt
161 augroup testreload
162 au!
163 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
164 augroup END
165 call assert_equal(&fileformat, 'unix')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100166 call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
Rob Pilling8196e942022-02-11 15:12:10 +0000167 let g:reason = ''
168 call feedkeys('L', 'L') " load file content only
169 checktime
170 call assert_equal('changed', g:reason)
171 call assert_equal(&fileformat, 'unix')
172 call assert_equal("line1\r", getline(1))
173 call assert_equal("line2\r", getline(2))
174 %s/\r
175 write
176
177 " File format changed, reload (file and options) via prompt
178 augroup testreload
179 au!
180 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
181 augroup END
182 call assert_equal(&fileformat, 'unix')
183 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
184 let g:reason = ''
185 call feedkeys('a', 'L') " load file content and options
186 checktime
187 call assert_equal('changed', g:reason)
188 call assert_equal(&fileformat, 'dos')
189 call assert_equal("line1", getline(1))
190 call assert_equal("line2", getline(2))
191 set fileformat=unix
192 write
193
194 au! testreload
195 bwipe!
196 call delete(undofile('Xchanged_r'))
Rob Pilling8196e942022-02-11 15:12:10 +0000197endfunc
198
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100199func Test_file_changed_dialog()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200200 CheckUnix
201 CheckNotGui
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100202 au! FileChangedShell
203
204 new Xchanged_d
205 call setline(1, 'reload this')
206 write
zeertzjq83cd2c72024-04-05 20:05:11 +0200207 " Need to wait until the timestamp would change.
208 if has('nanotime')
209 sleep 10m
210 else
211 sleep 2
212 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100213 silent !echo 'extra line' >>Xchanged_d
214 call feedkeys('L', 'L')
215 checktime
216 call assert_match('W11:', v:warningmsg)
217 call assert_equal(2, line('$'))
218 call assert_equal('reload this', getline(1))
219 call assert_equal('extra line', getline(2))
220
221 " delete buffer, only shows an error, no prompt
222 silent !rm Xchanged_d
223 checktime
224 call assert_match('E211:', v:warningmsg)
225 call assert_equal(2, line('$'))
226 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200227 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100228
Bram Moolenaar8239c622019-05-24 16:46:01 +0200229 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100230 call setline(1, 'buffer is changed')
231 silent !echo 'new line' >Xchanged_d
232 call feedkeys('L', 'L')
233 checktime
234 call assert_match('W12:', v:warningmsg)
235 call assert_equal(1, line('$'))
236 call assert_equal('new line', getline(1))
237
238 " Only mode changed, reload
239 silent !chmod +x Xchanged_d
240 call feedkeys('L', 'L')
241 checktime
242 call assert_match('W16:', v:warningmsg)
243 call assert_equal(1, line('$'))
244 call assert_equal('new line', getline(1))
245
246 " Only time changed, no prompt
zeertzjq83cd2c72024-04-05 20:05:11 +0200247 if has('nanotime')
248 sleep 10m
249 else
250 sleep 2
251 endif
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100252 silent !touch Xchanged_d
253 let v:warningmsg = ''
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100254 checktime Xchanged_d
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100255 call assert_equal('', v:warningmsg)
256 call assert_equal(1, line('$'))
257 call assert_equal('new line', getline(1))
258
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200259 " File created after starting to edit it
260 call delete('Xchanged_d')
261 new Xchanged_d
Bram Moolenaar70e67252022-09-27 19:34:35 +0100262 call writefile(['one'], 'Xchanged_d', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200263 call feedkeys('L', 'L')
264 checktime Xchanged_d
265 call assert_equal(['one'], getline(1, '$'))
266 close!
267
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100268 bwipe!
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100269endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100270
271" Test for editing a new buffer from a FileChangedShell autocmd
272func Test_FileChangedShell_newbuf()
Bram Moolenaar70e67252022-09-27 19:34:35 +0100273 call writefile(['one', 'two'], 'Xchfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100274 new Xchfile
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100275 augroup testnewbuf
276 autocmd FileChangedShell * enew
277 augroup END
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100278 call writefile(['red'], 'Xchfile')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100279 call assert_fails('checktime', 'E811:')
Bram Moolenaar70e67252022-09-27 19:34:35 +0100280
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100281 au! testnewbuf
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100282endfunc
283
284" vim: shiftwidth=2 sts=2 expandtab