blob: abded475515d20173342429375fa65f671ae9a9a [file] [log] [blame]
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02001" Tests for the XDG feature
2
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02003func s:get_rcs()
4 let rcs = {
Christian Brabandt4e7249a2024-09-05 17:46:19 +02005 \ 'file1': { 'path': '~/.vimrc', 'dir': expand('~/.vim/') },
6 \ 'file2': { 'path': '~/.vim/vimrc', 'dir': expand('~/.vim/') },
7 \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config",
8 \ 'dir': exists('$XDG_CONFIG_HOME') ? expand("$XDG_CONFIG_HOME/vim") : '~/.config/vim/'},
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02009 \}
10 for v in values(rcs)
11 let v.exists = filereadable(expand(v.path))
12 endfor
13 return rcs
14endfunc
15
16func Test_xdg_rc_detection()
17 CheckUnix
18 let rc = s:get_rcs()
19 let before =<< trim CODE
20 call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
Christian Brabandt1a741d32025-03-01 16:30:33 +010021 call writefile([expand('$MYVIMDIR')], "XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020022 quit!
23 CODE
24 call RunVim(before, [], "")
25 let my_rc = readfile("XMY_VIMRC")
Christian Brabandt4e7249a2024-09-05 17:46:19 +020026 let my_rcdir = readfile("XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020027 if rc.file1.exists
28 call assert_equal(rc.file1.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020029 call assert_equal(rc.file1.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020030 elseif !rc.file1.exists && rc.file2.exists
31 call assert_equal(rc.file2.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020032 call assert_equal(rc.file2.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020033 elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
34 call assert_equal(rc.xdg.path, my_rc)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020035 call assert_equal(rc.xdg.dir, my_rcdir)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020036 endif
37 call delete("XMY_VIMRC")
Christian Brabandt4e7249a2024-09-05 17:46:19 +020038 call delete("XMY_VIMDIR")
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020039endfunc
40
41func Test_xdg_runtime_files()
42 " This tests, that the initialization file from
43 " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
Diego Viola133ed2a2024-04-18 20:54:06 +020044 " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020045 CheckUnix
46 call mkdir(expand('~/.vim/'), 'pD')
47 call mkdir(expand('~/.config/vim/'), 'pD')
48 call mkdir(expand('~/xdg/vim/'), 'pD')
Maxim Kima34ba822024-04-17 22:29:06 +020049
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020050 let rc1=expand('~/.vimrc')
51 let rc2=expand('~/.vim/vimrc')
52 let rc3=expand('~/.config/vim/vimrc')
53 let rc4=expand('~/xdg/vim/vimrc')
54
55 " g:rc_one|two|three|four is to verify, that the other
Diego Viola2da68c82024-04-15 20:08:38 +020056 " init files are not sourced
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020057 " g:rc is to verify which rc file has been loaded.
Christian Brabandt1a741d32025-03-01 16:30:33 +010058 " g:rc_vimdir is to verify $MYVIMDIR is set and valid
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020059 let file1 =<< trim CODE
60 let g:rc_one = 'one'
61 let g:rc = '.vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010062 let g:rc_vimdir = expand('~/.vim/')
63 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020064 CODE
65 let file2 =<< trim CODE
66 let g:rc_two = 'two'
67 let g:rc = '.vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010068 let g:rc_vimdir = expand('~/.vim/')
69 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020070 CODE
71 let file3 =<< trim CODE
72 let g:rc_three = 'three'
73 let g:rc = '.config/vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010074 let g:rc_vimdir = expand('~/.config/vim/')
75 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020076 CODE
77 let file4 =<< trim CODE
78 let g:rc_four = 'four'
79 let g:rc = 'xdg/vim/vimrc'
Christian Brabandt1a741d32025-03-01 16:30:33 +010080 let g:rc_vimdir = expand('~/xdg/vim/')
81 call assert_equal(g:rc_vimdir, $MYVIMDIR)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020082 CODE
83 call writefile(file1, rc1)
84 call writefile(file2, rc2)
85 call writefile(file3, rc3)
86 call writefile(file4, rc4)
87
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020088 " Get the Vim command to run without the '-u NONE' argument
89 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
90
91 " Test for ~/.vimrc
92 let lines =<< trim END
93 call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +020094 call assert_match('XfakeHOME/.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020095 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +010096 call assert_equal(#{rc_one: 'one', rc: '.vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +020097 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020098 call writefile(v:errors, 'Xresult')
99 quit
100 END
101 call writefile(lines, 'Xscript', 'D')
102 call system($'{vimcmd} -S Xscript')
103 call assert_equal([], readfile('Xresult'))
104
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200105 call delete(rc1)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200106
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200107 " Test for ~/.vim/vimrc
108 let lines =<< trim END
109 call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200110 call assert_match('XfakeHOME/\.vim/', $MYVIMDIR)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200111 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100112 call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200113 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200114 call writefile(v:errors, 'Xresult')
115 quit
116 END
117 call writefile(lines, 'Xscript', 'D')
118 call system($'{vimcmd} -S Xscript')
119 call assert_equal([], readfile('Xresult'))
120
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200121 call delete(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200122
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200123 " XDG_CONFIG_HOME is set in Github CI runners
124 unlet $XDG_CONFIG_HOME
125
126 " Test for ~/.config/vim/vimrc
127 let lines =<< trim END
128 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
129 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200130 call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200131 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100132 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200133 call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200134 call writefile(v:errors, 'Xresult')
135 quit
136 END
137 call writefile(lines, 'Xscript', 'D')
138 call system($'{vimcmd} -S Xscript')
139 call assert_equal([], readfile('Xresult'))
140
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200141 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200142
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200143 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200144 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200145 let lines =<< trim END
146 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
147 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200148 call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200149 call filter(g:, {idx, _ -> idx =~ '^rc'})
Christian Brabandt1a741d32025-03-01 16:30:33 +0100150 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc', rc_vimdir: $MYVIMDIR}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200151 call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200152 call writefile(v:errors, 'Xresult')
153 quit
154 END
155 call writefile(lines, 'Xscript', 'D')
156 call system($'{vimcmd} -S Xscript')
157 call assert_equal([], readfile('Xresult'))
158
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100159 " Test for $MYVIMDIR changes when updating runtimepath
160 let lines =<< trim END
161 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}" rtp-prepend'
162 set rtp^=/non-existing
163 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
164 call assert_match('/non-existing', $MYVIMDIR, msg)
165 call writefile(v:errors, 'Xresult')
166 quit
167 END
168 call writefile(lines, 'Xscript', 'D')
169 call system($'{vimcmd} -S Xscript')
170 call assert_equal([], readfile('Xresult'))
171
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200172 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200173 unlet $XDG_CONFIG_HOME
174endfunc
175
Diego Violad1068a22024-04-16 20:58:45 +0200176func Test_xdg_version()
177 CheckUnix
178 let $HOME = getcwd() .. '/XfakeHOME'
179 unlet $XDG_CONFIG_HOME
180 let a = execute(':version')->split('\n')
181 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200182 " There should be 1 entry for gvimrc and 1 entry for vimrc,
183 " but only if Vim was compiled with gui support
184 call assert_equal(1 + has("gui"), len(a))
185 call assert_match('\~/\.config/vim/vimrc', a[0])
186 if has("gui")
187 call assert_match('\~/\.config/vim/gvimrc', a[1])
188 endif
Diego Violad1068a22024-04-16 20:58:45 +0200189
190 let $XDG_CONFIG_HOME = expand('~/.xdg')
191 let a = execute(':version')->split('\n')
192 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200193 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200194 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200195 if has("gui")
196 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
197 endif
Diego Violad1068a22024-04-16 20:58:45 +0200198 unlet $XDG_CONFIG_HOME
199endfunc
200
Maxim Kima34ba822024-04-17 22:29:06 +0200201" Test for gvimrc, must be last, since it starts the GUI
202" and sources a few extra test files
203func Test_zzz_xdg_runtime_files()
204 CheckCanRunGui
205 CheckUnix
206
207 " Is setup in Github Runner
208 unlet $XDG_CONFIG_HOME
Christian Brabandteb380b92025-07-07 20:53:55 +0200209 source util/setup_gui.vim
Maxim Kima34ba822024-04-17 22:29:06 +0200210 call GUISetUpCommon()
211
Diego Viola133ed2a2024-04-18 20:54:06 +0200212 " This tests, that the GUI initialization file from
213 " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
214 " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
215 " when starting GUI mode
Maxim Kima34ba822024-04-17 22:29:06 +0200216 call mkdir(expand('~/.vim/'), 'pD')
217 call mkdir(expand('~/.config/vim/'), 'pD')
218 call mkdir(expand('~/xdg/vim/'), 'pD')
219
220 let rc1=expand('~/.gvimrc')
221 let rc2=expand('~/.vim/gvimrc')
222 let rc3=expand('~/.config/vim/gvimrc')
223 let rc4=expand('~/xdg/vim/gvimrc')
224
225 " g:rc_one|two|three|four is to verify, that the other
226 " init files are not sourced
227 " g:rc is to verify which rc file has been loaded.
228 let file1 =<< trim CODE
229 let g:rc_one = 'one'
230 let g:rc = '.gvimrc'
231 CODE
232 let file2 =<< trim CODE
233 let g:rc_two = 'two'
234 let g:rc = '.vim/gvimrc'
235 CODE
236 let file3 =<< trim CODE
237 let g:rc_three = 'three'
238 let g:rc = '.config/vim/gvimrc'
239 CODE
240 let file4 =<< trim CODE
241 let g:rc_four = 'four'
242 let g:rc = 'xdg/vim/gvimrc'
243 CODE
244 call writefile(file1, rc1)
245 call writefile(file2, rc2)
246 call writefile(file3, rc3)
247 call writefile(file4, rc4)
248
249 " Get the Vim command to run without the '-u NONE' argument
250 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
251
252 " Test for ~/.gvimrc
253 let lines =<< trim END
254 " Ignore the "failed to create input context" error.
255 call test_ignore_error('E285')
256 gui -f
257 call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200258 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200259 call filter(g:, {idx, _ -> idx =~ '^rc'})
260 call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
261 call writefile(v:errors, 'Xresult')
262 quit
263 END
264 call writefile(lines, 'Xscript', 'D')
265 call system($'{vimcmd} -S Xscript')
266 call assert_equal([], readfile('Xresult'))
267
268 call delete(rc1)
269
270 " Test for ~/.vim/gvimrc
271 let lines =<< trim END
272 " Ignore the "failed to create input context" error.
273 call test_ignore_error('E285')
274 gui -f
275 call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
Christian Brabandt4e7249a2024-09-05 17:46:19 +0200276 call assert_match('Xhome/\.vim/', $MYVIMDIR)
Maxim Kima34ba822024-04-17 22:29:06 +0200277 call filter(g:, {idx, _ -> idx =~ '^rc'})
278 call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
279 call writefile(v:errors, 'Xresult')
280 quit
281 END
282 call writefile(lines, 'Xscript', 'D')
283 call system($'{vimcmd} -S Xscript')
284 call assert_equal([], readfile('Xresult'))
285
286 call delete(rc2)
287
Maxim Kima34ba822024-04-17 22:29:06 +0200288 " Test for ~/.config/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100289 " MYVIMDIR is only set to ~/config/.vim if ~/.config/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200290 let lines =<< trim END
291 " Ignore the "failed to create input context" error.
292 call test_ignore_error('E285')
293 gui -f
294 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
295 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100296 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200297 call filter(g:, {idx, _ -> idx =~ '^rc'})
298 call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
299 call writefile(v:errors, 'Xresult')
300 quit
301 END
302 call writefile(lines, 'Xscript', 'D')
303 call system($'{vimcmd} -S Xscript')
304 call assert_equal([], readfile('Xresult'))
305
306 call delete(rc3)
307
308 " Test for ~/xdg/vim/gvimrc
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100309 " MYVIMDIR is only set to ~/xdg/vim if ~/xdg/vim/vimrc exists!
Maxim Kima34ba822024-04-17 22:29:06 +0200310 let $XDG_CONFIG_HOME=expand('~/xdg/')
311 let lines =<< trim END
312 " Ignore the "failed to create input context" error.
313 call test_ignore_error('E285')
314 gui -f
315 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
316 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100317 call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
Maxim Kima34ba822024-04-17 22:29:06 +0200318 call filter(g:, {idx, _ -> idx =~ '^rc'})
319 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
320 call writefile(v:errors, 'Xresult')
321 quit
322 END
323 call writefile(lines, 'Xscript', 'D')
324 call system($'{vimcmd} -S Xscript')
325 call assert_equal([], readfile('Xresult'))
326
327 call delete(rc4)
328
329 " Clean up
330 unlet $XDG_CONFIG_HOME
331 call GUITearDownCommon()
332 call delete('Xhome', 'rf')
333endfunc
334
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200335" vim: shiftwidth=2 sts=2 expandtab