blob: 8ec2a42c2456492cbd5698f64391190268511831 [file] [log] [blame]
Bram Moolenaar8767f522016-07-01 17:17:39 +02001" Tests for stat functions and checktime
2
Bram Moolenaar82de3c22017-08-17 17:35:36 +02003func CheckFileTime(doSleep)
Bram Moolenaaraddc1562018-11-18 12:25:09 +01004 let fnames = ['Xtest1.tmp', 'Xtest2.tmp', 'Xtest3.tmp']
5 let times = []
Bram Moolenaar82de3c22017-08-17 17:35:36 +02006 let result = 0
Bram Moolenaar8767f522016-07-01 17:17:39 +02007
Bram Moolenaaraddc1562018-11-18 12:25:09 +01008 " Use three files istead of localtim(), with a network filesystem the file
9 " times may differ at bit
Bram Moolenaara2f28852017-02-01 22:05:28 +010010 let fl = ['Hello World!']
Bram Moolenaaraddc1562018-11-18 12:25:09 +010011 for fname in fnames
12 call writefile(fl, fname)
Bram Moolenaar4c313b12019-08-24 22:58:31 +020013 call add(times, fname->getftime())
Bram Moolenaaraddc1562018-11-18 12:25:09 +010014 if a:doSleep
15 sleep 1
16 endif
17 endfor
Bram Moolenaar8767f522016-07-01 17:17:39 +020018
Bram Moolenaaraddc1562018-11-18 12:25:09 +010019 let time_correct = (times[0] <= times[1] && times[1] <= times[2])
Bram Moolenaar82de3c22017-08-17 17:35:36 +020020 if a:doSleep || time_correct
Bram Moolenaaraddc1562018-11-18 12:25:09 +010021 call assert_true(time_correct, printf('Expected %s <= %s <= %s', times[0], times[1], times[2]))
Bram Moolenaar4c313b12019-08-24 22:58:31 +020022 call assert_equal(strlen(fl[0] . "\n"), fnames[0]->getfsize())
23 call assert_equal('file', fnames[0]->getftype())
Bram Moolenaaraddc1562018-11-18 12:25:09 +010024 call assert_equal('rw-', getfperm(fnames[0])[0:2])
Bram Moolenaar82de3c22017-08-17 17:35:36 +020025 let result = 1
26 endif
Bram Moolenaara2f28852017-02-01 22:05:28 +010027
Bram Moolenaaraddc1562018-11-18 12:25:09 +010028 for fname in fnames
29 call delete(fname)
30 endfor
Bram Moolenaar82de3c22017-08-17 17:35:36 +020031 return result
32endfunc
33
34func Test_existent_file()
35 " On some systems the file timestamp is rounded to a multiple of 2 seconds.
36 " We need to sleep to handle that, but that makes the test slow. First try
37 " without the sleep, and if it fails try again with the sleep.
38 if CheckFileTime(0) == 0
39 call CheckFileTime(1)
40 endif
Bram Moolenaar8767f522016-07-01 17:17:39 +020041endfunc
42
43func Test_existent_directory()
Bram Moolenaara2f28852017-02-01 22:05:28 +010044 let dname = '.'
Bram Moolenaar8767f522016-07-01 17:17:39 +020045
46 call assert_equal(0, getfsize(dname))
47 call assert_equal('dir', getftype(dname))
48 call assert_equal('rwx', getfperm(dname)[0:2])
49endfunc
50
Bram Moolenaar386bc822018-07-07 18:34:12 +020051func SleepForTimestamp()
52 " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
53 if has('win32')
54 sleep 2
55 else
56 sleep 1
57 endif
58endfunc
59
Bram Moolenaar8767f522016-07-01 17:17:39 +020060func Test_checktime()
Bram Moolenaara2f28852017-02-01 22:05:28 +010061 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020062
Bram Moolenaara2f28852017-02-01 22:05:28 +010063 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020064 call writefile(fl, fname)
65 set autoread
66 exec 'e' fname
Bram Moolenaar386bc822018-07-07 18:34:12 +020067 call SleepForTimestamp()
Bram Moolenaara2f28852017-02-01 22:05:28 +010068 let fl = readfile(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020069 let fl[0] .= ' - checktime'
70 call writefile(fl, fname)
71 checktime
72 call assert_equal(fl[0], getline(1))
Bram Moolenaara2f28852017-02-01 22:05:28 +010073
74 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020075endfunc
76
Bram Moolenaar386bc822018-07-07 18:34:12 +020077func Test_autoread_file_deleted()
78 new Xautoread
79 set autoread
80 call setline(1, 'original')
81 w!
82
83 call SleepForTimestamp()
84 if has('win32')
85 silent !echo changed > Xautoread
86 else
87 silent !echo 'changed' > Xautoread
88 endif
89 checktime
90 call assert_equal('changed', trim(getline(1)))
91
92 call SleepForTimestamp()
93 messages clear
94 if has('win32')
95 silent !del Xautoread
96 else
97 silent !rm Xautoread
98 endif
99 checktime
100 call assert_match('E211:', execute('messages'))
101 call assert_equal('changed', trim(getline(1)))
102
103 call SleepForTimestamp()
104 if has('win32')
105 silent !echo recreated > Xautoread
106 else
107 silent !echo 'recreated' > Xautoread
108 endif
109 checktime
110 call assert_equal('recreated', trim(getline(1)))
111
112 call delete('Xautoread')
113 bwipe!
114endfunc
115
116
Bram Moolenaar8767f522016-07-01 17:17:39 +0200117func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +0100118 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200119
120 call delete(fname)
121 call assert_equal(-1, getftime(fname))
122 call assert_equal(-1, getfsize(fname))
123 call assert_equal('', getftype(fname))
124 call assert_equal('', getfperm(fname))
125endfunc
126
Bram Moolenaar1598f992018-08-09 22:08:57 +0200127func Test_getftype()
128 call assert_equal('file', getftype(v:progpath))
129 call assert_equal('dir', getftype('.'))
130
131 if !has('unix')
132 return
133 endif
134
135 silent !ln -s Xfile Xlink
136 call assert_equal('link', getftype('Xlink'))
137 call delete('Xlink')
138
139 if executable('mkfifo')
140 silent !mkfifo Xfifo
141 call assert_equal('fifo', getftype('Xfifo'))
142 call delete('Xfifo')
143 endif
144
145 for cdevfile in systemlist('find /dev -type c -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200146 let type = getftype(cdevfile)
147 " ignore empty result, can happen if the file disappeared
148 if type != ''
149 call assert_equal('cdev', type)
150 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200151 endfor
152
153 for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200154 let type = getftype(bdevfile)
155 " ignore empty result, can happen if the file disappeared
156 if type != ''
157 call assert_equal('bdev', type)
158 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200159 endfor
160
161 " The /run/ directory typically contains socket files.
162 " If it does not, test won't fail but will not test socket files.
163 for socketfile in systemlist('find /run -type s -maxdepth 2 2>/dev/null')
Bram Moolenaar3b3a5062018-08-22 20:16:16 +0200164 let type = getftype(socketfile)
165 " ignore empty result, can happen if the file disappeared
166 if type != ''
167 call assert_equal('socket', type)
168 endif
Bram Moolenaar1598f992018-08-09 22:08:57 +0200169 endfor
170
171 " TODO: file type 'other' is not tested. How can we test it?
172endfunc
173
Bram Moolenaar8767f522016-07-01 17:17:39 +0200174func Test_win32_symlink_dir()
175 " On Windows, non-admin users cannot create symlinks.
176 " So we use an existing symlink for this test.
177 if has('win32')
178 " Check if 'C:\Users\All Users' is a symlink to a directory.
Bram Moolenaara2f28852017-02-01 22:05:28 +0100179 let res = system('dir C:\Users /a')
Bram Moolenaar8767f522016-07-01 17:17:39 +0200180 if match(res, '\C<SYMLINKD> *All Users') >= 0
181 " Get the filetype of the symlink.
182 call assert_equal('dir', getftype('C:\Users\All Users'))
183 endif
184 endif
185endfunc