test_stat.vim 5.4 KB

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