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_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 100 " this is timing sensitive 101 let g:test_is_flaky = 1 102 103 new Xautoread 104 setlocal autoread 105 call setline(1, 'foo') 106 w! 107 silent !echo bar > Xautoread 108 sleep 10m 109 checktime 110 call assert_equal('bar', trim(getline(1))) 111 112 call delete('Xautoread') 113endfunc 114 115func Test_autoread_file_deleted() 116 new Xautoread 117 set autoread 118 call setline(1, 'original') 119 w! 120 121 call SleepForTimestamp() 122 if has('win32') 123 silent !echo changed > Xautoread 124 else 125 silent !echo 'changed' > Xautoread 126 endif 127 checktime 128 call assert_equal('changed', trim(getline(1))) 129 130 call SleepForTimestamp() 131 messages clear 132 if has('win32') 133 silent !del Xautoread 134 else 135 silent !rm Xautoread 136 endif 137 checktime 138 call assert_match('E211:', execute('messages')) 139 call assert_equal('changed', trim(getline(1))) 140 141 call SleepForTimestamp() 142 if has('win32') 143 silent !echo recreated > Xautoread 144 else 145 silent !echo 'recreated' > Xautoread 146 endif 147 checktime 148 call assert_equal('recreated', trim(getline(1))) 149 150 call delete('Xautoread') 151 bwipe! 152endfunc 153 154 155func Test_nonexistent_file() 156 let fname = 'Xtest.tmp' 157 158 call delete(fname) 159 call assert_equal(-1, getftime(fname)) 160 call assert_equal(-1, getfsize(fname)) 161 call assert_equal('', getftype(fname)) 162 call assert_equal('', getfperm(fname)) 163endfunc 164 165func Test_getftype() 166 call assert_equal('file', getftype(v:progpath)) 167 call assert_equal('dir', getftype('.')) 168 169 if !has('unix') 170 return 171 endif 172 173 silent !ln -s Xfile Xlink 174 call assert_equal('link', getftype('Xlink')) 175 call delete('Xlink') 176 177 if executable('mkfifo') 178 silent !mkfifo Xfifo 179 call assert_equal('fifo', getftype('Xfifo')) 180 call delete('Xfifo') 181 endif 182 183 for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null') 184 " On Mac /def/fd/2 is found but the type is "fifo" 185 if cdevfile !~ '/dev/fd/' 186 let type = getftype(cdevfile) 187 " ignore empty result, can happen if the file disappeared 188 if type != '' 189 call assert_equal('cdev', type, 'for ' .. cdevfile) 190 endif 191 endif 192 endfor 193 194 for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null') 195 let type = getftype(bdevfile) 196 " ignore empty result, can happen if the file disappeared 197 if type != '' 198 call assert_equal('bdev', type, 'for ' .. bdevfile) 199 endif 200 endfor 201 202 " The /run/ directory typically contains socket files. 203 " If it does not, test won't fail but will not test socket files. 204 for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null') 205 let type = getftype(socketfile) 206 " ignore empty result, can happen if the file disappeared 207 if type != '' 208 call assert_equal('socket', type, 'for ' .. socketfile) 209 endif 210 endfor 211 212 " TODO: file type 'other' is not tested. How can we test it? 213endfunc 214 215func Test_win32_symlink_dir() 216 " On Windows, non-admin users cannot create symlinks. 217 " So we use an existing symlink for this test. 218 CheckMSWindows 219 " Check if 'C:\Users\All Users' is a symlink to a directory. 220 let res = system('dir C:\Users /a') 221 if match(res, '\C<SYMLINKD> *All Users') >= 0 222 " Get the filetype of the symlink. 223 call assert_equal('dir', getftype('C:\Users\All Users')) 224 else 225 throw 'Skipped: cannot find an existing symlink' 226 endif 227endfunc 228 229" vim: shiftwidth=2 sts=2 expandtab 230