2016-07-01 17:17:39 +02:00
|
|
|
" Tests for stat functions and checktime
|
|
|
|
|
|
2017-08-17 17:35:36 +02:00
|
|
|
func CheckFileTime(doSleep)
|
2017-02-01 22:05:28 +01:00
|
|
|
let fname = 'Xtest.tmp'
|
2017-08-17 17:35:36 +02:00
|
|
|
let result = 0
|
2016-07-01 17:17:39 +02:00
|
|
|
|
2017-02-01 22:05:28 +01:00
|
|
|
let ts = localtime()
|
2017-08-17 17:35:36 +02:00
|
|
|
if a:doSleep
|
|
|
|
|
sleep 1
|
|
|
|
|
endif
|
2017-02-01 22:05:28 +01:00
|
|
|
let fl = ['Hello World!']
|
2016-07-01 17:17:39 +02:00
|
|
|
call writefile(fl, fname)
|
2017-02-01 22:05:28 +01:00
|
|
|
let tf = getftime(fname)
|
2017-08-17 17:35:36 +02:00
|
|
|
if a:doSleep
|
|
|
|
|
sleep 1
|
|
|
|
|
endif
|
2017-02-01 22:05:28 +01:00
|
|
|
let te = localtime()
|
2016-07-01 17:17:39 +02:00
|
|
|
|
2017-08-17 17:35:36 +02:00
|
|
|
let time_correct = (ts <= tf && tf <= te)
|
|
|
|
|
if a:doSleep || time_correct
|
|
|
|
|
call assert_true(time_correct)
|
|
|
|
|
call assert_equal(strlen(fl[0] . "\n"), getfsize(fname))
|
|
|
|
|
call assert_equal('file', getftype(fname))
|
|
|
|
|
call assert_equal('rw-', getfperm(fname)[0:2])
|
|
|
|
|
let result = 1
|
|
|
|
|
endif
|
2017-02-01 22:05:28 +01:00
|
|
|
|
|
|
|
|
call delete(fname)
|
2017-08-17 17:35:36 +02: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-07-01 17:17:39 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func Test_existent_directory()
|
2017-02-01 22:05:28 +01:00
|
|
|
let dname = '.'
|
2016-07-01 17:17:39 +02:00
|
|
|
|
|
|
|
|
call assert_equal(0, getfsize(dname))
|
|
|
|
|
call assert_equal('dir', getftype(dname))
|
|
|
|
|
call assert_equal('rwx', getfperm(dname)[0:2])
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func Test_checktime()
|
2017-02-01 22:05:28 +01:00
|
|
|
let fname = 'Xtest.tmp'
|
2016-07-01 17:17:39 +02:00
|
|
|
|
2017-02-01 22:05:28 +01:00
|
|
|
let fl = ['Hello World!']
|
2016-07-01 17:17:39 +02:00
|
|
|
call writefile(fl, fname)
|
|
|
|
|
set autoread
|
|
|
|
|
exec 'e' fname
|
2017-02-01 22:05:28 +01:00
|
|
|
" FAT has a granularity of 2 seconds, otherwise it's usually 1 second
|
|
|
|
|
if has('win32')
|
|
|
|
|
sleep 2
|
|
|
|
|
else
|
|
|
|
|
sleep 1
|
|
|
|
|
endif
|
|
|
|
|
let fl = readfile(fname)
|
2016-07-01 17:17:39 +02:00
|
|
|
let fl[0] .= ' - checktime'
|
|
|
|
|
call writefile(fl, fname)
|
|
|
|
|
checktime
|
|
|
|
|
call assert_equal(fl[0], getline(1))
|
2017-02-01 22:05:28 +01:00
|
|
|
|
|
|
|
|
call delete(fname)
|
2016-07-01 17:17:39 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func Test_nonexistent_file()
|
2017-02-01 22:05:28 +01:00
|
|
|
let fname = 'Xtest.tmp'
|
2016-07-01 17:17:39 +02: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
|
|
|
|
|
|
|
|
|
|
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-02-01 22:05:28 +01:00
|
|
|
let res = system('dir C:\Users /a')
|
2016-07-01 17:17:39 +02:00
|
|
|
if match(res, '\C<SYMLINKD> *All Users') >= 0
|
|
|
|
|
" Get the filetype of the symlink.
|
|
|
|
|
call assert_equal('dir', getftype('C:\Users\All Users'))
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endfunc
|