blob: ce52578919dbcff98d433f3570f7225740698feb [file] [log] [blame]
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001" Tests for the swap feature
2
Bram Moolenaar110bd602018-09-16 18:46:59 +02003func s:swapname()
4 return trim(execute('swapname'))
5endfunc
6
Bram Moolenaarffe010f2017-11-04 22:30:40 +01007" Tests for 'directory' option.
8func 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 Moolenaar110bd602018-09-16 18:46:59 +020024 let swfname = s:swapname()
Bram Moolenaarffe010f2017-11-04 22:30:40 +010025 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 Moolenaar110bd602018-09-16 18:46:59 +020034 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010035 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 Moolenaar110bd602018-09-16 18:46:59 +020045 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010046 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")
52endfunc
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010053
54func Test_swap_group()
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010055 if !has("unix")
56 return
57 endif
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010058 let groups = split(system('groups'))
59 if len(groups) <= 1
Bram Moolenaarad7dac82017-11-04 22:21:21 +010060 throw 'Skipped: need at least two groups, got ' . string(groups)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010061 endif
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010062
Bram Moolenaar5842a742017-11-04 22:36:53 +010063 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 Moolenaar5a73e0c2017-11-04 21:35:01 +010070 else
Bram Moolenaar5842a742017-11-04 22:36:53 +010071 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 Moolenaar110bd602018-09-16 18:46:59 +020077 let swapname = s:swapname()
Bram Moolenaar5842a742017-11-04 22:36:53 +010078 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 Moolenaar430dc5d2017-11-02 21:04:47 +010081
Bram Moolenaar5842a742017-11-04 22:36:53 +010082 bwipe!
83 endif
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010084 endif
Bram Moolenaar5842a742017-11-04 22:36:53 +010085 finally
86 call delete('Xtest')
87 endtry
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010088endfunc
Bram Moolenaar8c3169c2018-05-12 17:04:12 +020089
90func 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 Moolenaare3d06542019-01-27 14:29:24 +0100101 " 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 Moolenaar8c3169c2018-05-12 17:04:12 +0200104 set directory&
105 call delete('Xswapdir', 'rf')
106endfunc
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200107
108func Test_swapinfo()
109 new Xswapinfo
110 call setline(1, ['one', 'two', 'three'])
111 w
Bram Moolenaar110bd602018-09-16 18:46:59 +0200112 let fname = s:swapname()
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200113 call assert_match('Xswapinfo', fname)
114 let info = swapinfo(fname)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200115
116 let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
117 call assert_equal(ver, info.version)
118
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200119 call assert_match('\w', info.user)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200120 " host name is truncated to 39 bytes in the swap file
121 call assert_equal(hostname()[:38], info.host)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200122 call assert_match('Xswapinfo', info.fname)
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200123 call assert_match(0, info.dirty)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200124 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 Moolenaar47ad5652018-08-21 21:09:07 +0200143 call assert_equal('Not a swap file', info.error)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200144 call delete('Xnotaswapfile')
145endfunc
Bram Moolenaar110bd602018-09-16 18:46:59 +0200146
147func 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')
166endfunc