blob: 208d4d966ef42c6d96d5757697a29b0d84a197c6 [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
101 set directory&
102 call delete('Xswapdir', 'rf')
103endfunc
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200104
105func Test_swapinfo()
106 new Xswapinfo
107 call setline(1, ['one', 'two', 'three'])
108 w
Bram Moolenaar110bd602018-09-16 18:46:59 +0200109 let fname = s:swapname()
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200110 call assert_match('Xswapinfo', fname)
111 let info = swapinfo(fname)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200112
113 let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
114 call assert_equal(ver, info.version)
115
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200116 call assert_match('\w', info.user)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200117 " host name is truncated to 39 bytes in the swap file
118 call assert_equal(hostname()[:38], info.host)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200119 call assert_match('Xswapinfo', info.fname)
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200120 call assert_match(0, info.dirty)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200121 call assert_equal(getpid(), info.pid)
122 call assert_match('^\d*$', info.mtime)
123 if has_key(info, 'inode')
124 call assert_match('\d', info.inode)
125 endif
126 bwipe!
127 call delete(fname)
128 call delete('Xswapinfo')
129
130 let info = swapinfo('doesnotexist')
131 call assert_equal('Cannot open file', info.error)
132
133 call writefile(['burp'], 'Xnotaswapfile')
134 let info = swapinfo('Xnotaswapfile')
135 call assert_equal('Cannot read file', info.error)
136 call delete('Xnotaswapfile')
137
138 call writefile([repeat('x', 10000)], 'Xnotaswapfile')
139 let info = swapinfo('Xnotaswapfile')
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200140 call assert_equal('Not a swap file', info.error)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200141 call delete('Xnotaswapfile')
142endfunc
Bram Moolenaar110bd602018-09-16 18:46:59 +0200143
144func Test_swapname()
145 edit Xtest1
146 let expected = s:swapname()
147 call assert_equal(expected, swapname('%'))
148
149 new Xtest2
150 let buf = bufnr('%')
151 let expected = s:swapname()
152 wincmd p
153 call assert_equal(expected, swapname(buf))
154
155 new Xtest3
156 setlocal noswapfile
157 call assert_equal('', swapname('%'))
158
159 bwipe!
160 call delete('Xtest1')
161 call delete('Xtest2')
162 call delete('Xtest3')
163endfunc