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 | |
| 5 | source shared.vim |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 6 | source mouse.vim |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 7 | |
| 8 | func s:get_rcs() |
| 9 | let rcs = { |
| 10 | \ 'file1': { 'path': '~/.vimrc' }, |
| 11 | \ 'file2': { 'path': '~/.vim/vimrc' }, |
| 12 | \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" }, |
| 13 | \} |
| 14 | for v in values(rcs) |
| 15 | let v.exists = filereadable(expand(v.path)) |
| 16 | endfor |
| 17 | return rcs |
| 18 | endfunc |
| 19 | |
| 20 | func Test_xdg_rc_detection() |
| 21 | CheckUnix |
| 22 | let rc = s:get_rcs() |
| 23 | let before =<< trim CODE |
| 24 | call writefile([expand('$MYVIMRC')], "XMY_VIMRC") |
| 25 | quit! |
| 26 | CODE |
| 27 | call RunVim(before, [], "") |
| 28 | let my_rc = readfile("XMY_VIMRC") |
| 29 | if rc.file1.exists |
| 30 | call assert_equal(rc.file1.path, my_rc) |
| 31 | elseif !rc.file1.exists && rc.file2.exists |
| 32 | call assert_equal(rc.file2.path, my_rc) |
| 33 | elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists |
| 34 | call assert_equal(rc.xdg.path, my_rc) |
| 35 | endif |
| 36 | call delete("XMY_VIMRC") |
| 37 | endfunc |
| 38 | |
| 39 | func Test_xdg_runtime_files() |
| 40 | " This tests, that the initialization file from |
| 41 | " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or |
| 42 | " $XDG_HOMECONFIG/vim/vimrc) are sourced in that order |
| 43 | CheckUnix |
| 44 | call mkdir(expand('~/.vim/'), 'pD') |
| 45 | call mkdir(expand('~/.config/vim/'), 'pD') |
| 46 | call mkdir(expand('~/xdg/vim/'), 'pD') |
| 47 | |
| 48 | let rc1=expand('~/.vimrc') |
| 49 | let rc2=expand('~/.vim/vimrc') |
| 50 | let rc3=expand('~/.config/vim/vimrc') |
| 51 | let rc4=expand('~/xdg/vim/vimrc') |
| 52 | |
| 53 | " g:rc_one|two|three|four is to verify, that the other |
Diego Viola | 2da68c8 | 2024-04-15 20:08:38 +0200 | [diff] [blame] | 54 | " init files are not sourced |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 55 | " g:rc is to verify which rc file has been loaded. |
| 56 | let file1 =<< trim CODE |
| 57 | let g:rc_one = 'one' |
| 58 | let g:rc = '.vimrc' |
| 59 | CODE |
| 60 | let file2 =<< trim CODE |
| 61 | let g:rc_two = 'two' |
| 62 | let g:rc = '.vim/vimrc' |
| 63 | CODE |
| 64 | let file3 =<< trim CODE |
| 65 | let g:rc_three = 'three' |
| 66 | let g:rc = '.config/vim/vimrc' |
| 67 | CODE |
| 68 | let file4 =<< trim CODE |
| 69 | let g:rc_four = 'four' |
| 70 | let g:rc = 'xdg/vim/vimrc' |
| 71 | CODE |
| 72 | call writefile(file1, rc1) |
| 73 | call writefile(file2, rc2) |
| 74 | call writefile(file3, rc3) |
| 75 | call writefile(file4, rc4) |
| 76 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 77 | " Get the Vim command to run without the '-u NONE' argument |
| 78 | let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '') |
| 79 | |
| 80 | " Test for ~/.vimrc |
| 81 | let lines =<< trim END |
| 82 | call assert_match('XfakeHOME/\.vimrc', $MYVIMRC) |
| 83 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 84 | call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:) |
| 85 | call writefile(v:errors, 'Xresult') |
| 86 | quit |
| 87 | END |
| 88 | call writefile(lines, 'Xscript', 'D') |
| 89 | call system($'{vimcmd} -S Xscript') |
| 90 | call assert_equal([], readfile('Xresult')) |
| 91 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 92 | call delete(rc1) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 93 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 94 | " Test for ~/.vim/vimrc |
| 95 | let lines =<< trim END |
| 96 | call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC) |
| 97 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 98 | call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:) |
| 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:) |
| 117 | call writefile(v:errors, 'Xresult') |
| 118 | quit |
| 119 | END |
| 120 | call writefile(lines, 'Xscript', 'D') |
| 121 | call system($'{vimcmd} -S Xscript') |
| 122 | call assert_equal([], readfile('Xresult')) |
| 123 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 124 | call delete(rc3) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 125 | |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 126 | " Test for ~/xdg/vim/vimrc |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 127 | let $XDG_CONFIG_HOME=expand('~/xdg/') |
Yegappan Lakshmanan | 8560e6c | 2024-04-16 22:18:15 +0200 | [diff] [blame] | 128 | let lines =<< trim END |
| 129 | let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"' |
| 130 | call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg) |
| 131 | call filter(g:, {idx, _ -> idx =~ '^rc'}) |
| 132 | call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:) |
| 133 | call writefile(v:errors, 'Xresult') |
| 134 | quit |
| 135 | END |
| 136 | call writefile(lines, 'Xscript', 'D') |
| 137 | call system($'{vimcmd} -S Xscript') |
| 138 | call assert_equal([], readfile('Xresult')) |
| 139 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 140 | call delete(rc4) |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 141 | unlet $XDG_CONFIG_HOME |
| 142 | endfunc |
| 143 | |
Diego Viola | d1068a2 | 2024-04-16 20:58:45 +0200 | [diff] [blame] | 144 | func Test_xdg_version() |
| 145 | CheckUnix |
| 146 | let $HOME = getcwd() .. '/XfakeHOME' |
| 147 | unlet $XDG_CONFIG_HOME |
| 148 | let a = execute(':version')->split('\n') |
| 149 | let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' }) |
| 150 | call assert_equal(1, len(a)) |
| 151 | call assert_match('\~/.config/vim/vimrc', a[0]) |
| 152 | |
| 153 | let $XDG_CONFIG_HOME = expand('~/.xdg') |
| 154 | let a = execute(':version')->split('\n') |
| 155 | let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' }) |
| 156 | call assert_equal(1, len(a)) |
| 157 | call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0]) |
| 158 | unlet $XDG_CONFIG_HOME |
| 159 | endfunc |
| 160 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 161 | " vim: shiftwidth=2 sts=2 expandtab |