neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_filechanged.vim (8251B)


      1 " Tests for when a file was changed outside of Vim.
      2 
      3 source check.vim
      4 
      5 func 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.
     15  if has('nanotime')
     16    sleep 10m
     17  else
     18    sleep 2
     19  endif
     20  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
     57  if has('nanotime')
     58    sleep 10m
     59  else
     60    sleep 2
     61  endif
     62  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')
     76    if has('nanotime')
     77      sleep 10m
     78    else
     79      sleep 2
     80    endif
     81    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!
    102  call delete(undofile('Xchanged_r'))
    103  call delete('Xchanged_r')
    104 endfunc
    105 
    106 func 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')
    120  sleep 10m  " make the test less flaky in Nvim
    121  call writefile(["line1\r", "line2\r"], 'Xchanged_r')
    122  let g:reason = ''
    123  checktime
    124  call assert_equal('changed', g:reason)
    125  call assert_equal(&fileformat, 'unix')
    126  call assert_equal("line1\r", getline(1))
    127  call assert_equal("line2\r", getline(2))
    128  %s/\r
    129  write
    130 
    131  " File format changed, reload with 'ff', etc
    132  augroup testreload
    133    au!
    134    au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit'
    135  augroup END
    136  call assert_equal(&fileformat, 'unix')
    137  sleep 10m  " make the test less flaky in Nvim
    138  call writefile(["line1\r", "line2\r"], 'Xchanged_r')
    139  let g:reason = ''
    140  checktime
    141  call assert_equal('changed', g:reason)
    142  call assert_equal(&fileformat, 'dos')
    143  call assert_equal('line1', getline(1))
    144  call assert_equal('line2', getline(2))
    145  set fileformat=unix
    146  write
    147 
    148  au! testreload
    149  bwipe!
    150  call delete(undofile('Xchanged_r'))
    151  call delete('Xchanged_r')
    152 endfunc
    153 
    154 func Test_FileChangedShell_edit_dialog()
    155  CheckNotGui
    156  CheckUnix  " Using low level feedkeys() does not work on MS-Windows.
    157 
    158  new Xchanged_r
    159  call setline(1, 'reload this')
    160  set fileformat=unix
    161  write
    162 
    163  " File format changed, reload (content only) via prompt
    164  augroup testreload
    165    au!
    166    au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
    167  augroup END
    168  call assert_equal(&fileformat, 'unix')
    169  sleep 10m  " make the test less flaky in Nvim
    170  call writefile(["line1\r", "line2\r"], 'Xchanged_r')
    171  let g:reason = ''
    172  call feedkeys('L', 'L') " load file content only
    173  checktime
    174  call assert_equal('changed', g:reason)
    175  call assert_equal(&fileformat, 'unix')
    176  call assert_equal("line1\r", getline(1))
    177  call assert_equal("line2\r", getline(2))
    178  %s/\r
    179  write
    180 
    181  " File format changed, reload (file and options) via prompt
    182  augroup testreload
    183    au!
    184    au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask'
    185  augroup END
    186  call assert_equal(&fileformat, 'unix')
    187  sleep 10m  " make the test less flaky in Nvim
    188  call writefile(["line1\r", "line2\r"], 'Xchanged_r')
    189  let g:reason = ''
    190  call feedkeys('a', 'L') " load file content and options
    191  checktime
    192  call assert_equal('changed', g:reason)
    193  call assert_equal(&fileformat, 'dos')
    194  call assert_equal("line1", getline(1))
    195  call assert_equal("line2", getline(2))
    196  set fileformat=unix
    197  write
    198 
    199  au! testreload
    200  bwipe!
    201  call delete(undofile('Xchanged_r'))
    202  call delete('Xchanged_r')
    203 endfunc
    204 
    205 func Test_file_changed_dialog()
    206  CheckUnix
    207  CheckNotGui
    208  au! FileChangedShell
    209 
    210  new Xchanged_d
    211  call setline(1, 'reload this')
    212  write
    213  " Need to wait until the timestamp would change.
    214  if has('nanotime')
    215    sleep 10m
    216  else
    217    sleep 2
    218  endif
    219  silent !echo 'extra line' >>Xchanged_d
    220  call feedkeys('L', 'L')
    221  checktime
    222  call assert_match('W11:', v:warningmsg)
    223  call assert_equal(2, line('$'))
    224  call assert_equal('reload this', getline(1))
    225  call assert_equal('extra line', getline(2))
    226 
    227  " delete buffer, only shows an error, no prompt
    228  silent !rm Xchanged_d
    229  checktime
    230  call assert_match('E211:', v:warningmsg)
    231  call assert_equal(2, line('$'))
    232  call assert_equal('extra line', getline(2))
    233  let v:warningmsg = 'empty'
    234 
    235  " change buffer, recreate the file and reload
    236  call setline(1, 'buffer is changed')
    237  silent !echo 'new line' >Xchanged_d
    238  call feedkeys('L', 'L')
    239  checktime
    240  call assert_match('W12:', v:warningmsg)
    241  call assert_equal(1, line('$'))
    242  call assert_equal('new line', getline(1))
    243 
    244  " Only mode changed, reload
    245  silent !chmod +x Xchanged_d
    246  call feedkeys('L', 'L')
    247  checktime
    248  call assert_match('W16:', v:warningmsg)
    249  call assert_equal(1, line('$'))
    250  call assert_equal('new line', getline(1))
    251 
    252  " Only time changed, no prompt
    253  if has('nanotime')
    254    sleep 10m
    255  else
    256    sleep 2
    257  endif
    258  silent !touch Xchanged_d
    259  let v:warningmsg = ''
    260  checktime Xchanged_d
    261  call assert_equal('', v:warningmsg)
    262  call assert_equal(1, line('$'))
    263  call assert_equal('new line', getline(1))
    264 
    265  " File created after starting to edit it
    266  call delete('Xchanged_d')
    267  new Xchanged_d
    268  call writefile(['one'], 'Xchanged_d')
    269  call feedkeys('L', 'L')
    270  checktime Xchanged_d
    271  call assert_equal(['one'], getline(1, '$'))
    272  close!
    273 
    274  bwipe!
    275  call delete('Xchanged_d')
    276 endfunc
    277 
    278 " Test for editing a new buffer from a FileChangedShell autocmd
    279 func Test_FileChangedShell_newbuf()
    280  call writefile(['one', 'two'], 'Xfile')
    281  new Xfile
    282  augroup testnewbuf
    283    autocmd FileChangedShell * enew
    284  augroup END
    285  sleep 10m  " make the test less flaky in Nvim
    286  call writefile(['red'], 'Xfile')
    287  call assert_fails('checktime', 'E811:')
    288  au! testnewbuf
    289  call delete('Xfile')
    290 endfunc
    291 
    292 func Test_file_changed_wipeout()
    293  call writefile(['foo'], 'Xchanged_bw', 'D')
    294  edit Xchanged_bw
    295  augroup FileChangedWipeout
    296    autocmd FileChangedShell * ++once let v:fcs_choice = 'reload'
    297    autocmd BufReadPost * ++once %bw!
    298  augroup END
    299 
    300  " Need to wait until the timestamp would change.
    301  if has('nanotime')
    302    sleep 10m
    303  else
    304    sleep 2
    305  endif
    306  call writefile(['bar'], 'Xchanged_bw')
    307  call assert_equal(1, bufexists('Xchanged_bw'))
    308  checktime " used to be a heap UAF
    309  call assert_equal(0, bufexists('Xchanged_bw'))
    310 
    311  au! FileChangedWipeout
    312  %bw!
    313 endfunc
    314 
    315 " vim: shiftwidth=2 sts=2 expandtab