summaryrefslogtreecommitdiff
path: root/src/testdir/test_stat.vim
blob: 307cf5e6402a197defd33440bf3ca094458a555b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
" Tests for stat functions and checktime

func CheckFileTime(doSleep)
  let fname = 'Xtest.tmp'
  let result = 0

  let ts = localtime()
  if a:doSleep
    sleep 1
  endif
  let fl = ['Hello World!']
  call writefile(fl, fname)
  let tf = getftime(fname)
  if a:doSleep
    sleep 1
  endif
  let te = localtime()

  let time_correct = (ts <= tf && tf <= te)
  if a:doSleep || time_correct
    call assert_true(time_correct)
    call assert_equal(strlen(fl[0] . "\n"), getfsize(fname))
    call assert_equal('file', getftype(fname))
    call assert_equal('rw-', getfperm(fname)[0:2])
    let result = 1
  endif

  call delete(fname)
  return result
endfunc

func Test_existent_file()
  " On some systems the file timestamp is rounded to a multiple of 2 seconds.
  " We need to sleep to handle that, but that makes the test slow.  First try
  " without the sleep, and if it fails try again with the sleep.
  if CheckFileTime(0) == 0
    call CheckFileTime(1)
  endif
endfunc

func Test_existent_directory()
  let dname = '.'

  call assert_equal(0, getfsize(dname))
  call assert_equal('dir', getftype(dname))
  call assert_equal('rwx', getfperm(dname)[0:2])
endfunc

func SleepForTimestamp()
  " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
  if has('win32')
    sleep 2
  else
    sleep 1
  endif
endfunc

func Test_checktime()
  let fname = 'Xtest.tmp'

  let fl = ['Hello World!']
  call writefile(fl, fname)
  set autoread
  exec 'e' fname
  call SleepForTimestamp()
  let fl = readfile(fname)
  let fl[0] .= ' - checktime'
  call writefile(fl, fname)
  checktime
  call assert_equal(fl[0], getline(1))

  call delete(fname)
endfunc

func Test_autoread_file_deleted()
  new Xautoread
  set autoread
  call setline(1, 'original')
  w!

  call SleepForTimestamp()
  if has('win32')
    silent !echo changed > Xautoread
  else
    silent !echo 'changed' > Xautoread
  endif
  checktime
  call assert_equal('changed', trim(getline(1)))

  call SleepForTimestamp()
  messages clear
  if has('win32')
    silent !del Xautoread
  else
    silent !rm Xautoread
  endif
  checktime
  call assert_match('E211:', execute('messages'))
  call assert_equal('changed', trim(getline(1)))

  call SleepForTimestamp()
  if has('win32')
    silent !echo recreated > Xautoread
  else
    silent !echo 'recreated' > Xautoread
  endif
  checktime
  call assert_equal('recreated', trim(getline(1)))

  call delete('Xautoread')
  bwipe!
endfunc


func Test_nonexistent_file()
  let fname = 'Xtest.tmp'

  call delete(fname)
  call assert_equal(-1, getftime(fname))
  call assert_equal(-1, getfsize(fname))
  call assert_equal('', getftype(fname))
  call assert_equal('', getfperm(fname))
endfunc

func Test_win32_symlink_dir()
  " On Windows, non-admin users cannot create symlinks.
  " So we use an existing symlink for this test.
  if has('win32')
    " Check if 'C:\Users\All Users' is a symlink to a directory.
    let res = system('dir C:\Users /a')
    if match(res, '\C<SYMLINKD> *All Users') >= 0
      " Get the filetype of the symlink.
      call assert_equal('dir', getftype('C:\Users\All Users'))
    endif
  endif
endfunc