blob: a937bb8d102644ce1533ebd411e2fc41d4765feb [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()
4 if !has('unix')
5 return
6 endif
7 augroup testreload
8 au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload'
9 augroup END
10 new Xchanged_r
11 call setline(1, 'reload this')
12 write
13 " Need to wait until the timestamp would change by at least a second.
14 sleep 2
15 silent !echo 'extra line' >>Xchanged_r
16 checktime
17 call assert_equal('changed', g:reason)
18 call assert_equal(2, line('$'))
19 call assert_equal('extra line', getline(2))
20
21 " Only triggers once
22 let g:reason = ''
23 checktime
24 call assert_equal('', g:reason)
25
26 " When deleted buffer is not reloaded
27 silent !rm Xchanged_r
28 let g:reason = ''
29 checktime
30 call assert_equal('deleted', g:reason)
31 call assert_equal(2, line('$'))
32 call assert_equal('extra line', getline(2))
33
34 " When recreated buffer is reloaded
35 call setline(1, 'buffer is changed')
36 silent !echo 'new line' >>Xchanged_r
37 let g:reason = ''
38 checktime
39 call assert_equal('conflict', g:reason)
40 call assert_equal(1, line('$'))
41 call assert_equal('new line', getline(1))
42
43 " Only mode changed
44 silent !chmod +x Xchanged_r
45 let g:reason = ''
46 checktime
47 call assert_equal('mode', g:reason)
48 call assert_equal(1, line('$'))
49 call assert_equal('new line', getline(1))
50
51 " Only time changed
52 sleep 2
53 silent !touch Xchanged_r
54 let g:reason = ''
55 checktime
56 call assert_equal('time', g:reason)
57 call assert_equal(1, line('$'))
58 call assert_equal('new line', getline(1))
59
60 if has('persistent_undo')
61 " With an undo file the reload can be undone and a change before the
62 " reload.
63 set undofile
64 call setline(2, 'before write')
65 write
66 call setline(2, 'after write')
67 sleep 2
68 silent !echo 'different line' >>Xchanged_r
69 let g:reason = ''
70 checktime
71 call assert_equal('conflict', g:reason)
72 call assert_equal(3, line('$'))
73 call assert_equal('before write', getline(2))
74 call assert_equal('different line', getline(3))
75 " undo the reload
76 undo
77 call assert_equal(2, line('$'))
78 call assert_equal('after write', getline(2))
79 " undo the change before reload
80 undo
81 call assert_equal(2, line('$'))
82 call assert_equal('before write', getline(2))
83
84 set noundofile
85 endif
86
87 au! testreload
88 bwipe!
Bram Moolenaar137c14b2019-04-18 20:30:55 +020089 call delete(undofile('Xchanged_r'))
Bram Moolenaar5e66b422019-01-24 21:58:10 +010090 call delete('Xchanged_r')
91endfunc
92
93func Test_file_changed_dialog()
Bram Moolenaare2956092019-01-25 21:01:17 +010094 if !has('unix') || has('gui_running')
Bram Moolenaar5e66b422019-01-24 21:58:10 +010095 return
96 endif
97 au! FileChangedShell
98
99 new Xchanged_d
100 call setline(1, 'reload this')
101 write
102 " Need to wait until the timestamp would change by at least a second.
103 sleep 2
104 silent !echo 'extra line' >>Xchanged_d
105 call feedkeys('L', 'L')
106 checktime
107 call assert_match('W11:', v:warningmsg)
108 call assert_equal(2, line('$'))
109 call assert_equal('reload this', getline(1))
110 call assert_equal('extra line', getline(2))
111
112 " delete buffer, only shows an error, no prompt
113 silent !rm Xchanged_d
114 checktime
115 call assert_match('E211:', v:warningmsg)
116 call assert_equal(2, line('$'))
117 call assert_equal('extra line', getline(2))
Bram Moolenaar8239c622019-05-24 16:46:01 +0200118 let v:warningmsg = 'empty'
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100119
Bram Moolenaar8239c622019-05-24 16:46:01 +0200120 " change buffer, recreate the file and reload
Bram Moolenaar5e66b422019-01-24 21:58:10 +0100121 call setline(1, 'buffer is changed')
122 silent !echo 'new line' >Xchanged_d
123 call feedkeys('L', 'L')
124 checktime
125 call assert_match('W12:', v:warningmsg)
126 call assert_equal(1, line('$'))
127 call assert_equal('new line', getline(1))
128
129 " Only mode changed, reload
130 silent !chmod +x Xchanged_d
131 call feedkeys('L', 'L')
132 checktime
133 call assert_match('W16:', v:warningmsg)
134 call assert_equal(1, line('$'))
135 call assert_equal('new line', getline(1))
136
137 " Only time changed, no prompt
138 sleep 2
139 silent !touch Xchanged_d
140 let v:warningmsg = ''
141 checktime
142 call assert_equal('', v:warningmsg)
143 call assert_equal(1, line('$'))
144 call assert_equal('new line', getline(1))
145
146 bwipe!
147 call delete('Xchanged_d')
148endfunc