blob: 6adc5a945f099ddb9d71260527ef765361776af2 [file] [log] [blame]
Bram Moolenaar8767f522016-07-01 17:17:39 +02001" Tests for stat functions and checktime
2
3func Test_existent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +01004 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +02005
Bram Moolenaara2f28852017-02-01 22:05:28 +01006 let ts = localtime()
7 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +02008 call writefile(fl, fname)
Bram Moolenaara2f28852017-02-01 22:05:28 +01009 let tf = getftime(fname)
10 let te = localtime()
Bram Moolenaar8767f522016-07-01 17:17:39 +020011
12 call assert_true(ts <= tf && tf <= te)
13 call assert_equal(strlen(fl[0] . "\n"), getfsize(fname))
14 call assert_equal('file', getftype(fname))
15 call assert_equal('rw-', getfperm(fname)[0:2])
Bram Moolenaara2f28852017-02-01 22:05:28 +010016
17 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020018endfunc
19
20func Test_existent_directory()
Bram Moolenaara2f28852017-02-01 22:05:28 +010021 let dname = '.'
Bram Moolenaar8767f522016-07-01 17:17:39 +020022
23 call assert_equal(0, getfsize(dname))
24 call assert_equal('dir', getftype(dname))
25 call assert_equal('rwx', getfperm(dname)[0:2])
26endfunc
27
28func Test_checktime()
Bram Moolenaara2f28852017-02-01 22:05:28 +010029 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020030
Bram Moolenaara2f28852017-02-01 22:05:28 +010031 let fl = ['Hello World!']
Bram Moolenaar8767f522016-07-01 17:17:39 +020032 call writefile(fl, fname)
33 set autoread
34 exec 'e' fname
Bram Moolenaara2f28852017-02-01 22:05:28 +010035 " FAT has a granularity of 2 seconds, otherwise it's usually 1 second
36 if has('win32')
37 sleep 2
38 else
39 sleep 1
40 endif
41 let fl = readfile(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020042 let fl[0] .= ' - checktime'
43 call writefile(fl, fname)
44 checktime
45 call assert_equal(fl[0], getline(1))
Bram Moolenaara2f28852017-02-01 22:05:28 +010046
47 call delete(fname)
Bram Moolenaar8767f522016-07-01 17:17:39 +020048endfunc
49
50func Test_nonexistent_file()
Bram Moolenaara2f28852017-02-01 22:05:28 +010051 let fname = 'Xtest.tmp'
Bram Moolenaar8767f522016-07-01 17:17:39 +020052
53 call delete(fname)
54 call assert_equal(-1, getftime(fname))
55 call assert_equal(-1, getfsize(fname))
56 call assert_equal('', getftype(fname))
57 call assert_equal('', getfperm(fname))
58endfunc
59
60func Test_win32_symlink_dir()
61 " On Windows, non-admin users cannot create symlinks.
62 " So we use an existing symlink for this test.
63 if has('win32')
64 " Check if 'C:\Users\All Users' is a symlink to a directory.
Bram Moolenaara2f28852017-02-01 22:05:28 +010065 let res = system('dir C:\Users /a')
Bram Moolenaar8767f522016-07-01 17:17:39 +020066 if match(res, '\C<SYMLINKD> *All Users') >= 0
67 " Get the filetype of the symlink.
68 call assert_equal('dir', getftype('C:\Users\All Users'))
69 endif
70 endif
71endfunc