xref: /vim-8.2.3635/src/testdir/test_packadd.vim (revision cf1ba35f)
1" Tests for 'packpath' and :packadd
2
3
4func SetUp()
5  let s:topdir = getcwd() . '/Xdir'
6  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
14func Test_packadd()
15  call mkdir(s:plugdir . '/plugin/also', 'p')
16  call mkdir(s:plugdir . '/ftdetect', 'p')
17  call mkdir(s:plugdir . '/after', 'p')
18  set rtp&
19  let rtp = &rtp
20  filetype on
21
22  exe 'split ' . s:plugdir . '/plugin/test.vim'
23  call setline(1, 'let g:plugin_works = 42')
24  wq
25
26  exe 'split ' . s:plugdir . '/plugin/also/loaded.vim'
27  call setline(1, 'let g:plugin_also_works = 77')
28  wq
29
30  exe 'split ' . s:plugdir . '/ftdetect/test.vim'
31  call setline(1, 'let g:ftdetect_works = 17')
32  wq
33
34  packadd mytest
35
36  call assert_equal(42, g:plugin_works)
37  call assert_equal(77, g:plugin_also_works)
38  call assert_equal(17, g:ftdetect_works)
39  call assert_true(len(&rtp) > len(rtp))
40  call assert_true(&rtp =~ '/testdir/Xdir/pack/mine/opt/mytest\($\|,\)')
41  call assert_true(&rtp =~ '/testdir/Xdir/pack/mine/opt/mytest/after$')
42
43  " Check exception
44  call assert_fails("packadd directorynotfound", 'E919:')
45  call assert_fails("packadd", 'E471:')
46endfunc
47
48func Test_packadd_noload()
49  call mkdir(s:plugdir . '/plugin', 'p')
50  call mkdir(s:plugdir . '/syntax', 'p')
51  set rtp&
52  let rtp = &rtp
53
54  exe 'split ' . s:plugdir . '/plugin/test.vim'
55  call setline(1, 'let g:plugin_works = 42')
56  wq
57  let g:plugin_works = 0
58
59  packadd! mytest
60
61  call assert_true(len(&rtp) > len(rtp))
62  call assert_true(&rtp =~ 'testdir/Xdir/pack/mine/opt/mytest\($\|,\)')
63  call assert_equal(0, g:plugin_works)
64
65  " check the path is not added twice
66  let new_rtp = &rtp
67  packadd! mytest
68  call assert_equal(new_rtp, &rtp)
69endfunc
70
71func Test_packadd_symlink_dir()
72  if !has('unix')
73    return
74  endif
75  let top2_dir = s:topdir . '/Xdir2'
76  let real_dir = s:topdir . '/Xsym'
77  call mkdir(real_dir, 'p')
78  exec "silent !ln -s Xsym"  top2_dir
79  let &rtp = top2_dir . ',' . top2_dir . '/after'
80  let &packpath = &rtp
81
82  let s:plugdir = top2_dir . '/pack/mine/opt/mytest'
83  call mkdir(s:plugdir . '/plugin', 'p')
84
85  exe 'split ' . s:plugdir . '/plugin/test.vim'
86  call setline(1, 'let g:plugin_works = 44')
87  wq
88  let g:plugin_works = 0
89
90  packadd mytest
91
92  " Must have been inserted in the middle, not at the end
93  call assert_true(&rtp =~ '/pack/mine/opt/mytest,')
94  call assert_equal(44, g:plugin_works)
95
96  " No change when doing it again.
97  let rtp_before = &rtp
98  packadd mytest
99  call assert_equal(rtp_before, &rtp)
100
101  set rtp&
102  let rtp = &rtp
103  exec "silent !rm" top2_dir
104endfunc
105
106" Check command-line completion for 'packadd'
107func Test_packadd_completion()
108  let optdir1 = &packpath . '/pack/mine/opt'
109  let optdir2 = &packpath . '/pack/candidate/opt'
110
111  call mkdir(optdir1 . '/pluginA', 'p')
112  call mkdir(optdir1 . '/pluginC', 'p')
113  call mkdir(optdir2 . '/pluginB', 'p')
114  call mkdir(optdir2 . '/pluginC', 'p')
115
116  let li = []
117  call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
118  call feedkeys(":packadd " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
119  call feedkeys(":packadd " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
120  call feedkeys(":packadd " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
121  call assert_equal("packadd pluginA", li[0])
122  call assert_equal("packadd pluginB", li[1])
123  call assert_equal("packadd pluginC", li[2])
124  call assert_equal("packadd ", li[3])
125endfunc
126
127func Test_packloadall()
128  " plugin foo with an autoload directory
129  let fooplugindir = &packpath . '/pack/mine/start/foo/plugin'
130  call mkdir(fooplugindir, 'p')
131  call writefile(['let g:plugin_foo_number = 1234',
132	\ 'let g:plugin_foo_auto = bbb#value',
133	\ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim')
134  let fooautodir = &packpath . '/pack/mine/start/foo/autoload'
135  call mkdir(fooautodir, 'p')
136  call writefile(['let bar#value = 77'], fooautodir . '/bar.vim')
137
138  " plugin aaa with an autoload directory
139  let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin'
140  call mkdir(aaaplugindir, 'p')
141  call writefile(['let g:plugin_aaa_number = 333',
142	\ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim')
143  let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload'
144  call mkdir(aaaautodir, 'p')
145  call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim')
146
147  " plugin extra with only an autoload directory
148  let extraautodir = &packpath . '/pack/mine/start/extra/autoload'
149  call mkdir(extraautodir, 'p')
150  call writefile(['let extra#value = 99'], extraautodir . '/extra.vim')
151
152  packloadall
153  call assert_equal(1234, g:plugin_foo_number)
154  call assert_equal(55, g:plugin_foo_auto)
155  call assert_equal(99, g:plugin_extra_auto)
156  call assert_equal(333, g:plugin_aaa_number)
157  call assert_equal(77, g:plugin_aaa_auto)
158
159  " only works once
160  call writefile(['let g:plugin_bar_number = 4321'], fooplugindir . '/bar2.vim')
161  packloadall
162  call assert_false(exists('g:plugin_bar_number'))
163
164  " works when ! used
165  packloadall!
166  call assert_equal(4321, g:plugin_bar_number)
167endfunc
168
169func Test_helptags()
170  let docdir1 = &packpath . '/pack/mine/start/foo/doc'
171  let docdir2 = &packpath . '/pack/mine/start/bar/doc'
172  call mkdir(docdir1, 'p')
173  call mkdir(docdir2, 'p')
174  call writefile(['look here: *look-here*'], docdir1 . '/bar.txt')
175  call writefile(['look away: *look-away*'], docdir2 . '/foo.txt')
176  exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar'
177
178  helptags ALL
179
180  let tags1 = readfile(docdir1 . '/tags')
181  call assert_true(tags1[0] =~ 'look-here')
182  let tags2 = readfile(docdir2 . '/tags')
183  call assert_true(tags2[0] =~ 'look-away')
184endfunc
185
186func Test_colorscheme()
187  let colordirrun = &packpath . '/runtime/colors'
188  let colordirstart = &packpath . '/pack/mine/start/foo/colors'
189  let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
190  call mkdir(colordirrun, 'p')
191  call mkdir(colordirstart, 'p')
192  call mkdir(colordiropt, 'p')
193  call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
194  call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
195  call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
196  exe 'set rtp=' . &packpath . '/runtime'
197
198  colorscheme one
199  call assert_equal(1, g:found_one)
200  colorscheme two
201  call assert_equal(1, g:found_two)
202  colorscheme three
203  call assert_equal(1, g:found_three)
204endfunc
205
206func Test_colorscheme_completion()
207  let colordirrun = &packpath . '/runtime/colors'
208  let colordirstart = &packpath . '/pack/mine/start/foo/colors'
209  let colordiropt = &packpath . '/pack/mine/opt/bar/colors'
210  call mkdir(colordirrun, 'p')
211  call mkdir(colordirstart, 'p')
212  call mkdir(colordiropt, 'p')
213  call writefile(['let g:found_one = 1'], colordirrun . '/one.vim')
214  call writefile(['let g:found_two = 1'], colordirstart . '/two.vim')
215  call writefile(['let g:found_three = 1'], colordiropt . '/three.vim')
216  exe 'set rtp=' . &packpath . '/runtime'
217
218  let li=[]
219  call feedkeys(":colorscheme " . repeat("\<Tab>", 1) . "')\<C-B>call add(li, '\<CR>", 't')
220  call feedkeys(":colorscheme " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't')
221  call feedkeys(":colorscheme " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't')
222  call feedkeys(":colorscheme " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx')
223  call assert_equal("colorscheme one", li[0])
224  call assert_equal("colorscheme three", li[1])
225  call assert_equal("colorscheme two", li[2])
226  call assert_equal("colorscheme ", li[3])
227endfunc
228
229func Test_runtime()
230  let rundir = &packpath . '/runtime/extra'
231  let startdir = &packpath . '/pack/mine/start/foo/extra'
232  let optdir = &packpath . '/pack/mine/opt/bar/extra'
233  call mkdir(rundir, 'p')
234  call mkdir(startdir, 'p')
235  call mkdir(optdir, 'p')
236  call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim')
237  call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim')
238  call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim')
239  call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim')
240  call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim')
241  exe 'set rtp=' . &packpath . '/runtime'
242
243  let g:sequence = ''
244  runtime extra/bar.vim
245  call assert_equal('run', g:sequence)
246  let g:sequence = ''
247  runtime START extra/bar.vim
248  call assert_equal('start', g:sequence)
249  let g:sequence = ''
250  runtime OPT extra/bar.vim
251  call assert_equal('opt', g:sequence)
252  let g:sequence = ''
253  runtime PACK extra/bar.vim
254  call assert_equal('start', g:sequence)
255  let g:sequence = ''
256  runtime! PACK extra/bar.vim
257  call assert_equal('startopt', g:sequence)
258  let g:sequence = ''
259  runtime PACK extra/xxx.vim
260  call assert_equal('xxxopt', g:sequence)
261
262  let g:sequence = ''
263  runtime ALL extra/bar.vim
264  call assert_equal('run', g:sequence)
265  let g:sequence = ''
266  runtime ALL extra/foo.vim
267  call assert_equal('foostart', g:sequence)
268  let g:sequence = ''
269  runtime! ALL extra/xxx.vim
270  call assert_equal('xxxopt', g:sequence)
271  let g:sequence = ''
272  runtime! ALL extra/bar.vim
273  call assert_equal('runstartopt', g:sequence)
274endfunc
275