Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 1 | " Tests for the XDG feature |
| 2 | |
| 3 | source check.vim |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 4 | source shared.vim |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 5 | |
| 6 | func 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 |
| 16 | endfunc |
| 17 | |
| 18 | func 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") |
| 35 | endfunc |
| 36 | |
| 37 | func Test_xdg_runtime_files() |
| 38 | " This tests, that the initialization file from |
| 39 | " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or |
Diego Viola | 133ed2a | 2024-04-18 20:54:06 +0200 | [diff] [blame] | 40 | " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 41 | CheckUnix |
| 42 | call mkdir(expand('~/.vim/'), 'pD') |
| 43 | call mkdir(expand('~/.config/vim/'), 'pD') |
| 44 | call mkdir(expand('~/xdg/vim/'), 'pD') |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 45 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 46 | 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 Viola | 2da68c8 | 2024-04-15 20:08:38 +0200 | [diff] [blame] | 52 | " init files are not sourced |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 53 | " 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 Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 75 | " 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 Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame^] | 83 | call assert_match('XfakeHOME/\.vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 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 Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 91 | call delete(rc1) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 92 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 93 | " 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 Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame^] | 98 | call assert_match('XfakeHOME/\.vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 99 | 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 Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 106 | call delete(rc2) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 107 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 108 | " 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 Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame^] | 117 | call assert_match('XfakeHOME/\.config/vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 118 | 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 Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 125 | call delete(rc3) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 126 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 127 | " Test for ~/xdg/vim/vimrc |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 128 | let $XDG_CONFIG_HOME=expand('~/xdg/') |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 129 | 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 Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame^] | 134 | call assert_match('XfakeHOME/xdg/vim/view, &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 135 | 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 Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 142 | call delete(rc4) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 143 | unlet $XDG_CONFIG_HOME |
| 144 | endfunc |
| 145 | |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 146 | func 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 Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 152 | " 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 Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 159 | |
| 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 Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 163 | call assert_equal(1 + has("gui"), len(a)) |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 164 | call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0]) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 165 | if has("gui") |
| 166 | call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1]) |
| 167 | endif |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 168 | unlet $XDG_CONFIG_HOME |
| 169 | endfunc |
| 170 | |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 171 | " Test for gvimrc, must be last, since it starts the GUI |
| 172 | " and sources a few extra test files |
| 173 | func 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 Viola | 133ed2a | 2024-04-18 20:54:06 +0200 | [diff] [blame] | 182 | " 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 Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 186 | 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 Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 256 | " 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') |
| 297 | endfunc |
| 298 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 299 | " vim: shiftwidth=2 sts=2 expandtab |