robin/laufey

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

Jan Edmund Lazovim-patch:8.2.0232: the :compiler command causes a crash (#41087)0df53062df

master
30.5 KiB778 linesraw
1" Test the :compiler command
2
3source check.vim
4source shared.vim
5
6func Test_compiler()
7  CheckExecutable perl
8  CheckFeature quickfix
9
10  let save_LC_ALL = $LC_ALL
11  let $LC_ALL= "C"
12
13  let save_shellslash = &shellslash
14  " Nvim doesn't allow setting value of a hidden option to non-default value
15  if exists('+shellslash')
16    " %:S does not work properly with 'shellslash' set
17    set noshellslash
18  endif
19
20  e Xfoo.pl
21  " Play nice with other tests.
22  defer setqflist([])
23
24  compiler perl
25  call assert_equal('perl', b:current_compiler)
26  call assert_fails('let g:current_compiler', 'E121:')
27  let verbose_efm = execute('verbose set efm')
28  call assert_match('Last set from .*[/\\]compiler[/\\]perl.vim ', verbose_efm)
29  " Not using the global value
30  call assert_notequal('', &l:efm)
31
32  call setline(1, ['#!/usr/bin/perl -w', 'use strict;', 'my $foo=1'])
33  w!
34  call feedkeys(":make\<CR>\<CR>", 'tx')
35  call assert_fails('clist', 'E42:')
36
37  call setline(1, ['#!/usr/bin/perl -w', 'use strict;', '$foo=1'])
38  w!
39  call feedkeys(":make\<CR>\<CR>", 'tx')
40  let a=execute('clist')
41  call assert_match('\d\+ Xfoo.pl:3: Global symbol "$foo" '
42  \ .               'requires explicit package name', a)
43
44  compiler make
45  call assert_fails('let b:current_compiler', 'E121:')
46  call assert_fails('let g:current_compiler', 'E121:')
47  let verbose_efm = execute('verbose set efm')
48  call assert_match('Last set from .*[/\\]compiler[/\\]make.vim ', verbose_efm)
49
50  compiler! perl
51  call assert_equal('perl', b:current_compiler)
52  call assert_equal('perl', g:current_compiler)
53  let verbose_efm = execute('verbose set efm')
54  call assert_match('Last set from .*[/\\]compiler[/\\]perl.vim ', verbose_efm)
55  call assert_equal(verbose_efm, execute('verbose setglobal efm'))
56  " Using the global value
57  call assert_equal('', &l:efm)
58
59  compiler! make
60  call assert_fails('let b:current_compiler', 'E121:')
61  call assert_fails('let g:current_compiler', 'E121:')
62  let verbose_efm = execute('verbose set efm')
63  call assert_match('Last set from .*[/\\]compiler[/\\]make.vim ', verbose_efm)
64  call assert_equal(verbose_efm, execute('verbose setglobal efm'))
65  " Using the global value
66  call assert_equal('', &l:efm)
67
68  let &shellslash = save_shellslash
69  call delete('Xfoo.pl')
70  bw!
71  let $LC_ALL = save_LC_ALL
72endfunc
73
74func GetCompilerNames()
75  return glob('$VIMRUNTIME/compiler/*.vim', 0, 1)
76        \ ->map({i, v -> substitute(v, '.*[\\/]\([a-zA-Z0-9_\-]*\).vim', '\1', '')})
77        \ ->sort()
78endfunc
79
80func Test_compiler_without_arg()
81  let runtime = substitute($VIMRUNTIME, '\\', '/', 'g')
82  let a = split(execute('compiler'))
83  let exp = GetCompilerNames()
84  call assert_match(runtime .. '/compiler/' .. exp[0] .. '.vim$',  a[0])
85  call assert_match(runtime .. '/compiler/' .. exp[1] .. '.vim$',  a[1])
86  call assert_match(runtime .. '/compiler/' .. exp[-1] .. '.vim$', a[-1])
87endfunc
88
89" Test executing :compiler from the command line, not from a script
90func Test_compiler_commandline()
91  call system(GetVimCommandClean() .. ' -c "compiler gcc" -c "call writefile([b:current_compiler], ''XcompilerOut'')" -c "quit"')
92  call assert_equal(0, v:shell_error)
93  call assert_equal(["gcc"], readfile('XcompilerOut'))
94
95  call delete('XcompilerOut')
96endfunc
97
98func Test_compiler_completion()
99  let clist = GetCompilerNames()->join(' ')
100  call feedkeys(":compiler \<C-A>\<C-B>\"\<CR>", 'tx')
101  call assert_match('^"compiler ' .. clist .. '$', @:)
102
103  call feedkeys(":compiler p\<C-A>\<C-B>\"\<CR>", 'tx')
104  call assert_match('"compiler pandoc pbx perl\( p[a-z_]\+\)\+ pyunit', @:)
105
106  call feedkeys(":compiler! p\<C-A>\<C-B>\"\<CR>", 'tx')
107  call assert_match('"compiler! pandoc pbx perl\( p[a-z_]\+\)\+ pyunit', @:)
108endfunc
109
110func Test_compiler_error()
111  let g:current_compiler = 'abc'
112  call assert_fails('compiler doesnotexist', 'E666:')
113  call assert_equal('abc', g:current_compiler)
114  call assert_fails('compiler! doesnotexist', 'E666:')
115  unlet! g:current_compiler
116endfunc
117
118func s:SpotBugsParseFilterMakePrg(dirname, makeprg)
119  let result = {}
120  let result.sourcepath = ''
121  let result.classfiles = []
122
123  " Get the argument after the rightmost occurrence of "-sourcepath".
124  let offset = strridx(a:makeprg, '-sourcepath')
125  if offset < 0
126    return result
127  endif
128  let offset += 1 + strlen('-sourcepath')
129  let result.sourcepath = matchstr(strpart(a:makeprg, offset), '.\{-}\ze[ \t]')
130  let offset += 1 + strlen(result.sourcepath)
131
132  " Get the class file arguments, dropping the pathname prefix.
133  let offset = stridx(a:makeprg, a:dirname, offset)
134  if offset < 0
135    return result
136  endif
137
138  while offset > -1
139    let candidate = matchstr(a:makeprg, '[^ \t]\{-}\.class\>', offset)
140    if empty(candidate)
141      break
142    endif
143    call add(result.classfiles, candidate)
144    let offset = stridx(a:makeprg, a:dirname, (1 + strlen(candidate) + offset))
145  endwhile
146
147  call sort(result.classfiles)
148  return result
149endfunc
150
151func Test_compiler_spotbugs_makeprg()
152  let save_shellslash = &shellslash
153  set shellslash
154
155  call assert_true(mkdir('Xspotbugs/src/tests/α/β/γ/δ', 'pR'))
156  call assert_true(mkdir('Xspotbugs/tests/α/β/γ/δ', 'pR'))
157
158  let lines =<< trim END
159      // EOL comment. /*
160      abstract class
161      𐌂1 /* Multiline comment. */ {
162          /* Multiline comment. */ // EOL comment. /*
163          static final String COMMENT_A_LIKE = "/*";
164          { new Object() {/* Try globbing. */}; }
165          static { interface 𐌉𐌉1 {} }
166          static class 𐌂11 { interface 𐌉𐌉2 {} }
167      }
168      /* Multiline comment. */ // EOL comment. /*
169      final class 𐌂2 {
170          public static void main(String... aa) {
171              record 𐌓() {}
172              enum 𐌄 {}
173          }
174      } // class
175  END
176
177  " THE EXPECTED RESULTS.
178  let results = {}
179  let results['Xspotbugs/src/tests/𐌂1.java'] = {
180      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/𐌂1.java',
181          \ ':p:h:S')},
182      \ 'classfiles': sort([
183          \ 'Xspotbugs/tests/𐌂1$1.class',
184          \ 'Xspotbugs/tests/𐌂1$1𐌉𐌉1.class',
185          \ 'Xspotbugs/tests/𐌂1$𐌂11$𐌉𐌉2.class',
186          \ 'Xspotbugs/tests/𐌂1$𐌂11.class',
187          \ 'Xspotbugs/tests/𐌂1.class',
188          \ 'Xspotbugs/tests/𐌂2$1𐌄.class',
189          \ 'Xspotbugs/tests/𐌂2$1𐌓.class',
190          \ 'Xspotbugs/tests/𐌂2.class']),
191      \ }
192  " No class file for an empty source file even with "-Xpkginfo:always".
193  let results['Xspotbugs/src/tests/package-info.java'] = {
194      \ 'Sourcepath': {-> ''},
195      \ 'classfiles': [],
196      \ }
197  let results['Xspotbugs/src/tests/α/𐌂1.java'] = {
198      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/𐌂1.java',
199          \ ':p:h:h:S')},
200      \ 'classfiles': sort([
201          \ 'Xspotbugs/tests/α/𐌂1$1.class',
202          \ 'Xspotbugs/tests/α/𐌂1$1𐌉𐌉1.class',
203          \ 'Xspotbugs/tests/α/𐌂1$𐌂11$𐌉𐌉2.class',
204          \ 'Xspotbugs/tests/α/𐌂1$𐌂11.class',
205          \ 'Xspotbugs/tests/α/𐌂1.class',
206          \ 'Xspotbugs/tests/α/𐌂2$1𐌄.class',
207          \ 'Xspotbugs/tests/α/𐌂2$1𐌓.class',
208          \ 'Xspotbugs/tests/α/𐌂2.class']),
209      \ }
210  let results['Xspotbugs/src/tests/α/package-info.java'] = {
211      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/package-info.java',
212          \ ':p:h:S')},
213      \ 'classfiles': ['Xspotbugs/tests/α/package-info.class'],
214      \ }
215  let results['Xspotbugs/src/tests/α/β/𐌂1.java'] = {
216      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/𐌂1.java',
217          \ ':p:h:h:h:S')},
218      \ 'classfiles': sort([
219          \ 'Xspotbugs/tests/α/β/𐌂1$1.class',
220          \ 'Xspotbugs/tests/α/β/𐌂1$1𐌉𐌉1.class',
221          \ 'Xspotbugs/tests/α/β/𐌂1$𐌂11$𐌉𐌉2.class',
222          \ 'Xspotbugs/tests/α/β/𐌂1$𐌂11.class',
223          \ 'Xspotbugs/tests/α/β/𐌂1.class',
224          \ 'Xspotbugs/tests/α/β/𐌂2$1𐌄.class',
225          \ 'Xspotbugs/tests/α/β/𐌂2$1𐌓.class',
226          \ 'Xspotbugs/tests/α/β/𐌂2.class']),
227      \ }
228  let results['Xspotbugs/src/tests/α/β/package-info.java'] = {
229      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/package-info.java',
230          \ ':p:h:S')},
231      \ 'classfiles': ['Xspotbugs/tests/α/β/package-info.class'],
232      \ }
233  let results['Xspotbugs/src/tests/α/β/γ/𐌂1.java'] = {
234      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/γ/𐌂1.java',
235          \ ':p:h:h:h:h:S')},
236      \ 'classfiles': sort([
237          \ 'Xspotbugs/tests/α/β/γ/𐌂1$1.class',
238          \ 'Xspotbugs/tests/α/β/γ/𐌂1$1𐌉𐌉1.class',
239          \ 'Xspotbugs/tests/α/β/γ/𐌂1$𐌂11$𐌉𐌉2.class',
240          \ 'Xspotbugs/tests/α/β/γ/𐌂1$𐌂11.class',
241          \ 'Xspotbugs/tests/α/β/γ/𐌂1.class',
242          \ 'Xspotbugs/tests/α/β/γ/𐌂2$1𐌄.class',
243          \ 'Xspotbugs/tests/α/β/γ/𐌂2$1𐌓.class',
244          \ 'Xspotbugs/tests/α/β/γ/𐌂2.class']),
245      \ }
246  let results['Xspotbugs/src/tests/α/β/γ/package-info.java'] = {
247      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/γ/package-info.java',
248          \ ':p:h:S')},
249      \ 'classfiles': ['Xspotbugs/tests/α/β/γ/package-info.class'],
250      \ }
251  let results['Xspotbugs/src/tests/α/β/γ/δ/𐌂1.java'] = {
252      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/γ/δ/𐌂1.java',
253          \ ':p:h:h:h:h:h:S')},
254      \ 'classfiles': sort([
255          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂1$1.class',
256          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂1$1𐌉𐌉1.class',
257          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂1$𐌂11$𐌉𐌉2.class',
258          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂1$𐌂11.class',
259          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂1.class',
260          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂2$1𐌄.class',
261          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂2$1𐌓.class',
262          \ 'Xspotbugs/tests/α/β/γ/δ/𐌂2.class']),
263      \ }
264  let results['Xspotbugs/src/tests/α/β/γ/δ/package-info.java'] = {
265      \ 'Sourcepath': {-> fnamemodify('Xspotbugs/src/tests/α/β/γ/δ/package-info.java',
266          \ ':p:h:S')},
267      \ 'classfiles': ['Xspotbugs/tests/α/β/γ/δ/package-info.class'],
268      \ }
269
270  " MAKE CLASS FILES DISCOVERABLE!
271  let g:spotbugs_properties = {
272      \ 'sourceDirPath': ['src/tests'],
273      \ 'classDirPath': ['tests'],
274  \ }
275
276  call assert_true(has_key(s:SpotBugsParseFilterMakePrg('Xspotbugs', ''), 'sourcepath'))
277  call assert_true(has_key(s:SpotBugsParseFilterMakePrg('Xspotbugs', ''), 'classfiles'))
278
279  " Write 45 mock-up class files for 10 source files.
280  for [class_dir, src_dir, package] in [
281        \ ['Xspotbugs/tests/', 'Xspotbugs/src/tests/', ''],
282        \ ['Xspotbugs/tests/α/', 'Xspotbugs/src/tests/α/', 'package α;'],
283        \ ['Xspotbugs/tests/α/β/', 'Xspotbugs/src/tests/α/β/', 'package α.β;'],
284        \ ['Xspotbugs/tests/α/β/γ/', 'Xspotbugs/src/tests/α/β/γ/', 'package α.β.γ;'],
285        \ ['Xspotbugs/tests/α/β/γ/δ/', 'Xspotbugs/src/tests/α/β/γ/δ/', 'package α.β.γ.δ;']]
286    for class_file in ['𐌂1$1.class', '𐌂1$1𐌉𐌉1.class', '𐌂1$𐌂11$𐌉𐌉2.class',
287          \ '𐌂1$𐌂11.class', '𐌂1.class', '𐌂2$1𐌄.class', '𐌂2$1𐌓.class', '𐌂2.class']
288      call writefile(0zcafe.babe.0000.0041, class_dir .. class_file)
289    endfor
290    call writefile(0zcafe.babe.0000.0041, class_dir .. 'package-info.class')
291
292    " Write Java source files.
293    let type_file = src_dir .. '𐌂1.java'
294    call writefile(insert(copy(lines), package), type_file)
295    let package_file = src_dir .. 'package-info.java'
296    call writefile([package], src_dir .. 'package-info.java')
297
298    " Note that using "off" for the first _outer_ iteration is preferable
299    " because only then "hlexists()" may be 0 (see "compiler/spotbugs.vim").
300    for s in ['off', 'on']
301      execute 'syntax ' .. s
302
303      execute 'edit ' .. type_file
304      compiler spotbugs
305      let result = s:SpotBugsParseFilterMakePrg('Xspotbugs', &l:makeprg)
306      call assert_equal(results[type_file].Sourcepath(), result.sourcepath)
307      call assert_equal(results[type_file].classfiles, result.classfiles)
308      bwipeout
309
310      execute 'edit ' .. package_file
311      compiler spotbugs
312      let result = s:SpotBugsParseFilterMakePrg('Xspotbugs', &l:makeprg)
313      call assert_equal(results[package_file].Sourcepath(), result.sourcepath)
314      call assert_equal(results[package_file].classfiles, result.classfiles)
315      bwipeout
316    endfor
317  endfor
318
319  let &shellslash = save_shellslash
320endfunc
321
322func s:SpotBugsBeforeFileTypeTryPluginAndClearCache(state)
323  " Ponder over "extend(spotbugs#DefaultProperties(), g:spotbugs_properties)"
324  " in "ftplugin/java.vim".
325  let g:spotbugs#state = a:state
326  runtime autoload/spotbugs.vim
327endfunc
328
329func Test_compiler_spotbugs_properties()
330  let save_shellslash = &shellslash
331  set shellslash
332  setlocal makeprg=
333  filetype plugin on
334
335  call assert_true(mkdir('Xspotbugs/src', 'pR'))
336  call assert_true(mkdir('Xspotbugs/tests', 'pR'))
337  let type_file = 'Xspotbugs/src/𐌄.java'
338  let test_file = 'Xspotbugs/tests/𐌄$.java'
339  call writefile(['enum 𐌄{}'], type_file)
340  call writefile(['class 𐌄${}'], test_file)
341
342  " TEST INTEGRATION WITH A BOGUS COMPILER PLUGIN.
343  if !filereadable($VIMRUNTIME .. '/compiler/foo.vim') && !executable('foo')
344    let g:spotbugs_properties = {'compiler': 'foo'}
345    " XXX: In case this "if" block is no longer first.
346    call s:SpotBugsBeforeFileTypeTryPluginAndClearCache({
347        \ 'compiler': g:spotbugs_properties.compiler,
348    \ })
349    execute 'edit ' .. type_file
350    call assert_equal('java', &l:filetype)
351    " This variable will indefinitely keep the compiler name.
352    call assert_equal('foo', g:spotbugs#state.compiler)
353    " The "compiler" entry should be gone after FileType and default entries
354    " should only appear for a supported compiler.
355    call assert_false(has_key(g:spotbugs_properties, 'compiler'))
356    call assert_true(empty(g:spotbugs_properties))
357    " Query default implementations.
358    call assert_true(exists('*spotbugs#DefaultProperties'))
359    call assert_true(exists('*spotbugs#DefaultPreCompilerAction'))
360    call assert_true(exists('*spotbugs#DefaultPreCompilerTestAction'))
361    call assert_true(empty(spotbugs#DefaultProperties()))
362    " Get a ":message".
363    redir => out
364    call spotbugs#DefaultPreCompilerAction()
365    redir END
366    call assert_equal('Not supported: "foo"', out[stridx(out, 'Not') :])
367    " Get a ":message".
368    redir => out
369    call spotbugs#DefaultPreCompilerTestAction()
370    redir END
371    call assert_equal('Not supported: "foo"', out[stridx(out, 'Not') :])
372    " No ":autocmd"s without one of "PreCompiler*Action", "PostCompilerAction".
373    call assert_false(exists('#java_spotbugs'))
374    bwipeout
375  endif
376
377  let s:spotbugs_results = {
378      \ 'preActionDone': 0,
379      \ 'preTestActionDone': 0,
380      \ 'preTestLocalActionDone': 0,
381      \ 'postActionDone': 0,
382      \ 'preCommandArguments': '',
383      \ 'preTestCommandArguments': '',
384      \ 'postCommandArguments': '',
385  \ }
386  defer execute('unlet s:spotbugs_results')
387
388  func! g:SpotBugsPreAction() abort
389    let s:spotbugs_results.preActionDone = 1
390    " XXX: Notify the spotbugs compiler about success or failure.
391    cc
392  endfunc
393  defer execute('delfunction g:SpotBugsPreAction')
394
395  func! g:SpotBugsPreTestAction() abort
396    let s:spotbugs_results.preTestActionDone = 1
397    " XXX: Let see compilation fail.
398    throw 'Oops'
399  endfunc
400  defer execute('delfunction g:SpotBugsPreTestAction')
401
402  func! g:SpotBugsPreTestLocalAction() abort
403    let s:spotbugs_results.preTestLocalActionDone = 1
404    " XXX: Notify the spotbugs compiler about success or failure.
405    cc
406  endfunc
407  defer execute('delfunction g:SpotBugsPreTestLocalAction')
408
409  func! g:SpotBugsPostAction() abort
410    let s:spotbugs_results.postActionDone = 1
411  endfunc
412  defer execute('delfunction g:SpotBugsPostAction')
413
414  func! g:SpotBugsPreCommand(arguments) abort
415    let s:spotbugs_results.preActionDone = 1
416    let s:spotbugs_results.preCommandArguments = a:arguments
417    " XXX: Notify the spotbugs compiler about success or failure.
418    cc
419  endfunc
420  defer execute('delfunction g:SpotBugsPreCommand')
421
422  func! g:SpotBugsPreTestCommand(arguments) abort
423    let s:spotbugs_results.preTestActionDone = 1
424    let s:spotbugs_results.preTestCommandArguments = a:arguments
425    " XXX: Notify the spotbugs compiler about success or failure.
426    cc
427  endfunc
428  defer execute('delfunction g:SpotBugsPreTestCommand')
429
430  func! g:SpotBugsPostCommand(arguments) abort
431    let s:spotbugs_results.postActionDone = 1
432    let s:spotbugs_results.postCommandArguments = a:arguments
433  endfunc
434  defer execute('delfunction g:SpotBugsPostCommand')
435
436  func! g:SpotBugsPostCompilerActionExecutor(action) abort
437    try
438      " XXX: Notify the spotbugs compiler about success or failure.
439      cc
440    catch /\<E42:/
441      execute a:action
442    endtry
443  endfunc
444  defer execute('delfunction g:SpotBugsPostCompilerActionExecutor')
445
446  " TEST INTEGRATION WITH A SUPPORTED COMPILER PLUGIN.
447  if filereadable($VIMRUNTIME .. '/compiler/maven.vim')
448    let save_PATH = $PATH
449    if !executable('mvn')
450      if has('win32')
451        let $PATH = 'Xspotbugs;' .. $PATH
452        " This is what ":help executable()" suggests.
453        call writefile([], 'Xspotbugs/mvn.cmd')
454      else
455        let $PATH = 'Xspotbugs:' .. $PATH
456        call writefile([], 'Xspotbugs/mvn')
457        call setfperm('Xspotbugs/mvn', 'rwx------')
458      endif
459    endif
460
461    let g:spotbugs_properties = {
462        \ 'compiler': 'maven',
463        \ 'PreCompilerAction': function('g:SpotBugsPreAction'),
464        \ 'PreCompilerTestAction': function('g:SpotBugsPreTestAction'),
465        \ 'PostCompilerAction': function('g:SpotBugsPostAction'),
466    \ }
467    " XXX: In case this is a runner-up ":edit".
468    call s:SpotBugsBeforeFileTypeTryPluginAndClearCache({
469        \ 'compiler': g:spotbugs_properties.compiler,
470    \ })
471    execute 'edit ' .. type_file
472    call assert_equal('java', &l:filetype)
473    call assert_equal('maven', g:spotbugs#state.compiler)
474    call assert_false(has_key(g:spotbugs_properties, 'compiler'))
475    call assert_false(empty(g:spotbugs_properties))
476    " Query default implementations.
477    call assert_true(exists('*spotbugs#DefaultProperties'))
478    call assert_equal(sort([
479            \ 'PreCompilerAction',
480            \ 'PreCompilerTestAction',
481            \ 'PostCompilerAction',
482            \ 'sourceDirPath',
483            \ 'classDirPath',
484            \ 'testSourceDirPath',
485            \ 'testClassDirPath',
486        \ ]),
487        \ sort(keys(spotbugs#DefaultProperties())))
488    " Some ":autocmd"s with one of "PreCompiler*Action", "PostCompilerAction".
489    call assert_true(exists('#java_spotbugs'))
490    call assert_true(exists('#java_spotbugs#Syntax'))
491    call assert_true(exists('#java_spotbugs#User'))
492    call assert_equal(2, exists(':SpotBugsDefineBufferAutocmd'))
493    " SpotBugsDefineBufferAutocmd SigUSR1 User SigUSR1 User SigUSR1 User
494    " call assert_true(exists('#java_spotbugs#SigUSR1'))
495    SpotBugsDefineBufferAutocmd Signal User Signal User Signal User
496    call assert_true(exists('#java_spotbugs#Signal'))
497    call assert_true(exists('#java_spotbugs#Syntax'))
498    call assert_true(exists('#java_spotbugs#User'))
499    call assert_equal(2, exists(':SpotBugsRemoveBufferAutocmd'))
500    " SpotBugsRemoveBufferAutocmd SigUSR1 User SigUSR1 User UserGettingBored
501    " call assert_false(exists('#java_spotbugs#SigUSR1'))
502    SpotBugsRemoveBufferAutocmd Signal User Signal User UserGettingBored
503    call assert_false(exists('#java_spotbugs#Signal'))
504    call assert_true(exists('#java_spotbugs#Syntax'))
505    call assert_true(exists('#java_spotbugs#User'))
506
507    let s:spotbugs_results.preActionDone = 0
508    let s:spotbugs_results.preTestActionDone = 0
509    let s:spotbugs_results.postActionDone = 0
510
511    doautocmd java_spotbugs Syntax
512    call assert_false(exists('#java_spotbugs#Syntax'))
513
514    " No match: "type_file !~# 'src/main/java'".
515    call assert_false(s:spotbugs_results.preActionDone)
516    " No match: "type_file !~# 'src/test/java'".
517    call assert_false(s:spotbugs_results.preTestActionDone)
518    " No pre-match, no post-action.
519    call assert_false(s:spotbugs_results.postActionDone)
520    " Without a match, confirm that ":compiler spotbugs" has NOT run.
521    call assert_true(empty(&l:makeprg))
522
523    let s:spotbugs_results.preActionDone = 0
524    let s:spotbugs_results.preTestActionDone = 0
525    let s:spotbugs_results.postActionDone = 0
526    " Update path entries.  (Note that we cannot use just "src" because there
527    " is another "src" directory nearer the filesystem root directory, i.e.
528    " "vim/vim/src/testdir/Xspotbugs/src", and "s:DispatchAction()" (see
529    " "ftplugin/java.vim") will match "vim/vim/src/testdir/Xspotbugs/tests"
530    " against "src".)
531    let g:spotbugs_properties.sourceDirPath = ['Xspotbugs/src']
532    let g:spotbugs_properties.classDirPath = ['Xspotbugs/src']
533    let g:spotbugs_properties.testSourceDirPath = ['tests']
534    let g:spotbugs_properties.testClassDirPath = ['tests']
535
536    doautocmd java_spotbugs User
537    " No match: "type_file !~# 'src/main/java'" (with old "*DirPath" values
538    " cached).
539    call assert_false(s:spotbugs_results.preActionDone)
540    " No match: "type_file !~# 'src/test/java'" (with old "*DirPath" values
541    " cached).
542    call assert_false(s:spotbugs_results.preTestActionDone)
543    " No pre-match, no post-action.
544    call assert_false(s:spotbugs_results.postActionDone)
545    " Without a match, confirm that ":compiler spotbugs" has NOT run.
546    call assert_true(empty(&l:makeprg))
547
548    let s:spotbugs_results.preActionDone = 0
549    let s:spotbugs_results.preTestActionDone = 0
550    let s:spotbugs_results.postActionDone = 0
551    " XXX: Re-build ":autocmd"s from scratch with new values applied.
552    doautocmd FileType
553
554    call assert_true(exists('b:spotbugs_syntax_once'))
555    doautocmd java_spotbugs User
556    " A match: "type_file =~# 'Xspotbugs/src'" (with new "*DirPath" values
557    " cached).
558    call assert_true(s:spotbugs_results.preActionDone)
559    " No match: "type_file !~# 'tests'" (with new "*DirPath" values cached).
560    call assert_false(s:spotbugs_results.preTestActionDone)
561    " For a pre-match, a post-action.
562    call assert_true(s:spotbugs_results.postActionDone)
563
564    " With a match, confirm that ":compiler spotbugs" has run.
565    if has('win32')
566      call assert_match('^spotbugs\.bat\s', &l:makeprg)
567    else
568      call assert_match('^spotbugs\s', &l:makeprg)
569    endif
570
571    bwipeout
572    setlocal makeprg=
573    let s:spotbugs_results.preActionDone = 0
574    let s:spotbugs_results.preTestActionDone = 0
575    let s:spotbugs_results.preTestLocalActionDone = 0
576    let s:spotbugs_results.postActionDone = 0
577
578    execute 'edit ' .. test_file
579    " Prepare a buffer-local, incomplete variant of properties, relying on
580    " "ftplugin/java.vim" to take care of merging in unique entries, if any,
581    " from "g:spotbugs_properties".
582    let b:spotbugs_properties = {
583        \ 'PreCompilerTestAction': function('g:SpotBugsPreTestLocalAction'),
584    \ }
585    call assert_equal('java', &l:filetype)
586    call assert_true(exists('#java_spotbugs'))
587    call assert_true(exists('#java_spotbugs#Syntax'))
588    call assert_true(exists('#java_spotbugs#User'))
589    call assert_fails('doautocmd java_spotbugs Syntax', 'Oops')
590    call assert_false(exists('#java_spotbugs#Syntax'))
591    " No match: "test_file !~# 'Xspotbugs/src'".
592    call assert_false(s:spotbugs_results.preActionDone)
593    " A match: "test_file =~# 'tests'".
594    call assert_true(s:spotbugs_results.preTestActionDone)
595    call assert_false(s:spotbugs_results.preTestLocalActionDone)
596    " No action after pre-failure (the thrown "Oops" doesn't qualify for ":cc").
597    call assert_false(s:spotbugs_results.postActionDone)
598    " No ":compiler spotbugs" will be run after pre-failure.
599    call assert_true(empty(&l:makeprg))
600
601    let s:spotbugs_results.preActionDone = 0
602    let s:spotbugs_results.preTestActionDone = 0
603    let s:spotbugs_results.preTestLocalActionDone = 0
604    let s:spotbugs_results.postActionDone = 0
605    " XXX: Re-build ":autocmd"s from scratch with buffer-local values applied.
606    doautocmd FileType
607
608    call assert_true(exists('b:spotbugs_syntax_once'))
609    doautocmd java_spotbugs User
610    " No match: "test_file !~# 'Xspotbugs/src'".
611    call assert_false(s:spotbugs_results.preActionDone)
612    " A match: "test_file =~# 'tests'".
613    call assert_true(s:spotbugs_results.preTestLocalActionDone)
614    call assert_false(s:spotbugs_results.preTestActionDone)
615    " For a pre-match, a post-action.
616    call assert_true(s:spotbugs_results.postActionDone)
617
618    " With a match, confirm that ":compiler spotbugs" has run.
619    if has('win32')
620      call assert_match('^spotbugs\.bat\s', &l:makeprg)
621    else
622      call assert_match('^spotbugs\s', &l:makeprg)
623    endif
624
625    setlocal makeprg=
626    let s:spotbugs_results.preActionDone = 0
627    let s:spotbugs_results.preTestActionDone = 0
628    let s:spotbugs_results.preTestLocalActionDone = 0
629    let s:spotbugs_results.postActionDone = 0
630    let s:spotbugs_results.preCommandArguments = ''
631    let s:spotbugs_results.preTestCommandArguments = ''
632    let s:spotbugs_results.postCommandArguments = ''
633    " XXX: Compose the assigned "*Command"s with the default Maven "*Action"s.
634    let b:spotbugs_properties = {
635        \ 'compiler': 'maven',
636        \ 'DefaultPreCompilerTestCommand': function('g:SpotBugsPreTestCommand'),
637        \ 'DefaultPreCompilerCommand': function('g:SpotBugsPreCommand'),
638        \ 'DefaultPostCompilerCommand': function('g:SpotBugsPostCommand'),
639        \ 'PostCompilerActionExecutor': function('g:SpotBugsPostCompilerActionExecutor'),
640        \ 'augroupForPostCompilerAction': 'java_spotbugs_test',
641        \ 'sourceDirPath': ['Xspotbugs/src'],
642        \ 'classDirPath': ['Xspotbugs/src'],
643        \ 'testSourceDirPath': ['tests'],
644        \ 'testClassDirPath': ['tests'],
645    \ }
646    unlet g:spotbugs_properties
647    " XXX: Re-build ":autocmd"s from scratch with buffer-local values applied.
648    call s:SpotBugsBeforeFileTypeTryPluginAndClearCache({
649        \ 'compiler': b:spotbugs_properties.compiler,
650        \ 'commands': {
651            \ 'DefaultPreCompilerTestCommand':
652                \ b:spotbugs_properties.DefaultPreCompilerTestCommand,
653            \ 'DefaultPreCompilerCommand':
654                \ b:spotbugs_properties.DefaultPreCompilerCommand,
655            \ 'DefaultPostCompilerCommand':
656                \ b:spotbugs_properties.DefaultPostCompilerCommand,
657        \ },
658    \ })
659    doautocmd FileType
660
661    call assert_equal('maven', g:spotbugs#state.compiler)
662    call assert_equal(sort([
663            \ 'DefaultPreCompilerTestCommand',
664            \ 'DefaultPreCompilerCommand',
665            \ 'DefaultPostCompilerCommand',
666        \ ]),
667        \ sort(keys(g:spotbugs#state.commands)))
668    call assert_true(exists('b:spotbugs_syntax_once'))
669    doautocmd java_spotbugs User
670    " No match: "test_file !~# 'Xspotbugs/src'".
671    call assert_false(s:spotbugs_results.preActionDone)
672    call assert_true(empty(s:spotbugs_results.preCommandArguments))
673    " A match: "test_file =~# 'tests'".
674    call assert_true(s:spotbugs_results.preTestActionDone)
675    call assert_equal('test-compile', s:spotbugs_results.preTestCommandArguments)
676    " For a pre-match, a post-action.
677    call assert_true(s:spotbugs_results.postActionDone)
678    call assert_equal('%:S', s:spotbugs_results.postCommandArguments)
679
680    " With a match, confirm that ":compiler spotbugs" has run.
681    if has('win32')
682      call assert_match('^spotbugs\.bat\s', &l:makeprg)
683    else
684      call assert_match('^spotbugs\s', &l:makeprg)
685    endif
686
687    setlocal makeprg=
688    let s:spotbugs_results.preActionDone = 0
689    let s:spotbugs_results.preTestActionOtherDone = 0
690    let s:spotbugs_results.preTestLocalActionDone = 0
691    let s:spotbugs_results.postActionDone = 0
692    let s:spotbugs_results.preCommandArguments = ''
693    let s:spotbugs_results.preTestCommandArguments = ''
694    let s:spotbugs_results.postCommandArguments = ''
695
696    " When "PostCompilerActionExecutor", "Pre*Action" and/or "Pre*TestAction",
697    " and "Post*Action" are available, "#java_spotbugs_post" must be defined.
698    call assert_true(exists('#java_spotbugs_post'))
699    call assert_true(exists('#java_spotbugs_post#User'))
700    call assert_false(exists('#java_spotbugs_post#ShellCmdPost'))
701    call assert_false(exists('#java_spotbugs_test#ShellCmdPost'))
702
703    " Re-link a Funcref on the fly.
704    func! g:SpotBugsPreTestCommand(arguments) abort
705      let s:spotbugs_results.preTestActionOtherDone = 1
706      let s:spotbugs_results.preTestCommandArguments = a:arguments
707      " Define a once-only ":autocmd" for "#java_spotbugs_test#ShellCmdPost".
708      doautocmd java_spotbugs_post User
709      " XXX: Do NOT use ":cc" to notify the spotbugs compiler about success or
710      " failure, and assume the transfer of control to a ShellCmdPost command.
711    endfunc
712
713    doautocmd java_spotbugs User
714    " No match: "test_file !~# 'Xspotbugs/src'".
715    call assert_false(s:spotbugs_results.preActionDone)
716    call assert_true(empty(s:spotbugs_results.preCommandArguments))
717    " A match: "test_file =~# 'tests'".
718    call assert_true(s:spotbugs_results.preTestActionOtherDone)
719    call assert_equal('test-compile', s:spotbugs_results.preTestCommandArguments)
720    " For a pre-match, no post-action (without ":cc") UNLESS a ShellCmdPost
721    " event is consumed whose command will invoke "PostCompilerActionExecutor"
722    " and the latter will accept a post-compiler action argument.
723    call assert_false(s:spotbugs_results.postActionDone)
724    call assert_true(exists('#java_spotbugs_test#ShellCmdPost'))
725    doautocmd ShellCmdPost
726    call assert_false(exists('#java_spotbugs_test#ShellCmdPost'))
727    call assert_true(s:spotbugs_results.postActionDone)
728    call assert_equal('%:S', s:spotbugs_results.postCommandArguments)
729
730    " With a match, confirm that ":compiler spotbugs" has run.
731    if has('win32')
732      call assert_match('^spotbugs\.bat\s', &l:makeprg)
733    else
734      call assert_match('^spotbugs\s', &l:makeprg)
735    endif
736
737    bwipeout
738    setlocal makeprg=
739    let $PATH = save_PATH
740  endif
741
742  filetype plugin off
743  setlocal makeprg=
744  let &shellslash = save_shellslash
745endfunc
746
747func Test_compiler_just()
748  CheckFeature quickfix
749
750  compiler just
751  call assert_equal('just', b:current_compiler)
752  call assert_equal('just', &makeprg)
753  let verbose_efm = execute('verbose set efm')
754  call assert_match('Last set from .*[/\\]compiler[/\\]just.vim ', verbose_efm)
755
756  " Test that the errorformat can parse just error output
757  let lines =<< trim END
758    error: Variable `name` not defined
759      ——▶ justfile:2:15
760       │
761     2 │   echo {{name}}
762       │          ^^^^
763  END
764  call writefile(lines, 'Xjusterr.txt')
765  cgetfile Xjusterr.txt
766  let l = getqflist()
767  call assert_equal(1, len(l))
768  call assert_equal('E', l[0].type)
769  call assert_equal('Variable `name` not defined', l[0].text)
770  call assert_equal(2, l[0].lnum)
771  call assert_equal(15, l[0].col)
772
773  call setqflist([])
774  call delete('Xjusterr.txt')
775  compiler make
776endfunc
777
778" vim: shiftwidth=2 sts=2 expandtab