blob: 307cf5e6402a197defd33440bf3ca094458a555b [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
Bram Moolenaar386bc822018-07-07 18:34:12 +020049func SleepForTimestamp()
50 " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
51 if has('win32')
52 sleep 2
53 else
54 sleep 1
55 endif
56endfunc
57
Bram Moolenaar8767f522016-07-01 17:17:39 +020058func Test_checktime()
Bram Moolenaara2f28852017-02-01 22:05:28 +010059 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020060
Bram Moolenaara2f28852017-02-01 22:05:28 +010061 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020062 call writefile(fl, fname)
63 set autoread
64 exec 'e' fname
Bram Moolenaar386bc822018-07-07 18:34:12 +020065 call SleepForTimestamp()
Bram Moolenaara2f28852017-02-01 22:05:28 +010066 let fl = readfile(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020067 let fl[0] .= ' - checktime'
68 call writefile(fl, fname)
69 checktime
70 call assert_equal(fl[0], getline(1))
Bram Moolenaara2f28852017-02-01 22:05:28 +010071
72 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020073endfunc
74
Bram Moolenaar386bc822018-07-07 18:34:12 +020075func Test_autoread_file_deleted()
76 new Xautoread
77 set autoread
78 call setline(1, 'original')
79 w!
80
81 call SleepForTimestamp()
82 if has('win32')
83 silent !echo changed > Xautoread
84 else
85 silent !echo 'changed' > Xautoread
86 endif
87 checktime
88 call assert_equal('changed', trim(getline(1)))
89
90 call SleepForTimestamp()
91 messages clear
92 if has('win32')
93 silent !del Xautoread
94 else
95 silent !rm Xautoread
96 endif
97 checktime
98 call assert_match('E211:', execute('messages'))
99 call assert_equal('changed', trim(getline(1)))
100
101 call SleepForTimestamp()
102 if has('win32')
103 silent !echo recreated > Xautoread
104 else
105 silent !echo 'recreated' > Xautoread
106 endif
107 checktime
108 call assert_equal('recreated', trim(getline(1)))
109
110 call delete('Xautoread')
111 bwipe!
112endfunc
113
114
Bram Moolenaar8767f522016-07-01 17:17:39 +0200115func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +0100116 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +0200117
118 call delete(fname)
119 call assert_equal(-1, getftime(fname))
120 call assert_equal(-1, getfsize(fname))
121 call assert_equal('', getftype(fname))
122 call assert_equal('', getfperm(fname))
123endfunc
124
125func Test_win32_symlink_dir()
126 " On Windows, non-admin users cannot create symlinks.
127 " So we use an existing symlink for this test.
128 if has('win32')
129 " Check if 'C:\Users\All Users' is a symlink to a directory.
Bram Moolenaara2f28852017-02-01 22:05:28 +0100130 let res = system('dir C:\Users /a')
Bram Moolenaar8767f522016-07-01 17:17:39 +0200131 if match(res, '\C<SYMLINKD> *All Users') >= 0
132 " Get the filetype of the symlink.
133 call assert_equal('dir', getftype('C:\Users\All Users'))
134 endif
135 endif
136endfunc