blob: 0b5b8e0334bc8702d0b79498c4114c67ac1aaa10 [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
142
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')
154 call writefile(["line1\r", "line2\r"], 'Xchanged_r')
155 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'))
185 call delete('Xchanged_r')
186endfunc
187
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100188func Test_file_changed_dialog()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200189 CheckUnix
190 CheckNotGui
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100191 au! FileChangedShell
192
193 new Xchanged_d
194 call setline(1, 'reload this')
195 write
196 " Need to wait until the timestamp would change by at least a second.
197 sleep 2
198 silent !echo 'extra line' >>Xchanged_d
199 call feedkeys('L', 'L')
200 checktime
201 call assert_match('W11:', v:warningmsg)
202 call assert_equal(2, line('$'))
203 call assert_equal('reload this', getline(1))
204 call assert_equal('extra line', getline(2))
205
206 " delete buffer, only shows an error, no prompt
207 silent !rm Xchanged_d
208 checktime
209 call assert_match('E211:', v:warningmsg)
210 call assert_equal(2, line('$'))
211 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200212 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100213
Bram Moolenaar8239c622019-05-24 16:46:01 +0200214 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100215 call setline(1, 'buffer is changed')
216 silent !echo 'new line' >Xchanged_d
217 call feedkeys('L', 'L')
218 checktime
219 call assert_match('W12:', v:warningmsg)
220 call assert_equal(1, line('$'))
221 call assert_equal('new line', getline(1))
222
223 " Only mode changed, reload
224 silent !chmod +x Xchanged_d
225 call feedkeys('L', 'L')
226 checktime
227 call assert_match('W16:', v:warningmsg)
228 call assert_equal(1, line('$'))
229 call assert_equal('new line', getline(1))
230
231 " Only time changed, no prompt
232 sleep 2
233 silent !touch Xchanged_d
234 let v:warningmsg = ''
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100235 checktime Xchanged_d
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100236 call assert_equal('', v:warningmsg)
237 call assert_equal(1, line('$'))
238 call assert_equal('new line', getline(1))
239
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200240 " File created after starting to edit it
241 call delete('Xchanged_d')
242 new Xchanged_d
243 call writefile(['one'], 'Xchanged_d')
244 call feedkeys('L', 'L')
245 checktime Xchanged_d
246 call assert_equal(['one'], getline(1, '$'))
247 close!
248
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100249 bwipe!
250 call delete('Xchanged_d')
251endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100252
253" Test for editing a new buffer from a FileChangedShell autocmd
254func Test_FileChangedShell_newbuf()
255 call writefile(['one', 'two'], 'Xfile')
256 new Xfile
257 augroup testnewbuf
258 autocmd FileChangedShell * enew
259 augroup END
260 call writefile(['red'], 'Xfile')
261 call assert_fails('checktime', 'E811:')
262 au! testnewbuf
263 call delete('Xfile')
264endfunc
265
266" vim: shiftwidth=2 sts=2 expandtab