xref: /vim-8.2.3635/src/testdir/test_stat.vim (revision 96f45c0b)
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, fname->getftime())
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"), fnames[0]->getfsize())
23    call assert_equal('file', fnames[0]->getftype())
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    " On Mac /def/fd/2 is found but the type is "fifo"
147    if cdevfile !~ '/dev/fd/'
148      let type = getftype(cdevfile)
149      " ignore empty result, can happen if the file disappeared
150      if type != ''
151	call assert_equal('cdev', type, 'for ' .. cdevfile)
152      endif
153    endif
154  endfor
155
156  for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
157    let type = getftype(bdevfile)
158    " ignore empty result, can happen if the file disappeared
159    if type != ''
160      call assert_equal('bdev', type, 'for ' .. bdevfile)
161    endif
162  endfor
163
164  " The /run/ directory typically contains socket files.
165  " If it does not, test won't fail but will not test socket files.
166  for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
167    let type = getftype(socketfile)
168    " ignore empty result, can happen if the file disappeared
169    if type != ''
170      call assert_equal('socket', type, 'for ' .. socketfile)
171    endif
172  endfor
173
174  " TODO: file type 'other' is not tested. How can we test it?
175endfunc
176
177func Test_win32_symlink_dir()
178  " On Windows, non-admin users cannot create symlinks.
179  " So we use an existing symlink for this test.
180  if has('win32')
181    " Check if 'C:\Users\All Users' is a symlink to a directory.
182    let res = system('dir C:\Users /a')
183    if match(res, '\C<SYMLINKD> *All Users') >= 0
184      " Get the filetype of the symlink.
185      call assert_equal('dir', getftype('C:\Users\All Users'))
186    endif
187  endif
188endfunc
189