xref: /vim-8.2.3635/src/testdir/test_stat.vim (revision 51ad4eaa)
1" Tests for stat functions and checktime
2
3func CheckFileTime(doSleep)
4  let fname = 'Xtest.tmp'
5  let result = 0
6
7  let ts = localtime()
8  if a:doSleep
9    sleep 1
10  endif
11  let fl = ['Hello World!']
12  call writefile(fl, fname)
13  let tf = getftime(fname)
14  if a:doSleep
15    sleep 1
16  endif
17  let te = localtime()
18
19  let time_correct = (ts <= tf && tf <= te)
20  if a:doSleep || time_correct
21    call assert_true(time_correct)
22    call assert_equal(strlen(fl[0] . "\n"), getfsize(fname))
23    call assert_equal('file', getftype(fname))
24    call assert_equal('rw-', getfperm(fname)[0:2])
25    let result = 1
26  endif
27
28  call delete(fname)
29  return result
30endfunc
31
32func Test_existent_file()
33  " On some systems the file timestamp is rounded to a multiple of 2 seconds.
34  " We need to sleep to handle that, but that makes the test slow.  First try
35  " without the sleep, and if it fails try again with the sleep.
36  if CheckFileTime(0) == 0
37    call CheckFileTime(1)
38  endif
39endfunc
40
41func Test_existent_directory()
42  let dname = '.'
43
44  call assert_equal(0, getfsize(dname))
45  call assert_equal('dir', getftype(dname))
46  call assert_equal('rwx', getfperm(dname)[0:2])
47endfunc
48
49func Test_checktime()
50  let fname = 'Xtest.tmp'
51
52  let fl = ['Hello World!']
53  call writefile(fl, fname)
54  set autoread
55  exec 'e' fname
56  " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
57  if has('win32')
58    sleep 2
59  else
60    sleep 1
61  endif
62  let fl = readfile(fname)
63  let fl[0] .= ' - checktime'
64  call writefile(fl, fname)
65  checktime
66  call assert_equal(fl[0], getline(1))
67
68  call delete(fname)
69endfunc
70
71func Test_nonexistent_file()
72  let fname = 'Xtest.tmp'
73
74  call delete(fname)
75  call assert_equal(-1, getftime(fname))
76  call assert_equal(-1, getfsize(fname))
77  call assert_equal('', getftype(fname))
78  call assert_equal('', getfperm(fname))
79endfunc
80
81func Test_win32_symlink_dir()
82  " On Windows, non-admin users cannot create symlinks.
83  " So we use an existing symlink for this test.
84  if has('win32')
85    " Check if 'C:\Users\All Users' is a symlink to a directory.
86    let res = system('dir C:\Users /a')
87    if match(res, '\C<SYMLINKD> *All Users') >= 0
88      " Get the filetype of the symlink.
89      call assert_equal('dir', getftype('C:\Users\All Users'))
90    endif
91  endif
92endfunc
93