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