summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_xdg.vim
blob: b9ec3c73148cf7fc53fd03bbe11c8f4ca35650a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
" Tests for the XDG feature

source check.vim
source shared.vim

func s:get_rcs()
  let rcs = {
        \ 'file1': { 'path': '~/.vimrc' },
        \ 'file2': { 'path': '~/.vim/vimrc' },
        \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" },
        \}
  for v in values(rcs)
    let v.exists = filereadable(expand(v.path))
  endfor
  return rcs
endfunc

func Test_xdg_rc_detection()
  CheckUnix
  let rc = s:get_rcs()
  let before =<< trim CODE
    call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
    quit!
  CODE
  call RunVim(before, [], "")
  let my_rc = readfile("XMY_VIMRC")
  if rc.file1.exists
    call assert_equal(rc.file1.path, my_rc)
  elseif !rc.file1.exists && rc.file2.exists
    call assert_equal(rc.file2.path, my_rc)
  elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
    call assert_equal(rc.xdg.path, my_rc)
  endif
  call delete("XMY_VIMRC")
endfunc

func Test_xdg_runtime_files()
  " This tests, that the initialization file from
  " ~/.vimrc, ~/.vim/vimrc and ~/.config/vim/vimrc (or
  " $XDG_CONFIG_HOME/vim/vimrc) are sourced in that order
  CheckUnix
  call mkdir(expand('~/.vim/'), 'pD')
  call mkdir(expand('~/.config/vim/'), 'pD')
  call mkdir(expand('~/xdg/vim/'), 'pD')

  let rc1=expand('~/.vimrc')
  let rc2=expand('~/.vim/vimrc')
  let rc3=expand('~/.config/vim/vimrc')
  let rc4=expand('~/xdg/vim/vimrc')

  " g:rc_one|two|three|four is to verify, that the other
  " init files are not sourced
  " g:rc is to verify which rc file has been loaded.
  let file1 =<< trim CODE
    let g:rc_one = 'one'
    let g:rc = '.vimrc'
  CODE
  let file2 =<< trim CODE
    let g:rc_two = 'two'
    let g:rc = '.vim/vimrc'
  CODE
  let file3 =<< trim CODE
    let g:rc_three = 'three'
    let g:rc = '.config/vim/vimrc'
  CODE
  let file4 =<< trim CODE
    let g:rc_four = 'four'
    let g:rc = 'xdg/vim/vimrc'
  CODE
  call writefile(file1, rc1)
  call writefile(file2, rc2)
  call writefile(file3, rc3)
  call writefile(file4, rc4)

  " Get the Vim command to run without the '-u NONE' argument
  let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')

  " Test for ~/.vimrc
  let lines =<< trim END
    call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
    call assert_match('XfakeHOME/\.vim/view', &viewdir)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc1)

  " Test for ~/.vim/vimrc
  let lines =<< trim END
    call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
    call assert_match('XfakeHOME/\.vim/view', &viewdir)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc2)

  " XDG_CONFIG_HOME is set in Github CI runners
  unlet $XDG_CONFIG_HOME

  " Test for ~/.config/vim/vimrc
  let lines =<< trim END
    let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
    call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
    call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc3)

  " Test for ~/xdg/vim/vimrc
  let $XDG_CONFIG_HOME=expand('~/xdg/')
  let lines =<< trim END
    let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
    call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
    call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc4)
  unlet $XDG_CONFIG_HOME
endfunc

