1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
" Tests for when a file was changed outside of Vim.
source check.vim
func Test_FileChangedShell_reload()
CheckUnix
augroup testreload
au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
augroup END
new Xchanged_r
call setline(1, 'reload this')
write
" Need to wait until the timestamp would change by at least a second.
sleep 2
silent !echo 'extra line' >>Xchanged_r
checktime
call assert_equal('changed', g:reason)
call assert_equal(2, line('$'))
call assert_equal('extra line', getline(2))
" Only triggers once
let g:reason = ''
checktime
call assert_equal('', g:reason)
" When deleted buffer is not reloaded
silent !rm Xchanged_r
let g:reason = ''
checktime
call assert_equal('deleted', g:reason)
call assert_equal(2, line('$'))
call assert_equal('extra line', getline(2))
" When recreated buffer is reloaded
call setline(1, 'buffer is changed')
silent !echo 'new line' >>Xchanged_r
let g:reason = ''
checktime
call assert_equal('conflict', g:reason)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
" Only mode changed
silent !chmod +x Xchanged_r
let g:reason = ''
checktime
call assert_equal('mode', g:reason)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
" Only time changed
sleep 2
silent !touch Xchanged_r
let g:reason = ''
checktime
call assert_equal('time', g:reason)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
if has('persistent_undo')
" With an undo file the reload can be undone and a change before the
" reload.
set undofile
call setline(2, 'before write')
write
call setline(2, 'after write')
sleep 2
silent !echo 'different line' >>Xchanged_r
let g:reason = ''
checktime
call assert_equal('conflict', g:reason)
call assert_equal(3, line('$'))
call assert_equal('before write', getline(2))
call assert_equal('different line', getline(3))
" undo the reload
undo
call assert_equal(2, line('$'))
call assert_equal('after write', getline(2))
" undo the change before reload
undo
call assert_equal(2, line('$'))
call assert_equal('before write', getline(2))
set noundofile
endif
au! testreload
bwipe!
call delete(undofile('Xchanged_r'))
call delete('Xchanged_r')
endfunc
func Test_FileChangedShell_edit()
CheckUnix
new Xchanged_r
call setline(1, 'reload this')
set fileformat=unix
write
" File format changed, reload (content only, no 'ff' etc)
augroup testreload
au!
au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
augroup END
call assert_equal(&fileformat, 'unix')
call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
let g:reason = ''
checktime
call assert_equal('changed', g:reason)
call assert_equal(&fileformat, 'unix')
call assert_equal("line1\r", getline(1))
call assert_equal("line2\r", getline(2))
%s/\r
write
" File format changed, reload with 'ff', etc
augroup testreload
au!
au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
augroup END
call assert_equal(&fileformat, 'unix')
call writefile(["line1\r", "line2\r"], 'Xchanged_r')
let g:reason = ''
checktime
call assert_equal('changed', g:reason)
call assert_equal(&fileformat, 'dos')
call assert_equal('line1', getline(1))
call assert_equal('line2', getline(2))
set fileformat=unix
write
au! testreload
bwipe!
call delete(undofile('Xchanged_r'))
endfunc
func Test_FileChangedShell_edit_dialog()
CheckNotGui
CheckUnix " Using low level feedkeys() does not work on MS-Windows.
new Xchanged_r
call setline(1, 'reload this')
set fileformat=unix
write
" File format changed, reload (content only) via prompt
augroup testreload
au!
au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
augroup END
call assert_equal(&fileformat, 'unix')
call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D')
let g:reason = ''
call feedkeys('L', 'L') " load file content only
checktime
call assert_equal('changed', g:reason)
call assert_equal(&fileformat, 'unix')
call assert_equal("line1\r", getline(1))
call assert_equal("line2\r", getline(2))
%s/\r
write
" File format changed, reload (file and options) via prompt
augroup testreload
au!
au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
augroup END
call assert_equal(&fileformat, 'unix')
call writefile(["line1\r", "line2\r"], 'Xchanged_r')
let g:reason = ''
call feedkeys('a', 'L') " load file content and options
checktime
call assert_equal('changed', g:reason)
call assert_equal(&fileformat, 'dos')
call assert_equal("line1", getline(1))
call assert_equal("line2", getline(2))
set fileformat=unix
write
au! testreload
bwipe!
call delete(undofile('Xchanged_r'))
endfunc
func Test_file_changed_dialog()
CheckUnix
CheckNotGui
au! FileChangedShell
new Xchanged_d
call setline(1, 'reload this')
write
" Need to wait until the timestamp would change by at least a second.
sleep 2
silent !echo 'extra line' >>Xchanged_d
call feedkeys('L', 'L')
checktime
call assert_match('W11:', v:warningmsg)
call assert_equal(2, line('$'))
call assert_equal('reload this', getline(1))
call assert_equal('extra line', getline(2))
" delete buffer, only shows an error, no prompt
silent !rm Xchanged_d
checktime
call assert_match('E211:', v:warningmsg)
call assert_equal(2, line('$'))
call assert_equal('extra line', getline(2))
let v:warningmsg = 'empty'
" change buffer, recreate the file and reload
call setline(1, 'buffer is changed')
silent !echo 'new line' >Xchanged_d
call feedkeys('L', 'L')
checktime
call assert_match('W12:', v:warningmsg)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
" Only mode changed, reload
silent !chmod +x Xchanged_d
call feedkeys('L', 'L')
checktime
call assert_match('W16:', v:warningmsg)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
" Only time changed, no prompt
sleep 2
silent !touch Xchanged_d
let v:warningmsg = ''
checktime Xchanged_d
call assert_equal('', v:warningmsg)
call assert_equal(1, line('$'))
call assert_equal('new line', getline(1))
" File created after starting to edit it
call delete('Xchanged_d')
new Xchanged_d
call writefile(['one'], 'Xchanged_d', 'D')
call feedkeys('L', 'L')
checktime Xchanged_d
call assert_equal(['one'], getline(1, '$'))
close!
bwipe!
endfunc
" Test for editing a new buffer from a FileChangedShell autocmd
func Test_FileChangedShell_newbuf()
call writefile(['one', 'two'], 'Xchfile', 'D')
new Xchfile
augroup testnewbuf
autocmd FileChangedShell * enew
augroup END
call writefile(['red'], 'Xchfile')
call assert_fails('checktime', 'E811:')
au! testnewbuf
endfunc
" vim: shiftwidth=2 sts=2 expandtab
|