blob: e801bfc5cea04e8f47d5a4e92d0947fab8bd76c4 [file] [log] [blame]
Bram Moolenaar8767f522016-07-01 17:17:39 +02001" Tests for stat functions and checktime
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
4
Bram Moolenaar82de3c22017-08-17 17:35:36 +02005func CheckFileTime(doSleep)
Bram Moolenaaraddc1562018-11-18 12:25:09 +01006 let fnames = ['Xtest1.tmp', 'Xtest2.tmp', 'Xtest3.tmp']
7 let times = []
Bram Moolenaar82de3c22017-08-17 17:35:36 +02008 let result = 0
Bram Moolenaar8767f522016-07-01 17:17:39 +02009
Bram Moolenaar4b96df52020-01-26 22:00:26 +010010 " Use three files instead of localtim(), with a network filesystem the file
Bram Moolenaaraddc1562018-11-18 12:25:09 +010011 " times may differ at bit
Bram Moolenaara2f28852017-02-01 22:05:28 +010012 let fl = ['Hello World!']
Bram Moolenaaraddc1562018-11-18 12:25:09 +010013 for fname in fnames
14 call writefile(fl, fname)
Bram Moolenaar4c313b12019-08-24 22:58:31 +020015 call add(times, fname->getftime())
Bram Moolenaaraddc1562018-11-18 12:25:09 +010016 if a:doSleep
17 sleep 1
18 endif
19 endfor
Bram Moolenaar8767f522016-07-01 17:17:39 +020020
Bram Moolenaaraddc1562018-11-18 12:25:09 +010021 let time_correct = (times[0] <= times[1] && times[1] <= times[2])
Bram Moolenaar82de3c22017-08-17 17:35:36 +020022 if a:doSleep || time_correct
Bram Moolenaaraddc1562018-11-18 12:25:09 +010023 call assert_true(time_correct, printf('Expected %s <= %s <= %s', times[0], times[1], times[2]))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020024 call assert_equal(strlen(fl[0] . "\n"), fnames[0]->getfsize())
25 call assert_equal('file', fnames[0]->getftype())
Bram Moolenaaraddc1562018-11-18 12:25:09 +010026 call assert_equal('rw-', getfperm(fnames[0])[0:2])
Bram Moolenaar82de3c22017-08-17 17:35:36 +020027 let result = 1
28 endif
Bram Moolenaara2f28852017-02-01 22:05:28 +010029
Bram Moolenaaraddc1562018-11-18 12:25:09 +010030 for fname in fnames
31 call delete(fname)
32 endfor
Bram Moolenaar82de3c22017-08-17 17:35:36 +020033 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
Bram Moolenaar8767f522016-07-01 17:17:39 +020043endfunc
44
45func Test_existent_directory()
Bram Moolenaara2f28852017-02-01 22:05:28 +010046 let dname = '.'
Bram Moolenaar8767f522016-07-01 17:17:39 +020047
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
Bram Moolenaar386bc822018-07-07 18:34:12 +020053func 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
Bram Moolenaar8767f522016-07-01 17:17:39 +020062func Test_checktime()
Bram Moolenaara2f28852017-02-01 22:05:28 +010063 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020064
Bram Moolenaara2f28852017-02-01 22:05:28 +010065 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020066 call writefile(fl, fname)
67 set autoread
68 exec 'e' fname
Bram Moolenaar386bc822018-07-07 18:34:12 +020069 call SleepForTimestamp()
Bram Moolenaara2f28852017-02-01 22:05:28 +010070 let fl = readfile(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020071 let fl[0] .= ' - checktime'
72 call writefile(fl, fname)
73 checktime
74 call assert_equal(fl[0], getline(1))
Bram Moolenaara2f28852017-02-01 22:05:28 +010075
76 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020077endfunc
78
Leah Neukirchen0a7984a2021-10-14 21:27:55 +010079func Test_checktime_fast()
80 CheckFeature nanotime
81
82 let fname = 'Xtest.tmp'
83
84 let fl = ['Hello World!']
85 call writefile(fl, fname)
86 set autoread
87 exec 'e' fname
88 let fl = readfile(fname)
89 let fl[0] .= ' - checktime'
90 call writefile(fl, fname)
91 checktime
92 call assert_equal(fl[0], getline(1))
93
94 call delete(fname)
95endfunc
96
97func Test_autoread_fast()
98 CheckFeature nanotime
99
Bram Moolenaareaa006d2021-10-15 17:09:50 +0100100 " this is timing sensitive
101 let g:test_is_flaky = 1
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100102
Bram Moolenaareaa006d2021-10-15 17:09:50 +0100103 new Xautoread
104 setlocal autoread
105 call setline(1, 'foo')
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100106 w!
Bram Moolenaar944eeb42021-10-18 14:37:13 +0100107 sleep 10m
108 call writefile(['bar'], 'Xautoread')
Bram Moolenaaraccf4ed2021-10-15 00:38:02 +0100109 sleep 10m
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100110 checktime
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100111 call assert_equal('bar', trim(getline(1)))
Bram Moolenaareaa006d2021-10-15 17:09:50 +0100112
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100113 call delete('Xautoread')
114endfunc
115
Bram Moolenaar386bc822018-07-07 18:34:12 +0200116func Test_autoread_file_deleted()
117 new Xautoread
118 set autoread
119 call setline(1, 'original')
120 w!
121
122 call SleepForTimestamp()
123 if has('win32')
124 silent !echo changed > Xautoread
125 else
126 silent !echo 'changed' > Xautoread
127 endif
128 checktime
129 call assert_equal('changed', trim(getline(1)))
130
131 call SleepForTimestamp()
132 messages clear
133 if has('win32')
134 silent !del Xautoread
135 else
136 silent !rm Xautoread
137 endif
138 checktime
139 call assert_match('E211:', execute('messages'))
140 call assert_equal('changed', trim(getline(1)))
141
142 call SleepForTimestamp()
143 if has('win32')
144 silent !echo recreated > Xautoread
145 else
146 silent !echo 'recreated' > Xautoread
147 endif
148 checktime
149 call assert_equal('recreated', trim(getline(1)))
150
151 call delete('Xautoread')
152 bwipe!
153endfunc
154
155
Bram Moolenaar8767f522016-07-01 17:17:39 +0200156func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +0100157 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200158
159 call delete(fname)
160 call assert_equal(-1, getftime(fname))
161 call assert_equal(-1, getfsize(fname))
162 call assert_equal('', getftype(fname))
163 call assert_equal('', getfperm(fname))
164endfunc
165
Bram Moolenaar1598f992018-08-09 22:08:57 +0200166func Test_getftype()
167 call assert_equal('file', getftype(v:progpath))
168 call assert_equal('dir', getftype('.'))
169
170 if !has('unix')
171 return
172 endif
173
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100174 silent !ln -s Xlinkfile Xlink
Bram Moolenaar1598f992018-08-09 22:08:57 +0200175 call assert_equal('link', getftype('Xlink'))
176 call delete('Xlink')
177
178 if executable('mkfifo')
179 silent !mkfifo Xfifo
180 call assert_equal('fifo', getftype('Xfifo'))
181 call delete('Xfifo')
182 endif
183
184 for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
Bram Moolenaarad5db442019-08-30 13:12:25 +0200185 " On Mac /def/fd/2 is found but the type is "fifo"
186 if cdevfile !~ '/dev/fd/'
187 let type = getftype(cdevfile)
188 " ignore empty result, can happen if the file disappeared
189 if type != ''
190 call assert_equal('cdev', type, 'for ' .. cdevfile)
191 endif
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200192 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200193 endfor
194
195 for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200196 let type = getftype(bdevfile)
197 " ignore empty result, can happen if the file disappeared
198 if type != ''
Bram Moolenaarad5db442019-08-30 13:12:25 +0200199 call assert_equal('bdev', type, 'for ' .. bdevfile)
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200200 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200201 endfor
202
203 " The /run/ directory typically contains socket files.
204 " If it does not, test won't fail but will not test socket files.
205 for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200206 let type = getftype(socketfile)
207 " ignore empty result, can happen if the file disappeared
208 if type != ''
Bram Moolenaarad5db442019-08-30 13:12:25 +0200209 call assert_equal('socket', type, 'for ' .. socketfile)
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200210 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200211 endfor
212
213 " TODO: file type 'other' is not tested. How can we test it?
214endfunc
215
Bram Moolenaar8767f522016-07-01 17:17:39 +0200216func Test_win32_symlink_dir()
217 " On Windows, non-admin users cannot create symlinks.
218 " So we use an existing symlink for this test.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200219 CheckMSWindows
220 " Check if 'C:\Users\All Users' is a symlink to a directory.
221 let res = system('dir C:\Users /a')
222 if match(res, '\C<SYMLINKD> *All Users') >= 0
223 " Get the filetype of the symlink.
224 call assert_equal('dir', getftype('C:\Users\All Users'))
225 else
226 throw 'Skipped: cannot find an existing symlink'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200227 endif
228endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200229
230" vim: shiftwidth=2 sts=2 expandtab