blob: 6f35b7254ad60ca67b139cd0a580ced796b91130 [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 = {
8 \ 'file1': { 'path': '~/.vimrc' },
9 \ 'file2': { 'path': '~/.vim/vimrc' },
10 \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" },
11 \}
12 for v in values(rcs)
13 let v.exists = filereadable(expand(v.path))
14 endfor
15 return rcs
16endfunc
17
18func Test_xdg_rc_detection()
19 CheckUnix
20 let rc = s:get_rcs()
21 let before =<< trim CODE
22 call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
23 quit!
24 CODE
25 call RunVim(before, [], "")
26 let my_rc = readfile("XMY_VIMRC")
27 if rc.file1.exists
28 call assert_equal(rc.file1.path, my_rc)
29 elseif !rc.file1.exists && rc.file2.exists
30 call assert_equal(rc.file2.path, my_rc)
31 elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
32 call assert_equal(rc.xdg.path, my_rc)
33 endif
34 call delete("XMY_VIMRC")
35endfunc
36
37func Test_xdg_runtime_files()
38 " This tests, that the initialization file from
39 " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
Diego Viola133ed2a2024-04-18 20:54:06 +020040 " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020041 CheckUnix
42 call mkdir(expand('~/.vim/'), 'pD')
43 call mkdir(expand('~/.config/vim/'), 'pD')
44 call mkdir(expand('~/xdg/vim/'), 'pD')
Maxim Kima34ba822024-04-17 22:29:06 +020045
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020046 let rc1=expand('~/.vimrc')
47 let rc2=expand('~/.vim/vimrc')
48 let rc3=expand('~/.config/vim/vimrc')
49 let rc4=expand('~/xdg/vim/vimrc')
50
51 " g:rc_one|two|three|four is to verify, that the other
Diego Viola2da68c82024-04-15 20:08:38 +020052 " init files are not sourced
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020053 " g:rc is to verify which rc file has been loaded.
54 let file1 =<< trim CODE
55 let g:rc_one = 'one'
56 let g:rc = '.vimrc'
57 CODE
58 let file2 =<< trim CODE
59 let g:rc_two = 'two'
60 let g:rc = '.vim/vimrc'
61 CODE
62 let file3 =<< trim CODE
63 let g:rc_three = 'three'
64 let g:rc = '.config/vim/vimrc'
65 CODE
66 let file4 =<< trim CODE
67 let g:rc_four = 'four'
68 let g:rc = 'xdg/vim/vimrc'
69 CODE
70 call writefile(file1, rc1)
71 call writefile(file2, rc2)
72 call writefile(file3, rc3)
73 call writefile(file4, rc4)
74
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020075 " Get the Vim command to run without the '-u NONE' argument
76 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
77
78 " Test for ~/.vimrc
79 let lines =<< trim END
80 call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
81 call filter(g:, {idx, _ -> idx =~ '^rc'})
82 call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
83 call writefile(v:errors, 'Xresult')
84 quit
85 END
86 call writefile(lines, 'Xscript', 'D')
87 call system($'{vimcmd} -S Xscript')
88 call assert_equal([], readfile('Xresult'))
89
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020090 call delete(rc1)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020091
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020092 " Test for ~/.vim/vimrc
93 let lines =<< trim END
94 call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
95 call filter(g:, {idx, _ -> idx =~ '^rc'})
96 call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
97 call writefile(v:errors, 'Xresult')
98 quit
99 END
100 call writefile(lines, 'Xscript', 'D')
101 call system($'{vimcmd} -S Xscript')
102 call assert_equal([], readfile('Xresult'))
103
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200104 call delete(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200105
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200106 " XDG_CONFIG_HOME is set in Github CI runners
107 unlet $XDG_CONFIG_HOME
108
109 " Test for ~/.config/vim/vimrc
110 let lines =<< trim END
111 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
112 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
113 call filter(g:, {idx, _ -> idx =~ '^rc'})
114 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
115 call writefile(v:errors, 'Xresult')
116 quit
117 END
118 call writefile(lines, 'Xscript', 'D')
119 call system($'{vimcmd} -S Xscript')
120 call assert_equal([], readfile('Xresult'))
121
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200122 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200123
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200124 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200125 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200126 let lines =<< trim END
127 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
128 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
129 call filter(g:, {idx, _ -> idx =~ '^rc'})
130 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
131 call writefile(v:errors, 'Xresult')
132 quit
133 END
134 call writefile(lines, 'Xscript', 'D')
135 call system($'{vimcmd} -S Xscript')
136 call assert_equal([], readfile('Xresult'))
137
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200138 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200139 unlet $XDG_CONFIG_HOME
140endfunc
141
Diego Violad1068a22024-04-16 20:58:45 +0200142func Test_xdg_version()
143 CheckUnix
144 let $HOME = getcwd() .. '/XfakeHOME'
145 unlet $XDG_CONFIG_HOME
146 let a = execute(':version')->split('\n')
147 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200148 " There should be 1 entry for gvimrc and 1 entry for vimrc,
149 " but only if Vim was compiled with gui support
150 call assert_equal(1 + has("gui"), len(a))
151 call assert_match('\~/\.config/vim/vimrc', a[0])
152 if has("gui")
153 call assert_match('\~/\.config/vim/gvimrc', a[1])
154 endif
Diego Violad1068a22024-04-16 20:58:45 +0200155
156 let $XDG_CONFIG_HOME = expand('~/.xdg')
157 let a = execute(':version')->split('\n')
158 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200159 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200160 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200161 if has("gui")
162 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
163 endif
Diego Violad1068a22024-04-16 20:58:45 +0200164 unlet $XDG_CONFIG_HOME
165endfunc
166
Maxim Kima34ba822024-04-17 22:29:06 +0200167" Test for gvimrc, must be last, since it starts the GUI
168" and sources a few extra test files
169func Test_zzz_xdg_runtime_files()
170 CheckCanRunGui
171 CheckUnix
172
173 " Is setup in Github Runner
174 unlet $XDG_CONFIG_HOME
175 source setup_gui.vim
176 call GUISetUpCommon()
177
Diego Viola133ed2a2024-04-18 20:54:06 +0200178 " This tests, that the GUI initialization file from
179 " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
180 " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
181 " when starting GUI mode
Maxim Kima34ba822024-04-17 22:29:06 +0200182 call mkdir(expand('~/.vim/'), 'pD')
183 call mkdir(expand('~/.config/vim/'), 'pD')
184 call mkdir(expand('~/xdg/vim/'), 'pD')
185
186 let rc1=expand('~/.gvimrc')
187 let rc2=expand('~/.vim/gvimrc')
188 let rc3=expand('~/.config/vim/gvimrc')
189 let rc4=expand('~/xdg/vim/gvimrc')
190
191 " g:rc_one|two|three|four is to verify, that the other
192 " init files are not sourced
193 " g:rc is to verify which rc file has been loaded.
194 let file1 =<< trim CODE
195 let g:rc_one = 'one'
196 let g:rc = '.gvimrc'
197 CODE
198 let file2 =<< trim CODE
199 let g:rc_two = 'two'
200 let g:rc = '.vim/gvimrc'
201 CODE
202 let file3 =<< trim CODE
203 let g:rc_three = 'three'
204 let g:rc = '.config/vim/gvimrc'
205 CODE
206 let file4 =<< trim CODE
207 let g:rc_four = 'four'
208 let g:rc = 'xdg/vim/gvimrc'
209 CODE
210 call writefile(file1, rc1)
211 call writefile(file2, rc2)
212 call writefile(file3, rc3)
213 call writefile(file4, rc4)
214
215 " Get the Vim command to run without the '-u NONE' argument
216 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
217
218 " Test for ~/.gvimrc
219 let lines =<< trim END
220 " Ignore the "failed to create input context" error.
221 call test_ignore_error('E285')
222 gui -f
223 call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
224 call filter(g:, {idx, _ -> idx =~ '^rc'})
225 call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
226 call writefile(v:errors, 'Xresult')
227 quit
228 END
229 call writefile(lines, 'Xscript', 'D')
230 call system($'{vimcmd} -S Xscript')
231 call assert_equal([], readfile('Xresult'))
232
233 call delete(rc1)
234
235 " Test for ~/.vim/gvimrc
236 let lines =<< trim END
237 " Ignore the "failed to create input context" error.
238 call test_ignore_error('E285')
239 gui -f
240 call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
241 call filter(g:, {idx, _ -> idx =~ '^rc'})
242 call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
243 call writefile(v:errors, 'Xresult')
244 quit
245 END
246 call writefile(lines, 'Xscript', 'D')
247 call system($'{vimcmd} -S Xscript')
248 call assert_equal([], readfile('Xresult'))
249
250 call delete(rc2)
251
Maxim Kima34ba822024-04-17 22:29:06 +0200252 " Test for ~/.config/vim/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 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
258 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
259 call filter(g:, {idx, _ -> idx =~ '^rc'})
260 call assert_equal(#{rc_three: 'three', rc: '.config/vim/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(rc3)
269
270 " Test for ~/xdg/vim/gvimrc
271 let $XDG_CONFIG_HOME=expand('~/xdg/')
272 let lines =<< trim END
273 " Ignore the "failed to create input context" error.
274 call test_ignore_error('E285')
275 gui -f
276 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
277 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
278 call filter(g:, {idx, _ -> idx =~ '^rc'})
279 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
280 call writefile(v:errors, 'Xresult')
281 quit
282 END
283 call writefile(lines, 'Xscript', 'D')
284 call system($'{vimcmd} -S Xscript')
285 call assert_equal([], readfile('Xresult'))
286
287 call delete(rc4)
288
289 " Clean up
290 unlet $XDG_CONFIG_HOME
291 call GUITearDownCommon()
292 call delete('Xhome', 'rf')
293endfunc
294
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200295" vim: shiftwidth=2 sts=2 expandtab