blob: b9ec3c73148cf7fc53fd03bbe11c8f4ca35650a1 [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:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +020083 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020084 call writefile(v:errors, 'Xresult')
85 quit
86 END
87 call writefile(lines, 'Xscript', 'D')
88 call system($'{vimcmd} -S Xscript')
89 call assert_equal([], readfile('Xresult'))
90
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020091 call delete(rc1)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020092
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020093 " Test for ~/.vim/vimrc
94 let lines =<< trim END
95 call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
96 call filter(g:, {idx, _ -> idx =~ '^rc'})
97 call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +020098 call assert_match('XfakeHOME/\.vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020099 call writefile(v:errors, 'Xresult')
100 quit
101 END
102 call writefile(lines, 'Xscript', 'D')
103 call system($'{vimcmd} -S Xscript')
104 call assert_equal([], readfile('Xresult'))
105
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200106 call delete(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200107
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200108 " XDG_CONFIG_HOME is set in Github CI runners
109 unlet $XDG_CONFIG_HOME
110
111 " Test for ~/.config/vim/vimrc
112 let lines =<< trim END
113 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
114 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
115 call filter(g:, {idx, _ -> idx =~ '^rc'})
116 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200117 call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200118 call writefile(v:errors, 'Xresult')
119 quit
120 END
121 call writefile(lines, 'Xscript', 'D')
122 call system($'{vimcmd} -S Xscript')
123 call assert_equal([], readfile('Xresult'))
124
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200125 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200126
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200127 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200128 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200129 let lines =<< trim END
130 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
131 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
132 call filter(g:, {idx, _ -> idx =~ '^rc'})
133 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200134 call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200135 call writefile(v:errors, 'Xresult')
136 quit
137 END
138 call writefile(lines, 'Xscript', 'D')
139 call system($'{vimcmd} -S Xscript')
140 call assert_equal([], readfile('Xresult'))
141
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200142 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200143 unlet $XDG_CONFIG_HOME
144endfunc
145
Diego Violad1068a22024-04-16 20:58:45 +0200146func Test_xdg_version()
147 CheckUnix
148 let $HOME = getcwd() .. '/XfakeHOME'
149 unlet $XDG_CONFIG_HOME
150 let a = execute(':version')->split('\n')
151 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200152 " There should be 1 entry for gvimrc and 1 entry for vimrc,
153 " but only if Vim was compiled with gui support
154 call assert_equal(1 + has("gui"), len(a))
155 call assert_match('\~/\.config/vim/vimrc', a[0])
156 if has("gui")
157 call assert_match('\~/\.config/vim/gvimrc', a[1])
158 endif
Diego Violad1068a22024-04-16 20:58:45 +0200159
160 let $XDG_CONFIG_HOME = expand('~/.xdg')
161 let a = execute(':version')->split('\n')
162 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200163 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200164 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200165 if has("gui")
166 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
167 endif
Diego Violad1068a22024-04-16 20:58:45 +0200168 unlet $XDG_CONFIG_HOME
169endfunc
170
Maxim Kima34ba822024-04-17 22:29:06 +0200171" Test for gvimrc, must be last, since it starts the GUI
172" and sources a few extra test files
173func Test_zzz_xdg_runtime_files()
174 CheckCanRunGui
175 CheckUnix
176
177 " Is setup in Github Runner
178 unlet $XDG_CONFIG_HOME
179 source setup_gui.vim
180 call GUISetUpCommon()
181
Diego Viola133ed2a2024-04-18 20:54:06 +0200182 " This tests, that the GUI initialization file from
183 " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
184 " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
185 " when starting GUI mode
Maxim Kima34ba822024-04-17 22:29:06 +0200186 call mkdir(expand('~/.vim/'), 'pD')
187 call mkdir(expand('~/.config/vim/'), 'pD')
188 call mkdir(expand('~/xdg/vim/'), 'pD')
189
190 let rc1=expand('~/.gvimrc')
191 let rc2=expand('~/.vim/gvimrc')
192 let rc3=expand('~/.config/vim/gvimrc')
193 let rc4=expand('~/xdg/vim/gvimrc')
194
195 " g:rc_one|two|three|four is to verify, that the other
196 " init files are not sourced
197 " g:rc is to verify which rc file has been loaded.
198 let file1 =<< trim CODE
199 let g:rc_one = 'one'
200 let g:rc = '.gvimrc'
201 CODE
202 let file2 =<< trim CODE
203 let g:rc_two = 'two'
204 let g:rc = '.vim/gvimrc'
205 CODE
206 let file3 =<< trim CODE
207 let g:rc_three = 'three'
208 let g:rc = '.config/vim/gvimrc'
209 CODE
210 let file4 =<< trim CODE
211 let g:rc_four = 'four'
212 let g:rc = 'xdg/vim/gvimrc'
213 CODE
214 call writefile(file1, rc1)
215 call writefile(file2, rc2)
216 call writefile(file3, rc3)
217 call writefile(file4, rc4)
218
219 " Get the Vim command to run without the '-u NONE' argument
220 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
221
222 " Test for ~/.gvimrc
223 let lines =<< trim END
224 " Ignore the "failed to create input context" error.
225 call test_ignore_error('E285')
226 gui -f
227 call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
228 call filter(g:, {idx, _ -> idx =~ '^rc'})
229 call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
230 call writefile(v:errors, 'Xresult')
231 quit
232 END
233 call writefile(lines, 'Xscript', 'D')
234 call system($'{vimcmd} -S Xscript')
235 call assert_equal([], readfile('Xresult'))
236
237 call delete(rc1)
238
239 " Test for ~/.vim/gvimrc
240 let lines =<< trim END
241 " Ignore the "failed to create input context" error.
242 call test_ignore_error('E285')
243 gui -f
244 call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
245 call filter(g:, {idx, _ -> idx =~ '^rc'})
246 call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
247 call writefile(v:errors, 'Xresult')
248 quit
249 END
250 call writefile(lines, 'Xscript', 'D')
251 call system($'{vimcmd} -S Xscript')
252 call assert_equal([], readfile('Xresult'))
253
254 call delete(rc2)
255
Maxim Kima34ba822024-04-17 22:29:06 +0200256 " Test for ~/.config/vim/gvimrc
257 let lines =<< trim END
258 " Ignore the "failed to create input context" error.
259 call test_ignore_error('E285')
260 gui -f
261 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
262 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
263 call filter(g:, {idx, _ -> idx =~ '^rc'})
264 call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
265 call writefile(v:errors, 'Xresult')
266 quit
267 END
268 call writefile(lines, 'Xscript', 'D')
269 call system($'{vimcmd} -S Xscript')
270 call assert_equal([], readfile('Xresult'))
271
272 call delete(rc3)
273
274 " Test for ~/xdg/vim/gvimrc
275 let $XDG_CONFIG_HOME=expand('~/xdg/')
276 let lines =<< trim END
277 " Ignore the "failed to create input context" error.
278 call test_ignore_error('E285')
279 gui -f
280 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
281 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
282 call filter(g:, {idx, _ -> idx =~ '^rc'})
283 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
284 call writefile(v:errors, 'Xresult')
285 quit
286 END
287 call writefile(lines, 'Xscript', 'D')
288 call system($'{vimcmd} -S Xscript')
289 call assert_equal([], readfile('Xresult'))
290
291 call delete(rc4)
292
293 " Clean up
294 unlet $XDG_CONFIG_HOME
295 call GUITearDownCommon()
296 call delete('Xhome', 'rf')
297endfunc
298
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200299" vim: shiftwidth=2 sts=2 expandtab