blob: 6d0565b86438f8a7f5984e708cba81d53adaceac [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,')
Bram Moolenaar53c8a472018-09-08 19:12:12 +020056 let forwarded = substitute(first_after_entry, '\\', '[/\\\\]', 'g')
57 let old_after = match(&rtp, ',' . forwarded . '\>')
Bram Moolenaar99396d42018-09-08 18:21:16 +020058 call assert_true(new_after > 0, 'rtp is ' . &rtp)
Bram Moolenaar53c8a472018-09-08 19:12:12 +020059 call assert_true(old_after > 0, 'match ' . forwarded . ' in ' . &rtp)
Bram Moolenaar99396d42018-09-08 18:21:16 +020060 call assert_true(new_after < old_after, 'rtp is ' . &rtp)
Bram Moolenaarbe82c252016-03-06 14:44:08 +010061
Bram Moolenaarf98a39c2018-04-18 22:18:23 +020062 " NOTE: '/.../opt/myte' forwardly matches with '/.../opt/mytest'
63 call mkdir(fnamemodify(s:plugdir, ':h') . '/myte', 'p')
64 let rtp = &rtp
65 packadd myte
66
67 " Check the path of 'myte' is added
68 call assert_true(len(&rtp) > len(rtp))
69 call assert_match('/testdir/Xdir/pack/mine/opt/myte\($\|,\)', &rtp)
70
Bram Moolenaarbe82c252016-03-06 14:44:08 +010071 " Check exception
72 call assert_fails("packadd directorynotfound", 'E919:')
73 call assert_fails("packadd", 'E471:')
Bram Moolenaar91715872016-03-03 17:13:03 +010074endfunc
75
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010076func Test_packadd_start()
77 let plugdir = s:topdir . '/pack/mine/start/other'
78 call mkdir(plugdir . '/plugin', 'p')
79 set rtp&
80 let rtp = &rtp
81 filetype on
82
83 exe 'split ' . plugdir . '/plugin/test.vim'
84 call setline(1, 'let g:plugin_works = 24')
85 wq
86
87 packadd other
88
89 call assert_equal(24, g:plugin_works)
90 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar2374faa2018-02-04 17:47:42 +010091 call assert_match('/testdir/Xdir/pack/mine/start/other\($\|,\)', &rtp)
Bram Moolenaar9e1d3992017-12-17 14:26:46 +010092endfunc
93
Bram Moolenaarf3654822016-03-04 22:12:23 +010094func Test_packadd_noload()
95 call mkdir(s:plugdir . '/plugin', 'p')
Bram Moolenaar91715872016-03-03 17:13:03 +010096 call mkdir(s:plugdir . '/syntax', 'p')
97 set rtp&
98 let rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +010099
100 exe 'split ' . s:plugdir . '/plugin/test.vim'
101 call setline(1, 'let g:plugin_works = 42')
102 wq
103 let g:plugin_works = 0
104
105 packadd! mytest
106
Bram Moolenaar91715872016-03-03 17:13:03 +0100107 call assert_true(len(&rtp) > len(rtp))
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100108 call assert_match('testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp)
Bram Moolenaarf3654822016-03-04 22:12:23 +0100109 call assert_equal(0, g:plugin_works)
Bram Moolenaar91715872016-03-03 17:13:03 +0100110
111 " check the path is not added twice
112 let new_rtp = &rtp
Bram Moolenaarf3654822016-03-04 22:12:23 +0100113 packadd! mytest
Bram Moolenaar91715872016-03-03 17:13:03 +0100114 call assert_equal(new_rtp, &rtp)
Bram Moolenaar863c1a92016-03-03 15:47:06 +0100115endfunc
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100117func Test_packadd_symlink_dir()
118 if !has('unix')
Bram Moolenaar644df412017-03-09 13:58:02 +0100119 return
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100120 endif
121 let top2_dir = s:topdir . '/Xdir2'
122 let real_dir = s:topdir . '/Xsym'
Bram Moolenaar644df412017-03-09 13:58:02 +0100123 call mkdir(real_dir, 'p')
124 exec "silent !ln -s Xsym" top2_dir
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100125 let &rtp = top2_dir . ',' . top2_dir . '/after'
126 let &packpath = &rtp
127
128 let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
129 call mkdir(s:plugdir . '/plugin', 'p')
130
131 exe 'split ' . s:plugdir . '/plugin/test.vim'
132 call setline(1, 'let g:plugin_works = 44')
133 wq
134 let g:plugin_works = 0
135
136 packadd mytest
137
138 " Must have been inserted in the middle, not at the end
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100139 call assert_match('/pack/mine/opt/mytest,', &rtp)
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100140 call assert_equal(44, g:plugin_works)
141
142 " No change when doing it again.
143 let rtp_before = &rtp
144 packadd mytest
145 call assert_equal(rtp_before, &rtp)
146
147 set rtp&
148 let rtp = &rtp
Bram Moolenaar24f8f542017-02-11 23:00:36 +0100149 exec "silent !rm" top2_dir
Bram Moolenaar2f9e5752017-02-05 16:07:54 +0100150endfunc
151
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100152func Test_packadd_symlink_dir2()
153 if !has('unix')
154 return
155 endif
156 let top2_dir = s:topdir . '/Xdir2'
157 let real_dir = s:topdir . '/Xsym/pack'
158 call mkdir(top2_dir, 'p')
159 call mkdir(real_dir, 'p')
160 let &rtp = top2_dir . ',' . top2_dir . '/after'
161 let &packpath = &rtp
162
163 exec "silent !ln -s ../Xsym/pack" top2_dir . '/pack'
164 let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
165 call mkdir(s:plugdir . '/plugin', 'p')
166
167 exe 'split ' . s:plugdir . '/plugin/test.vim'
168 call setline(1, 'let g:plugin_works = 48')
169 wq
170 let g:plugin_works = 0
171
172 packadd mytest
173
174 " Must have been inserted in the middle, not at the end
175 call assert_match('/Xdir2/pack/mine/opt/mytest,', &rtp)
176 call assert_equal(48, g:plugin_works)
177
178 " No change when doing it again.
179 let rtp_before = &rtp
180 packadd mytest
181 call assert_equal(rtp_before, &rtp)
182
183 set rtp&
184 let rtp = &rtp
185 exec "silent !rm" top2_dir . '/pack'
186 exec "silent !rmdir" top2_dir
187endfunc
188
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100189" Check command-line completion for 'packadd'
190func Test_packadd_completion()
191 let optdir1 = &packpath . '/pack/mine/opt'
192 let optdir2 = &packpath . '/pack/candidate/opt'
193
194 call mkdir(optdir1 . '/pluginA', 'p')
195 call mkdir(optdir1 . '/pluginC', 'p')
196 call mkdir(optdir2 . '/pluginB', 'p')
197 call mkdir(optdir2 . '/pluginC', 'p')
198
199 let li = []
200 call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
201 call feedkeys(":packadd " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
202 call feedkeys(":packadd " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
203 call feedkeys(":packadd " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
204 call assert_equal("packadd pluginA", li[0])
205 call assert_equal("packadd pluginB", li[1])
206 call assert_equal("packadd pluginC", li[2])
207 call assert_equal("packadd ", li[3])
208endfunc
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100209
210func Test_packloadall()
Bram Moolenaar49b27322016-04-05 21:13:00 +0200211 " plugin foo with an autoload directory
212 let fooplugindir = &packpath . '/pack/mine/start/foo/plugin'
213 call mkdir(fooplugindir, 'p')
214 call writefile(['let g:plugin_foo_number = 1234',
215 \ 'let g:plugin_foo_auto = bbb#value',
216 \ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim')
217 let fooautodir = &packpath . '/pack/mine/start/foo/autoload'
218 call mkdir(fooautodir, 'p')
219 call writefile(['let bar#value = 77'], fooautodir . '/bar.vim')
220
221 " plugin aaa with an autoload directory
222 let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin'
223 call mkdir(aaaplugindir, 'p')
224 call writefile(['let g:plugin_aaa_number = 333',
225 \ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim')
226 let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload'
227 call mkdir(aaaautodir, 'p')
228 call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim')
229
230 " plugin extra with only an autoload directory
231 let extraautodir = &packpath . '/pack/mine/start/extra/autoload'
232 call mkdir(extraautodir, 'p')
233 call writefile(['let extra#value = 99'], extraautodir . '/extra.vim')
234
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100235 packloadall
236 call assert_equal(1234, g:plugin_foo_number)
Bram Moolenaar49b27322016-04-05 21:13:00 +0200237 call assert_equal(55, g:plugin_foo_auto)
238 call assert_equal(99, g:plugin_extra_auto)
239 call assert_equal(333, g:plugin_aaa_number)
240 call assert_equal(77, g:plugin_aaa_auto)
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100241
242 " only works once
Bram Moolenaar49b27322016-04-05 21:13:00 +0200243 call writefile(['let g:plugin_bar_number = 4321'], fooplugindir . '/bar2.vim')
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100244 packloadall
245 call assert_false(exists('g:plugin_bar_number'))
246
247 " works when ! used
248 packloadall!
249 call assert_equal(4321, g:plugin_bar_number)
250endfunc
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100251
252func Test_helptags()
253 let docdir1 = &packpath . '/pack/mine/start/foo/doc'
254 let docdir2 = &packpath . '/pack/mine/start/bar/doc'
255 call mkdir(docdir1, 'p')
256 call mkdir(docdir2, 'p')
257 call writefile(['look here: *look-here*'], docdir1 . '/bar.txt')
258 call writefile(['look away: *look-away*'], docdir2 . '/foo.txt')
259 exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar'
260
261 helptags ALL
262
263 let tags1 = readfile(docdir1 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100264 call assert_match('look-here', tags1[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100265 let tags2 = readfile(docdir2 . '/tags')
Bram Moolenaar2374faa2018-02-04 17:47:42 +0100266 call assert_match('look-away', tags2[0])
Bram Moolenaar6bef5302016-03-12 21:28:26 +0100267endfunc
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100268
269func Test_colorscheme()
270 let colordirrun = &packpath . '/runtime/colors'
271 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
272 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
273 call mkdir(colordirrun, 'p')
274 call mkdir(colordirstart, 'p')
275 call mkdir(colordiropt, 'p')
276 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
277 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
278 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
279 exe 'set rtp=' . &packpath . '/runtime'
280
281 colorscheme one
282 call assert_equal(1, g:found_one)
283 colorscheme two
284 call assert_equal(1, g:found_two)
285 colorscheme three
286 call assert_equal(1, g:found_three)
287endfunc
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100288
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100289func Test_colorscheme_completion()
290 let colordirrun = &packpath . '/runtime/colors'
291 let colordirstart = &packpath . '/pack/mine/start/foo/colors'
292 let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
293 call mkdir(colordirrun, 'p')
294 call mkdir(colordirstart, 'p')
295 call mkdir(colordiropt, 'p')
296 call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
297 call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
298 call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
299 exe 'set rtp=' . &packpath . '/runtime'
300
301 let li=[]
302 call feedkeys(":colorscheme " . repeat("\<Tab>", 1) . "')\<C-B>call add(li, '\<CR>", 't')
303 call feedkeys(":colorscheme " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
304 call feedkeys(":colorscheme " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
305 call feedkeys(":colorscheme " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
306 call assert_equal("colorscheme one", li[0])
307 call assert_equal("colorscheme three", li[1])
308 call assert_equal("colorscheme two", li[2])
309 call assert_equal("colorscheme ", li[3])
310endfunc
311
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100312func Test_runtime()
313 let rundir = &packpath . '/runtime/extra'
314 let startdir = &packpath . '/pack/mine/start/foo/extra'
315 let optdir = &packpath . '/pack/mine/opt/bar/extra'
316 call mkdir(rundir, 'p')
317 call mkdir(startdir, 'p')
318 call mkdir(optdir, 'p')
319 call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim')
320 call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim')
321 call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim')
322 call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim')
323 call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim')
324 exe 'set rtp=' . &packpath . '/runtime'
325
326 let g:sequence = ''
327 runtime extra/bar.vim
328 call assert_equal('run', g:sequence)
329 let g:sequence = ''
330 runtime START extra/bar.vim
331 call assert_equal('start', g:sequence)
332 let g:sequence = ''
333 runtime OPT extra/bar.vim
334 call assert_equal('opt', g:sequence)
335 let g:sequence = ''
336 runtime PACK extra/bar.vim
337 call assert_equal('start', g:sequence)
338 let g:sequence = ''
339 runtime! PACK extra/bar.vim
340 call assert_equal('startopt', g:sequence)
341 let g:sequence = ''
342 runtime PACK extra/xxx.vim
343 call assert_equal('xxxopt', g:sequence)
344
345 let g:sequence = ''
346 runtime ALL extra/bar.vim
347 call assert_equal('run', g:sequence)
348 let g:sequence = ''
349 runtime ALL extra/foo.vim
350 call assert_equal('foostart', g:sequence)
351 let g:sequence = ''
352 runtime! ALL extra/xxx.vim
353 call assert_equal('xxxopt', g:sequence)
354 let g:sequence = ''
355 runtime! ALL extra/bar.vim
356 call assert_equal('runstartopt', g:sequence)
357endfunc