blob: f511a4d5872c4abaffc30e307f8610a729c65f4e [file] [log] [blame]
Bram Moolenaarf3654822016-03-04 22:12:23 +01001" Tests for 'packpath' and :packadd
Bram Moolenaar863c1a92016-03-03 15:47:06 +01002
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01003
Bram Moolenaar91715872016-03-03 17:13:03 +01004func SetUp()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02005 let s:topdir = getcwd() . '/Xdir'
Bram Moolenaar91715872016-03-03 17:13:03 +01006 exe 'set packpath=' . s:topdir
7 let s:plugdir = s:topdir . '/pack/mine/opt/mytest'
8endfunc
9
10func TearDown()
11 call delete(s:topdir, 'rf')
12endfunc
13
Bram Moolenaarf3654822016-03-04 22:12:23 +010014func Test_packadd()
Bram Moolenaar99396d42018-09-08 18:21:16 +020015 if !exists('s:plugdir')
16 echomsg 'when running this test manually, call SetUp() first'
17 return
18 endif
19
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020020 call mkdir(s:plugdir . '/plugin/also', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010021 call mkdir(s:plugdir . '/ftdetect', 'p')
Bram Moolenaara5702442016-05-24 19:37:29 +020022 call mkdir(s:plugdir . '/after', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010023 set rtp&
24 let rtp = &rtp
Bram Moolenaar863c1a92016-03-03 15:47:06 +010025 filetype on
Bram Moolenaar863c1a92016-03-03 15:47:06 +010026
Bram Moolenaar99396d42018-09-08 18:21:16 +020027 let rtp_entries = split(rtp, ',')
28 for entry in rtp_entries
29 if entry =~? '\<after\>'
30 let first_after_entry = entry
31 break
32 endif
33 endfor
34
Bram Moolenaar91715872016-03-03 17:13:03 +010035 exe 'split ' . s:plugdir . '/plugin/test.vim'
36 call setline(1, 'let g:plugin_works = 42')
37 wq
Bram Moolenaar863c1a92016-03-03 15:47:06 +010038
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020039 exe 'split ' . s:plugdir . '/plugin/also/loaded.vim'
40 call setline(1, 'let g:plugin_also_works = 77')
41 wq
42
Bram Moolenaar91715872016-03-03 17:13:03 +010043 exe 'split ' . s:plugdir . '/ftdetect/test.vim'
44 call setline(1, 'let g:ftdetect_works = 17')
45 wq
46
Bram Moolenaarf3654822016-03-04 22:12:23 +010047 packadd mytest
Bram Moolenaar91715872016-03-03 17:13:03 +010048
49 call assert_equal(42, g:plugin_works)
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020050 call assert_equal(77, g:plugin_also_works)
Bram Moolenaar91715872016-03-03 17:13:03 +010051 call assert_equal(17, g:ftdetect_works)
52 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar2374faa2018-02-04 17:47:42 +010053 call assert_match('/testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaar99396d42018-09-08 18:21:16 +020054
55 let new_after = match(&rtp, '/testdir/Xdir/pack/mine/opt/mytest/after,')
56 let old_after = match(&rtp, ',' . first_after_entry . '\>')
57 call assert_true(new_after > 0, 'rtp is ' . &rtp)
58 call assert_true(old_after > 0, 'rtp is ' . &rtp)
59 call assert_true(new_after < old_after, 'rtp is ' . &rtp)
Bram Moolenaarbe82c252016-03-06 14:44:08 +010060
Bram Moolenaarf98a39c2018-04-18 22:18:23 +020061 " NOTE: '/.../opt/myte' forwardly matches with '/.../opt/mytest'
62 call mkdir(fnamemodify(s:plugdir, ':h') . '/myte', 'p')
63 let rtp = &rtp
64 packadd myte
65
66 " Check the path of 'myte' is added
67 call assert_true(len(&rtp) > len(rtp))
68 call assert_match('/testdir/Xdir/pack/mine/opt/myte\($\|,\)', &rtp)
69
Bram Moolenaarbe82c252016-03-06 14:44:08 +010070 " Check exception
71 call assert_fails("packadd directorynotfound", 'E919:')
72 call assert_fails("packadd", 'E471:')
Bram Moolenaar91715872016-03-03 17:13:03 +010073endfunc
74
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010075func Test_packadd_start()
76 let plugdir = s:topdir . '/pack/mine/start/other'
77 call mkdir(plugdir . '/plugin', 'p')
78 set rtp&
79 let rtp = &rtp
80 filetype on
81
82 exe 'split ' . plugdir . '/plugin/test.vim'
83 call setline(1, 'let g:plugin_works = 24')
84 wq
85
86 packadd other
87
88 call assert_equal(24, g:plugin_works)
89 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar2374faa2018-02-04 17:47:42 +010090 call assert_match('/testdir/Xdir/pack/mine/start/other\($\|,\)', &rtp)
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010091endfunc
92
Bram Moolenaarf3654822016-03-04 22:12:23 +010093func Test_packadd_noload()
94 call mkdir(s:plugdir . '/plugin', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010095 call mkdir(s:plugdir . '/syntax', 'p')
96 set rtp&
97 let rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +010098
99 exe 'split ' . s:plugdir . '/plugin/test.vim'
100 call setline(1, 'let g:plugin_works = 42')
101 wq
102 let g:plugin_works = 0
103
104 packadd! mytest
105
Bram Moolenaar91715872016-03-03 17:13:03 +0100106 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100107 call assert_match('testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaarf3654822016-03-04 22:12:23 +0100108 call assert_equal(0, g:plugin_works)
Bram Moolenaar91715872016-03-03 17:13:03 +0100109
110 " check the path is not added twice
111 let new_rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +0100112 packadd! mytest
Bram Moolenaar91715872016-03-03 17:13:03 +0100113 call assert_equal(new_rtp, &rtp)
Bram Moolenaar863c1a92016-03-03 15:47:06 +0100114endfunc
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100115
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100116func Test_packadd_symlink_dir()
117 if !has('unix')
Bram Moolenaar644df412017-03-09 13:58:02 +0100118 return
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100119 endif
120 let top2_dir = s:topdir . '/Xdir2'
121 let real_dir = s:topdir . '/Xsym'
Bram Moolenaar644df412017-03-09 13:58:02 +0100122 call mkdir(real_dir, 'p')
123 exec "silent !ln -s Xsym" top2_dir
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100124 let &rtp = top2_dir . ',' . top2_dir . '/after'
125 let &packpath = &rtp
126
127 let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
128 call mkdir(s:plugdir . '/plugin', 'p')
129
130 exe 'split ' . s:plugdir . '/plugin/test.vim'
131 call setline(1, 'let g:plugin_works = 44')
132 wq
133 let g:plugin_works = 0
134
135 packadd mytest
136
137 " Must have been inserted in the middle, not at the end
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100138 call assert_match('/pack/mine/opt/mytest,', &rtp)
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100139 call assert_equal(44, g:plugin_works)
140
141 " No change when doing it again.
142 let rtp_before = &rtp
143 packadd mytest
144 call assert_equal(rtp_before, &rtp)
145
146 set rtp&
147 let rtp = &rtp
Bram Moolenaar24f8f542017-02-11 23:00:36 +0100148 exec "silent !rm" top2_dir
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100149endfunc
150
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100151func Test_packadd_symlink_dir2()
152 if !has('unix')
153 return
154 endif
155 let top2_dir = s:topdir . '/Xdir2'
156 let real_dir = s:topdir . '/Xsym/pack'
157 call mkdir(top2_dir, 'p')
158 call mkdir(real_dir, 'p')
159 let &rtp = top2_dir . ',' . top2_dir . '/after'
160 let &packpath = &rtp
161
162 exec "silent !ln -s ../Xsym/pack" top2_dir . '/pack'
163 let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
164 call mkdir(s:plugdir . '/plugin', 'p')
165
166 exe 'split ' . s:plugdir . '/plugin/test.vim'
167 call setline(1, 'let g:plugin_works = 48')
168 wq
169 let g:plugin_works = 0
170
171 packadd mytest
172
173 " Must have been inserted in the middle, not at the end
174 call assert_match('/Xdir2/pack/mine/opt/mytest,', &rtp)
175 call assert_equal(48, g:plugin_works)
176
177 " No change when doing it again.
178 let rtp_before = &rtp
179 packadd mytest
180 call assert_equal(rtp_before, &rtp)
181
182 set rtp&
183 let rtp = &rtp
184 exec "silent !rm" top2_dir . '/pack'
185 exec "silent !rmdir" top2_dir
186endfunc
187
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100188" Check command-line completion for 'packadd'
189func Test_packadd_completion()
190 let optdir1 = &packpath . '/pack/mine/opt'
191 let optdir2 = &packpath . '/pack/candidate/opt'
192
193 call mkdir(optdir1 . '/pluginA', 'p')
194 call mkdir(optdir1 . '/pluginC', 'p')
195 call mkdir(optdir2 . '/pluginB', 'p')
196 call mkdir(optdir2 . '/pluginC', 'p')
197
198 let li = []
199 call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
200 call feedkeys(":packadd " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
201 call feedkeys(":packadd " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
202 call feedkeys(":packadd " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
203 call assert_equal("packadd pluginA", li[0])
204 call assert_equal("packadd pluginB", li[1])
205 call assert_equal("packadd pluginC", li[2])
206 call assert_equal("packadd ", li[3])
207endfunc
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100208
209func Test_packloadall()
Bram Moolenaar49b27322016-04-05 21:13:00 +0200210 " plugin foo with an autoload directory
211 let fooplugindir = &packpath . '/pack/mine/start/foo/plugin'
212 call mkdir(fooplugindir, 'p')
213 call writefile(['let g:plugin_foo_number = 1234',
214 \ 'let g:plugin_foo_auto = bbb#value',
215 \ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim')
216 let fooautodir = &packpath . '/pack/mine/start/foo/autoload'
217 call mkdir(fooautodir, 'p')
218 call writefile(['let bar#value = 77'], fooautodir . '/bar.vim')
219
220 " plugin aaa with an autoload directory
221 let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin'
222 call mkdir(aaaplugindir, 'p')
223 call writefile(['let g:plugin_aaa_number = 333',
224 \ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim')
225 let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload'
226 call mkdir(aaaautodir, 'p')
227 call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim')
228
229 " plugin extra with only an autoload directory
230 let extraautodir = &packpath . '/pack/mine/start/extra/autoload'
231 call mkdir(extraautodir, 'p')
232 call writefile(['let extra#value = 99'], extraautodir . '/extra.vim')
233
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100234 packloadall
235 call assert_equal(1234, g:plugin_foo_number)
Bram Moolenaar49b27322016-04-05 21:13:00 +0200236 call assert_equal(55, g:plugin_foo_auto)
237 call assert_equal(99, g:plugin_extra_auto)
238 call assert_equal(333, g:plugin_aaa_number)
239 call assert_equal(77, g:plugin_aaa_auto)
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100240
241 " only works once
Bram Moolenaar49b27322016-04-05 21:13:00 +0200242 call writefile(['let g:plugin_bar_number = 4321'], fooplugindir . '/bar2.vim')
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100243 packloadall
244 call assert_false(exists('g:plugin_bar_number'))
245
246 " works when ! used
247 packloadall!
248 call assert_equal(4321, g:plugin_bar_number)
249endfunc
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100250
251func Test_helptags()
252 let docdir1 = &packpath . '/pack/mine/start/foo/doc'
253 let docdir2 = &packpath . '/pack/mine/start/bar/doc'
254 call mkdir(docdir1, 'p')
255 call mkdir(docdir2, 'p')
256 call writefile(['look here: *look-here*'], docdir1 . '/bar.txt')
257 call writefile(['look away: *look-away*'], docdir2 . '/foo.txt')
258 exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar'
259
260 helptags ALL
261
262 let tags1 = readfile(docdir1 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100263 call assert_match('look-here', tags1[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100264 let tags2 = readfile(docdir2 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100265 call assert_match('look-away', tags2[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100266endfunc
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100267
268func Test_colorscheme()
269 let colordirrun = &packpath . '/runtime/colors'
270 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
271 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
272 call mkdir(colordirrun, 'p')
273 call mkdir(colordirstart, 'p')
274 call mkdir(colordiropt, 'p')
275 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
276 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
277 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
278 exe 'set rtp=' . &packpath . '/runtime'
279
280 colorscheme one
281 call assert_equal(1, g:found_one)
282 colorscheme two
283 call assert_equal(1, g:found_two)
284 colorscheme three
285 call assert_equal(1, g:found_three)
286endfunc
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100287
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100288func Test_colorscheme_completion()
289 let colordirrun = &packpath . '/runtime/colors'
290 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
291 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
292 call mkdir(colordirrun, 'p')
293 call mkdir(colordirstart, 'p')
294 call mkdir(colordiropt, 'p')
295 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
296 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
297 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
298 exe 'set rtp=' . &packpath . '/runtime'
299
300 let li=[]
301 call feedkeys(":colorscheme " . repeat("\<Tab>", 1) . "')\<C-B>call add(li, '\<CR>", 't')
302 call feedkeys(":colorscheme " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
303 call feedkeys(":colorscheme " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
304 call feedkeys(":colorscheme " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
305 call assert_equal("colorscheme one", li[0])
306 call assert_equal("colorscheme three", li[1])
307 call assert_equal("colorscheme two", li[2])
308 call assert_equal("colorscheme ", li[3])
309endfunc
310
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100311func Test_runtime()
312 let rundir = &packpath . '/runtime/extra'
313 let startdir = &packpath . '/pack/mine/start/foo/extra'
314 let optdir = &packpath . '/pack/mine/opt/bar/extra'
315 call mkdir(rundir, 'p')
316 call mkdir(startdir, 'p')
317 call mkdir(optdir, 'p')
318 call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim')
319 call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim')
320 call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim')
321 call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim')
322 call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim')
323 exe 'set rtp=' . &packpath . '/runtime'
324
325 let g:sequence = ''
326 runtime extra/bar.vim
327 call assert_equal('run', g:sequence)
328 let g:sequence = ''
329 runtime START extra/bar.vim
330 call assert_equal('start', g:sequence)
331 let g:sequence = ''
332 runtime OPT extra/bar.vim
333 call assert_equal('opt', g:sequence)
334 let g:sequence = ''
335 runtime PACK extra/bar.vim
336 call assert_equal('start', g:sequence)
337 let g:sequence = ''
338 runtime! PACK extra/bar.vim
339 call assert_equal('startopt', g:sequence)
340 let g:sequence = ''
341 runtime PACK extra/xxx.vim
342 call assert_equal('xxxopt', g:sequence)
343
344 let g:sequence = ''
345 runtime ALL extra/bar.vim
346 call assert_equal('run', g:sequence)
347 let g:sequence = ''
348 runtime ALL extra/foo.vim
349 call assert_equal('foostart', g:sequence)
350 let g:sequence = ''
351 runtime! ALL extra/xxx.vim
352 call assert_equal('xxxopt', g:sequence)
353 let g:sequence = ''
354 runtime! ALL extra/bar.vim
355 call assert_equal('runstartopt', g:sequence)
356endfunc