blob: d7a38daf311e62c25bc2cb9c2c2d2a70493787e1 [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 Moolenaarcf1ba352017-10-27 00:55:04 +02006 let s:topdir = getcwd() . '/Xdir'
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 Moolenaar2374faa2018-02-04 17:47:42 +010054 call assert_match('/testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaar99396d42018-09-08 18:21:16 +020055
56 let new_after = match(&rtp, '/testdir/Xdir/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))
70 call assert_match('/testdir/Xdir/pack/mine/opt/myte\($\|,\)', &rtp)
71
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 Moolenaar2374faa2018-02-04 17:47:42 +010092 call assert_match('/testdir/Xdir/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 Moolenaar2374faa2018-02-04 17:47:42 +0100109 call assert_match('testdir/Xdir/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
249func Test_helptags()
250 let docdir1 = &packpath . '/pack/mine/start/foo/doc'
251 let docdir2 = &packpath . '/pack/mine/start/bar/doc'
252 call mkdir(docdir1, 'p')
253 call mkdir(docdir2, 'p')
254 call writefile(['look here: *look-here*'], docdir1 . '/bar.txt')
255 call writefile(['look away: *look-away*'], docdir2 . '/foo.txt')
256 exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar'
257
258 helptags ALL
259
260 let tags1 = readfile(docdir1 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100261 call assert_match('look-here', tags1[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100262 let tags2 = readfile(docdir2 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100263 call assert_match('look-away', tags2[0])
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100264
265 call assert_fails('helptags abcxyz', 'E150:')
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
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100357
358" vim: shiftwidth=2 sts=2 expandtab