xref: /vim-8.2.3635/src/testdir/test_stat.vim (revision fc65cabb)
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 SleepForTimestamp()
50  " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
51  if has('win32')
52    sleep 2
53  else
54    sleep 1
55  endif
56endfunc
57
58func Test_checktime()
59  let fname = 'Xtest.tmp'
60
61  let fl = ['Hello World!']
62  call writefile(fl, fname)
63  set autoread
64  exec 'e' fname
65  call SleepForTimestamp()
66  let fl = readfile(fname)
67  let fl[0] .= ' - checktime'
68  call writefile(fl, fname)
69  checktime
70  call assert_equal(fl[0], getline(1))
71
72  call delete(fname)
73endfunc
74
75func Test_autoread_file_deleted()
76  new Xautoread
77  set autoread
78  call setline(1, 'original')
79  w!
80
81  call SleepForTimestamp()
82  if has('win32')
83    silent !echo changed > Xautoread
84  else
85    silent !echo 'changed' > Xautoread
86  endif
87  checktime
88  call assert_equal('changed', trim(getline(1)))
89
90  call SleepForTimestamp()
91  messages clear
92  if has('win32')
93    silent !del Xautoread
94  else
95    silent !rm Xautoread
96  endif
97  checktime
98  call assert_match('E211:', execute('messages'))
99  call assert_equal('changed', trim(getline(1)))
100
101  call SleepForTimestamp()
102  if has('win32')
103    silent !echo recreated > Xautoread
104  else
105    silent !echo 'recreated' > Xautoread
106  endif
107  checktime
108  call assert_equal('recreated', trim(getline(1)))
109
110  call delete('Xautoread')
111  bwipe!
112endfunc
113
114
115func Test_nonexistent_file()
116  let fname = 'Xtest.tmp'
117
118  call delete(fname)
119  call assert_equal(-1, getftime(fname))
120  call assert_equal(-1, getfsize(fname))
121  call assert_equal('', getftype(fname))
122  call assert_equal('', getfperm(fname))
123endfunc
124
125func Test_getftype()
126  call assert_equal('file', getftype(v:progpath))
127  call assert_equal('dir',  getftype('.'))
128
129  if !has('unix')
130    return
131  endif
132
133  silent !ln -s Xfile Xlink
134  call assert_equal('link', getftype('Xlink'))
135  call delete('Xlink')
136
137  if executable('mkfifo')
138    silent !mkfifo Xfifo
139    call assert_equal('fifo', getftype('Xfifo'))
140    call delete('Xfifo')
141  endif
142
143  for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
144    let type = getftype(cdevfile)
145    " ignore empty result, can happen if the file disappeared
146    if type != ''
147      call assert_equal('cdev', type)
148    endif
149  endfor
150
151  for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
152    let type = getftype(bdevfile)
153    " ignore empty result, can happen if the file disappeared
154    if type != ''
155      call assert_equal('bdev', type)
156    endif
157  endfor
158
159  " The /run/ directory typically contains socket files.
160  " If it does not, test won't fail but will not test socket files.
161  for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
162    let type = getftype(socketfile)
163    " ignore empty result, can happen if the file disappeared
164    if type != ''
165      call assert_equal('socket', type)
166    endif
167  endfor
168
169  " TODO: file type 'other' is not tested. How can we test it?
170endfunc
171
172func Test_win32_symlink_dir()
173  " On Windows, non-admin users cannot create symlinks.
174  " So we use an existing symlink for this test.
175  if has('win32')
176    " Check if 'C:\Users\All Users' is a symlink to a directory.
177    let res = system('dir C:\Users /a')
178    if match(res, '\C<SYMLINKD> *All Users') >= 0
179      " Get the filetype of the symlink.
180      call assert_equal('dir', getftype('C:\Users\All Users'))
181    endif
182  endif
183endfunc
184