Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 1 | " Tests for stat functions and checktime |
| 2 | |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 3 | func CheckFileTime(doSleep) |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 4 | let fname = 'Xtest.tmp' |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 5 | let result = 0 |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 7 | let ts = localtime() |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 8 | if a:doSleep |
| 9 | sleep 1 |
| 10 | endif |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 11 | let fl = ['Hello World!'] |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 12 | call writefile(fl, fname) |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 13 | let tf = getftime(fname) |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 14 | if a:doSleep |
| 15 | sleep 1 |
| 16 | endif |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 17 | let te = localtime() |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 18 | |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 19 | 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 Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 27 | |
| 28 | call delete(fname) |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 29 | return result |
| 30 | endfunc |
| 31 | |
| 32 | func 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 Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 39 | endfunc |
| 40 | |
| 41 | func Test_existent_directory() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 42 | let dname = '.' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 43 | |
| 44 | call assert_equal(0, getfsize(dname)) |
| 45 | call assert_equal('dir', getftype(dname)) |
| 46 | call assert_equal('rwx', getfperm(dname)[0:2]) |
| 47 | endfunc |
| 48 | |
| 49 | func Test_checktime() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 50 | let fname = 'Xtest.tmp' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 51 | |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 52 | let fl = ['Hello World!'] |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 53 | call writefile(fl, fname) |
| 54 | set autoread |
| 55 | exec 'e' fname |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 56 | " 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 Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 63 | let fl[0] .= ' - checktime' |
| 64 | call writefile(fl, fname) |
| 65 | checktime |
| 66 | call assert_equal(fl[0], getline(1)) |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 67 | |
| 68 | call delete(fname) |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 69 | endfunc |
| 70 | |
| 71 | func Test_nonexistent_file() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 72 | let fname = 'Xtest.tmp' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 73 | |
| 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)) |
| 79 | endfunc |
| 80 | |
| 81 | func 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 Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 86 | let res = system('dir C:\Users /a') |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 87 | 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 |
| 92 | endfunc |