neovim

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

test_stat.vim (5580B)


      1 " Tests for stat functions and checktime
      2 
      3 source check.vim
      4 
      5 func CheckFileTime(doSleep)
      6  let fnames = ['Xtest1.tmp', 'Xtest2.tmp', 'Xtest3.tmp']
      7  let times = []
      8  let result = 0
      9 
     10  " Use three files instead of localtim(), with a network filesystem the file
     11  " times may differ at bit
     12  let fl = ['Hello World!']
     13  for fname in fnames
     14    call writefile(fl, fname)
     15    call add(times, fname->getftime())
     16    if a:doSleep
     17      sleep 1
     18    endif
     19  endfor
     20 
     21  let time_correct = (times[0] <= times[1] && times[1] <= times[2])
     22  if a:doSleep || time_correct
     23    call assert_true(time_correct, printf('Expected %s <= %s <= %s', times[0], times[1], times[2]))
     24    call assert_equal(strlen(fl[0] . "\n"), fnames[0]->getfsize())
     25    call assert_equal('file', fnames[0]->getftype())
     26    call assert_equal('rw-', getfperm(fnames[0])[0:2])
     27    let result = 1
     28  endif
     29 
     30  for fname in fnames
     31    call delete(fname)
     32  endfor
     33  return result
     34 endfunc
     35 
     36 func Test_existent_file()
     37  " On some systems the file timestamp is rounded to a multiple of 2 seconds.
     38  " We need to sleep to handle that, but that makes the test slow.  First try
     39  " without the sleep, and if it fails try again with the sleep.
     40  if CheckFileTime(0) == 0
     41    call CheckFileTime(1)
     42  endif
     43 endfunc
     44 
     45 func Test_existent_directory()
     46  let dname = '.'
     47 
     48  call assert_equal(0, getfsize(dname))
     49  call assert_equal('dir', getftype(dname))
     50  call assert_equal(has('win32') ? 'rw-' : 'rwx', getfperm(dname)[0:2])
     51 endfunc
     52 
     53 func SleepForTimestamp()
     54  " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
     55  if has('win32')
     56    sleep 2
     57  else
     58    sleep 1
     59  endif
     60 endfunc
     61 
     62 func Test_checktime()
     63  let fname = 'Xtest.tmp'
     64 
     65  let fl = ['Hello World!']
     66  call writefile(fl, fname, 'D')
     67  set autoread
     68  exec 'e' fname
     69  call SleepForTimestamp()
     70  let fl = readfile(fname)
     71  let fl[0] .= ' - checktime'
     72  call writefile(fl, fname)
     73  checktime
     74  call assert_equal(fl[0], getline(1))
     75 endfunc
     76 
     77 func Test_checktime_fast()
     78  CheckFeature nanotime
     79 
     80  let fname = 'Xtest.tmp'
     81 
     82  let fl = ['Hello World!']
     83  call writefile(fl, fname, 'D')
     84  set autoread
     85  exec 'e' fname
     86  let fl = readfile(fname)
     87  let fl[0] .= ' - checktime'
     88  sleep 10m  " make test less flaky in Nvim
     89  call writefile(fl, fname)
     90  checktime
     91  call assert_equal(fl[0], getline(1))
     92 endfunc
     93 
     94 func Test_autoread_fast()
     95  CheckFeature nanotime
     96 
     97  " this is timing sensitive
     98  let g:test_is_flaky = 1
     99 
    100  new Xautoread
    101  setlocal autoread
    102  call setline(1, 'foo')
    103  w!
    104  sleep 10m
    105  call writefile(['bar'], 'Xautoread', 'D')
    106  sleep 10m
    107  checktime
    108  call assert_equal('bar', trim(getline(1)))
    109 endfunc
    110 
    111 func Test_autoread_file_deleted()
    112  new Xautoread
    113  set autoread
    114  call setline(1, 'original')
    115  w!
    116 
    117  call SleepForTimestamp()
    118  if has('win32')
    119    silent !echo changed > Xautoread
    120  else
    121    silent !echo 'changed' > Xautoread
    122  endif
    123  checktime
    124  call assert_equal('changed', trim(getline(1)))
    125 
    126  call SleepForTimestamp()
    127  messages clear
    128  if has('win32')
    129    silent !del Xautoread
    130  else
    131    silent !rm Xautoread
    132  endif
    133  checktime
    134  call assert_match('E211:', execute('messages'))
    135  call assert_equal('changed', trim(getline(1)))
    136 
    137  call SleepForTimestamp()
    138  if has('win32')
    139    silent !echo recreated > Xautoread
    140  else
    141    silent !echo 'recreated' > Xautoread
    142  endif
    143  checktime
    144  call assert_equal('recreated', trim(getline(1)))
    145 
    146  call delete('Xautoread')
    147  bwipe!
    148 endfunc
    149 
    150 
    151 func Test_nonexistent_file()
    152  let fname = 'Xtest.tmp'
    153 
    154  call delete(fname)
    155  call assert_equal(-1, getftime(fname))
    156  call assert_equal(-1, getfsize(fname))
    157  call assert_equal('', getftype(fname))
    158  call assert_equal('', getfperm(fname))
    159 endfunc
    160 
    161 func Test_getftype()
    162  call assert_equal('file', getftype(v:progpath))
    163  call assert_equal('dir',  getftype('.'))
    164 
    165  if !has('unix')
    166    return
    167  endif
    168 
    169  silent !ln -s Xfile Xlink
    170  call assert_equal('link', getftype('Xlink'))
    171  call delete('Xlink')
    172 
    173  if executable('mkfifo')
    174    silent !mkfifo Xfifo
    175    call assert_equal('fifo', getftype('Xfifo'))
    176    call delete('Xfifo')
    177  endif
    178 
    179  for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
    180    " On Mac /def/fd/2 is found but the type is "fifo"
    181    if cdevfile !~ '/dev/fd/'
    182      let type = getftype(cdevfile)
    183      " ignore empty result, can happen if the file disappeared
    184      if type != ''
    185 call assert_equal('cdev', type, 'for ' .. cdevfile)
    186      endif
    187    endif
    188  endfor
    189 
    190  for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
    191    let type = getftype(bdevfile)
    192    " ignore empty result, can happen if the file disappeared
    193    if type != ''
    194      call assert_equal('bdev', type, 'for ' .. bdevfile)
    195    endif
    196  endfor
    197 
    198  " The /run/ directory typically contains socket files.
    199  " If it does not, test won't fail but will not test socket files.
    200  for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
    201    let type = getftype(socketfile)
    202    " ignore empty result, can happen if the file disappeared
    203    if type != ''
    204      call assert_equal('socket', type, 'for ' .. socketfile)
    205    endif
    206  endfor
    207 
    208  " TODO: file type 'other' is not tested. How can we test it?
    209 endfunc
    210 
    211 func Test_win32_symlink_dir()
    212  " On Windows, non-admin users cannot create symlinks.
    213  " So we use an existing symlink for this test.
    214  if has('win32')
    215    " Check if 'C:\Users\All Users' is a symlink to a directory.
    216    let res = system('dir C:\Users /a')
    217    if match(res, '\C<SYMLINKD> *All Users') >= 0
    218      " Get the filetype of the symlink.
    219      call assert_equal('link', getftype('C:\Users\All Users'))
    220    endif
    221  endif
    222 endfunc