blob: 09fbcc00a141482a43e93c4d6a36a7a7a41ddf2e [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 Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01004
Bram Moolenaar91715872016-03-03 17:13:03 +01005func SetUp()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01006 let s:topdir = getcwd() . '/Xppdir'
Bram Moolenaar91715872016-03-03 17:13:03 +01007 exe 'set packpath=' . s:topdir
8 let s:plugdir = s:topdir . '/pack/mine/opt/mytest'
9endfunc
10
11func TearDown()
12 call delete(s:topdir, 'rf')
13endfunc
14
Bram Moolenaarf3654822016-03-04 22:12:23 +010015func Test_packadd()
Bram Moolenaar99396d42018-09-08 18:21:16 +020016 if !exists('s:plugdir')
17 echomsg 'when running this test manually, call SetUp() first'
18 return
19 endif
20
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020021 call mkdir(s:plugdir . '/plugin/also', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010022 call mkdir(s:plugdir . '/ftdetect', 'p')
Bram Moolenaara5702442016-05-24 19:37:29 +020023 call mkdir(s:plugdir . '/after', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010024 set rtp&
25 let rtp = &rtp
Bram Moolenaar863c1a92016-03-03 15:47:06 +010026 filetype on
Bram Moolenaar863c1a92016-03-03 15:47:06 +010027
Bram Moolenaar99396d42018-09-08 18:21:16 +020028 let rtp_entries = split(rtp, ',')
29 for entry in rtp_entries
30 if entry =~? '\<after\>'
31 let first_after_entry = entry
32 break
33 endif
34 endfor
35
Bram Moolenaar91715872016-03-03 17:13:03 +010036 exe 'split ' . s:plugdir . '/plugin/test.vim'
37 call setline(1, 'let g:plugin_works = 42')
38 wq
Bram Moolenaar863c1a92016-03-03 15:47:06 +010039
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020040 exe 'split ' . s:plugdir . '/plugin/also/loaded.vim'
41 call setline(1, 'let g:plugin_also_works = 77')
42 wq
43
Bram Moolenaar91715872016-03-03 17:13:03 +010044 exe 'split ' . s:plugdir . '/ftdetect/test.vim'
45 call setline(1, 'let g:ftdetect_works = 17')
46 wq
47
Bram Moolenaarf3654822016-03-04 22:12:23 +010048 packadd mytest
Bram Moolenaar91715872016-03-03 17:13:03 +010049
50 call assert_equal(42, g:plugin_works)
Bram Moolenaar71fb0c12016-04-02 22:44:16 +020051 call assert_equal(77, g:plugin_also_works)
Bram Moolenaar91715872016-03-03 17:13:03 +010052 call assert_equal(17, g:ftdetect_works)
53 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010054 call assert_match('/testdir/Xppdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaar99396d42018-09-08 18:21:16 +020055
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010056 let new_after = match(&rtp, '/testdir/Xppdir/pack/mine/opt/mytest/after,')
Bram Moolenaar53c8a472018-09-08 19:12:12 +020057 let forwarded = substitute(first_after_entry, '\\', '[/\\\\]', 'g')
58 let old_after = match(&rtp, ',' . forwarded . '\>')
Bram Moolenaar99396d42018-09-08 18:21:16 +020059 call assert_true(new_after > 0, 'rtp is ' . &rtp)
Bram Moolenaar53c8a472018-09-08 19:12:12 +020060 call assert_true(old_after > 0, 'match ' . forwarded . ' in ' . &rtp)
Bram Moolenaar99396d42018-09-08 18:21:16 +020061 call assert_true(new_after < old_after, 'rtp is ' . &rtp)
Bram Moolenaarbe82c252016-03-06 14:44:08 +010062
Bram Moolenaarf98a39c2018-04-18 22:18:23 +020063 " NOTE: '/.../opt/myte' forwardly matches with '/.../opt/mytest'
64 call mkdir(fnamemodify(s:plugdir, ':h') . '/myte', 'p')
65 let rtp = &rtp
66 packadd myte
67
68 " Check the path of 'myte' is added
69 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010070 call assert_match('/testdir/Xppdir/pack/mine/opt/myte\($\|,\)', &rtp)
Bram Moolenaarf98a39c2018-04-18 22:18:23 +020071
Bram Moolenaarbe82c252016-03-06 14:44:08 +010072 " Check exception
73 call assert_fails("packadd directorynotfound", 'E919:')
74 call assert_fails("packadd", 'E471:')
Bram Moolenaar91715872016-03-03 17:13:03 +010075endfunc
76
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010077func Test_packadd_start()
78 let plugdir = s:topdir . '/pack/mine/start/other'
79 call mkdir(plugdir . '/plugin', 'p')
80 set rtp&
81 let rtp = &rtp
82 filetype on
83
84 exe 'split ' . plugdir . '/plugin/test.vim'
85 call setline(1, 'let g:plugin_works = 24')
86 wq
87
88 packadd other
89
90 call assert_equal(24, g:plugin_works)
91 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010092 call assert_match('/testdir/Xppdir/pack/mine/start/other\($\|,\)', &rtp)
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010093endfunc
94
Bram Moolenaarf3654822016-03-04 22:12:23 +010095func Test_packadd_noload()
96 call mkdir(s:plugdir . '/plugin', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010097 call mkdir(s:plugdir . '/syntax', 'p')
98 set rtp&
99 let rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +0100100
101 exe 'split ' . s:plugdir . '/plugin/test.vim'
102 call setline(1, 'let g:plugin_works = 42')
103 wq
104 let g:plugin_works = 0
105
106 packadd! mytest
107
Bram Moolenaar91715872016-03-03 17:13:03 +0100108 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100109 call assert_match('testdir/Xppdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaarf3654822016-03-04 22:12:23 +0100110 call assert_equal(0, g:plugin_works)
Bram Moolenaar91715872016-03-03 17:13:03 +0100111
112 " check the path is not added twice
113 let new_rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +0100114 packadd! mytest
Bram Moolenaar91715872016-03-03 17:13:03 +0100115 call assert_equal(new_rtp, &rtp)
Bram Moolenaar863c1a92016-03-03 15:47:06 +0100116endfunc
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100117
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100118func Test_packadd_symlink_dir()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200119 CheckUnix
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100120 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()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200152 CheckUnix
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100153 let top2_dir = s:topdir . '/Xdir2'
154 let real_dir = s:topdir . '/Xsym/pack'
155 call mkdir(top2_dir, 'p')
156 call mkdir(real_dir, 'p')
157 let &rtp = top2_dir . ',' . top2_dir . '/after'
158 let &packpath = &rtp
159
160 exec "silent !ln -s ../Xsym/pack" top2_dir . '/pack'
161 let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
162 call mkdir(s:plugdir . '/plugin', 'p')
163
164 exe 'split ' . s:plugdir . '/plugin/test.vim'
165 call setline(1, 'let g:plugin_works = 48')
166 wq
167 let g:plugin_works = 0
168
169 packadd mytest
170
171 " Must have been inserted in the middle, not at the end
172 call assert_match('/Xdir2/pack/mine/opt/mytest,', &rtp)
173 call assert_equal(48, g:plugin_works)
174
175 " No change when doing it again.
176 let rtp_before = &rtp
177 packadd mytest
178 call assert_equal(rtp_before, &rtp)
179
180 set rtp&
181 let rtp = &rtp
182 exec "silent !rm" top2_dir . '/pack'
183 exec "silent !rmdir" top2_dir
184endfunc
185
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100186" Check command-line completion for 'packadd'
187func Test_packadd_completion()
188 let optdir1 = &packpath . '/pack/mine/opt'
189 let optdir2 = &packpath . '/pack/candidate/opt'
190
191 call mkdir(optdir1 . '/pluginA', 'p')
192 call mkdir(optdir1 . '/pluginC', 'p')
193 call mkdir(optdir2 . '/pluginB', 'p')
194 call mkdir(optdir2 . '/pluginC', 'p')
195
196 let li = []
197 call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
198 call feedkeys(":packadd " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
199 call feedkeys(":packadd " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
200 call feedkeys(":packadd " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
201 call assert_equal("packadd pluginA", li[0])
202 call assert_equal("packadd pluginB", li[1])
203 call assert_equal("packadd pluginC", li[2])
204 call assert_equal("packadd ", li[3])
205endfunc
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100206
207func Test_packloadall()
Bram Moolenaar49b27322016-04-05 21:13:00 +0200208 " plugin foo with an autoload directory
209 let fooplugindir = &packpath . '/pack/mine/start/foo/plugin'
210 call mkdir(fooplugindir, 'p')
211 call writefile(['let g:plugin_foo_number = 1234',
212 \ 'let g:plugin_foo_auto = bbb#value',
213 \ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim')
214 let fooautodir = &packpath . '/pack/mine/start/foo/autoload'
215 call mkdir(fooautodir, 'p')
216 call writefile(['let bar#value = 77'], fooautodir . '/bar.vim')
217
218 " plugin aaa with an autoload directory
219 let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin'
220 call mkdir(aaaplugindir, 'p')
221 call writefile(['let g:plugin_aaa_number = 333',
222 \ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim')
223 let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload'
224 call mkdir(aaaautodir, 'p')
225 call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim')
226
227 " plugin extra with only an autoload directory
228 let extraautodir = &packpath . '/pack/mine/start/extra/autoload'
229 call mkdir(extraautodir, 'p')
230 call writefile(['let extra#value = 99'], extraautodir . '/extra.vim')
231
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100232 packloadall
233 call assert_equal(1234, g:plugin_foo_number)
Bram Moolenaar49b27322016-04-05 21:13:00 +0200234 call assert_equal(55, g:plugin_foo_auto)
235 call assert_equal(99, g:plugin_extra_auto)
236 call assert_equal(333, g:plugin_aaa_number)
237 call assert_equal(77, g:plugin_aaa_auto)
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100238
239 " only works once
Bram Moolenaar49b27322016-04-05 21:13:00 +0200240 call writefile(['let g:plugin_bar_number = 4321'], fooplugindir . '/bar2.vim')
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100241 packloadall
242 call assert_false(exists('g:plugin_bar_number'))
243
244 " works when ! used
245 packloadall!
246 call assert_equal(4321, g:plugin_bar_number)
247endfunc
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100248
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +0000249func Test_start_autoload()
250 " plugin foo with an autoload directory
251 let autodir = &packpath .. '/pack/mine/start/foo/autoload'
252 call mkdir(autodir, 'p')
253 let fname = autodir .. '/foobar.vim'
254 call writefile(['func foobar#test()',
255 \ ' return 1666',
256 \ 'endfunc'], fname)
257
258 call assert_equal(1666, foobar#test())
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +0000259endfunc
260
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100261func Test_helptags()
262 let docdir1 = &packpath . '/pack/mine/start/foo/doc'
263 let docdir2 = &packpath . '/pack/mine/start/bar/doc'
264 call mkdir(docdir1, 'p')
265 call mkdir(docdir2, 'p')
266 call writefile(['look here: *look-here*'], docdir1 . '/bar.txt')
267 call writefile(['look away: *look-away*'], docdir2 . '/foo.txt')
268 exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar'
269
270 helptags ALL
271
272 let tags1 = readfile(docdir1 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100273 call assert_match('look-here', tags1[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100274 let tags2 = readfile(docdir2 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100275 call assert_match('look-away', tags2[0])
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100276
277 call assert_fails('helptags abcxyz', 'E150:')
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100278endfunc
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100279
280func Test_colorscheme()
281 let colordirrun = &packpath . '/runtime/colors'
282 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
283 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
284 call mkdir(colordirrun, 'p')
285 call mkdir(colordirstart, 'p')
286 call mkdir(colordiropt, 'p')
287 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
288 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
289 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
290 exe 'set rtp=' . &packpath . '/runtime'
291
292 colorscheme one
293 call assert_equal(1, g:found_one)
294 colorscheme two
295 call assert_equal(1, g:found_two)
296 colorscheme three
297 call assert_equal(1, g:found_three)
298endfunc
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100299
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100300func Test_colorscheme_completion()
301 let colordirrun = &packpath . '/runtime/colors'
302 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
303 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
304 call mkdir(colordirrun, 'p')
305 call mkdir(colordirstart, 'p')
306 call mkdir(colordiropt, 'p')
307 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
308 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
309 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
310 exe 'set rtp=' . &packpath . '/runtime'
311
312 let li=[]
313 call feedkeys(":colorscheme " . repeat("\<Tab>", 1) . "')\<C-B>call add(li, '\<CR>", 't')
314 call feedkeys(":colorscheme " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
315 call feedkeys(":colorscheme " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
316 call feedkeys(":colorscheme " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
317 call assert_equal("colorscheme one", li[0])
318 call assert_equal("colorscheme three", li[1])
319 call assert_equal("colorscheme two", li[2])
320 call assert_equal("colorscheme ", li[3])
321endfunc
322
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100323func Test_runtime()
324 let rundir = &packpath . '/runtime/extra'
325 let startdir = &packpath . '/pack/mine/start/foo/extra'
326 let optdir = &packpath . '/pack/mine/opt/bar/extra'
327 call mkdir(rundir, 'p')
328 call mkdir(startdir, 'p')
329 call mkdir(optdir, 'p')
330 call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim')
331 call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim')
332 call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim')
333 call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim')
334 call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim')
335 exe 'set rtp=' . &packpath . '/runtime'
336
337 let g:sequence = ''
338 runtime extra/bar.vim
339 call assert_equal('run', g:sequence)
340 let g:sequence = ''
341 runtime START extra/bar.vim
342 call assert_equal('start', g:sequence)
343 let g:sequence = ''
344 runtime OPT extra/bar.vim
345 call assert_equal('opt', g:sequence)
346 let g:sequence = ''
347 runtime PACK extra/bar.vim
348 call assert_equal('start', g:sequence)
349 let g:sequence = ''
350 runtime! PACK extra/bar.vim
351 call assert_equal('startopt', g:sequence)
352 let g:sequence = ''
353 runtime PACK extra/xxx.vim
354 call assert_equal('xxxopt', g:sequence)
355
356 let g:sequence = ''
357 runtime ALL extra/bar.vim
358 call assert_equal('run', g:sequence)
359 let g:sequence = ''
360 runtime ALL extra/foo.vim
361 call assert_equal('foostart', g:sequence)
362 let g:sequence = ''
363 runtime! ALL extra/xxx.vim
364 call assert_equal('xxxopt', g:sequence)
365 let g:sequence = ''
366 runtime! ALL extra/bar.vim
367 call assert_equal('runstartopt', g:sequence)
368endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100369
370" vim: shiftwidth=2 sts=2 expandtab