1" Tests for when a file was changed outside of Vim. 2 3source check.vim 4 5func Test_FileChangedShell_reload() 6 CheckUnix 7 8 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! 90 call delete(undofile('Xchanged_r')) 91 call delete('Xchanged_r') 92endfunc 93 94func Test_file_changed_dialog() 95 CheckUnix 96 CheckNotGui 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)) 118 let v:warningmsg = 'empty' 119 120 " change buffer, recreate the file and reload 121 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 Xchanged_d 142 call assert_equal('', v:warningmsg) 143 call assert_equal(1, line('$')) 144 call assert_equal('new line', getline(1)) 145 146 " File created after starting to edit it 147 call delete('Xchanged_d') 148 new Xchanged_d 149 call writefile(['one'], 'Xchanged_d') 150 call feedkeys('L', 'L') 151 checktime Xchanged_d 152 call assert_equal(['one'], getline(1, '$')) 153 close! 154 155 bwipe! 156 call delete('Xchanged_d') 157endfunc 158 159" Test for editing a new buffer from a FileChangedShell autocmd 160func Test_FileChangedShell_newbuf() 161 call writefile(['one', 'two'], 'Xfile') 162 new Xfile 163 augroup testnewbuf 164 autocmd FileChangedShell * enew 165 augroup END 166 call writefile(['red'], 'Xfile') 167 call assert_fails('checktime', 'E811:') 168 au! testnewbuf 169 call delete('Xfile') 170endfunc 171 172" vim: shiftwidth=2 sts=2 expandtab 173