blob: 15f82e2f6a3d76d733aff70940f8ef99b6b44cbf [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 Brabandt4e7249a2024-09-05 17:46:19 +020024 call writefile([expand('$MYVIMRCDIR')], "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.
61 let file1 =<< trim CODE
62 let g:rc_one = 'one'
63 let g:rc = '.vimrc'
64 CODE
65 let file2 =<< trim CODE
66 let g:rc_two = 'two'
67 let g:rc = '.vim/vimrc'
68 CODE
69 let file3 =<< trim CODE
70 let g:rc_three = 'three'
71 let g:rc = '.config/vim/vimrc'
72 CODE
73 let file4 =<< trim CODE
74 let g:rc_four = 'four'
75 let g:rc = 'xdg/vim/vimrc'
76 CODE
77 call writefile(file1, rc1)
78 call writefile(file2, rc2)
79 call writefile(file3, rc3)
80 call writefile(file4, rc4)
81
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020082 " Get the Vim command to run without the '-u NONE' argument
83 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
84
85 " Test for ~/.vimrc
86 let lines =<< trim END
87 call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020088 call assert_match('XfakeHOME/.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020089 call filter(g:, {idx, _ -> idx =~ '^rc'})
90 call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +020091 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020092 call writefile(v:errors, 'Xresult')
93 quit
94 END
95 call writefile(lines, 'Xscript', 'D')
96 call system($'{vimcmd} -S Xscript')
97 call assert_equal([], readfile('Xresult'))
98
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020099 call delete(rc1)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200100
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200101 " Test for ~/.vim/vimrc
102 let lines =<< trim END
103 call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200104 call assert_match('XfakeHOME/\.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200105 call filter(g:, {idx, _ -> idx =~ '^rc'})
106 call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200107 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200108 call writefile(v:errors, 'Xresult')
109 quit
110 END
111 call writefile(lines, 'Xscript', 'D')
112 call system($'{vimcmd} -S Xscript')
113 call assert_equal([], readfile('Xresult'))
114
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200115 call delete(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200116
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200117 " XDG_CONFIG_HOME is set in Github CI runners
118 unlet $XDG_CONFIG_HOME
119
120 " Test for ~/.config/vim/vimrc
121 let lines =<< trim END
122 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
123 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200124 call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200125 call filter(g:, {idx, _ -> idx =~ '^rc'})
126 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200127 call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200128 call writefile(v:errors, 'Xresult')
129 quit
130 END
131 call writefile(lines, 'Xscript', 'D')
132 call system($'{vimcmd} -S Xscript')
133 call assert_equal([], readfile('Xresult'))
134
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200135 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200136
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200137 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200138 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200139 let lines =<< trim END
140 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
141 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200142 call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200143 call filter(g:, {idx, _ -> idx =~ '^rc'})
144 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200145 call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200146 call writefile(v:errors, 'Xresult')
147 quit
148 END
149 call writefile(lines, 'Xscript', 'D')
150 call system($'{vimcmd} -S Xscript')
151 call assert_equal([], readfile('Xresult'))
152
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100153 " Test for $MYVIMDIR changes when updating runtimepath
154 let lines =<< trim END
155 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}" rtp-prepend'
156 set rtp^=/non-existing
157 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
158 call assert_match('/non-existing', $MYVIMDIR, msg)
159 call writefile(v:errors, 'Xresult')
160 quit
161 END
162 call writefile(lines, 'Xscript', 'D')
163 call system($'{vimcmd} -S Xscript')
164 call assert_equal([], readfile('Xresult'))
165
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200166 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200167 unlet $XDG_CONFIG_HOME
168endfunc
169
Diego Violad1068a22024-04-16 20:58:45 +0200170func Test_xdg_version()
171 CheckUnix
172 let $HOME = getcwd() .. '/XfakeHOME'
173 unlet $XDG_CONFIG_HOME
174 let a = execute(':version')->split('\n')
175 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200176 " There should be 1 entry for gvimrc and 1 entry for vimrc,
177 " but only if Vim was compiled with gui support
178 call assert_equal(1 + has("gui"), len(a))
179 call assert_match('\~/\.config/vim/vimrc', a[0])
180 if has("gui")
181 call assert_match('\~/\.config/vim/gvimrc', a[1])
182 endif
Diego Violad1068a22024-04-16 20:58:45 +0200183
184 let $XDG_CONFIG_HOME = expand('~/.xdg')
185 let a = execute(':version')->split('\n')
186 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200187 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200188 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200189 if has("gui")
190 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
191 endif
Diego Violad1068a22024-04-16 20:58:45 +0200192 unlet $XDG_CONFIG_HOME
193endfunc
194
Maxim Kima34ba822024-04-17 22:29:06 +0200195" Test for gvimrc, must be last, since it starts the GUI
196" and sources a few extra test files
197func Test_zzz_xdg_runtime_files()
198 CheckCanRunGui
199 CheckUnix
200
201 " Is setup in Github Runner
202 unlet $XDG_CONFIG_HOME
203 source setup_gui.vim
204 call GUISetUpCommon()
205
Diego Viola133ed2a2024-04-18 20:54:06 +0200206 " This tests, that the GUI initialization file from
207 " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
208 " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
209 " when starting GUI mode
Maxim Kima34ba822024-04-17 22:29:06 +0200210 call mkdir(expand('~/.vim/'), 'pD')
211 call mkdir(expand('~/.config/vim/'), 'pD')
212 call mkdir(expand('~/xdg/vim/'), 'pD')
213
214 let rc1=expand('~/.gvimrc')
215 let rc2=expand('~/.vim/gvimrc')
216 let rc3=expand('~/.config/vim/gvimrc')
217 let rc4=expand('~/xdg/vim/gvimrc')
218
219 " g:rc_one|two|three|four is to verify, that the other
220 " init files are not sourced
221 " g:rc is to verify which rc file has been loaded.
222 let file1 =<< trim CODE
223 let g:rc_one = 'one'
224 let g:rc = '.gvimrc'
225 CODE
226 let file2 =<< trim CODE
227 let g:rc_two = 'two'
228 let g:rc = '.vim/gvimrc'
229 CODE
230 let file3 =<< trim CODE
231 let g:rc_three = 'three'
232 let g:rc = '.config/vim/gvimrc'
233 CODE
234 let file4 =<< trim CODE
235 let g:rc_four = 'four'
236 let g:rc = 'xdg/vim/gvimrc'
237 CODE
238 call writefile(file1, rc1)
239 call writefile(file2, rc2)
240 call writefile(file3, rc3)
241 call writefile(file4, rc4)
242
243 " Get the Vim command to run without the '-u NONE' argument
244 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
245
246 " Test for ~/.gvimrc
247 let lines =<< trim END
248 " Ignore the "failed to create input context" error.
249 call test_ignore_error('E285')
250 gui -f
251 call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200252 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200253 call filter(g:, {idx, _ -> idx =~ '^rc'})
254 call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
255 call writefile(v:errors, 'Xresult')
256 quit
257 END
258 call writefile(lines, 'Xscript', 'D')
259 call system($'{vimcmd} -S Xscript')
260 call assert_equal([], readfile('Xresult'))
261
262 call delete(rc1)
263
264 " Test for ~/.vim/gvimrc
265 let lines =<< trim END
266 " Ignore the "failed to create input context" error.
267 call test_ignore_error('E285')
268 gui -f
269 call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200270 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200271 call filter(g:, {idx, _ -> idx =~ '^rc'})
272 call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
273 call writefile(v:errors, 'Xresult')
274 quit
275 END
276 call writefile(lines, 'Xscript', 'D')
277 call system($'{vimcmd} -S Xscript')
278 call assert_equal([], readfile('Xresult'))
279
280 call delete(rc2)
281
Maxim Kima34ba822024-04-17 22:29:06 +0200282 " Test for ~/.config/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100283 " MYVIMDIR is only set to ~/config/.vim if ~/.config/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200284 let lines =<< trim END
285 " Ignore the "failed to create input context" error.
286 call test_ignore_error('E285')
287 gui -f
288 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
289 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100290 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200291 call filter(g:, {idx, _ -> idx =~ '^rc'})
292 call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
293 call writefile(v:errors, 'Xresult')
294 quit
295 END
296 call writefile(lines, 'Xscript', 'D')
297 call system($'{vimcmd} -S Xscript')
298 call assert_equal([], readfile('Xresult'))
299
300 call delete(rc3)
301
302 " Test for ~/xdg/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100303 " MYVIMDIR is only set to ~/xdg/vim if ~/xdg/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200304 let $XDG_CONFIG_HOME=expand('~/xdg/')
305 let lines =<< trim END
306 " Ignore the "failed to create input context" error.
307 call test_ignore_error('E285')
308 gui -f
309 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
310 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100311 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200312 call filter(g:, {idx, _ -> idx =~ '^rc'})
313 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
314 call writefile(v:errors, 'Xresult')
315 quit
316 END
317 call writefile(lines, 'Xscript', 'D')
318 call system($'{vimcmd} -S Xscript')
319 call assert_equal([], readfile('Xresult'))
320
321 call delete(rc4)
322
323 " Clean up
324 unlet $XDG_CONFIG_HOME
325 call GUITearDownCommon()
326 call delete('Xhome', 'rf')
327endfunc
328
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200329" vim: shiftwidth=2 sts=2 expandtab