blob: ed123e4347183a453eded1aca970bc984e1ccf81 [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 Moolenaar56564962022-10-10 22:39:42 +010066 call writefile(fl, fname, 'D')
Bram Moolenaar8767f522016-07-01 17:17:39 +020067 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))
75endfunc
76
Leah Neukirchen0a7984a2021-10-14 21:27:55 +010077func Test_checktime_fast()
78 CheckFeature nanotime
79
80 let fname = 'Xtest.tmp'
81
82 let fl = ['Hello World!']
Bram Moolenaar56564962022-10-10 22:39:42 +010083 call writefile(fl, fname, 'D')
Leah Neukirchen0a7984a2021-10-14 21:27:55 +010084 set autoread
85 exec 'e' fname
86 let fl = readfile(fname)
87 let fl[0] .= ' - checktime'
88 call writefile(fl, fname)
89 checktime
90 call assert_equal(fl[0], getline(1))
Leah Neukirchen0a7984a2021-10-14 21:27:55 +010091endfunc
92
93func Test_autoread_fast()
94 CheckFeature nanotime
95
Bram Moolenaareaa006d2021-10-15 17:09:50 +010096 " this is timing sensitive
97 let g:test_is_flaky = 1
Leah Neukirchen0a7984a2021-10-14 21:27:55 +010098
Bram Moolenaareaa006d2021-10-15 17:09:50 +010099 new Xautoread
100 setlocal autoread
101 call setline(1, 'foo')
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100102 w!
Bram Moolenaar944eeb42021-10-18 14:37:13 +0100103 sleep 10m
Bram Moolenaar56564962022-10-10 22:39:42 +0100104 call writefile(['bar'], 'Xautoread', 'D')
Bram Moolenaaraccf4ed2021-10-15 00:38:02 +0100105 sleep 10m
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100106 checktime
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100107 call assert_equal('bar', trim(getline(1)))
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100108endfunc
109
Bram Moolenaar386bc822018-07-07 18:34:12 +0200110func Test_autoread_file_deleted()
111 new Xautoread
112 set autoread
113 call setline(1, 'original')
114 w!
115
116 call SleepForTimestamp()
117 if has('win32')
118 silent !echo changed > Xautoread
119 else
120 silent !echo 'changed' > Xautoread
121 endif
122 checktime
123 call assert_equal('changed', trim(getline(1)))
124
125 call SleepForTimestamp()
126 messages clear
127 if has('win32')
128 silent !del Xautoread
129 else
130 silent !rm Xautoread
131 endif
132 checktime
133 call assert_match('E211:', execute('messages'))
134 call assert_equal('changed', trim(getline(1)))
135
136 call SleepForTimestamp()
137 if has('win32')
138 silent !echo recreated > Xautoread
139 else
140 silent !echo 'recreated' > Xautoread
141 endif
142 checktime
143 call assert_equal('recreated', trim(getline(1)))
144
145 call delete('Xautoread')
146 bwipe!
147endfunc
148
149
Bram Moolenaar8767f522016-07-01 17:17:39 +0200150func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +0100151 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200152
153 call delete(fname)
154 call assert_equal(-1, getftime(fname))
155 call assert_equal(-1, getfsize(fname))
156 call assert_equal('', getftype(fname))
157 call assert_equal('', getfperm(fname))
158endfunc
159
Bram Moolenaar1598f992018-08-09 22:08:57 +0200160func Test_getftype()
161 call assert_equal('file', getftype(v:progpath))
162 call assert_equal('dir', getftype('.'))
163
164 if !has('unix')
165 return
166 endif
167
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100168 silent !ln -s Xlinkfile Xlink
Bram Moolenaar1598f992018-08-09 22:08:57 +0200169 call assert_equal('link', getftype('Xlink'))
170 call delete('Xlink')
171
172 if executable('mkfifo')
173 silent !mkfifo Xfifo
174 call assert_equal('fifo', getftype('Xfifo'))
175 call delete('Xfifo')
176 endif
177
178 for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
Bram Moolenaarad5db442019-08-30 13:12:25 +0200179 " On Mac /def/fd/2 is found but the type is "fifo"
180 if cdevfile !~ '/dev/fd/'
181 let type = getftype(cdevfile)
182 " ignore empty result, can happen if the file disappeared
183 if type != ''
184 call assert_equal('cdev', type, 'for ' .. cdevfile)
185 endif
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200186 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200187 endfor
188
189 for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200190 let type = getftype(bdevfile)
191 " ignore empty result, can happen if the file disappeared
192 if type != ''
Bram Moolenaarad5db442019-08-30 13:12:25 +0200193 call assert_equal('bdev', type, 'for ' .. bdevfile)
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200194 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200195 endfor
196
197 " The /run/ directory typically contains socket files.
198 " If it does not, test won't fail but will not test socket files.
199 for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200200 let type = getftype(socketfile)
201 " ignore empty result, can happen if the file disappeared
202 if type != ''
Bram Moolenaarad5db442019-08-30 13:12:25 +0200203 call assert_equal('socket', type, 'for ' .. socketfile)
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200204 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200205 endfor
206
207 " TODO: file type 'other' is not tested. How can we test it?
208endfunc
209
Bram Moolenaar8767f522016-07-01 17:17:39 +0200210func Test_win32_symlink_dir()
211 " On Windows, non-admin users cannot create symlinks.
212 " So we use an existing symlink for this test.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200213 CheckMSWindows
214 " Check if 'C:\Users\All Users' is a symlink to a directory.
215 let res = system('dir C:\Users /a')
216 if match(res, '\C<SYMLINKD> *All Users') >= 0
217 " Get the filetype of the symlink.
218 call assert_equal('dir', getftype('C:\Users\All Users'))
219 else
220 throw 'Skipped: cannot find an existing symlink'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200221 endif
222endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200223
224" vim: shiftwidth=2 sts=2 expandtab