func Test_xdg_version()
  CheckUnix
  let $HOME = getcwd() .. '/XfakeHOME'
  unlet $XDG_CONFIG_HOME
  let a = execute(':version')->split('\n')
  let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
  " There should be 1 entry for gvimrc and 1 entry for vimrc,
  " but only if Vim was compiled with gui support
  call assert_equal(1 + has("gui"), len(a))
  call assert_match('\~/\.config/vim/vimrc', a[0])
  if has("gui")
    call assert_match('\~/\.config/vim/gvimrc', a[1])
  endif

  let $XDG_CONFIG_HOME = expand('~/.xdg')
  let a = execute(':version')->split('\n')
  let a = filter(a, { _, val -> val =~ '\.config\|XDG_CONFIG_HOME' })
  call assert_equal(1 + has("gui"), len(a))
  call assert_match('XDG_CONFIG_HOME/vim/vimrc', a[0])
  if has("gui")
    call assert_match('XDG_CONFIG_HOME/vim/gvimrc', a[1])
  endif
  unlet $XDG_CONFIG_HOME
endfunc

" Test for gvimrc, must be last, since it starts the GUI
" and sources a few extra test files
func Test_zzz_xdg_runtime_files()
  CheckCanRunGui
  CheckUnix

  " Is setup in Github Runner
  unlet $XDG_CONFIG_HOME
  source setup_gui.vim
  call GUISetUpCommon()

  " This tests, that the GUI initialization file from
  " ~/.gvimrc, ~/.vim/gvimrc, ~/.config/vim/gvimrc
  " and ~/XDG_CONFIG_HOME/vim/gvimrc is sourced
  " when starting GUI mode
  call mkdir(expand('~/.vim/'), 'pD')
  call mkdir(expand('~/.config/vim/'), 'pD')
  call mkdir(expand('~/xdg/vim/'), 'pD')

  let rc1=expand('~/.gvimrc')
  let rc2=expand('~/.vim/gvimrc')
  let rc3=expand('~/.config/vim/gvimrc')
  let rc4=expand('~/xdg/vim/gvimrc')

  " g:rc_one|two|three|four is to verify, that the other
  " init files are not sourced
  " g:rc is to verify which rc file has been loaded.
  let file1 =<< trim CODE
    let g:rc_one = 'one'
    let g:rc = '.gvimrc'
  CODE
  let file2 =<< trim CODE
    let g:rc_two = 'two'
    let g:rc = '.vim/gvimrc'
  CODE
  let file3 =<< trim CODE
    let g:rc_three = 'three'
    let g:rc = '.config/vim/gvimrc'
  CODE
  let file4 =<< trim CODE
    let g:rc_four = 'four'
    let g:rc = 'xdg/vim/gvimrc'
  CODE
  call writefile(file1, rc1)
  call writefile(file2, rc2)
  call writefile(file3, rc3)
  call writefile(file4, rc4)

  " Get the Vim command to run without the '-u NONE' argument
  let vimcmd = substitute(GetVimCommand(), '-u NONE', '', '')

  " Test for ~/.gvimrc
  let lines =<< trim END
    " Ignore the "failed to create input context" error.
    call test_ignore_error('E285')
    gui -f
    call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc1)

  " Test for ~/.vim/gvimrc
  let lines =<< trim END
    " Ignore the "failed to create input context" error.
    call test_ignore_error('E285')
    gui -f
    call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc2)

  " Test for ~/.config/vim/gvimrc
  let lines =<< trim END
    " Ignore the "failed to create input context" error.
    call test_ignore_error('E285')
    gui -f
    let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
    call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc3)

  " Test for ~/xdg/vim/gvimrc
  let $XDG_CONFIG_HOME=expand('~/xdg/')
  let lines =<< trim END
    " Ignore the "failed to create input context" error.
    call test_ignore_error('E285')
    gui -f
    let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
    call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
    call filter(g:, {idx, _ -> idx =~ '^rc'})
    call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
    call writefile(v:errors, 'Xresult')
    quit
  END
  call writefile(lines, 'Xscript', 'D')
  call system($'{vimcmd} -S Xscript')
  call assert_equal([], readfile('Xresult'))

  call delete(rc4)

  " Clean up
  unlet $XDG_CONFIG_HOME
  call GUITearDownCommon()
  call delete('Xhome', 'rf')
endfunc

" vim: shiftwidth=2 sts=2 expandtab