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 | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 4 | let fnames = ['Xtest1.tmp', 'Xtest2.tmp', 'Xtest3.tmp'] |
| 5 | let times = [] |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 6 | let result = 0 |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 7 | |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 8 | " Use three files istead of localtim(), with a network filesystem the file |
| 9 | " times may differ at bit |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 10 | let fl = ['Hello World!'] |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 11 | for fname in fnames |
| 12 | call writefile(fl, fname) |
Bram Moolenaar | 4c313b1 | 2019-08-24 22:58:31 +0200 | [diff] [blame] | 13 | call add(times, fname->getftime()) |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 14 | if a:doSleep |
| 15 | sleep 1 |
| 16 | endif |
| 17 | endfor |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 18 | |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 19 | let time_correct = (times[0] <= times[1] && times[1] <= times[2]) |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 20 | if a:doSleep || time_correct |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 21 | call assert_true(time_correct, printf('Expected %s <= %s <= %s', times[0], times[1], times[2])) |
Bram Moolenaar | 4c313b1 | 2019-08-24 22:58:31 +0200 | [diff] [blame] | 22 | call assert_equal(strlen(fl[0] . "\n"), fnames[0]->getfsize()) |
| 23 | call assert_equal('file', fnames[0]->getftype()) |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 24 | call assert_equal('rw-', getfperm(fnames[0])[0:2]) |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 25 | let result = 1 |
| 26 | endif |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 27 | |
Bram Moolenaar | addc156 | 2018-11-18 12:25:09 +0100 | [diff] [blame] | 28 | for fname in fnames |
| 29 | call delete(fname) |
| 30 | endfor |
Bram Moolenaar | 82de3c2 | 2017-08-17 17:35:36 +0200 | [diff] [blame] | 31 | return result |
| 32 | endfunc |
| 33 | |
| 34 | func 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 Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 41 | endfunc |
| 42 | |
| 43 | func Test_existent_directory() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 44 | let dname = '.' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 45 | |
| 46 | call assert_equal(0, getfsize(dname)) |
| 47 | call assert_equal('dir', getftype(dname)) |
| 48 | call assert_equal('rwx', getfperm(dname)[0:2]) |
| 49 | endfunc |
| 50 | |
Bram Moolenaar | 386bc82 | 2018-07-07 18:34:12 +0200 | [diff] [blame] | 51 | func 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 |
| 58 | endfunc |
| 59 | |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 60 | func Test_checktime() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 61 | let fname = 'Xtest.tmp' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 62 | |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 63 | let fl = ['Hello World!'] |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 64 | call writefile(fl, fname) |
| 65 | set autoread |
| 66 | exec 'e' fname |
Bram Moolenaar | 386bc82 | 2018-07-07 18:34:12 +0200 | [diff] [blame] | 67 | call SleepForTimestamp() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 68 | let fl = readfile(fname) |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 69 | let fl[0] .= ' - checktime' |
| 70 | call writefile(fl, fname) |
| 71 | checktime |
| 72 | call assert_equal(fl[0], getline(1)) |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 73 | |
| 74 | call delete(fname) |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 75 | endfunc |
| 76 | |
Bram Moolenaar | 386bc82 | 2018-07-07 18:34:12 +0200 | [diff] [blame] | 77 | func 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! |
| 114 | endfunc |
| 115 | |
| 116 | |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 117 | func Test_nonexistent_file() |
Bram Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 118 | let fname = 'Xtest.tmp' |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 119 | |
| 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)) |
| 125 | endfunc |
| 126 | |
Bram Moolenaar | 1598f99 | 2018-08-09 22:08:57 +0200 | [diff] [blame] | 127 | func 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 Moolenaar | 3b3a506 | 2018-08-22 20:16:16 +0200 | [diff] [blame] | 146 | 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 Moolenaar | 1598f99 | 2018-08-09 22:08:57 +0200 | [diff] [blame] | 151 | endfor |
| 152 | |
| 153 | for bdevfile in systemlist('find /dev -type b -maxdepth 2 2>/dev/null') |
Bram Moolenaar | 3b3a506 | 2018-08-22 20:16:16 +0200 | [diff] [blame] | 154 | 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 Moolenaar | 1598f99 | 2018-08-09 22:08:57 +0200 | [diff] [blame] | 159 | 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 Moolenaar | 3b3a506 | 2018-08-22 20:16:16 +0200 | [diff] [blame] | 164 | 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 Moolenaar | 1598f99 | 2018-08-09 22:08:57 +0200 | [diff] [blame] | 169 | endfor |
| 170 | |
| 171 | " TODO: file type 'other' is not tested. How can we test it? |
| 172 | endfunc |
| 173 | |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 174 | func 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 Moolenaar | a2f2885 | 2017-02-01 22:05:28 +0100 | [diff] [blame] | 179 | let res = system('dir C:\Users /a') |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 180 | 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 |
| 185 | endfunc |