blob: b7abd04e6d831f025d91d406fe67081a1ed2688e [file] [log] [blame]
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02001" Tests for the XDG feature
2
3source check.vim
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02004source shared.vim
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02005
6func s:get_rcs()
7 let rcs = {
Christian Brabandt4e7249a2024-09-05 17:46:19 +02008 \ 'file1': { 'path': '~/.vimrc', 'dir': expand('~/.vim/') },
9 \ 'file2': { 'path': '~/.vim/vimrc', 'dir': expand('~/.vim/') },
10 \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config",
11 \ 'dir': exists('$XDG_CONFIG_HOME') ? expand("$XDG_CONFIG_HOME/vim") : '~/.config/vim/'},
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020012 \}
13 for v in values(rcs)
14 let v.exists = filereadable(expand(v.path))
15 endfor
16 return rcs
17endfunc
18
19func Test_xdg_rc_detection()
20 CheckUnix
21 let rc = s:get_rcs()
22 let before =<< trim CODE
23 call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
Christian Brabandt1a741d32025-03-01 16:30:33 +010024 call writefile([expand('$MYVIMDIR')], "XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020025 quit!
26 CODE
27 call RunVim(before, [], "")
28 let my_rc = readfile("XMY_VIMRC")
Christian Brabandt4e7249a2024-09-05 17:46:19 +020029 let my_rcdir = readfile("XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020030 if rc.file1.exists
31 call assert_equal(rc.file1.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020032 call assert_equal(rc.file1.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020033 elseif !rc.file1.exists && rc.file2.exists
34 call assert_equal(rc.file2.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020035 call assert_equal(rc.file2.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020036 elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
37 call assert_equal(rc.xdg.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020038 call assert_equal(rc.xdg.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020039 endif
40 call delete("XMY_VIMRC")
Christian Brabandt4e7249a2024-09-05 17:46:19 +020041 call delete("XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020042endfunc
43
44func Test_xdg_runtime_files()
45 " This tests, that the initialization file from
46 " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
Diego Viola133ed2a2024-04-18 20:54:06 +020047 " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020048 CheckUnix
49 call mkdir(expand('~/.vim/'), 'pD')
50 call mkdir(expand('~/.config/vim/'), 'pD')
51 call mkdir(expand('~/xdg/vim/'), 'pD')
Maxim Kima34ba822024-04-17 22:29:06 +020052
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020053 let rc1=expand('~/.vimrc')
54 let rc2=expand('~/.vim/vimrc')
55 let rc3=expand('~/.config/vim/vimrc')
56 let rc4=expand('~/xdg/vim/vimrc')
57
58 " g:rc_one|two|three|four is to verify, that the other
Diego Viola2da68c82024-04-15 20:08:38 +020059 " init files are not sourced
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020060 " g:rc is to verify which rc file has been loaded.
Christian Brabandt1a741d32025-03-01 16:30:33 +010061 " g:rc_vimdir is to verify $MYVIMDIR is set and valid
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020062 let file1 =<< trim CODE
63 let g:rc_one = 'one'
64 let g:rc = '.vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010065 let g:rc_vimdir = expand('~/.vim/')
66 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020067 CODE
68 let file2 =<< trim CODE
69 let g:rc_two = 'two'
70 let g:rc = '.vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010071 let g:rc_vimdir = expand('~/.vim/')
72 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020073 CODE
74 let file3 =<< trim CODE
75 let g:rc_three = 'three'
76 let g:rc = '.config/vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010077 let g:rc_vimdir = expand('~/.config/vim/')
78 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020079 CODE
80 let file4 =<< trim CODE
81 let g:rc_four = 'four'
82 let g:rc = 'xdg/vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010083 let g:rc_vimdir = expand('~/xdg/vim/')
84 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020085 CODE
86 call writefile(file1, rc1)
87 call writefile(file2, rc2)
88 call writefile(file3, rc3)
89 call writefile(file4, rc4)
90
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020091 " Get the Vim command to run without the '-u NONE' argument
92 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
93
94 " Test for ~/.vimrc
95 let lines =<< trim END
96 call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020097 call assert_match('XfakeHOME/.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020098 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +010099 call assert_equal(#{rc_one: 'one', rc: '.vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200100 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200101 call writefile(v:errors, 'Xresult')
102 quit
103 END
104 call writefile(lines, 'Xscript', 'D')
105 call system($'{vimcmd} -S Xscript')
106 call assert_equal([], readfile('Xresult'))
107
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200108 call delete(rc1)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200109
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200110 " Test for ~/.vim/vimrc
111 let lines =<< trim END
112 call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200113 call assert_match('XfakeHOME/\.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200114 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100115 call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200116 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200117 call writefile(v:errors, 'Xresult')
118 quit
119 END
120 call writefile(lines, 'Xscript', 'D')
121 call system($'{vimcmd} -S Xscript')
122 call assert_equal([], readfile('Xresult'))
123
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200124 call delete(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200125
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200126 " XDG_CONFIG_HOME is set in Github CI runners
127 unlet $XDG_CONFIG_HOME
128
129 " Test for ~/.config/vim/vimrc
130 let lines =<< trim END
131 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
132 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200133 call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200134 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100135 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200136 call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200137 call writefile(v:errors, 'Xresult')
138 quit
139 END
140 call writefile(lines, 'Xscript', 'D')
141 call system($'{vimcmd} -S Xscript')
142 call assert_equal([], readfile('Xresult'))
143
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200144 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200145
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200146 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200147 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200148 let lines =<< trim END
149 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
150 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200151 call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200152 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100153 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200154 call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200155 call writefile(v:errors, 'Xresult')
156 quit
157 END
158 call writefile(lines, 'Xscript', 'D')
159 call system($'{vimcmd} -S Xscript')
160 call assert_equal([], readfile('Xresult'))
161
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100162 " Test for $MYVIMDIR changes when updating runtimepath
163 let lines =<< trim END
164 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}" rtp-prepend'
165 set rtp^=/non-existing
166 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
167 call assert_match('/non-existing', $MYVIMDIR, msg)
168 call writefile(v:errors, 'Xresult')
169 quit
170 END
171 call writefile(lines, 'Xscript', 'D')
172 call system($'{vimcmd} -S Xscript')
173 call assert_equal([], readfile('Xresult'))
174
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200175 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200176 unlet $XDG_CONFIG_HOME
177endfunc
178
Diego Violad1068a22024-04-16 20:58:45 +0200179func Test_xdg_version()
180 CheckUnix
181 let $HOME = getcwd() .. '/XfakeHOME'
182 unlet $XDG_CONFIG_HOME
183 let a = execute(':version')->split('\n')
184 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200185 " There should be 1 entry for gvimrc and 1 entry for vimrc,
186 " but only if Vim was compiled with gui support
187 call assert_equal(1 + has("gui"), len(a))
188 call assert_match('\~/\.config/vim/vimrc', a[0])
189 if has("gui")
190 call assert_match('\~/\.config/vim/gvimrc', a[1])
191 endif
Diego Violad1068a22024-04-16 20:58:45 +0200192
193 let $XDG_CONFIG_HOME = expand('~/.xdg')
194 let a = execute(':version')->split('\n')
195 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200196 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200197 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200198 if has("gui")
199 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
200 endif
Diego Violad1068a22024-04-16 20:58:45 +0200201 unlet $XDG_CONFIG_HOME
202endfunc
203
Maxim Kima34ba822024-04-17 22:29:06 +0200204" Test for gvimrc, must be last, since it starts the GUI
205" and sources a few extra test files
206func Test_zzz_xdg_runtime_files()
207 CheckCanRunGui
208 CheckUnix
209
210 " Is setup in Github Runner
211 unlet $XDG_CONFIG_HOME
212 source setup_gui.vim
213 call GUISetUpCommon()
214
Diego Viola133ed2a2024-04-18 20:54:06 +0200215 " This tests, that the GUI initialization file from
216 " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
217 " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
218 " when starting GUI mode
Maxim Kima34ba822024-04-17 22:29:06 +0200219 call mkdir(expand('~/.vim/'), 'pD')
220 call mkdir(expand('~/.config/vim/'), 'pD')
221 call mkdir(expand('~/xdg/vim/'), 'pD')
222
223 let rc1=expand('~/.gvimrc')
224 let rc2=expand('~/.vim/gvimrc')
225 let rc3=expand('~/.config/vim/gvimrc')
226 let rc4=expand('~/xdg/vim/gvimrc')
227
228 " g:rc_one|two|three|four is to verify, that the other
229 " init files are not sourced
230 " g:rc is to verify which rc file has been loaded.
231 let file1 =<< trim CODE
232 let g:rc_one = 'one'
233 let g:rc = '.gvimrc'
234 CODE
235 let file2 =<< trim CODE
236 let g:rc_two = 'two'
237 let g:rc = '.vim/gvimrc'
238 CODE
239 let file3 =<< trim CODE
240 let g:rc_three = 'three'
241 let g:rc = '.config/vim/gvimrc'
242 CODE
243 let file4 =<< trim CODE
244 let g:rc_four = 'four'
245 let g:rc = 'xdg/vim/gvimrc'
246 CODE
247 call writefile(file1, rc1)
248 call writefile(file2, rc2)
249 call writefile(file3, rc3)
250 call writefile(file4, rc4)
251
252 " Get the Vim command to run without the '-u NONE' argument
253 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
254
255 " Test for ~/.gvimrc
256 let lines =<< trim END
257 " Ignore the "failed to create input context" error.
258 call test_ignore_error('E285')
259 gui -f
260 call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200261 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200262 call filter(g:, {idx, _ -> idx =~ '^rc'})
263 call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
264 call writefile(v:errors, 'Xresult')
265 quit
266 END
267 call writefile(lines, 'Xscript', 'D')
268 call system($'{vimcmd} -S Xscript')
269 call assert_equal([], readfile('Xresult'))
270
271 call delete(rc1)
272
273 " Test for ~/.vim/gvimrc
274 let lines =<< trim END
275 " Ignore the "failed to create input context" error.
276 call test_ignore_error('E285')
277 gui -f
278 call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200279 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200280 call filter(g:, {idx, _ -> idx =~ '^rc'})
281 call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
282 call writefile(v:errors, 'Xresult')
283 quit
284 END
285 call writefile(lines, 'Xscript', 'D')
286 call system($'{vimcmd} -S Xscript')
287 call assert_equal([], readfile('Xresult'))
288
289 call delete(rc2)
290
Maxim Kima34ba822024-04-17 22:29:06 +0200291 " Test for ~/.config/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100292 " MYVIMDIR is only set to ~/config/.vim if ~/.config/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200293 let lines =<< trim END
294 " Ignore the "failed to create input context" error.
295 call test_ignore_error('E285')
296 gui -f
297 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
298 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100299 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200300 call filter(g:, {idx, _ -> idx =~ '^rc'})
301 call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
302 call writefile(v:errors, 'Xresult')
303 quit
304 END
305 call writefile(lines, 'Xscript', 'D')
306 call system($'{vimcmd} -S Xscript')
307 call assert_equal([], readfile('Xresult'))
308
309 call delete(rc3)
310
311 " Test for ~/xdg/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100312 " MYVIMDIR is only set to ~/xdg/vim if ~/xdg/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200313 let $XDG_CONFIG_HOME=expand('~/xdg/')
314 let lines =<< trim END
315 " Ignore the "failed to create input context" error.
316 call test_ignore_error('E285')
317 gui -f
318 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
319 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100320 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200321 call filter(g:, {idx, _ -> idx =~ '^rc'})
322 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
323 call writefile(v:errors, 'Xresult')
324 quit
325 END
326 call writefile(lines, 'Xscript', 'D')
327 call system($'{vimcmd} -S Xscript')
328 call assert_equal([], readfile('Xresult'))
329
330 call delete(rc4)
331
332 " Clean up
333 unlet $XDG_CONFIG_HOME
334 call GUITearDownCommon()
335 call delete('Xhome', 'rf')
336endfunc
337
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200338" vim: shiftwidth=2 sts=2 expandtab