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