robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.1.0161: expand() removes slash after env variable that ends with colon (#27791)241c161299

master
6.8 KiB242 linesraw
1" Test for expanding file names
2
3source shared.vim
4source check.vim
5
6func Test_with_directories()
7  call mkdir('Xdir1')
8  call mkdir('Xdir2')
9  call mkdir('Xdir3')
10  cd Xdir3
11  call mkdir('Xdir4')
12  cd ..
13
14  split Xdir1/file
15  call setline(1, ['a', 'b'])
16  w
17  w Xdir3/Xdir4/file
18  close
19
20  next Xdir?/*/file
21  call assert_equal('Xdir3/Xdir4/file', expand('%'))
22  if has('unix')
23    next! Xdir?/*/nofile
24    call assert_equal('Xdir?/*/nofile', expand('%'))
25  endif
26  " Edit another file, on MS-Windows the swap file would be in use and can't
27  " be deleted.
28  edit foo
29
30  call assert_equal(0, delete('Xdir1', 'rf'))
31  call assert_equal(0, delete('Xdir2', 'rf'))
32  call assert_equal(0, delete('Xdir3', 'rf'))
33endfunc
34
35func Test_with_tilde()
36  let dir = getcwd()
37  call mkdir('Xdir ~ dir')
38  call assert_true(isdirectory('Xdir ~ dir'))
39  cd Xdir\ ~\ dir
40  call assert_true(getcwd() =~ 'Xdir \~ dir')
41  call chdir(dir)
42  call delete('Xdir ~ dir', 'd')
43  call assert_false(isdirectory('Xdir ~ dir'))
44endfunc
45
46func Test_expand_tilde_filename()
47  split ~
48  call assert_equal('~', expand('%'))
49  call assert_notequal(expand('%:p'), expand('~/'))
50  call assert_match('\~', expand('%:p'))
51  bwipe!
52endfunc
53
54func Test_expand_env_pathsep()
55  let $FOO = './foo'
56  call assert_equal('./foo/bar', expand('$FOO/bar'))
57  let $FOO = './foo/'
58  call assert_equal('./foo/bar', expand('$FOO/bar'))
59  let $FOO = 'C:'
60  call assert_equal('C:/bar', expand('$FOO/bar'))
61  let $FOO = 'C:/'
62  call assert_equal('C:/bar', expand('$FOO/bar'))
63
64  unlet $FOO
65endfunc
66
67func Test_expandcmd()
68  let $FOO = 'Test'
69  call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
70  unlet $FOO
71
72  new
73  edit Xfile1
74  call assert_equal('e Xfile1', expandcmd('e %'))
75  edit Xfile2
76  edit Xfile1
77  call assert_equal('e Xfile2', 'e #'->expandcmd())
78  edit Xfile2
79  edit Xfile3
80  edit Xfile4
81  let bnum = bufnr('Xfile2')
82  call assert_equal('e Xfile2', expandcmd('e #' . bnum))
83  call setline('.', 'Vim!@#')
84  call assert_equal('e Vim', expandcmd('e <cword>'))
85  call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
86  enew!
87  edit Xfile.java
88  call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
89  call assert_equal('make abc.java', expandcmd('make abc.%:e'))
90  call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
91  edit a1a2a3.rb
92  call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
93
94  call assert_equal('make <afile>', expandcmd("make <afile>"))
95  call assert_equal('make <amatch>', expandcmd("make <amatch>"))
96  call assert_equal('make <abuf>', expandcmd("make <abuf>"))
97  enew
98  call assert_equal('make %', expandcmd("make %"))
99  let $FOO="blue\tsky"
100  call setline(1, "$FOO")
101  call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
102
103  " Test for expression expansion `=
104  let $FOO= "blue"
105  call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
106  let x = expandcmd("`=axbycz`")
107  call assert_equal('`=axbycz`', x)
108  call assert_fails('let x = expandcmd("`=axbycz`", #{errmsg: 1})', 'E121:')
109  let x = expandcmd("`=axbycz`", #{abc: []})
110  call assert_equal('`=axbycz`', x)
111
112  " Test for env variable with spaces
113  let $FOO= "foo bar baz"
114  call assert_equal("e foo bar baz", expandcmd("e $FOO"))
115
116  if has('unix') && executable('bash')
117    " test for using the shell to expand a command argument.
118    " only bash supports the {..} syntax
119    set shell=bash
120    let x = expandcmd('{1..4}')
121    call assert_equal('{1..4}', x)
122    call assert_fails("let x = expandcmd('{1..4}', #{errmsg: v:true})", 'E77:')
123    let x = expandcmd('{1..4}', #{error: v:true})
124    call assert_equal('{1..4}', x)
125    set shell&
126  endif
127
128  unlet $FOO
129  close!
130endfunc
131
132" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
133func Test_source_sfile()
134  let lines =<< trim [SCRIPT]
135    :call assert_equal('<sfile>', expandcmd("<sfile>"))
136    :call assert_equal('<slnum>', expandcmd("<slnum>"))
137    :call assert_equal('<sflnum>', expandcmd("<sflnum>"))
138    :call assert_equal('edit <cfile>', expandcmd("edit <cfile>"))
139    :call assert_equal('edit #', expandcmd("edit #"))
140    :call assert_equal('edit #<2', expandcmd("edit #<2"))
141    :call assert_equal('edit <cword>', expandcmd("edit <cword>"))
142    :call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>"))
143    :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
144    :
145    :call assert_equal('', expand('<script>'))
146    :verbose echo expand('<script>')
147    :call add(v:errors, v:errmsg)
148    :verbose echo expand('<sfile>')
149    :call add(v:errors, v:errmsg)
150    :call writefile(v:errors, 'Xresult')
151    :qall!
152  [SCRIPT]
153  call writefile(lines, 'Xscript')
154  if RunVim([], [], '--clean -s Xscript')
155    call assert_equal([
156          \ 'E1274: No script file name to substitute for "<script>"',
157          \ 'E498: No :source file name to substitute for "<sfile>"'],
158          \ readfile('Xresult'))
159  endif
160  call delete('Xscript')
161  call delete('Xresult')
162endfunc
163
164" Test for expanding filenames multiple times in a command line
165func Test_expand_filename_multicmd()
166  edit foo
167  call setline(1, 'foo!')
168  new
169  call setline(1, 'foo!')
170  new <cword> | new <cWORD>
171  call assert_equal(4, winnr('$'))
172  call assert_equal('foo!', bufname(winbufnr(1)))
173  call assert_equal('foo', bufname(winbufnr(2)))
174  call assert_fails('e %:s/.*//', 'E500:')
175  %bwipe!
176endfunc
177
178func Test_expandcmd_shell_nonomatch()
179  CheckNotMSWindows
180  call assert_equal('$*', expandcmd('$*'))
181endfunc
182
183func Test_expand_script_source()
184  let lines0 =<< trim [SCRIPT]
185    call extend(g:script_level, [expand('<script>:t')])
186    so Xscript1
187    func F0()
188      call extend(g:func_level, [expand('<script>:t')])
189    endfunc
190
191    au User * call extend(g:au_level, [expand('<script>:t')])
192  [SCRIPT]
193
194  let lines1 =<< trim [SCRIPT]
195    call extend(g:script_level, [expand('<script>:t')])
196    so Xscript2
197    func F1()
198      call extend(g:func_level, [expand('<script>:t')])
199    endfunc
200
201    au User * call extend(g:au_level, [expand('<script>:t')])
202  [SCRIPT]
203
204  let lines2 =<< trim [SCRIPT]
205    call extend(g:script_level, [expand('<script>:t')])
206    func F2()
207      call extend(g:func_level, [expand('<script>:t')])
208    endfunc
209
210    au User * call extend(g:au_level, [expand('<script>:t')])
211  [SCRIPT]
212
213  call writefile(lines0, 'Xscript0')
214  call writefile(lines1, 'Xscript1')
215  call writefile(lines2, 'Xscript2')
216
217  " Check the expansion of <script> at different levels.
218  let g:script_level = []
219  let g:func_level = []
220  let g:au_level = []
221
222  so Xscript0
223  call F0()
224  call F1()
225  call F2()
226  doautocmd User
227
228  call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:script_level)
229  call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:func_level)
230  call assert_equal(['Xscript2', 'Xscript1', 'Xscript0'], g:au_level)
231
232  unlet g:script_level g:func_level
233  delfunc F0
234  delfunc F1
235  delfunc F2
236
237  call delete('Xscript0')
238  call delete('Xscript1')
239  call delete('Xscript2')
240endfunc
241
242" vim: shiftwidth=2 sts=2 expandtab