Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1 | " Tests for the swap feature |
| 2 | |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 3 | func s:swapname() |
| 4 | return trim(execute('swapname')) |
| 5 | endfunc |
| 6 | |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 7 | " Tests for 'directory' option. |
| 8 | func Test_swap_directory() |
| 9 | if !has("unix") |
| 10 | return |
| 11 | endif |
| 12 | let content = ['start of testfile', |
| 13 | \ 'line 2 Abcdefghij', |
| 14 | \ 'line 3 Abcdefghij', |
| 15 | \ 'end of testfile'] |
| 16 | call writefile(content, 'Xtest1') |
| 17 | |
| 18 | " '.', swap file in the same directory as file |
| 19 | set dir=.,~ |
| 20 | |
| 21 | " Verify that the swap file doesn't exist in the current directory |
| 22 | call assert_equal([], glob(".Xtest1*.swp", 1, 1, 1)) |
| 23 | edit Xtest1 |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 24 | let swfname = s:swapname() |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 25 | call assert_equal([swfname], glob(swfname, 1, 1, 1)) |
| 26 | |
| 27 | " './dir', swap file in a directory relative to the file |
| 28 | set dir=./Xtest2,.,~ |
| 29 | |
| 30 | call mkdir("Xtest2") |
| 31 | edit Xtest1 |
| 32 | call assert_equal([], glob(swfname, 1, 1, 1)) |
| 33 | let swfname = "Xtest2/Xtest1.swp" |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 34 | call assert_equal(swfname, s:swapname()) |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 35 | call assert_equal([swfname], glob("Xtest2/*", 1, 1, 1)) |
| 36 | |
| 37 | " 'dir', swap file in directory relative to the current dir |
| 38 | set dir=Xtest.je,~ |
| 39 | |
| 40 | call mkdir("Xtest.je") |
| 41 | call writefile(content, 'Xtest2/Xtest3') |
| 42 | edit Xtest2/Xtest3 |
| 43 | call assert_equal(["Xtest2/Xtest3"], glob("Xtest2/*", 1, 1, 1)) |
| 44 | let swfname = "Xtest.je/Xtest3.swp" |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 45 | call assert_equal(swfname, s:swapname()) |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 46 | call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1)) |
| 47 | |
| 48 | set dir& |
| 49 | call delete("Xtest1") |
| 50 | call delete("Xtest2", "rf") |
| 51 | call delete("Xtest.je", "rf") |
| 52 | endfunc |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 53 | |
| 54 | func Test_swap_group() |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 55 | if !has("unix") |
| 56 | return |
| 57 | endif |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 58 | let groups = split(system('groups')) |
| 59 | if len(groups) <= 1 |
Bram Moolenaar | ad7dac8 | 2017-11-04 22:21:21 +0100 | [diff] [blame] | 60 | throw 'Skipped: need at least two groups, got ' . string(groups) |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 61 | endif |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 62 | |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 63 | try |
| 64 | call delete('Xtest') |
| 65 | split Xtest |
| 66 | call setline(1, 'just some text') |
| 67 | wq |
| 68 | if system('ls -l Xtest') !~ ' ' . groups[0] . ' \d' |
| 69 | throw 'Skipped: test file does not have the first group' |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 70 | else |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 71 | silent !chmod 640 Xtest |
| 72 | call system('chgrp ' . groups[1] . ' Xtest') |
| 73 | if system('ls -l Xtest') !~ ' ' . groups[1] . ' \d' |
| 74 | throw 'Skipped: cannot set second group on test file' |
| 75 | else |
| 76 | split Xtest |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 77 | let swapname = s:swapname() |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 78 | call assert_match('Xtest', swapname) |
| 79 | " Group of swapfile must now match original file. |
| 80 | call assert_match(' ' . groups[1] . ' \d', system('ls -l ' . swapname)) |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 81 | |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 82 | bwipe! |
| 83 | endif |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 84 | endif |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 85 | finally |
| 86 | call delete('Xtest') |
| 87 | endtry |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 88 | endfunc |
Bram Moolenaar | 8c3169c | 2018-05-12 17:04:12 +0200 | [diff] [blame] | 89 | |
| 90 | func Test_missing_dir() |
| 91 | call mkdir('Xswapdir') |
| 92 | exe 'set directory=' . getcwd() . '/Xswapdir' |
| 93 | |
| 94 | call assert_equal('', glob('foo')) |
| 95 | call assert_equal('', glob('bar')) |
| 96 | edit foo/x.txt |
| 97 | " This should not give a warning for an existing swap file. |
| 98 | split bar/x.txt |
| 99 | only |
| 100 | |
Bram Moolenaar | e3d0654 | 2019-01-27 14:29:24 +0100 | [diff] [blame] | 101 | " Delete the buffer so that swap file is removed before we try to delete the |
| 102 | " directory. That fails on MS-Windows. |
| 103 | %bdelete! |
Bram Moolenaar | 8c3169c | 2018-05-12 17:04:12 +0200 | [diff] [blame] | 104 | set directory& |
| 105 | call delete('Xswapdir', 'rf') |
| 106 | endfunc |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 107 | |
| 108 | func Test_swapinfo() |
| 109 | new Xswapinfo |
| 110 | call setline(1, ['one', 'two', 'three']) |
| 111 | w |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 112 | let fname = s:swapname() |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 113 | call assert_match('Xswapinfo', fname) |
| 114 | let info = swapinfo(fname) |
Bram Moolenaar | 4c5765b | 2018-08-22 11:28:01 +0200 | [diff] [blame] | 115 | |
| 116 | let ver = printf('VIM %d.%d', v:version / 100, v:version % 100) |
| 117 | call assert_equal(ver, info.version) |
| 118 | |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 119 | call assert_match('\w', info.user) |
Bram Moolenaar | 4c5765b | 2018-08-22 11:28:01 +0200 | [diff] [blame] | 120 | " host name is truncated to 39 bytes in the swap file |
| 121 | call assert_equal(hostname()[:38], info.host) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 122 | call assert_match('Xswapinfo', info.fname) |
Bram Moolenaar | 47ad565 | 2018-08-21 21:09:07 +0200 | [diff] [blame] | 123 | call assert_match(0, info.dirty) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 124 | call assert_equal(getpid(), info.pid) |
| 125 | call assert_match('^\d*$', info.mtime) |
| 126 | if has_key(info, 'inode') |
| 127 | call assert_match('\d', info.inode) |
| 128 | endif |
| 129 | bwipe! |
| 130 | call delete(fname) |
| 131 | call delete('Xswapinfo') |
| 132 | |
| 133 | let info = swapinfo('doesnotexist') |
| 134 | call assert_equal('Cannot open file', info.error) |
| 135 | |
| 136 | call writefile(['burp'], 'Xnotaswapfile') |
| 137 | let info = swapinfo('Xnotaswapfile') |
| 138 | call assert_equal('Cannot read file', info.error) |
| 139 | call delete('Xnotaswapfile') |
| 140 | |
| 141 | call writefile([repeat('x', 10000)], 'Xnotaswapfile') |
| 142 | let info = swapinfo('Xnotaswapfile') |
Bram Moolenaar | 47ad565 | 2018-08-21 21:09:07 +0200 | [diff] [blame] | 143 | call assert_equal('Not a swap file', info.error) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 144 | call delete('Xnotaswapfile') |
| 145 | endfunc |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 146 | |
| 147 | func Test_swapname() |
| 148 | edit Xtest1 |
| 149 | let expected = s:swapname() |
| 150 | call assert_equal(expected, swapname('%')) |
| 151 | |
| 152 | new Xtest2 |
| 153 | let buf = bufnr('%') |
| 154 | let expected = s:swapname() |
| 155 | wincmd p |
| 156 | call assert_equal(expected, swapname(buf)) |
| 157 | |
| 158 | new Xtest3 |
| 159 | setlocal noswapfile |
| 160 | call assert_equal('', swapname('%')) |
| 161 | |
| 162 | bwipe! |
| 163 | call delete('Xtest1') |
| 164 | call delete('Xtest2') |
| 165 | call delete('Xtest3') |
| 166 | endfunc |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 167 | |
| 168 | func Test_swapfile_delete() |
| 169 | autocmd! SwapExists |
| 170 | function s:swap_exists() |
| 171 | let v:swapchoice = s:swap_choice |
| 172 | let s:swapname = v:swapname |
| 173 | let s:filename = expand('<afile>') |
| 174 | endfunc |
| 175 | augroup test_swapfile_delete |
| 176 | autocmd! |
| 177 | autocmd SwapExists * call s:swap_exists() |
| 178 | augroup END |
| 179 | |
| 180 | |
| 181 | " Create a valid swapfile by editing a file. |
| 182 | split XswapfileText |
| 183 | call setline(1, ['one', 'two', 'three']) |
| 184 | write " file is written, not modified |
| 185 | " read the swapfile as a Blob |
| 186 | let swapfile_name = swapname('%') |
| 187 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 188 | |
| 189 | " Close the file and recreate the swap file. |
| 190 | " Now editing the file will run into the process still existing |
| 191 | quit |
| 192 | call writefile(swapfile_bytes, swapfile_name) |
| 193 | let s:swap_choice = 'e' |
| 194 | let s:swapname = '' |
| 195 | split XswapfileText |
| 196 | quit |
| 197 | call assert_equal(swapfile_name, s:swapname) |
| 198 | |
| 199 | " Write the swapfile with a modified PID, now it will be automatically |
| 200 | " deleted. Process one should never be Vim. |
| 201 | let swapfile_bytes[24:27] = 0z01000000 |
| 202 | call writefile(swapfile_bytes, swapfile_name) |
| 203 | let s:swapname = '' |
| 204 | split XswapfileText |
| 205 | quit |
| 206 | call assert_equal('', s:swapname) |
| 207 | |
| 208 | " Now set the modified flag, the swap file will not be deleted |
| 209 | let swapfile_bytes[28 + 80 + 899] = 0x55 |
| 210 | call writefile(swapfile_bytes, swapfile_name) |
| 211 | let s:swapname = '' |
| 212 | split XswapfileText |
| 213 | quit |
| 214 | call assert_equal(swapfile_name, s:swapname) |
| 215 | |
| 216 | call delete('XswapfileText') |
| 217 | call delete(swapfile_name) |
| 218 | augroup test_swapfile_delete |
| 219 | autocmd! |
| 220 | augroup END |
| 221 | augroup! test_swapfile_delete |
| 222 | endfunc |