blob: 7c0c8ef619203addf32cddb247c5ab3659c20650 [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 +02004
5source shared.vim
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02006
7func s:get_rcs()
8 let rcs = {
9 \ 'file1': { 'path': '~/.vimrc' },
10 \ 'file2': { 'path': '~/.vim/vimrc' },
11 \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" },
12 \}
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")
24 quit!
25 CODE
26 call RunVim(before, [], "")
27 let my_rc = readfile("XMY_VIMRC")
28 if rc.file1.exists
29 call assert_equal(rc.file1.path, my_rc)
30 elseif !rc.file1.exists && rc.file2.exists
31 call assert_equal(rc.file2.path, my_rc)
32 elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
33 call assert_equal(rc.xdg.path, my_rc)
34 endif
35 call delete("XMY_VIMRC")
36endfunc
37
38func Test_xdg_runtime_files()
39 " This tests, that the initialization file from
40 " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
41 " $XDG_HOMECONFIG/vim/vimrc) are sourced in that order
42 CheckUnix
43 call mkdir(expand('~/.vim/'), 'pD')
44 call mkdir(expand('~/.config/vim/'), 'pD')
45 call mkdir(expand('~/xdg/vim/'), 'pD')
Maxim Kima34ba822024-04-17 22:29:06 +020046
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020047 let rc1=expand('~/.vimrc')
48 let rc2=expand('~/.vim/vimrc')
49 let rc3=expand('~/.config/vim/vimrc')
50 let rc4=expand('~/xdg/vim/vimrc')
51
52 " g:rc_one|two|three|four is to verify, that the other
Diego Viola2da68c82024-04-15 20:08:38 +020053 " init files are not sourced
Luca Saccarolac9df1fb2024-04-14 22:53:22 +020054 " g:rc is to verify which rc file has been loaded.
55 let file1 =<< trim CODE
56 let g:rc_one = 'one'
57 let g:rc = '.vimrc'
58 CODE
59 let file2 =<< trim CODE
60 let g:rc_two = 'two'
61 let g:rc = '.vim/vimrc'
62 CODE
63 let file3 =<< trim CODE
64 let g:rc_three = 'three'
65 let g:rc = '.config/vim/vimrc'
66 CODE
67 let file4 =<< trim CODE
68 let g:rc_four = 'four'
69 let g:rc = 'xdg/vim/vimrc'
70 CODE
71 call writefile(file1, rc1)
72 call writefile(file2, rc2)
73 call writefile(file3, rc3)
74 call writefile(file4, rc4)
75
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +020076 " Get the Vim command to run without the '-u NONE' argument
77 let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')
78
79 " Test for ~/.vimrc
80 let lines =<< trim END
81 call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
82 call filter(g:, {idx, _ -> idx =~ '^rc'})
83 call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
84 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:)
98 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(rc2)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200106
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200107 " XDG_CONFIG_HOME is set in Github CI runners
108 unlet $XDG_CONFIG_HOME
109
110 " Test for ~/.config/vim/vimrc
111 let lines =<< trim END
112 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
113 call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
114 call filter(g:, {idx, _ -> idx =~ '^rc'})
115 call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
116 call writefile(v:errors, 'Xresult')
117 quit
118 END
119 call writefile(lines, 'Xscript', 'D')
120 call system($'{vimcmd} -S Xscript')
121 call assert_equal([], readfile('Xresult'))
122
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200123 call delete(rc3)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200124
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200125 " Test for ~/xdg/vim/vimrc
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200126 let $XDG_CONFIG_HOME=expand('~/xdg/')
Yegappan Lakshmanan8560e6c2024-04-16 22:18:15 +0200127 let lines =<< trim END
128 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
129 call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
130 call filter(g:, {idx, _ -> idx =~ '^rc'})
131 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
132 call writefile(v:errors, 'Xresult')
133 quit
134 END
135 call writefile(lines, 'Xscript', 'D')
136 call system($'{vimcmd} -S Xscript')
137 call assert_equal([], readfile('Xresult'))
138
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200139 call delete(rc4)
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200140 unlet $XDG_CONFIG_HOME
141endfunc
142
Diego Violad1068a22024-04-16 20:58:45 +0200143func Test_xdg_version()
144 CheckUnix
145 let $HOME = getcwd() .. '/XfakeHOME'
146 unlet $XDG_CONFIG_HOME
147 let a = execute(':version')->split('\n')
148 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200149 " There should be 1 entry for gvimrc and 1 entry for vimrc,
150 " but only if Vim was compiled with gui support
151 call assert_equal(1 + has("gui"), len(a))
152 call assert_match('\~/\.config/vim/vimrc', a[0])
153 if has("gui")
154 call assert_match('\~/\.config/vim/gvimrc', a[1])
155 endif
Diego Violad1068a22024-04-16 20:58:45 +0200156
157 let $XDG_CONFIG_HOME = expand('~/.xdg')
158 let a = execute(':version')->split('\n')
159 let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
Maxim Kima34ba822024-04-17 22:29:06 +0200160 call assert_equal(1 + has("gui"), len(a))
Diego Violad1068a22024-04-16 20:58:45 +0200161 call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
Maxim Kima34ba822024-04-17 22:29:06 +0200162 if has("gui")
163 call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
164 endif
Diego Violad1068a22024-04-16 20:58:45 +0200165 unlet $XDG_CONFIG_HOME
166endfunc
167
Maxim Kima34ba822024-04-17 22:29:06 +0200168" Test for gvimrc, must be last, since it starts the GUI
169" and sources a few extra test files
170func Test_zzz_xdg_runtime_files()
171 CheckCanRunGui
172 CheckUnix
173
174 " Is setup in Github Runner
175 unlet $XDG_CONFIG_HOME
176 source setup_gui.vim
177 call GUISetUpCommon()
178
179 " This tests, that the initialization file from
180 " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
181 " $XDG_HOMECONFIG/vim/vimrc) are sourced in that order
182 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
252 " XDG_CONFIG_HOME is set in Github CI runners
253 unlet $XDG_CONFIG_HOME
254
255 " Test for ~/.config/vim/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 let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
261 call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
262 call filter(g:, {idx, _ -> idx =~ '^rc'})
263 call assert_equal(#{rc_three: 'three', rc: '.config/vim/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(rc3)
272
273 " Test for ~/xdg/vim/gvimrc
274 let $XDG_CONFIG_HOME=expand('~/xdg/')
275 let lines =<< trim END
276 " Ignore the "failed to create input context" error.
277 call test_ignore_error('E285')
278 gui -f
279 let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
280 call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
281 call filter(g:, {idx, _ -> idx =~ '^rc'})
282 call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
283 call writefile(v:errors, 'Xresult')
284 quit
285 END
286 call writefile(lines, 'Xscript', 'D')
287 call system($'{vimcmd} -S Xscript')
288 call assert_equal([], readfile('Xresult'))
289
290 call delete(rc4)
291
292 " Clean up
293 unlet $XDG_CONFIG_HOME
294 call GUITearDownCommon()
295 call delete('Xhome', 'rf')
296endfunc
297
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200298" vim: shiftwidth=2 sts=2 expandtab