blob: de5fac46fbc7ff7b51f2a59916b81808b74d2a61 [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 Moolenaara2f28852017-02-01 22:05:28 +01004 let fname = 'Xtest.tmp'
Bram Moolenaar82de3c22017-08-17 17:35:36 +02005 let result = 0
Bram Moolenaar8767f522016-07-01 17:17:39 +02006
Bram Moolenaara2f28852017-02-01 22:05:28 +01007 let ts = localtime()
Bram Moolenaar82de3c22017-08-17 17:35:36 +02008 if a:doSleep
9 sleep 1
10 endif
Bram Moolenaara2f28852017-02-01 22:05:28 +010011 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020012 call writefile(fl, fname)
Bram Moolenaara2f28852017-02-01 22:05:28 +010013 let tf = getftime(fname)
Bram Moolenaar82de3c22017-08-17 17:35:36 +020014 if a:doSleep
15 sleep 1
16 endif
Bram Moolenaara2f28852017-02-01 22:05:28 +010017 let te = localtime()
Bram Moolenaar8767f522016-07-01 17:17:39 +020018
Bram Moolenaar82de3c22017-08-17 17:35:36 +020019 let time_correct = (ts <= tf && tf <= te)
20 if a:doSleep || time_correct
21 call assert_true(time_correct)
22 call assert_equal(strlen(fl[0] . "\n"), getfsize(fname))
23 call assert_equal('file', getftype(fname))
24 call assert_equal('rw-', getfperm(fname)[0:2])
25 let result = 1
26 endif
Bram Moolenaara2f28852017-02-01 22:05:28 +010027
28 call delete(fname)
Bram Moolenaar82de3c22017-08-17 17:35:36 +020029 return result
30endfunc
31
32func Test_existent_file()
33 " On some systems the file timestamp is rounded to a multiple of 2 seconds.
34 " We need to sleep to handle that, but that makes the test slow. First try
35 " without the sleep, and if it fails try again with the sleep.
36 if CheckFileTime(0) == 0
37 call CheckFileTime(1)
38 endif
Bram Moolenaar8767f522016-07-01 17:17:39 +020039endfunc
40
41func Test_existent_directory()
Bram Moolenaara2f28852017-02-01 22:05:28 +010042 let dname = '.'
Bram Moolenaar8767f522016-07-01 17:17:39 +020043
44 call assert_equal(0, getfsize(dname))
45 call assert_equal('dir', getftype(dname))
46 call assert_equal('rwx', getfperm(dname)[0:2])
47endfunc
48
49func Test_checktime()
Bram Moolenaara2f28852017-02-01 22:05:28 +010050 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020051
Bram Moolenaara2f28852017-02-01 22:05:28 +010052 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020053 call writefile(fl, fname)
54 set autoread
55 exec 'e' fname
Bram Moolenaara2f28852017-02-01 22:05:28 +010056 " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
57 if has('win32')
58 sleep 2
59 else
60 sleep 1
61 endif
62 let fl = readfile(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020063 let fl[0] .= ' - checktime'
64 call writefile(fl, fname)
65 checktime
66 call assert_equal(fl[0], getline(1))
Bram Moolenaara2f28852017-02-01 22:05:28 +010067
68 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020069endfunc
70
71func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +010072 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020073
74 call delete(fname)
75 call assert_equal(-1, getftime(fname))
76 call assert_equal(-1, getfsize(fname))
77 call assert_equal('', getftype(fname))
78 call assert_equal('', getfperm(fname))
79endfunc
80
81func Test_win32_symlink_dir()
82 " On Windows, non-admin users cannot create symlinks.
83 " So we use an existing symlink for this test.
84 if has('win32')
85 " Check if 'C:\Users\All Users' is a symlink to a directory.
Bram Moolenaara2f28852017-02-01 22:05:28 +010086 let res = system('dir C:\Users /a')
Bram Moolenaar8767f522016-07-01 17:17:39 +020087 if match(res, '\C<SYMLINKD> *All Users') >= 0
88 " Get the filetype of the symlink.
89 call assert_equal('dir', getftype('C:\Users\All Users'))
90 endif
91 endif
92endfunc