2016-11-16 07:24:10 -07:00
|
|
|
" Tests for stat functions and checktime
|
|
|
|
|
2022-02-13 06:33:28 -07:00
|
|
|
source check.vim
|
|
|
|
|
2018-01-03 11:15:40 -07:00
|
|
|
func CheckFileTime(doSleep)
|
2018-11-18 10:05:43 -07:00
|
|
|
let fnames = ['Xtest1.tmp', 'Xtest2.tmp', 'Xtest3.tmp']
|
|
|
|
let times = []
|
2018-01-03 11:15:40 -07:00
|
|
|
let result = 0
|
2016-11-16 07:24:10 -07:00
|
|
|
|
2022-02-14 03:56:30 -07:00
|
|
|
" Use three files instead of localtim(), with a network filesystem the file
|
2018-11-18 10:05:43 -07:00
|
|
|
" times may differ at bit
|
2017-11-22 03:57:56 -07:00
|
|
|
let fl = ['Hello World!']
|
2018-11-18 10:05:43 -07:00
|
|
|
for fname in fnames
|
|
|
|
call writefile(fl, fname)
|
2021-08-27 13:09:37 -07:00
|
|
|
call add(times, fname->getftime())
|
2018-11-18 10:05:43 -07:00
|
|
|
if a:doSleep
|
|
|
|
sleep 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let time_correct = (times[0] <= times[1] && times[1] <= times[2])
|
2018-01-03 11:15:40 -07:00
|
|
|
if a:doSleep || time_correct
|
2018-11-18 10:05:43 -07:00
|
|
|
call assert_true(time_correct, printf('Expected %s <= %s <= %s', times[0], times[1], times[2]))
|
2021-08-27 13:09:37 -07:00
|
|
|
call assert_equal(strlen(fl[0] . "\n"), fnames[0]->getfsize())
|
|
|
|
call assert_equal('file', fnames[0]->getftype())
|
2018-11-18 10:05:43 -07:00
|
|
|
call assert_equal('rw-', getfperm(fnames[0])[0:2])
|
2018-01-03 11:15:40 -07:00
|
|
|
let result = 1
|
|
|
|
endif
|
2017-11-22 03:57:56 -07:00
|
|
|
|
2018-11-18 10:05:43 -07:00
|
|
|
for fname in fnames
|
|
|
|
call delete(fname)
|
|
|
|
endfor
|
2018-01-03 11:15:40 -07:00
|
|
|
return result
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_existent_file()
|
|
|
|
" On some systems the file timestamp is rounded to a multiple of 2 seconds.
|
|
|
|
" We need to sleep to handle that, but that makes the test slow. First try
|
|
|
|
" without the sleep, and if it fails try again with the sleep.
|
|
|
|
if CheckFileTime(0) == 0
|
|
|
|
call CheckFileTime(1)
|
|
|
|
endif
|
2016-11-16 07:24:10 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_existent_directory()
|
2017-11-22 03:57:56 -07:00
|
|
|
let dname = '.'
|
2016-11-16 07:24:10 -07:00
|
|
|
|
|
|
|
call assert_equal(0, getfsize(dname))
|
|
|
|
call assert_equal('dir', getftype(dname))
|
2018-08-13 17:56:41 -07:00
|
|
|
call assert_equal(has('win32') ? 'rw-' : 'rwx', getfperm(dname)[0:2])
|
2016-11-16 07:24:10 -07:00
|
|
|
endfunc
|
|
|
|
|
2018-08-13 03:59:30 -07:00
|
|
|
func SleepForTimestamp()
|
|
|
|
" FAT has a granularity of 2 seconds, otherwise it's usually 1 second
|
|
|
|
if has('win32')
|
|
|
|
sleep 2
|
|
|
|
else
|
2024-04-05 17:33:07 -07:00
|
|
|
sleep 1
|
2018-08-13 03:59:30 -07:00
|
|
|
endif
|
|
|
|
endfunc
|
|
|
|
|
2016-11-16 07:24:10 -07:00
|
|
|
func Test_checktime()
|
2017-11-22 03:57:56 -07:00
|
|
|
let fname = 'Xtest.tmp'
|
2016-11-16 07:24:10 -07:00
|
|
|
|
2017-11-22 03:57:56 -07:00
|
|
|
let fl = ['Hello World!']
|
2024-07-16 18:24:52 -07:00
|
|
|
call writefile(fl, fname, 'D')
|
2016-11-16 07:24:10 -07:00
|
|
|
set autoread
|
|
|
|
exec 'e' fname
|
2018-08-13 03:59:30 -07:00
|
|
|
call SleepForTimestamp()
|
2017-11-22 03:57:56 -07:00
|
|
|
let fl = readfile(fname)
|
2016-11-16 07:24:10 -07:00
|
|
|
let fl[0] .= ' - checktime'
|
|
|
|
call writefile(fl, fname)
|
|
|
|
checktime
|
|
|
|
call assert_equal(fl[0], getline(1))
|
|
|
|
endfunc
|
|
|
|
|
2022-02-13 06:33:28 -07:00
|
|
|
func Test_checktime_fast()
|
|
|
|
CheckFeature nanotime
|
|
|
|
|
|
|
|
let fname = 'Xtest.tmp'
|
|
|
|
|
|
|
|
let fl = ['Hello World!']
|
2024-07-16 18:24:52 -07:00
|
|
|
call writefile(fl, fname, 'D')
|
2022-02-13 06:33:28 -07:00
|
|
|
set autoread
|
|
|
|
exec 'e' fname
|
|
|
|
let fl = readfile(fname)
|
|
|
|
let fl[0] .= ' - checktime'
|
|
|
|
sleep 10m " make test less flaky in Nvim
|
|
|
|
call writefile(fl, fname)
|
|
|
|
checktime
|
|
|
|
call assert_equal(fl[0], getline(1))
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_autoread_fast()
|
|
|
|
CheckFeature nanotime
|
|
|
|
|
2022-02-13 06:33:28 -07:00
|
|
|
" this is timing sensitive
|
|
|
|
let g:test_is_flaky = 1
|
|
|
|
|
2022-02-13 06:33:28 -07:00
|
|
|
new Xautoread
|
2022-02-13 06:33:28 -07:00
|
|
|
setlocal autoread
|
2022-02-13 06:33:28 -07:00
|
|
|
call setline(1, 'foo')
|
|
|
|
w!
|
2022-02-13 06:33:28 -07:00
|
|
|
sleep 10m
|
2024-07-16 18:24:52 -07:00
|
|
|
call writefile(['bar'], 'Xautoread', 'D')
|
2022-02-13 06:33:28 -07:00
|
|
|
sleep 10m
|
2022-02-13 06:33:28 -07:00
|
|
|
checktime
|
|
|
|
call assert_equal('bar', trim(getline(1)))
|
|
|
|
endfunc
|
|
|
|
|
2018-08-13 03:59:30 -07:00
|
|
|
func Test_autoread_file_deleted()
|
|
|
|
new Xautoread
|
|
|
|
set autoread
|
|
|
|
call setline(1, 'original')
|
|
|
|
w!
|
|
|
|
|
|
|
|
call SleepForTimestamp()
|
|
|
|
if has('win32')
|
|
|
|
silent !echo changed > Xautoread
|
|
|
|
else
|
|
|
|
silent !echo 'changed' > Xautoread
|
|
|
|
endif
|
|
|
|
checktime
|
|
|
|
call assert_equal('changed', trim(getline(1)))
|
|
|
|
|
|
|
|
call SleepForTimestamp()
|
|
|
|
messages clear
|
|
|
|
if has('win32')
|
|
|
|
silent !del Xautoread
|
|
|
|
else
|
|
|
|
silent !rm Xautoread
|
|
|
|
endif
|
|
|
|
checktime
|
|
|
|
call assert_match('E211:', execute('messages'))
|
|
|
|
call assert_equal('changed', trim(getline(1)))
|
|
|
|
|
|
|
|
call SleepForTimestamp()
|
|
|
|
if has('win32')
|
|
|
|
silent !echo recreated > Xautoread
|
|
|
|
else
|
|
|
|
silent !echo 'recreated' > Xautoread
|
|
|
|
endif
|
|
|
|
checktime
|
|
|
|
call assert_equal('recreated', trim(getline(1)))
|
|
|
|
|
|
|
|
call delete('Xautoread')
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
2016-11-16 07:24:10 -07:00
|
|
|
func Test_nonexistent_file()
|
2017-11-22 03:57:56 -07:00
|
|
|
let fname = 'Xtest.tmp'
|
2016-11-16 07:24:10 -07:00
|
|
|
|
|
|
|
call delete(fname)
|
|
|
|
call assert_equal(-1, getftime(fname))
|
|
|
|
call assert_equal(-1, getfsize(fname))
|
|
|
|
call assert_equal('', getftype(fname))
|
|
|
|
call assert_equal('', getfperm(fname))
|
|
|
|
endfunc
|
|
|
|
|
2018-08-13 13:01:56 -07:00
|
|
|
func Test_getftype()
|
|
|
|
call assert_equal('file', getftype(v:progpath))
|
|
|
|
call assert_equal('dir', getftype('.'))
|
|
|
|
|
|
|
|
if !has('unix')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
silent !ln -s Xfile Xlink
|
|
|
|
call assert_equal('link', getftype('Xlink'))
|
|
|
|
call delete('Xlink')
|
|
|
|
|
|
|
|
if executable('mkfifo')
|
|
|
|
silent !mkfifo Xfifo
|
|
|
|
call assert_equal('fifo', getftype('Xfifo'))
|
|
|
|
call delete('Xfifo')
|
|
|
|
endif
|
|
|
|
|
|
|
|
for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
|
2019-08-30 13:47:28 -07:00
|
|
|
" On Mac /def/fd/2 is found but the type is "fifo"
|
|
|
|
if cdevfile !~ '/dev/fd/'
|
|
|
|
let type = getftype(cdevfile)
|
|
|
|
" ignore empty result, can happen if the file disappeared
|
|
|
|
if type != ''
|
|
|
|
call assert_equal('cdev', type, 'for ' .. cdevfile)
|
|
|
|
endif
|
2018-11-17 20:37:31 -07:00
|
|
|
endif
|
2018-08-13 13:01:56 -07:00
|
|
|
endfor
|
|
|
|
|
|
|
|
for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
|
2018-11-17 20:37:31 -07:00
|
|
|
let type = getftype(bdevfile)
|
|
|
|
" ignore empty result, can happen if the file disappeared
|
|
|
|
if type != ''
|
2019-08-30 13:47:28 -07:00
|
|
|
call assert_equal('bdev', type, 'for ' .. bdevfile)
|
2018-11-17 20:37:31 -07:00
|
|
|
endif
|
2018-08-13 13:01:56 -07:00
|
|
|
endfor
|
|
|
|
|
|
|
|
" The /run/ directory typically contains socket files.
|
|
|
|
" If it does not, test won't fail but will not test socket files.
|
|
|
|
for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
|
2018-11-17 20:37:31 -07:00
|
|
|
let type = getftype(socketfile)
|
|
|
|
" ignore empty result, can happen if the file disappeared
|
|
|
|
if type != ''
|
2019-08-30 13:47:28 -07:00
|
|
|
call assert_equal('socket', type, 'for ' .. socketfile)
|
2018-11-17 20:37:31 -07:00
|
|
|
endif
|
2018-08-13 13:01:56 -07:00
|
|
|
endfor
|
|
|
|
|
|
|
|
" TODO: file type 'other' is not tested. How can we test it?
|
|
|
|
endfunc
|
|
|
|
|
2016-11-16 07:24:10 -07:00
|
|
|
func Test_win32_symlink_dir()
|
|
|
|
" On Windows, non-admin users cannot create symlinks.
|
|
|
|
" So we use an existing symlink for this test.
|
|
|
|
if has('win32')
|
|
|
|
" Check if 'C:\Users\All Users' is a symlink to a directory.
|
2017-11-22 03:57:56 -07:00
|
|
|
let res = system('dir C:\Users /a')
|
2016-11-16 07:24:10 -07:00
|
|
|
if match(res, '\C<SYMLINKD> *All Users') >= 0
|
|
|
|
" Get the filetype of the symlink.
|
2018-04-06 11:00:38 -07:00
|
|
|
call assert_equal('link', getftype('C:\Users\All Users'))
|
2016-11-16 07:24:10 -07:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunc
|