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