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 = { |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 8 | \ 'file1': { 'path': '~/.vimrc', 'dir': expand('~/.vim/') }, |
| 9 | \ 'file2': { 'path': '~/.vim/vimrc', 'dir': expand('~/.vim/') }, |
| 10 | \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config", |
| 11 | \ 'dir': exists('$XDG_CONFIG_HOME') ? expand("$XDG_CONFIG_HOME/vim") : '~/.config/vim/'}, |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 12 | \} |
| 13 | for v in values(rcs) |
| 14 | let v.exists = filereadable(expand(v.path)) |
| 15 | endfor |
| 16 | return rcs |
| 17 | endfunc |
| 18 | |
| 19 | func Test_xdg_rc_detection() |
| 20 | CheckUnix |
| 21 | let rc = s:get_rcs() |
| 22 | let before =<< trim CODE |
| 23 | call writefile([expand('$MYVIMRC')], "XMY_VIMRC") |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 24 | call writefile([expand('$MYVIMRCDIR')], "XMY_VIMDIR") |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 25 | quit! |
| 26 | CODE |
| 27 | call RunVim(before, [], "") |
| 28 | let my_rc = readfile("XMY_VIMRC") |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 29 | let my_rcdir = readfile("XMY_VIMDIR") |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 30 | if rc.file1.exists |
| 31 | call assert_equal(rc.file1.path, my_rc) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 32 | call assert_equal(rc.file1.dir, my_rcdir) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 33 | elseif !rc.file1.exists && rc.file2.exists |
| 34 | call assert_equal(rc.file2.path, my_rc) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 35 | call assert_equal(rc.file2.dir, my_rcdir) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 36 | elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists |
| 37 | call assert_equal(rc.xdg.path, my_rc) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 38 | call assert_equal(rc.xdg.dir, my_rcdir) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 39 | endif |
| 40 | call delete("XMY_VIMRC") |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 41 | call delete("XMY_VIMDIR") |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 42 | endfunc |
| 43 | |
| 44 | func Test_xdg_runtime_files() |
| 45 | " This tests, that the initialization file from |
| 46 | " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or |
Diego Viola | 133ed2a | 2024-04-18 20:54:06 +0200 | [diff] [blame] | 47 | " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 48 | CheckUnix |
| 49 | call mkdir(expand('~/.vim/'), 'pD') |
| 50 | call mkdir(expand('~/.config/vim/'), 'pD') |
| 51 | call mkdir(expand('~/xdg/vim/'), 'pD') |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 52 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 53 | let rc1=expand('~/.vimrc') |
| 54 | let rc2=expand('~/.vim/vimrc') |
| 55 | let rc3=expand('~/.config/vim/vimrc') |
| 56 | let rc4=expand('~/xdg/vim/vimrc') |
| 57 | |
| 58 | " g:rc_one|two|three|four is to verify, that the other |
Diego Viola | 2da68c8 | 2024-04-15 20:08:38 +0200 | [diff] [blame] | 59 | " init files are not sourced |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 60 | " g:rc is to verify which rc file has been loaded. |
| 61 | let file1 =<< trim CODE |
| 62 | let g:rc_one = 'one' |
| 63 | let g:rc = '.vimrc' |
| 64 | CODE |
| 65 | let file2 =<< trim CODE |
| 66 | let g:rc_two = 'two' |
| 67 | let g:rc = '.vim/vimrc' |
| 68 | CODE |
| 69 | let file3 =<< trim CODE |
| 70 | let g:rc_three = 'three' |
| 71 | let g:rc = '.config/vim/vimrc' |
| 72 | CODE |
| 73 | let file4 =<< trim CODE |
| 74 | let g:rc_four = 'four' |
| 75 | let g:rc = 'xdg/vim/vimrc' |
| 76 | CODE |
| 77 | call writefile(file1, rc1) |
| 78 | call writefile(file2, rc2) |
| 79 | call writefile(file3, rc3) |
| 80 | call writefile(file4, rc4) |
| 81 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 82 | " Get the Vim command to run without the '-u NONE' argument |
| 83 | let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '') |
| 84 | |
| 85 | " Test for ~/.vimrc |
| 86 | let lines =<< trim END |
| 87 | call assert_match('XfakeHOME/\.vimrc', $MYVIMRC) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 88 | call assert_match('XfakeHOME/.vim/', $MYVIMDIR) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 89 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 90 | call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:) |
Christian Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame] | 91 | call assert_match('XfakeHOME/\.vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 92 | call writefile(v:errors, 'Xresult') |
| 93 | quit |
| 94 | END |
| 95 | call writefile(lines, 'Xscript', 'D') |
| 96 | call system($'{vimcmd} -S Xscript') |
| 97 | call assert_equal([], readfile('Xresult')) |
| 98 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 99 | call delete(rc1) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 100 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 101 | " Test for ~/.vim/vimrc |
| 102 | let lines =<< trim END |
| 103 | call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 104 | call assert_match('XfakeHOME/\.vim/', $MYVIMDIR) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 105 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 106 | call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:) |
Christian Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame] | 107 | call assert_match('XfakeHOME/\.vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 108 | call writefile(v:errors, 'Xresult') |
| 109 | quit |
| 110 | END |
| 111 | call writefile(lines, 'Xscript', 'D') |
| 112 | call system($'{vimcmd} -S Xscript') |
| 113 | call assert_equal([], readfile('Xresult')) |
| 114 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 115 | call delete(rc2) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 116 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 117 | " XDG_CONFIG_HOME is set in Github CI runners |
| 118 | unlet $XDG_CONFIG_HOME |
| 119 | |
| 120 | " Test for ~/.config/vim/vimrc |
| 121 | let lines =<< trim END |
| 122 | let msg = $'HOME="{$HOME}", ~="{expand("~")}"' |
| 123 | call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 124 | call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 125 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 126 | call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:) |
Christian Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame] | 127 | call assert_match('XfakeHOME/\.config/vim/view', &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 128 | call writefile(v:errors, 'Xresult') |
| 129 | quit |
| 130 | END |
| 131 | call writefile(lines, 'Xscript', 'D') |
| 132 | call system($'{vimcmd} -S Xscript') |
| 133 | call assert_equal([], readfile('Xresult')) |
| 134 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 135 | call delete(rc3) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 136 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 137 | " Test for ~/xdg/vim/vimrc |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 138 | let $XDG_CONFIG_HOME=expand('~/xdg/') |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 139 | let lines =<< trim END |
| 140 | let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"' |
| 141 | call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 142 | call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 143 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 144 | call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:) |
Christian Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame] | 145 | call assert_match('XfakeHOME/xdg/vim/view, &viewdir) |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 146 | call writefile(v:errors, 'Xresult') |
| 147 | quit |
| 148 | END |
| 149 | call writefile(lines, 'Xscript', 'D') |
| 150 | call system($'{vimcmd} -S Xscript') |
| 151 | call assert_equal([], readfile('Xresult')) |
| 152 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 153 | call delete(rc4) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 154 | unlet $XDG_CONFIG_HOME |
| 155 | endfunc |
| 156 | |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 157 | func Test_xdg_version() |
| 158 | CheckUnix |
| 159 | let $HOME = getcwd() .. '/XfakeHOME' |
| 160 | unlet $XDG_CONFIG_HOME |
| 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 | " There should be 1 entry for gvimrc and 1 entry for vimrc, |
| 164 | " but only if Vim was compiled with gui support |
| 165 | call assert_equal(1 + has("gui"), len(a)) |
| 166 | call assert_match('\~/\.config/vim/vimrc', a[0]) |
| 167 | if has("gui") |
| 168 | call assert_match('\~/\.config/vim/gvimrc', a[1]) |
| 169 | endif |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 170 | |
| 171 | let $XDG_CONFIG_HOME = expand('~/.xdg') |
| 172 | let a = execute(':version')->split('\n') |
| 173 | let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' }) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 174 | call assert_equal(1 + has("gui"), len(a)) |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 175 | call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0]) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 176 | if has("gui") |
| 177 | call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1]) |
| 178 | endif |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 179 | unlet $XDG_CONFIG_HOME |
| 180 | endfunc |
| 181 | |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 182 | " Test for gvimrc, must be last, since it starts the GUI |
| 183 | " and sources a few extra test files |
| 184 | func Test_zzz_xdg_runtime_files() |
| 185 | CheckCanRunGui |
| 186 | CheckUnix |
| 187 | |
| 188 | " Is setup in Github Runner |
| 189 | unlet $XDG_CONFIG_HOME |
| 190 | source setup_gui.vim |
| 191 | call GUISetUpCommon() |
| 192 | |
Diego Viola | 133ed2a | 2024-04-18 20:54:06 +0200 | [diff] [blame] | 193 | " This tests, that the GUI initialization file from |
| 194 | " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc |
| 195 | " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced |
| 196 | " when starting GUI mode |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 197 | call mkdir(expand('~/.vim/'), 'pD') |
| 198 | call mkdir(expand('~/.config/vim/'), 'pD') |
| 199 | call mkdir(expand('~/xdg/vim/'), 'pD') |
| 200 | |
| 201 | let rc1=expand('~/.gvimrc') |
| 202 | let rc2=expand('~/.vim/gvimrc') |
| 203 | let rc3=expand('~/.config/vim/gvimrc') |
| 204 | let rc4=expand('~/xdg/vim/gvimrc') |
| 205 | |
| 206 | " g:rc_one|two|three|four is to verify, that the other |
| 207 | " init files are not sourced |
| 208 | " g:rc is to verify which rc file has been loaded. |
| 209 | let file1 =<< trim CODE |
| 210 | let g:rc_one = 'one' |
| 211 | let g:rc = '.gvimrc' |
| 212 | CODE |
| 213 | let file2 =<< trim CODE |
| 214 | let g:rc_two = 'two' |
| 215 | let g:rc = '.vim/gvimrc' |
| 216 | CODE |
| 217 | let file3 =<< trim CODE |
| 218 | let g:rc_three = 'three' |
| 219 | let g:rc = '.config/vim/gvimrc' |
| 220 | CODE |
| 221 | let file4 =<< trim CODE |
| 222 | let g:rc_four = 'four' |
| 223 | let g:rc = 'xdg/vim/gvimrc' |
| 224 | CODE |
| 225 | call writefile(file1, rc1) |
| 226 | call writefile(file2, rc2) |
| 227 | call writefile(file3, rc3) |
| 228 | call writefile(file4, rc4) |
| 229 | |
| 230 | " Get the Vim command to run without the '-u NONE' argument |
| 231 | let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '') |
| 232 | |
| 233 | " Test for ~/.gvimrc |
| 234 | let lines =<< trim END |
| 235 | " Ignore the "failed to create input context" error. |
| 236 | call test_ignore_error('E285') |
| 237 | gui -f |
| 238 | call assert_match('Xhome/\.gvimrc', $MYGVIMRC) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 239 | call assert_match('Xhome/\.vim/', $MYVIMDIR) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 240 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 241 | call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:) |
| 242 | call writefile(v:errors, 'Xresult') |
| 243 | quit |
| 244 | END |
| 245 | call writefile(lines, 'Xscript', 'D') |
| 246 | call system($'{vimcmd} -S Xscript') |
| 247 | call assert_equal([], readfile('Xresult')) |
| 248 | |
| 249 | call delete(rc1) |
| 250 | |
| 251 | " Test for ~/.vim/gvimrc |
| 252 | let lines =<< trim END |
| 253 | " Ignore the "failed to create input context" error. |
| 254 | call test_ignore_error('E285') |
| 255 | gui -f |
| 256 | call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 257 | call assert_match('Xhome/\.vim/', $MYVIMDIR) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 258 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 259 | call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:) |
| 260 | call writefile(v:errors, 'Xresult') |
| 261 | quit |
| 262 | END |
| 263 | call writefile(lines, 'Xscript', 'D') |
| 264 | call system($'{vimcmd} -S Xscript') |
| 265 | call assert_equal([], readfile('Xresult')) |
| 266 | |
| 267 | call delete(rc2) |
| 268 | |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 269 | " Test for ~/.config/vim/gvimrc |
| 270 | let lines =<< trim END |
| 271 | " Ignore the "failed to create input context" error. |
| 272 | call test_ignore_error('E285') |
| 273 | gui -f |
| 274 | let msg = $'HOME="{$HOME}", ~="{expand("~")}"' |
| 275 | call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 276 | call assert_match('Xhome/\.config/vim/', $MYVIMDIR, msg) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 277 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 278 | call assert_equal(#{rc_three: 'three', rc: '.config/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(rc3) |
| 287 | |
| 288 | " Test for ~/xdg/vim/gvimrc |
| 289 | let $XDG_CONFIG_HOME=expand('~/xdg/') |
| 290 | 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}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"' |
| 295 | call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg) |
Christian Brabandt | 4e7249a | 2024-09-05 17:46:19 +0200 | [diff] [blame] | 296 | call assert_match('Xhome/xdg/vim/', $MYVIMDIR, msg) |
Maxim Kim | a34ba82 | 2024-04-17 22:29:06 +0200 | [diff] [blame] | 297 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 298 | call assert_equal(#{rc_four: 'four', rc: 'xdg/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(rc4) |
| 307 | |
| 308 | " Clean up |
| 309 | unlet $XDG_CONFIG_HOME |
| 310 | call GUITearDownCommon() |
| 311 | call delete('Xhome', 'rf') |
| 312 | endfunc |
| 313 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 314 | " vim: shiftwidth=2 sts=2 expandtab |